Add more hooks to active management

This commit is contained in:
2023-02-17 07:20:15 +00:00
parent dd3b3521d9
commit 1dbb3fcf79
17 changed files with 716 additions and 47 deletions

View File

@@ -1,9 +1,78 @@
from datetime import datetime
from decimal import Decimal as D
from core.exchanges.convert import side_to_direction
from core.trading import checks
from core.trading.market import get_base_quote, get_trade_size_in_base
class ActiveManagement(object):
def __init__(self, strategy):
self.strategy = strategy
self.policy = strategy.active_management_policy
self.trades = []
self.balance = None
def get_trades(self):
if not self.trades:
self.trades = self.strategy.account.client.get_all_open_trades()
return self.trades
def get_balance(self):
if self.balance is None:
self.balance = self.strategy.account.client.get_balance()
else:
return self.balance
def handle_violation(self, check_type, action, trade):
print("VIOLATION", check_type, action, trade)
def check_trading_time(self, trade):
open_ts = trade["openTime"]
open_ts_as_date = datetime.strptime(open_ts, "%Y-%m-%dT%H:%M:%S.%fZ")
trading_time_pass = checks.within_trading_times(self.strategy, open_ts_as_date)
if not trading_time_pass:
self.handle_violation(
"trading_time", self.policy.when_trading_time_violated, trade
)
def check_trends(self, trade):
direction = side_to_direction(trade["side"])
symbol = trade["symbol"]
trends_pass = checks.within_trends(self.strategy, symbol, direction)
if not trends_pass:
print("VIOLATION", "trends", self.policy.when_trends_violated, trade)
self.handle_violation("trends", self.policy.when_trends_violated, trade)
def check_position_size(self, trade):
balance = self.get_balance()
print("BALANCE", balance)
direction = side_to_direction(trade["side"])
symbol = trade["symbol"]
base, quote = get_base_quote(self.strategy.account.exchange, symbol)
expected_trade_size = get_trade_size_in_base(
direction, self.strategy.account, self.strategy, balance, base
)
print("TRADE SIZE", expected_trade_size)
deviation = D(0.05) # 5%
actual_trade_size = D(trade["currentUnits"])
# Ensure the trade size not above the expected trade size by more than 5%
max_trade_size = expected_trade_size + (deviation * expected_trade_size)
within_max_trade_size = actual_trade_size <= max_trade_size
if not within_max_trade_size:
self.handle_violation(
"position_size", self.policy.when_position_size_violated, trade
)
def run_checks(self):
pass
for trade in self.get_trades():
self.check_trading_time(trade)
self.check_trends(trade)
self.check_position_size(trade)
# Trading Time
# Max loss
# Trends

View File

@@ -53,7 +53,9 @@ def within_max_loss(strategy):
def within_trends(strategy, symbol, direction):
print("WITHIN TRENDS", symbol, direction)
if strategy.trend_signals.exists():
print("TREND SIGNALS EXIST")
if strategy.trends is None:
log.debug("Refusing to trade with no trend signals received")
sendmsg(
@@ -82,6 +84,9 @@ def within_trends(strategy, symbol, direction):
else:
log.debug(f"Trend check passed for {symbol} - {direction}")
return True
else:
log.debug("No trend signals configured")
return True
def within_position_size(strategy):

View File

@@ -50,6 +50,8 @@ def get_base_quote(exchange, symbol):
separator = "/"
elif exchange == "oanda":
separator = "_"
else:
separator = "_"
base, quote = symbol.split(separator)
return (base, quote)