Implement TP/SL price to percent conversion

This commit is contained in:
2023-01-05 19:27:59 +00:00
parent a6f9e74ee1
commit d3e2bc8648
6 changed files with 389 additions and 23 deletions

View File

@@ -266,6 +266,28 @@ def get_price_bound(direction, strategy, price, current_price):
return price_bound
def get_precision(account, symbol):
instruments = account.instruments
if not instruments:
log.error("No instruments found")
return
# Extract the information for the symbol
instrument = account.client.extract_instrument(instruments, symbol)
if not instrument:
# sendmsg(user, f"Symbol not found: {symbol}", title="Error")
log.error(f"Symbol not found: {symbol}")
return (None, None)
# Get the required precision
try:
trade_precision = instrument["tradeUnitsPrecision"]
display_precision = instrument["displayPrecision"]
return (trade_precision, display_precision)
except KeyError:
# sendmsg(user, f"Precision not found for {symbol}", title="Error")
log.error(f"Precision not found for {symbol}")
return (None, None)
def execute_strategy(callback, strategy, func):
"""
Execute a strategy.
@@ -299,11 +321,6 @@ def execute_strategy(callback, strategy, func):
# Refresh account object
strategy.account = Account.objects.get(id=strategy.account.id)
instruments = strategy.account.instruments
if not instruments:
log.error("No instruments found")
return
# Shorten some hook, strategy and callback vars for convenience
user = strategy.user
account = strategy.account
@@ -326,18 +343,9 @@ def execute_strategy(callback, strategy, func):
log.error(f"Symbol not supported by account: {symbol}")
return
# Extract the information for the symbol
instrument = strategy.account.client.extract_instrument(instruments, symbol)
if not instrument:
sendmsg(user, f"Symbol not found: {symbol}", title="Error")
log.error(f"Symbol not found: {symbol}")
return
# Get the required precision
try:
trade_precision = instrument["tradeUnitsPrecision"]
display_precision = instrument["displayPrecision"]
except KeyError:
# Get the precision for the symbol
trade_precision, display_precision = get_precision(account, symbol)
if not trade_precision or not display_precision:
sendmsg(user, f"Precision not found for {symbol}", title="Error")
log.error(f"Precision not found for {symbol}")
return