diff --git a/live_strategys/QQE_Hullband_VolumeOsc.py b/live_strategys/QQE_Hullband_VolumeOsc.py index 68eed18..f1b69e3 100644 --- a/live_strategys/QQE_Hullband_VolumeOsc.py +++ b/live_strategys/QQE_Hullband_VolumeOsc.py @@ -52,8 +52,9 @@ def __init__(self, **kwargs): def buy_or_short_condition(self): if not self.buy_executed and not self.conditions_checked: if (self.qqe.qqe_line[-1] > 0) and \ - (self.data.close[-1] > self.hma[0]) and \ - (self.volosc.osc[-1] > self.volosc.lines[0]): + (self.data.close[-1] > self.hma[0]) and \ + (self.volosc.osc[-1] > self.volosc.lines[0] + ): if self.params.backtest == False: self.entry_prices.append(self.data.close[0]) self.sizes.append(self.amount) @@ -64,12 +65,15 @@ def buy_or_short_condition(self): elif self.params.backtest == True: self.buy(size=self.stake, price=self.data.close[0], exectype=bt.Order.Market) self.buy_executed = True - self.conditions_checked = True + self.entry_prices.append(self.data.close[0]) + self.sizes.append(self.stake) + self.calc_averages() def sell_or_cover_condition(self): if self.buy_executed and (self.qqe.qqe_line[-1] > 0) and \ - (self.data.close[-1] < self.hma[0]) and \ - (self.volosc.osc[-1] < self.volosc.lines[0]): + (self.data.close[-1] < self.hma[0]) and \ + (self.volosc.osc[-1] < self.volosc.lines[0] + ): if self.params.backtest == False: self.rabbit.send_jrr_close_request(exchange=self.exchange, account=self.account, asset=self.asset) elif self.params.backtest == True: diff --git a/template_strategys/DCA_QQE_Example_backtesting.py b/template_strategys/DCA_QQE_Example_backtesting.py index d94945b..5ce9a2d 100644 --- a/template_strategys/DCA_QQE_Example_backtesting.py +++ b/template_strategys/DCA_QQE_Example_backtesting.py @@ -56,8 +56,10 @@ def __init__(self, **kwargs): def buy_or_short_condition(self): if not self.buy_executed and not self.conditions_checked: if (self.qqe.qqe_line[-1] > 0) and \ - (self.data.close[-1] > self.hma[0]) and \ - (self.volosc.osc[-1] > self.volosc.lines[0]): + (self.data.close[-1] > self.hma[0]) and \ + (self.volosc.osc[-1] > self.volosc.lines[0] + ): + if self.params.backtest == False: self.entry_prices.append(self.data.close[0]) self.sizes.append(self.amount) @@ -68,13 +70,17 @@ def buy_or_short_condition(self): elif self.params.backtest == True: self.buy(size=self.stake, price=self.data.close[0], exectype=bt.Order.Market) self.buy_executed = True - self.conditions_checked = True + self.entry_prices.append(self.data.close[0]) + self.sizes.append(self.stake) + self.calc_averages() def dca_or_short_condition(self): if self.buy_executed and not self.conditions_checked: if (self.qqe.qqe_line[-1] > 0) and \ - (self.data.close[-1] > self.hma[0]) and \ - (self.volosc.osc[-1] > self.volosc.lines[0]): + (self.data.close[-1] > self.hma[0]) and \ + (self.volosc.osc[-1] > self.volosc.lines[0] + ): + if self.params.backtest == False: self.entry_prices.append(self.data.close[0]) self.sizes.append(self.amount) @@ -85,12 +91,29 @@ def dca_or_short_condition(self): elif self.params.backtest == True: self.buy(size=self.stake, price=self.data.close[0], exectype=bt.Order.Market) self.buy_executed = True - self.conditions_checked = True + self.entry_prices.append(self.data.close[0]) + self.sizes.append(self.stake) + self.calc_averages() def sell_or_cover_condition(self): - if self.buy_executed and (self.qqe.qqe_line[-1] > 0) and \ - (self.data.close[-1] < self.hma[0]) and \ - (self.volosc.osc[-1] < self.volosc.lines[0]): + if self.buy_executed and self.data.close[0] >= self.take_profit_price: + average_entry_price = sum(self.entry_prices) / len(self.entry_prices) if self.entry_prices else 0 + + # Avoid selling at a loss or below the take profit price + if round(self.data.close[0], 9) < round(self.average_entry_price, 9) or round(self.data.close[0], 9) < round(self.take_profit_price, 9): + self.log( + f"| - Avoiding sell at a loss or below take profit. " + f"| - Current close price: {self.data.close[0]:.12f}, " + f"| - Average entry price: {average_entry_price:.12f}, " + f"| - Take profit price: {self.take_profit_price:.12f}" + ) + return + + if self.buy_executed and self.data.close[0] >= self.take_profit_price: + if self.data.close[0] < self.average_entry_price: + print(f'Nothing Todo here. {self.average_entry_price, self.take_profit_price}') + return + if self.params.backtest == False: self.rabbit.send_jrr_close_request(exchange=self.exchange, account=self.account, asset=self.asset) elif self.params.backtest == True: diff --git a/template_strategys/SuperTrend_Scalp.py b/template_strategys/SuperTrend_Scalp.py index 25ff245..fed3be0 100644 --- a/template_strategys/SuperTrend_Scalp.py +++ b/template_strategys/SuperTrend_Scalp.py @@ -57,9 +57,10 @@ def buy_or_short_condition(self): self.conditions_checked = True elif self.params.backtest == True: self.buy(size=self.stake, price=self.data.close[0], exectype=bt.Order.Market) - self.take_profit_price = self.data.close[-1] * (1 + self.params.take_profit / 100) self.buy_executed = True - self.conditions_checked = True + self.entry_prices.append(self.data.close[0]) + self.sizes.append(self.stake) + self.calc_averages() def dca_or_short_condition(self): if (self.position and \ @@ -67,7 +68,7 @@ def dca_or_short_condition(self): self.minusDI[0] > self.params.adxth and \ self.plusDI[0] < self.params.adxth and \ self.supertrend_uptrend_signal - ): + ): if self.params.backtest == False: self.entry_prices.append(self.data.close[0]) @@ -78,13 +79,30 @@ def dca_or_short_condition(self): self.conditions_checked = True elif self.params.backtest == True: self.buy(size=self.stake, price=self.data.close[0], exectype=bt.Order.Market) - self.take_profit_price = self.data.close[-1] * (1 + self.params.take_profit / 100) self.buy_executed = True - self.conditions_checked = True + self.entry_prices.append(self.data.close[0]) + self.sizes.append(self.stake) + self.calc_averages() - def sell_or_cover_condition(self): if self.buy_executed and self.data.close[0] >= self.take_profit_price: + average_entry_price = sum(self.entry_prices) / len(self.entry_prices) if self.entry_prices else 0 + + # Avoid selling at a loss or below the take profit price + if round(self.data.close[0], 9) < round(self.average_entry_price, 9) or round(self.data.close[0], 9) < round(self.take_profit_price, 9): + self.log( + f"| - Avoiding sell at a loss or below take profit. " + f"| - Current close price: {self.data.close[0]:.12f}, " + f"| - Average entry price: {average_entry_price:.12f}, " + f"| - Take profit price: {self.take_profit_price:.12f}" + ) + return + + if self.buy_executed and self.data.close[0] >= self.take_profit_price: + if self.data.close[0] < self.average_entry_price: + print(f'Nothing Todo here. {self.average_entry_price, self.take_profit_price}') + return + if self.params.backtest == False: self.rabbit.send_jrr_close_request(exchange=self.exchange, account=self.account, asset=self.asset) elif self.params.backtest == True: