How to disable auto pairlock

Warning!!! This trick can’t be backtested, so do use it at your own risk.

Credit to Atakan on Freqtrade’s discord server, modified to match current freqtrade.

Why do you want to disable auto pairlock? Usually this would happen when you are trading futures and you want to instantly switch trade direction (from long to short, or vice versa). By default, Freqtrade have 1 candle auto lock, so if you exited a trade, you have to wait one candle before you can enter a new trade with opposite direction. For some people, the lag of one candle can be seen as too much.

So to use this snippet, on top of the strategy, make sure these imports exist

from freqtrade.persistence import Trade
from datetime import datetime

Then put this anywhere inside your strategy

custom_info = {}

def bot_loop_start(self, current_time: datetime, **kwargs) -> None:

    for pair in list(self.custom_info):
        if "unlock_me" in self.custom_info[pair]:
            message = f"Found reverse position signal - unlocking {pair}"
            self.dp.send_msg(message)
            self.unlock_pair(pair)
            del self.custom_info[pair]

def confirm_trade_exit(self, pair: str, trade: Trade, order_type: str, amount: float,
                       rate: float, time_in_force: str, exit_reason: str,
                       current_time: datetime, **kwargs) -> bool:
    
    dataframe, _ = self.dp.get_analyzed_dataframe(trade.pair, self.timeframe)
    last_candle = dataframe.iloc[-1].squeeze()
    if trade.is_short:
        if last_candle['enter_long'] == 1:
            if not pair in self.custom_info:
                self.custom_info[pair] = {}
            self.custom_info[pair]["unlock_me"] = True
    else:
        if last_candle['enter_short'] == 1:
            if not pair in self.custom_info:
                self.custom_info[pair] = {}
            self.custom_info[pair]["unlock_me"] = True
    
    return True

2 Comments

  1. What if I need to do for spot trades?
    The logic is to wait until the day closes, then evaluate if there is a profit to exit in that past candle and then, exit the trade. If the pair still in the volume pair list and with a entry signal, then enter again.

Leave a Reply

Your email address will not be published. Required fields are marked *