2023-02-18 11:54:30 +00:00
|
|
|
from copy import deepcopy
|
2023-02-17 07:20:15 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from decimal import Decimal as D
|
|
|
|
|
2023-02-17 22:11:46 +00:00
|
|
|
from core.exchanges.convert import (
|
|
|
|
convert_trades,
|
|
|
|
sl_percent_to_price,
|
|
|
|
tp_percent_to_price,
|
|
|
|
)
|
2023-02-18 11:54:30 +00:00
|
|
|
from core.trading import assetfilter, checks, market, risk
|
2023-02-17 22:11:46 +00:00
|
|
|
from core.trading.crossfilter import crossfilter
|
2023-02-17 07:20:15 +00:00
|
|
|
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
|
2023-02-18 11:54:30 +00:00
|
|
|
self.balance_usd = None
|
2023-02-17 07:20:15 +00:00
|
|
|
|
|
|
|
def get_trades(self):
|
|
|
|
if not self.trades:
|
|
|
|
self.trades = self.strategy.account.client.get_all_open_trades()
|
|
|
|
return self.trades
|
|
|
|
|
2023-02-18 11:54:30 +00:00
|
|
|
def get_balance(self, return_usd=False):
|
|
|
|
if return_usd:
|
|
|
|
if self.balance_usd is None:
|
|
|
|
self.balance_usd = self.strategy.account.client.get_balance(
|
|
|
|
return_usd=True
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return self.balance_usd
|
2023-02-17 07:20:15 +00:00
|
|
|
else:
|
2023-02-18 11:54:30 +00:00
|
|
|
if self.balance is None:
|
|
|
|
self.balance = self.strategy.account.client.get_balance(
|
|
|
|
return_usd=False
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
return self.balance
|
2023-02-17 07:20:15 +00:00
|
|
|
|
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-18 14:36:58 +00:00
|
|
|
# TODO: close/notify for:
|
|
|
|
# - trading time
|
|
|
|
# - trends
|
|
|
|
# - position size
|
|
|
|
# - protection
|
|
|
|
# - asset groups
|
|
|
|
# - crossfilter
|
|
|
|
# - max open trades
|
|
|
|
# - max open trades per symbol
|
|
|
|
# - max loss
|
|
|
|
# - max risk
|
|
|
|
|
|
|
|
# TODO: adjust for:
|
|
|
|
# - position size
|
|
|
|
# - protection
|
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(
|
2023-02-18 14:36:58 +00:00
|
|
|
"trading_time", self.policy.when_trading_time_violated, trade["id"]
|
2023-02-17 07:20:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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:
|
2023-02-18 14:36:58 +00:00
|
|
|
self.handle_violation(
|
|
|
|
"trends", self.policy.when_trends_violated, trade["id"]
|
|
|
|
)
|
2023-02-17 07:20:15 +00:00
|
|
|
|
|
|
|
def check_position_size(self, trade):
|
2023-02-17 17:05:52 +00:00
|
|
|
"""
|
|
|
|
Check the position size is within the allowed deviation.
|
2023-02-18 11:54:30 +00:00
|
|
|
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.
|
2023-02-17 17:05:52 +00:00
|
|
|
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,
|
2023-02-18 14:36:58 +00:00
|
|
|
trade["id"],
|
2023-02-17 17:05:52 +00:00
|
|
|
{"size": expected_trade_size},
|
|
|
|
)
|
|
|
|
|
|
|
|
def check_protection(self, trade):
|
|
|
|
deviation = D(0.05) # 5%
|
|
|
|
|
2023-02-18 11:54:30 +00:00
|
|
|
# fmt: off
|
2023-02-17 17:05:52 +00:00
|
|
|
matches = {
|
|
|
|
"stop_loss_percent": self.strategy.order_settings.stop_loss_percent,
|
|
|
|
"take_profit_percent": self.strategy.order_settings.take_profit_percent,
|
2023-02-18 11:54:30 +00:00
|
|
|
"trailing_stop_percent":
|
|
|
|
self.strategy.order_settings.trailing_stop_loss_percent,
|
2023-02-17 17:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
violations = {}
|
|
|
|
|
|
|
|
for key, expected in matches.items():
|
2023-02-17 22:11:46 +00:00
|
|
|
if expected == 0:
|
|
|
|
continue
|
2023-02-17 17:05:52 +00:00
|
|
|
if key in trade:
|
|
|
|
actual = D(trade[key])
|
|
|
|
expected = D(expected)
|
|
|
|
min_val = expected - (deviation * expected)
|
|
|
|
max_val = expected + (deviation * expected)
|
|
|
|
within_deviation = min_val <= actual <= max_val
|
2023-02-17 22:11:46 +00:00
|
|
|
else:
|
|
|
|
within_deviation = False
|
|
|
|
|
|
|
|
if not within_deviation:
|
|
|
|
# violations[key] = expected
|
|
|
|
if key == "take_profit_percent":
|
|
|
|
tp_price = tp_percent_to_price(
|
|
|
|
expected,
|
|
|
|
trade["side"],
|
|
|
|
trade["current_price"],
|
|
|
|
trade["amount"],
|
|
|
|
trade["pl"],
|
|
|
|
)
|
|
|
|
violations["take_profit_price"] = tp_price
|
|
|
|
elif key == "stop_loss_percent":
|
|
|
|
sl_price = sl_percent_to_price(
|
|
|
|
expected,
|
|
|
|
trade["side"],
|
|
|
|
trade["current_price"],
|
|
|
|
trade["amount"],
|
|
|
|
trade["pl"],
|
|
|
|
)
|
|
|
|
violations["stop_loss_price"] = sl_price
|
|
|
|
elif key == "trailing_stop_loss_percent":
|
|
|
|
tsl_price = sl_percent_to_price(
|
|
|
|
expected,
|
|
|
|
trade["side"],
|
|
|
|
trade["current_price"],
|
|
|
|
trade["amount"],
|
|
|
|
trade["pl"],
|
|
|
|
)
|
|
|
|
violations["trailing_stop_loss_price"] = tsl_price
|
2023-02-17 17:05:52 +00:00
|
|
|
|
|
|
|
if violations:
|
|
|
|
self.handle_violation(
|
2023-02-18 14:36:58 +00:00
|
|
|
"protection",
|
|
|
|
self.policy.when_protection_violated,
|
|
|
|
trade["id"],
|
|
|
|
violations
|
2023-02-17 07:20:15 +00:00
|
|
|
)
|
2023-02-17 07:20:28 +00:00
|
|
|
|
2023-02-17 22:11:46 +00:00
|
|
|
def check_asset_groups(self, trade):
|
|
|
|
if self.strategy.asset_group is not None:
|
|
|
|
base, quote = get_base_quote(
|
|
|
|
self.strategy.account.exchange, trade["symbol"]
|
|
|
|
)
|
|
|
|
allowed = assetfilter.get_allowed(
|
|
|
|
self.strategy.asset_group, base, quote, trade["side"]
|
|
|
|
)
|
|
|
|
if not allowed:
|
|
|
|
self.handle_violation(
|
2023-02-18 14:36:58 +00:00
|
|
|
"asset_group", self.policy.when_asset_groups_violated, trade["id"]
|
2023-02-17 22:11:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_sorted_trades_copy(self, trades, reverse=True):
|
2023-02-18 11:54:30 +00:00
|
|
|
trades_copy = deepcopy(trades)
|
2023-02-17 22:11:46 +00:00
|
|
|
# sort by open time, newest first
|
|
|
|
trades_copy.sort(
|
|
|
|
key=lambda x: datetime.strptime(x["open_time"], "%Y-%m-%dT%H:%M:%S.%fZ"),
|
|
|
|
reverse=reverse,
|
|
|
|
)
|
|
|
|
return trades_copy
|
|
|
|
|
|
|
|
def check_crossfilter(self, trades):
|
|
|
|
close_trades = []
|
|
|
|
|
|
|
|
trades_copy = self.get_sorted_trades_copy(trades)
|
|
|
|
|
|
|
|
iterations = 0
|
|
|
|
finished = []
|
2023-02-18 11:54:30 +00:00
|
|
|
# Recursively run crossfilter on the newest-first list until we have no more
|
|
|
|
# conflicts
|
2023-02-17 22:11:46 +00:00
|
|
|
while not len(finished) == len(trades):
|
|
|
|
iterations += 1
|
|
|
|
if iterations > 10000:
|
|
|
|
raise Exception("Too many iterations")
|
|
|
|
# For each trade
|
|
|
|
for trade in trades_copy:
|
|
|
|
# Abort if we've already checked this trade
|
|
|
|
if trade in close_trades:
|
|
|
|
continue
|
|
|
|
# Calculate trades excluding this one
|
|
|
|
# Also remove if we have already checked this
|
|
|
|
others = [
|
|
|
|
t
|
|
|
|
for t in trades_copy
|
|
|
|
if t["id"] != trade["id"] and t not in close_trades
|
|
|
|
]
|
|
|
|
symbol = trade["symbol"]
|
|
|
|
direction = trade["direction"]
|
|
|
|
func = "entry"
|
|
|
|
|
|
|
|
# Check if this trade is filtered, pretending we are opening it
|
|
|
|
# And passing the remaining trades as the other trades in the account
|
|
|
|
filtered = crossfilter(
|
|
|
|
self.strategy.account, symbol, direction, func, all_positions=others
|
|
|
|
)
|
|
|
|
if not filtered:
|
|
|
|
# This trade is fine, add it to finished
|
|
|
|
finished.append(trade)
|
|
|
|
continue
|
|
|
|
if filtered["action"] == "rejected":
|
|
|
|
# It's rejected, add it to the close trades list
|
|
|
|
# And don't check it again
|
|
|
|
finished.append(trade)
|
|
|
|
close_trades.append(trade)
|
|
|
|
if not close_trades:
|
|
|
|
return
|
2023-02-18 11:54:30 +00:00
|
|
|
|
2023-02-17 22:11:46 +00:00
|
|
|
if close_trades:
|
|
|
|
for trade in close_trades:
|
|
|
|
self.handle_violation(
|
2023-02-18 14:36:58 +00:00
|
|
|
"crossfilter", self.policy.when_crossfilter_violated, trade["id"]
|
2023-02-17 22:11:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def check_max_open_trades(self, trades):
|
2023-02-18 11:54:30 +00:00
|
|
|
if self.strategy.risk_model is None:
|
|
|
|
return
|
|
|
|
max_open_pass = risk.check_max_open_trades(self.strategy.risk_model, trades)
|
|
|
|
if not max_open_pass:
|
|
|
|
trades_copy = self.get_sorted_trades_copy(trades, reverse=False)
|
|
|
|
# fmt: off
|
|
|
|
trades_over_limit = trades_copy[self.strategy.risk_model.max_open_trades:]
|
|
|
|
for trade in trades_over_limit:
|
|
|
|
self.handle_violation(
|
|
|
|
"max_open_trades",
|
|
|
|
self.policy.when_max_open_trades_violated,
|
2023-02-18 14:36:58 +00:00
|
|
|
trade["id"],
|
2023-02-18 11:54:30 +00:00
|
|
|
)
|
2023-02-17 22:11:46 +00:00
|
|
|
|
|
|
|
def check_max_open_trades_per_symbol(self, trades):
|
2023-02-18 11:54:30 +00:00
|
|
|
if self.strategy.risk_model is None:
|
|
|
|
return
|
|
|
|
max_open_pass = risk.check_max_open_trades_per_symbol(
|
|
|
|
self.strategy.risk_model, trades, return_symbols=True
|
|
|
|
)
|
|
|
|
max_open_pass = list(max_open_pass)
|
|
|
|
if max_open_pass:
|
|
|
|
trades_copy = self.get_sorted_trades_copy(trades, reverse=False)
|
|
|
|
trades_over_limit = []
|
|
|
|
for symbol in max_open_pass:
|
|
|
|
symbol_trades = [x for x in trades_copy if x["symbol"] == symbol]
|
|
|
|
# fmt: off
|
|
|
|
exceeding_limit = symbol_trades[
|
|
|
|
self.strategy.risk_model.max_open_trades_per_symbol:
|
|
|
|
]
|
|
|
|
for x in exceeding_limit:
|
|
|
|
trades_over_limit.append(x)
|
|
|
|
|
|
|
|
for trade in trades_over_limit:
|
|
|
|
self.handle_violation(
|
|
|
|
"max_open_trades_per_symbol",
|
|
|
|
self.policy.when_max_open_trades_violated,
|
2023-02-18 14:36:58 +00:00
|
|
|
trade["id"],
|
2023-02-18 11:54:30 +00:00
|
|
|
)
|
2023-02-17 22:11:46 +00:00
|
|
|
|
|
|
|
def check_max_loss(self):
|
2023-02-18 11:54:30 +00:00
|
|
|
if self.strategy.risk_model is None:
|
|
|
|
return
|
|
|
|
check_passed = risk.check_max_loss(
|
|
|
|
self.strategy.risk_model,
|
|
|
|
self.strategy.account.initial_balance,
|
|
|
|
self.get_balance(),
|
|
|
|
)
|
|
|
|
if not check_passed:
|
|
|
|
self.handle_violation(
|
|
|
|
"max_loss", self.policy.when_max_loss_violated, None # Close all trades
|
|
|
|
)
|
2023-02-17 22:11:46 +00:00
|
|
|
|
|
|
|
def check_max_risk(self, trades):
|
2023-02-18 11:54:30 +00:00
|
|
|
if self.strategy.risk_model is None:
|
|
|
|
return
|
|
|
|
close_trades = []
|
|
|
|
|
|
|
|
trades_copy = self.get_sorted_trades_copy(trades, reverse=False)
|
|
|
|
market.convert_trades_to_usd(self.strategy.account, trades_copy)
|
|
|
|
|
|
|
|
iterations = 0
|
|
|
|
finished = False
|
|
|
|
while not finished:
|
|
|
|
iterations += 1
|
|
|
|
if iterations > 10000:
|
|
|
|
raise Exception("Too many iterations")
|
|
|
|
|
|
|
|
check_passed = risk.check_max_risk(
|
|
|
|
self.strategy.risk_model,
|
|
|
|
self.get_balance(return_usd=True),
|
|
|
|
trades_copy,
|
|
|
|
)
|
|
|
|
if check_passed:
|
|
|
|
finished = True
|
|
|
|
else:
|
|
|
|
# Add the newest trade to close_trades and remove it from trades_copy
|
|
|
|
close_trades.append(trades_copy[-1])
|
|
|
|
trades_copy = trades_copy[:-1]
|
|
|
|
if close_trades:
|
|
|
|
for trade in close_trades:
|
|
|
|
self.handle_violation(
|
2023-02-18 14:36:58 +00:00
|
|
|
"max_risk", self.policy.when_max_risk_violated, trade["id"]
|
2023-02-18 11:54:30 +00:00
|
|
|
)
|
2023-02-17 22:11:46 +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 22:11:46 +00:00
|
|
|
self.check_asset_groups(trade)
|
|
|
|
|
|
|
|
self.check_crossfilter(converted_trades)
|
|
|
|
self.check_max_open_trades(converted_trades)
|
|
|
|
self.check_max_open_trades_per_symbol(converted_trades)
|
|
|
|
self.check_max_loss()
|
|
|
|
self.check_max_risk(converted_trades)
|
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
|