Saber2pr's Blog

FreqtradeTipsToAvoidSlippage

In automated trading, slippage is a common issue, especially in volatile markets. Slippage refers to the difference between the expected price of a trade and the actual price at which the trade is executed. This difference can negatively impact the performance of a strategy. Therefore, how to effectively avoid slippage becomes an important aspect of optimizing trading strategies. In this article, we will introduce how to avoid slippage in Freqtrade using simple logic to improve strategy stability and profitability.

1. What is Slippage?

Slippage usually occurs in the following situations:

  • High Market Volatility : Especially in high-frequency trading, where prices fluctuate rapidly.

  • Insufficient Liquidity : When there is a large spread between the buy and sell prices, making it impossible to execute trades at the expected price.

  • Execution Delay : A time delay between the strategy’s analysis and decision-making process and the actual execution of the trade.

These factors can impact the execution price of a trade, causing the actual buy or sell price to deviate from the expected price, thus affecting the strategy's profitability.

2. Common Tips for Avoiding Slippage in Freqtrade

2.1 Compare Current Price with the Last Candle’s Close

To minimize the impact of slippage, you can compare the current market price with the close price of the last completed candle. Generally, if the current price is higher than the close price of the last candle, it indicates that the market has already moved, which could lead to slippage. Therefore, only allow entering the market when the price change is within an acceptable range. Code Example:

def confirm_trade_entry(self, pair: str, order_type: str, amount: float, rate: float,
                        time_in_force: str, current_time: datetime, entry_tag: Optional[str],
                        **kwargs) -> bool:
    dataframe, _ = self.dp.get_analyzed_dataframe(pair, self.timeframe)

    if len(dataframe) < 1:
        return False

    last_candle = dataframe.iloc[-1]

    if 'close' not in last_candle:
        log.error("Missing 'close' data for pair %s", pair)
        return False

    if rate > last_candle['close']:
        slippage = (rate / last_candle['close']) - 1.0

        if slippage < 0.038:
            log.info("Allowing buy for %s with slippage %s", pair, slippage)
            return True
        else:
            log.warning(
                "Cancelling buy for %s due to excessive slippage %.2f%%",
                pair, slippage * 100
            )
            return False

    log.info("Allowing buy for %s, rate is less than or equal to close price", pair)
    return True

In this strategy, the close price of the last candle is first retrieved, and then compared with the current market price. If the current price exceeds the close price by a certain percentage (e.g., 3.8%), it is considered that slippage may occur, and the entry will be skipped.

2.2 Adjust Slippage Tolerance Dynamically

The tolerance for slippage can be adjusted depending on the market conditions. For instance, if the market volatility is high, you may allow a higher slippage tolerance. Conversely, in a more stable market, you can decrease the tolerance. This ensures that the strategy can adapt intelligently to different market environments. Code Adjustment Example:

def adjust_slippage_threshold(self, market_volatility: float) -> float:
    """
    Dynamically adjust slippage tolerance based on market volatility
    """
    if market_volatility > 0.02:  # If volatility is greater than 2%, increase tolerance
        return 0.05  # Higher tolerance
    else:
        return 0.038  # Default tolerance

In practice, volatility can be measured using technical indicators like the Average True Range (ATR). By dynamically adjusting the slippage tolerance based on market volatility, the strategy can avoid excessive slippage while remaining adaptable to changing market conditions.

2.3 Use Higher Timeframes

When trading on lower timeframes (such as 1-minute or 5-minute charts), the frequent price fluctuations can lead to higher slippage. Consider using higher timeframes (e.g., 15-minute or 30-minute) for trading. This helps to reduce the impact of short-term price movements and improves the strategy's stability.

2.4 Delay Processing and Batch Execution

In real-world trading, the time gap between strategy decision-making and actual trade execution is also a source of slippage. If possible, delay the decision-making process and wait for market fluctuations to settle before executing the trade. This can help reduce the slippage caused by sudden market movements.

3. Conclusion

Slippage is a key factor that affects the effectiveness of trading strategies, especially in automated trading. By comparing the current price with the close price of the last candle, dynamically adjusting slippage tolerance, and other techniques, we can effectively avoid excessive slippage and improve the execution efficiency and stability of the strategy.

  • Compare price differences : Avoid entering the market when there is a large price movement compared to the last candle’s close.

  • Dynamic adjustment of tolerance : Adjust slippage tolerance based on market volatility to ensure the strategy adapts to different market conditions.

  • Increase data timeframe : Reduce slippage caused by low timeframes with frequent price changes.

  • Delay execution : Reduce slippage by allowing market fluctuations to stabilize before executing trades.

By implementing these tips, we can better control slippage in Freqtrade, leading to more stable and profitable strategies.