Simplify protection options and allow none

This commit is contained in:
Mark Veidemanis 2022-12-20 23:20:07 +00:00
parent 29125d5087
commit 52ddef4c8f
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
1 changed files with 29 additions and 20 deletions

View File

@ -182,7 +182,7 @@ def get_sl(direction, stop_loss_percent, price, return_var=False):
return stop_loss
def get_tp_sl(direction, strategy, price):
def get_tp_sl(direction, strategy, price, round_to=None):
"""
Get the take profit and stop loss prices.
:param direction: Direction of the trade
@ -190,16 +190,22 @@ def get_tp_sl(direction, strategy, price):
:param price: Price of the trade
:return: Take profit and stop loss prices
"""
take_profit = get_tp(direction, strategy.take_profit_percent, price)
stop_loss = get_sl(direction, strategy.stop_loss_percent, price)
cast = {"tp": take_profit, "sl": stop_loss}
cast = {}
if strategy.take_profit_percent != 0:
cast["take_profit"] = get_tp(direction, strategy.take_profit_percent, price)
if strategy.stop_loss_percent != 0:
cast["stop_loss"] = get_sl(direction, strategy.stop_loss_percent, price)
# Look up the TSL if required by the strategy
if strategy.trailing_stop_loss_percent:
trailing_stop_loss = get_sl(
if strategy.trailing_stop_loss_percent != 0:
cast["trailing_stop_loss"] = get_sl(
direction, strategy.trailing_stop_loss_percent, price, return_var=True
)
cast["tsl"] = trailing_stop_loss
if round_to:
for key in cast:
cast[key] = float(round(cast[key], round_to))
return cast
@ -415,12 +421,21 @@ def execute_strategy(callback, strategy, func):
)
# Calculate TP/SL/TSL
protection = get_tp_sl(direction, strategy, current_price)
stop_loss = protection["sl"]
take_profit = protection["tp"]
trailing_stop_loss = None
if "tsl" in protection:
trailing_stop_loss = protection["tsl"]
protection = get_tp_sl(
direction, strategy, current_price, round_to=display_precision
)
# protection_cast = {}
# if "sl" in protection:
# protection_cast["stop_loss"] = float(round(protect
# ion["sl"], display_precision))
# if "tp" in protection:
# protection_cast["take_profit"] = float(
# round(protection["tp"], display_precision)
# )
# if "tsl" in protection:
# protection_cast["trailing_stop_loss"] = float(
# round(protection["tsl"], display_precision)
# )
# Create object, note that the amount is rounded to the trade precision
amount_rounded = float(round(trade_size_in_base, trade_precision))
@ -436,14 +451,8 @@ def execute_strategy(callback, strategy, func):
amount=amount_rounded,
# price=price_bound,
price=price_bound,
stop_loss=float(round(stop_loss, display_precision)),
take_profit=float(round(take_profit, display_precision)),
direction=direction,
)
# Add TSL if applicable
if trailing_stop_loss:
new_trade.trailing_stop_loss = float(
round(trailing_stop_loss, display_precision)
**protection,
)
new_trade.save()