From 52ddef4c8fccda290eb6c872f23ff72be827a3a2 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Tue, 20 Dec 2022 23:20:07 +0000 Subject: [PATCH] Simplify protection options and allow none --- core/trading/market.py | 49 +++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/core/trading/market.py b/core/trading/market.py index c005e17..f73018f 100644 --- a/core/trading/market.py +++ b/core/trading/market.py @@ -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,15 +451,9 @@ 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, + **protection, ) - # Add TSL if applicable - if trailing_stop_loss: - new_trade.trailing_stop_loss = float( - round(trailing_stop_loss, display_precision) - ) new_trade.save() # Run the crossfilter to ensure we don't trade the same pair in opposite directions