From 87279b9ba5a5a31fed4a11b9b6890b8c55980417 Mon Sep 17 00:00:00 2001 From: Kavinkumar <33546454+mkavinkumar1@users.noreply.github.com> Date: Tue, 22 Feb 2022 10:45:07 +0530 Subject: [PATCH 1/4] sell last rebuy with trailing stop loss --- NostalgiaForInfinityX.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/NostalgiaForInfinityX.py b/NostalgiaForInfinityX.py index 920e48ee80..919544a3e0 100644 --- a/NostalgiaForInfinityX.py +++ b/NostalgiaForInfinityX.py @@ -15,6 +15,7 @@ from typing import Dict from freqtrade.persistence import Trade from datetime import datetime, timedelta +from decimal import Decimal from technical.util import resample_to_interval, resampled_merge from technical.indicators import RMI, zema, VIDYA, ichimoku import time @@ -110,7 +111,7 @@ class NostalgiaForInfinityX(IStrategy): INTERFACE_VERSION = 2 def version(self) -> str: - return "v11.0.269" + return "v11.0.270" # ROI table: minimal_roi = { @@ -127,6 +128,8 @@ def version(self) -> str: use_custom_stoploss = False + custom_info = {} + # Optimal timeframe for the strategy. timeframe = '5m' res_timeframe = 'none' @@ -2293,6 +2296,10 @@ def custom_stake_amount(self, pair: str, current_time: datetime, current_rate: f else: return proposed_stake + def calc_profit_ratio2(self, open_rate: float, current_rate: float, trade: Trade) ->float: + return float(( Decimal(1-trade.fee_close)* Decimal(current_rate) )/ + (Decimal(1+trade.fee_open)* Decimal(open_rate) )-1) + def adjust_trade_position(self, trade: Trade, current_time: datetime, current_rate: float, current_profit: float, min_stake: float, max_stake: float, **kwargs): @@ -2333,7 +2340,7 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, if (count_of_buys == 1): if ( - (current_profit > -0.08) + (current_profit > -0.03) or ( (last_candle['crsi'] < 12.0) ) @@ -2341,7 +2348,7 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, return None elif (count_of_buys == 2): if ( - (current_profit > -0.14) + (current_profit > -0.04) or ( (last_candle['crsi'] < 20.0) or (last_candle['crsi_1h'] < 11.0) @@ -2373,7 +2380,16 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, return stake_amount except Exception as exception: return None - + max_profit2=self.custom_info.get(trade.pair,0) + last_buy_order = filled_buys[-1] + last_open_rate = last_buy_order.average or last_buy_order.price + current_profit2=self.calc_profit_ratio2(last_open_rate, current_rate, trade) + if current_time.second %10 == 0: log.info(trade.pair, max_profit2, current_profit2) + if max_profit2.005-0.0025*(count_of_buys-2) and current_profit2 > 0 and count_of_buys > 1: + self.custom_info[trade.pair]=0 + last_stake_amt=last_buy_order.amount + return -last_stake_amt return None def sell_signals(self, current_profit: float, max_profit:float, max_loss:float, last_candle, previous_candle_1, previous_candle_2, previous_candle_3, previous_candle_4, previous_candle_5, trade: 'Trade', current_time: 'datetime', buy_tag) -> tuple: From b44db04e58487ca62a596ca922d1db4fe8319f4c Mon Sep 17 00:00:00 2001 From: Kavinkumar <33546454+mkavinkumar1@users.noreply.github.com> Date: Tue, 22 Feb 2022 11:57:06 +0530 Subject: [PATCH 2/4] fix logic --- NostalgiaForInfinityX.py | 52 +++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/NostalgiaForInfinityX.py b/NostalgiaForInfinityX.py index 919544a3e0..d7c1576949 100644 --- a/NostalgiaForInfinityX.py +++ b/NostalgiaForInfinityX.py @@ -2324,6 +2324,21 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, if (self.position_adjustment_enable == False) or (current_profit > -0.03): return None + filled_buys = trade.select_filled_orders('buy') + count_of_buys = len(filled_buys) + max_profit2=self.custom_info.get(trade.pair,0) + last_buy_order = filled_buys[-1] + last_open_rate = last_buy_order.average or last_buy_order.price + current_profit2=self.calc_profit_ratio2(last_open_rate, current_rate, trade) + # if current_time.second %10 == 0: log.info('partial_sell ' + ', '.join(map(str,(trade.pair, max_profit2, current_profit2, last_open_rate, current_rate)))) + if max_profit2.005-0.0025*(count_of_buys-2) and current_profit2 > 0 and count_of_buys > 1: + self.custom_info[trade.pair]=0 + last_stake_amt=last_buy_order.amount + return -last_stake_amt + + if count_of_buys > self.max_rebuy_orders and self.dp.runmode.value not in ('backtest','dry_run'): + return dataframe, _ = self.dp.get_analyzed_dataframe(trade.pair, self.timeframe) last_candle = dataframe.iloc[-1].squeeze() previous_candle = dataframe.iloc[-2].squeeze() @@ -2335,12 +2350,9 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, ): return None - filled_buys = trade.select_filled_orders('buy') - count_of_buys = len(filled_buys) - if (count_of_buys == 1): if ( - (current_profit > -0.03) + (current_profit > -0.08) or ( (last_candle['crsi'] < 12.0) ) @@ -2348,7 +2360,7 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, return None elif (count_of_buys == 2): if ( - (current_profit > -0.04) + (current_profit > -0.14) or ( (last_candle['crsi'] < 20.0) or (last_candle['crsi_1h'] < 11.0) @@ -2369,27 +2381,19 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, # Log if the last candle triggered a buy signal, even if max rebuys reached if last_candle['buy'] == 1 and self.dp.runmode.value in ('backtest','dry_run'): log.info(f"Rebuy: a buy tag found for pair {trade.pair}") + if count_of_buys > self.max_rebuy_orders: + return # Maximum 2 rebuys. Half the stake of the original. - if 0 < count_of_buys <= self.max_rebuy_orders: - try: - # This returns first order stake size - stake_amount = filled_buys[0].cost - # This then calculates current safety order size - stake_amount = stake_amount * (0.35 + (count_of_buys * 0.005)) - return stake_amount - except Exception as exception: - return None - max_profit2=self.custom_info.get(trade.pair,0) - last_buy_order = filled_buys[-1] - last_open_rate = last_buy_order.average or last_buy_order.price - current_profit2=self.calc_profit_ratio2(last_open_rate, current_rate, trade) - if current_time.second %10 == 0: log.info(trade.pair, max_profit2, current_profit2) - if max_profit2.005-0.0025*(count_of_buys-2) and current_profit2 > 0 and count_of_buys > 1: - self.custom_info[trade.pair]=0 - last_stake_amt=last_buy_order.amount - return -last_stake_amt + try: + # This returns first order stake size + stake_amount = filled_buys[0].cost + # This then calculates current safety order size + stake_amount = stake_amount * (0.35 + (count_of_buys * 0.005)) + return stake_amount + except Exception as exception: + return None + return None def sell_signals(self, current_profit: float, max_profit:float, max_loss:float, last_candle, previous_candle_1, previous_candle_2, previous_candle_3, previous_candle_4, previous_candle_5, trade: 'Trade', current_time: 'datetime', buy_tag) -> tuple: From 41a9508d17cf87bb6c754542f0bf434cc335fb34 Mon Sep 17 00:00:00 2001 From: Kavinkumar <33546454+mkavinkumar1@users.noreply.github.com> Date: Tue, 22 Feb 2022 12:01:33 +0530 Subject: [PATCH 3/4] sell rework --- NostalgiaForInfinityX.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NostalgiaForInfinityX.py b/NostalgiaForInfinityX.py index d7c1576949..4395c13bec 100644 --- a/NostalgiaForInfinityX.py +++ b/NostalgiaForInfinityX.py @@ -2352,7 +2352,7 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, if (count_of_buys == 1): if ( - (current_profit > -0.08) + (current_profit > -0.03) or ( (last_candle['crsi'] < 12.0) ) @@ -2360,7 +2360,7 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, return None elif (count_of_buys == 2): if ( - (current_profit > -0.14) + (current_profit > -0.04) or ( (last_candle['crsi'] < 20.0) or (last_candle['crsi_1h'] < 11.0) From 5d26a7e4dd1ecd8d7c016435bfb9c0d070e29e25 Mon Sep 17 00:00:00 2001 From: Kavinkumar <33546454+mkavinkumar1@users.noreply.github.com> Date: Tue, 22 Feb 2022 12:23:37 +0530 Subject: [PATCH 4/4] fix spaces --- NostalgiaForInfinityX.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NostalgiaForInfinityX.py b/NostalgiaForInfinityX.py index 4395c13bec..a5ab4e95c7 100644 --- a/NostalgiaForInfinityX.py +++ b/NostalgiaForInfinityX.py @@ -2338,7 +2338,7 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, return -last_stake_amt if count_of_buys > self.max_rebuy_orders and self.dp.runmode.value not in ('backtest','dry_run'): - return + return dataframe, _ = self.dp.get_analyzed_dataframe(trade.pair, self.timeframe) last_candle = dataframe.iloc[-1].squeeze() previous_candle = dataframe.iloc[-2].squeeze() @@ -2393,7 +2393,7 @@ def adjust_trade_position(self, trade: Trade, current_time: datetime, return stake_amount except Exception as exception: return None - + return None def sell_signals(self, current_profit: float, max_profit:float, max_loss:float, last_candle, previous_candle_1, previous_candle_2, previous_candle_3, previous_candle_4, previous_candle_5, trade: 'Trade', current_time: 'datetime', buy_tag) -> tuple: