Start implementing active management actions

This commit is contained in:
2023-02-18 17:55:39 +00:00
parent 15a8bec105
commit ae104f446a
2 changed files with 242 additions and 2 deletions

View File

@@ -10,6 +10,9 @@ from core.exchanges.convert import (
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
from core.util import logs
log = logs.get_logger("ams")
class ActiveManagement(object):
@@ -18,9 +21,30 @@ class ActiveManagement(object):
self.policy = strategy.active_management_policy
self.trades = []
self.actions = {}
self.balance = None
self.balance_usd = None
def add_action(self, action, check_type, trade_id, **kwargs):
if action not in self.actions:
self.actions[action] = []
self.actions[action].append(
{"id": trade_id, "check": check_type, "extra": kwargs}
)
def reduce_actions(self):
"""
If a trade is in the close actions, remove it from adjust.
"""
if "close" in self.actions:
for close_action in self.actions["close"]:
if "adjust" in self.actions:
self.actions["adjust"] = [
action
for action in self.actions["adjust"]
if action["id"] != close_action["id"]
]
def get_trades(self):
if not self.trades:
self.trades = self.strategy.account.client.get_all_open_trades()
@@ -42,8 +66,64 @@ class ActiveManagement(object):
else:
return self.balance
def handle_violation(self, check_type, action, trade, **kwargs):
print("VIOLATION", check_type, action, trade, kwargs)
def close_trade(self, trade_id):
self.strategy.account.client.close_trade(trade_id)
def bulk_close_trades(self, trade_ids):
for trade_id in trade_ids:
self.close_trade(trade_id)
def bulk_notify(self, action, action_cast_list):
print("CALL", action, action_cast_list)
msg = ""
for action_cast in action_cast_list:
msg += f"ACTION: '{action}' on trade ID '{action_cast['id']}'\n"
msg += f"VIOLATION: '{action_cast['check']}'\n"
if action_cast["extra"]:
extra = action_cast["extra"]
extra = ", ".join([f"{k}: {v}" for k, v in extra.items()])
msg += f"EXTRA: {extra}\n"
msg += "=========\n"
print("NOTIFY", msg)
def adjust_position_size(self, trade_id, new_size):
pass
def adjust_protection(self, trade_id, new_protection):
pass
def bulk_adjust(self, action_cast_list):
for item in action_cast_list:
trade_id = item["id"]
check = item["check"]
if "extra" in item:
extra = item["extra"]
else:
log.error(f"Adjust action missing extra data: {item}")
continue
if check == "position_size":
new_size = extra["size"]
self.adjust_position_size(trade_id, new_size)
elif check == "protection":
self.adjust_protection(trade_id, extra)
def run_actions(self):
for action, action_cast_list in self.actions.items():
if action == "none":
continue
self.bulk_notify(action, action_cast_list)
if action == "close":
trade_ids = [action_cast["id"] for action_cast in action_cast_list]
self.bulk_close_trades(trade_ids)
elif action == "adjust":
self.bulk_adjust(action_cast_list)
def handle_violation(self, check_type, action, trade_id, **kwargs):
print("VIOLATION", check_type, action, trade_id, kwargs)
if action == "none":
return
self.add_action(action, check_type, trade_id, **kwargs)
# TODO: close/notify for:
# - trading time
# - trends