Implement asset rules as Asset Group children objects

This commit is contained in:
2023-02-13 20:45:23 +00:00
parent b48af50620
commit 6ff5f718ba
18 changed files with 565 additions and 349 deletions

View File

@@ -1,3 +1,6 @@
from core.models import AssetRule
def get_allowed(group, base, quote, direction):
"""
Determine whether the trade is allowed according to the group.
@@ -8,25 +11,35 @@ def get_allowed(group, base, quote, direction):
:param direction: The direction of the trade
"""
allowed = group.allowed
if not isinstance(allowed, dict):
return False
# If our base has allowed == False, we can only short it, or long the quote
if base in allowed:
if not allowed[base]:
base_rule = AssetRule.objects.filter(group=group, asset=base).first()
if base_rule:
if base_rule.status == 6:
# Always allow
return True
elif base_rule.status == 7:
# Always deny
return False
elif base_rule.status == 3:
if direction == "long":
return False
else:
elif base_rule.status == 2:
if direction == "short":
return False
# If our quote has allowed == False, we can only long it, or short the base
if quote in allowed:
if not allowed[quote]:
quote_rule = AssetRule.objects.filter(group=group, asset=quote).first()
if quote_rule:
if quote_rule.status == 6:
# Always allow
return True
elif quote_rule.status == 7:
# Always deny
return False
elif quote_rule.status == 3:
if direction == "short":
return False
else:
elif quote_rule.status == 2:
if direction == "long":
return False
@@ -40,16 +53,39 @@ def check_asset_aggregation(value, trigger_above, trigger_below):
:param trigger_above: Only trigger if the value is above this
:param trigger_below: Only trigger if the value is below this
"""
# If both are defined
if trigger_above is not None and trigger_below is not None:
if value > trigger_above and value < trigger_below:
return True
return False
if trigger_below is not None:
if value < trigger_below:
# Value is less than lower bound, match
return True
# Prohibited
return 3
if trigger_above is not None:
if value > trigger_above:
return True
return False
# Permitted
return 2
# Neither is defined, so we match
if not any(x is not None for x in [trigger_above, trigger_below]):
# No aggregation
return 4
# Not in bounds
return 5
def update_status_from_mappings(status, asset_group):
if status == 0:
if asset_group.when_no_data > -1:
return asset_group.when_no_data
elif status == 1:
if asset_group.when_no_match > -1:
return asset_group.when_no_match
elif status == 2:
if asset_group.when_bullish > -1:
return asset_group.when_bullish
elif status == 3:
if asset_group.when_bearish > -1:
return asset_group.when_bearish
elif status == 4:
if asset_group.when_no_aggregation > -1:
return asset_group.when_no_aggregation
elif status == 5:
if asset_group.when_not_in_bounds > -1:
return asset_group.when_not_in_bounds
return status