Write crossfilter, asset groups and max open trades implementation and tests
This commit is contained in:
@@ -1,8 +1,15 @@
|
||||
from datetime import datetime
|
||||
from decimal import Decimal as D
|
||||
|
||||
from core.exchanges.convert import convert_trades, side_to_direction
|
||||
from core.trading import checks
|
||||
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.crossfilter import crossfilter
|
||||
from core.trading.market import get_base_quote, get_trade_size_in_base
|
||||
|
||||
|
||||
@@ -77,7 +84,6 @@ class ActiveManagement(object):
|
||||
)
|
||||
|
||||
def check_protection(self, trade):
|
||||
print("CHECK PROTECTION", trade)
|
||||
deviation = D(0.05) # 5%
|
||||
|
||||
matches = {
|
||||
@@ -89,22 +95,187 @@ class ActiveManagement(object):
|
||||
violations = {}
|
||||
|
||||
for key, expected in matches.items():
|
||||
if expected == 0:
|
||||
continue
|
||||
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
|
||||
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
|
||||
|
||||
if violations:
|
||||
self.handle_violation(
|
||||
"protection", self.policy.when_protection_violated, trade, violations
|
||||
)
|
||||
|
||||
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(
|
||||
"asset_group", self.policy.when_asset_groups_violated, trade
|
||||
)
|
||||
|
||||
def get_sorted_trades_copy(self, trades, reverse=True):
|
||||
trades_copy = trades.copy()
|
||||
# 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 = []
|
||||
# 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:
|
||||
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
|
||||
# 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(
|
||||
"crossfilter", self.policy.when_crossfilter_violated, trade
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
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
|
||||
)
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
def check_max_loss(self):
|
||||
pass
|
||||
|
||||
def check_max_risk(self, trades):
|
||||
pass
|
||||
|
||||
def run_checks(self):
|
||||
converted_trades = convert_trades(self.get_trades())
|
||||
for trade in converted_trades:
|
||||
@@ -112,6 +283,13 @@ class ActiveManagement(object):
|
||||
self.check_trends(trade)
|
||||
self.check_position_size(trade)
|
||||
self.check_protection(trade)
|
||||
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)
|
||||
|
||||
# Trading Time
|
||||
# Max loss
|
||||
|
||||
Reference in New Issue
Block a user