Finish implementing active management hooks
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
from copy import deepcopy
|
||||
from datetime import datetime
|
||||
from decimal import Decimal as D
|
||||
|
||||
import core.trading.market # to avoid messy circular import
|
||||
from core.exchanges.convert import (
|
||||
convert_trades,
|
||||
side_to_direction,
|
||||
sl_percent_to_price,
|
||||
tp_percent_to_price,
|
||||
)
|
||||
from core.trading import assetfilter, checks, risk
|
||||
from core.trading import assetfilter, checks, market, risk
|
||||
from core.trading.crossfilter import crossfilter
|
||||
from core.trading.market import get_base_quote, get_trade_size_in_base
|
||||
|
||||
@@ -20,17 +19,28 @@ class ActiveManagement(object):
|
||||
|
||||
self.trades = []
|
||||
self.balance = None
|
||||
self.balance_usd = 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()
|
||||
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
|
||||
else:
|
||||
return self.balance
|
||||
if self.balance is None:
|
||||
self.balance = self.strategy.account.client.get_balance(
|
||||
return_usd=False
|
||||
)
|
||||
else:
|
||||
return self.balance
|
||||
|
||||
def handle_violation(self, check_type, action, trade, **kwargs):
|
||||
print("VIOLATION", check_type, action, trade, kwargs)
|
||||
@@ -54,8 +64,10 @@ class ActiveManagement(object):
|
||||
def check_position_size(self, trade):
|
||||
"""
|
||||
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.
|
||||
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
|
||||
@@ -86,10 +98,12 @@ class ActiveManagement(object):
|
||||
def check_protection(self, trade):
|
||||
deviation = D(0.05) # 5%
|
||||
|
||||
# fmt: off
|
||||
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,
|
||||
"trailing_stop_percent":
|
||||
self.strategy.order_settings.trailing_stop_loss_percent,
|
||||
}
|
||||
|
||||
violations = {}
|
||||
@@ -155,7 +169,7 @@ class ActiveManagement(object):
|
||||
)
|
||||
|
||||
def get_sorted_trades_copy(self, trades, reverse=True):
|
||||
trades_copy = trades.copy()
|
||||
trades_copy = deepcopy(trades)
|
||||
# sort by open time, newest first
|
||||
trades_copy.sort(
|
||||
key=lambda x: datetime.strptime(x["open_time"], "%Y-%m-%dT%H:%M:%S.%fZ"),
|
||||
@@ -170,7 +184,8 @@ class ActiveManagement(object):
|
||||
|
||||
iterations = 0
|
||||
finished = []
|
||||
# Recursively run crossfilter on the newest-first list until we have no more conflicts
|
||||
# Recursively run crossfilter on the newest-first list until we have no more
|
||||
# conflicts
|
||||
while not len(finished) == len(trades):
|
||||
iterations += 1
|
||||
if iterations > 10000:
|
||||
@@ -207,17 +222,7 @@ class ActiveManagement(object):
|
||||
close_trades.append(trade)
|
||||
if not close_trades:
|
||||
return
|
||||
# For each conflicting symbol, identify the oldest trades
|
||||
# removed_trades = []
|
||||
# for symbol in conflict:
|
||||
# newest_trade = max(conflict, key=lambda x: datetime.strptime(x["open_time"], "%Y-%m-%dT%H:%M:%S.%fZ"))
|
||||
# removed_trades.append(newest_trade)
|
||||
# print("KEEP TRADES", keep_trade_ids)
|
||||
# close_trades = []
|
||||
# for x in keep_trade_ids:
|
||||
# for position in conflict[x]:
|
||||
# if position["id"] not in keep_trade_ids[x]:
|
||||
# close_trades.append(position)
|
||||
|
||||
if close_trades:
|
||||
for trade in close_trades:
|
||||
self.handle_violation(
|
||||
@@ -225,57 +230,90 @@ class ActiveManagement(object):
|
||||
)
|
||||
|
||||
def check_max_open_trades(self, trades):
|
||||
if self.strategy.risk_model is not None:
|
||||
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)
|
||||
print("TRADES COPY", [x["id"] for x in trades_copy])
|
||||
print("MAX", self.strategy.risk_model.max_open_trades)
|
||||
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,
|
||||
trade,
|
||||
)
|
||||
print("TRADES OVER LIMNIT", trades_over_limit)
|
||||
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,
|
||||
trade,
|
||||
)
|
||||
|
||||
def check_max_open_trades_per_symbol(self, trades):
|
||||
if self.strategy.risk_model is not None:
|
||||
max_open_pass = risk.check_max_open_trades_per_symbol(
|
||||
self.strategy.risk_model, trades, return_symbols=True
|
||||
)
|
||||
print("max_open_pass", max_open_pass)
|
||||
max_open_pass = list(max_open_pass)
|
||||
print("MAX OPEN PASS", 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:
|
||||
print("SYMBOL", symbol)
|
||||
print("TRADES", trades)
|
||||
symbol_trades = [x for x in trades_copy if x["symbol"] == symbol]
|
||||
exceeding_limit = symbol_trades[
|
||||
self.strategy.risk_model.max_open_trades_per_symbol :
|
||||
]
|
||||
for x in exceeding_limit:
|
||||
trades_over_limit.append(x)
|
||||
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,
|
||||
trade,
|
||||
)
|
||||
print("TRADES OVER LIMNIT", trades_over_limit)
|
||||
for trade in trades_over_limit:
|
||||
self.handle_violation(
|
||||
"max_open_trades_per_symbol",
|
||||
self.policy.when_max_open_trades_violated,
|
||||
trade,
|
||||
)
|
||||
|
||||
def check_max_loss(self):
|
||||
check_passed = risk.check_max_loss(self.strategy.risk_model, self.strategy.account.initial_balance, self.get_balance())
|
||||
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
|
||||
)
|
||||
|
||||
def check_max_risk(self, trades):
|
||||
pass
|
||||
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(
|
||||
"max_risk", self.policy.when_max_risk_violated, trade
|
||||
)
|
||||
|
||||
def run_checks(self):
|
||||
converted_trades = convert_trades(self.get_trades())
|
||||
|
||||
@@ -20,7 +20,7 @@ def convert_trades_to_usd(account, trades):
|
||||
:return: List of trades, with amount_usd added
|
||||
"""
|
||||
for trade in trades:
|
||||
amount = trade["amount"]
|
||||
amount = D(trade["amount"])
|
||||
symbol = trade["symbol"]
|
||||
side = trade["side"]
|
||||
direction = side_to_direction(side)
|
||||
|
||||
@@ -26,6 +26,7 @@ def check_max_risk(risk_model, account_balance_usd, account_trades):
|
||||
# Calculate the max risk of the account in USD
|
||||
max_risk_usd = account_balance_usd * (max_risk_percent / D(100))
|
||||
total_risk = 0
|
||||
|
||||
for trade in account_trades:
|
||||
max_tmp = []
|
||||
# Need to calculate the max risk in base account currency
|
||||
@@ -36,7 +37,8 @@ def check_max_risk(risk_model, account_balance_usd, account_trades):
|
||||
if "trailing_stop_loss_usd" in trade:
|
||||
max_tmp.append(trade["trailing_stop_loss_usd"])
|
||||
if max_tmp:
|
||||
total_risk += max(max_tmp)
|
||||
max_risk = max(max_tmp)
|
||||
total_risk += max_risk
|
||||
|
||||
allowed = total_risk < max_risk_usd
|
||||
return allowed
|
||||
@@ -59,7 +61,6 @@ def check_max_open_trades_per_symbol(risk_model, account_trades, return_symbols=
|
||||
if symbol not in symbol_map:
|
||||
symbol_map[symbol] = 0
|
||||
symbol_map[symbol] += 1
|
||||
print("Symbol map: ", symbol_map)
|
||||
violating_symbols = []
|
||||
for symbol, count in symbol_map.items():
|
||||
if count >= risk_model.max_open_trades_per_symbol:
|
||||
|
||||
Reference in New Issue
Block a user