fisk/core/trading/active_management.py

86 lines
3.0 KiB
Python
Raw Normal View History

2023-02-17 07:20:15 +00:00
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
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
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):
2023-02-17 07:20:15 +00:00
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
# Asset Groups
# Position Size
# Protection
# Max open positions
# Max open positions per asset
# Max risk
# Crossfilter