2023-02-17 07:20:15 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from decimal import Decimal as D
|
|
|
|
|
2023-02-17 17:05:52 +00:00
|
|
|
from core.exchanges.convert import convert_trades, side_to_direction
|
2023-02-17 07:20:15 +00:00
|
|
|
from core.trading import checks
|
|
|
|
from core.trading.market import get_base_quote, get_trade_size_in_base
|
|
|
|
|
|
|
|
|
2023-02-17 07:20:19 +00:00
|
|
|
class ActiveManagement(object):
|
|
|
|
def __init__(self, strategy):
|
|
|
|
self.strategy = strategy
|
2023-02-17 07:20:15 +00:00
|
|
|
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
|
|
|
|
|
2023-02-17 17:05:52 +00:00
|
|
|
def handle_violation(self, check_type, action, trade, **kwargs):
|
|
|
|
print("VIOLATION", check_type, action, trade, kwargs)
|
2023-02-17 07:20:15 +00:00
|
|
|
|
|
|
|
def check_trading_time(self, trade):
|
2023-02-17 17:05:52 +00:00
|
|
|
open_ts = trade["open_time"]
|
2023-02-17 07:20:15 +00:00
|
|
|
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):
|
2023-02-17 17:05:52 +00:00
|
|
|
direction = trade["direction"]
|
2023-02-17 07:20:15 +00:00
|
|
|
symbol = trade["symbol"]
|
|
|
|
trends_pass = checks.within_trends(self.strategy, symbol, direction)
|
|
|
|
if not trends_pass:
|
|
|
|
self.handle_violation("trends", self.policy.when_trends_violated, trade)
|
|
|
|
|
|
|
|
def check_position_size(self, trade):
|
2023-02-17 17:05:52 +00:00
|
|
|
"""
|
|
|
|
Check the position size is within the allowed deviation.
|
|
|
|
WARNING: This uses the current balance, not the balance at the time of the trade.
|
|
|
|
WARNING: This uses the current symbol prices, not those at the time of the trade.
|
|
|
|
This should normally be run every 5 seconds, so this is fine.
|
|
|
|
"""
|
|
|
|
# TODO: add the trade value to the balance
|
|
|
|
# Need to determine which prices to use
|
2023-02-17 07:20:15 +00:00
|
|
|
balance = self.get_balance()
|
2023-02-17 17:05:52 +00:00
|
|
|
direction = trade["direction"]
|
2023-02-17 07:20:15 +00:00
|
|
|
symbol = trade["symbol"]
|
2023-02-17 17:05:52 +00:00
|
|
|
# TODO:
|
2023-02-17 07:20:15 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
deviation = D(0.05) # 5%
|
2023-02-17 17:05:52 +00:00
|
|
|
actual_trade_size = D(trade["amount"])
|
2023-02-17 07:20:15 +00:00
|
|
|
# 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(
|
2023-02-17 17:05:52 +00:00
|
|
|
"position_size",
|
|
|
|
self.policy.when_position_size_violated,
|
|
|
|
trade,
|
|
|
|
{"size": expected_trade_size},
|
|
|
|
)
|
|
|
|
|
|
|
|
def check_protection(self, trade):
|
|
|
|
print("CHECK PROTECTION", trade)
|
|
|
|
deviation = D(0.05) # 5%
|
|
|
|
|
|
|
|
matches = {
|
|
|
|
"stop_loss_percent": self.strategy.order_settings.stop_loss_percent,
|
|
|
|
"take_profit_percent": self.strategy.order_settings.take_profit_percent,
|
|
|
|
"trailing_stop_percent": self.strategy.order_settings.trailing_stop_loss_percent,
|
|
|
|
}
|
|
|
|
|
|
|
|
violations = {}
|
|
|
|
|
|
|
|
for key, expected in matches.items():
|
|
|
|
if key in trade:
|
|
|
|
actual = D(trade[key])
|
|
|
|
if expected is None:
|
|
|
|
continue
|
|
|
|
expected = D(expected)
|
|
|
|
min_val = expected - (deviation * expected)
|
|
|
|
max_val = expected + (deviation * expected)
|
|
|
|
within_deviation = min_val <= actual <= max_val
|
|
|
|
if not within_deviation:
|
|
|
|
violations[key] = expected
|
|
|
|
|
|
|
|
if violations:
|
|
|
|
self.handle_violation(
|
|
|
|
"protection", self.policy.when_protection_violated, trade, violations
|
2023-02-17 07:20:15 +00:00
|
|
|
)
|
2023-02-17 07:20:28 +00:00
|
|
|
|
|
|
|
def run_checks(self):
|
2023-02-17 17:05:52 +00:00
|
|
|
converted_trades = convert_trades(self.get_trades())
|
|
|
|
for trade in converted_trades:
|
2023-02-17 07:20:15 +00:00
|
|
|
self.check_trading_time(trade)
|
|
|
|
self.check_trends(trade)
|
|
|
|
self.check_position_size(trade)
|
2023-02-17 17:05:52 +00:00
|
|
|
self.check_protection(trade)
|
2023-02-17 07:20:15 +00:00
|
|
|
|
2023-02-17 07:20:28 +00:00
|
|
|
# Trading Time
|
|
|
|
# Max loss
|
|
|
|
# Trends
|
|
|
|
# Asset Groups
|
|
|
|
# Position Size
|
|
|
|
# Protection
|
|
|
|
# Max open positions
|
|
|
|
# Max open positions per asset
|
|
|
|
# Max risk
|
|
|
|
# Crossfilter
|