Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sell last rebuy with trailing stop loss #201

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 35 additions & 15 deletions NostalgiaForInfinityX.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = {
Expand All @@ -127,6 +128,8 @@ def version(self) -> str:

use_custom_stoploss = False

custom_info = {}

# Optimal timeframe for the strategy.
timeframe = '5m'
res_timeframe = 'none'
Expand Down Expand Up @@ -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):
Expand All @@ -2317,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<current_profit2: self.custom_info[trade.pair]=current_profit2
if max_profit2-current_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()
Expand All @@ -2328,20 +2350,17 @@ 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.08)
(current_profit > -0.03)
or (
(last_candle['crsi'] < 12.0)
)
):
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)
Expand All @@ -2362,17 +2381,18 @@ 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
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

Expand Down