Skip to content
All library documents

EMA and OBV Signals for a Short-Term Trend-Following Strategy

Code Freqtrade

Summary

This example outlines a short-term strategy that combines a 20-period exponential moving average with On-Balance Volume. It enters long when price crosses above the average and OBV rises, and enters short when price crosses below the average and OBV falls. Exit signals use a price cross in the opposite direction together with an OBV move that confirms the specified exit condition. The example also supplies a return-on-investment schedule, a stop loss, and trailing-stop settings, giving a starting point for a Freqtrade strategy.

The code sets a five-minute timeframe, but does not include backtest results, asset selection, trading fees, slippage assumptions, or a rationale for its risk settings. It is a template rather than a validated system; its crossover and volume rules may behave differently across markets and conditions, and the example provides no evidence of profitability.

Key ideas

  • The strategy uses a 20-period exponential moving average and OBV to define entry signals.
  • Long entries require an upward price cross and rising OBV, while short entries use the opposite pattern.
  • Exit signals combine an opposing price cross with a specified OBV condition.
  • The example includes ROI targets, a stop loss, and trailing-stop parameters, but gives no test results.

Tags

Full text
# TrendFollowingStrategy.py


```py
from functools import reduce
from pandas import DataFrame
from freqtrade.strategy import IStrategy

import talib.abstract as ta

from freqtrade.strategy.interface import IStrategy

class TrendFollowingStrategy(IStrategy):

    INTERFACE_VERSION: int = 3
    # ROI table:
    minimal_roi = {"0": 0.15, "30": 0.1, "60": 0.05}
    # minimal_roi = {"0": 1}

    # Stoploss:
    stoploss = -0.265

    # Trailing stop:
    trailing_stop = True
    trailing_stop_positive = 0.05
    trailing_stop_positive_offset = 0.1
    trailing_only_offset_is_reached = False

    timeframe = "5m"

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # Calculate OBV
        dataframe['obv'] = ta.OBV(dataframe['close'], dataframe['volume'])
        
        # Add your trend following indicators here
        dataframe['trend'] = dataframe['close'].ewm(span=20, adjust=False).mean()
        
        return dataframe
    
    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # Add your trend following buy signals here
        dataframe.loc[
            (dataframe['close'] > dataframe['trend']) & 
            (dataframe['close'].shift(1) <= dataframe['trend'].shift(1)) &
            (dataframe['obv'] > dataframe['obv'].shift(1)), 
            'enter_long'] = 1
        
        # Add your trend following sell signals here
        dataframe.loc[
            (dataframe['close'] < dataframe['trend']) & 
            (dataframe['close'].shift(1) >= dataframe['trend'].shift(1)) &
            (dataframe['obv'] < dataframe['obv'].shift(1)), 
            'enter_short'] = 1
        
        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        # Add your trend following exit signals for long positions here
        dataframe.loc[
            (dataframe['close'] < dataframe['trend']) & 
            (dataframe['close'].shift(1) >= dataframe['trend'].shift(1)) &
            (dataframe['obv'] > dataframe['obv'].shift(1)), 
            'exit_long'] = 1
        
        # Add your trend following exit signals for short positions here
        dataframe.loc[
            (dataframe['close'] > dataframe['trend']) & 
            (dataframe['close'].shift(1) <= dataframe['trend'].shift(1)) &
            (dataframe['obv'] < dataframe['obv'].shift(1)), 
            'exit_short'] = 1
        
        return dataframe


```

Shown in full with attribution under the source's licence. Licence: GPL-3.0

This summary was written by Stratmill's research agent from the original; it is not a copy of the source.