Skip to content

Commit

Permalink
TakeProfit Calculation fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aLca committed Jun 14, 2024
1 parent 7655ff2 commit df56d3b
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 20 deletions.
14 changes: 9 additions & 5 deletions live_strategys/QQE_Hullband_VolumeOsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:
Expand Down
41 changes: 32 additions & 9 deletions template_strategys/DCA_QQE_Example_backtesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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:
Expand Down
30 changes: 24 additions & 6 deletions template_strategys/SuperTrend_Scalp.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,18 @@ 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 \
self.adx[0] >= self.params.adxth and \
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])
Expand All @@ -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:
Expand Down

0 comments on commit df56d3b

Please sign in to comment.