J2897

Strategy Conceptualisation - SMA Crossover Signals

Jan 31st, 2023 (edited)
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!

The strategy below is simply to understand the signal implementation, and not to trade live...

The SMA Crossover strategy uses two SMAs (simple moving averages) of different lengths, fast_sma and slow_sma, to determine entry and exit signals. The faster SMA reflects short-term price trends, whereas the slower SMA reflects longer-term price trends.

from pandas import DataFrame
from freqtrade.strategy.interface import IStrategy

class SMACrossover(IStrategy):
    """
        Enter and exit based on crossover of fast and slow Simple Moving Averages.
    """
    INTERFACE_VERSION: int = 3
    minimal_roi = {"0": 10}   # Close immediately at 1000% ROI.
    stoploss = -1             # SL at -100%.
    timeframe = '4h'
    fast_sma = 50
    slow_sma = 200

    def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe['fast_sma'] = dataframe['close'].rolling(window=self.fast_sma).mean()
        dataframe['slow_sma'] = dataframe['close'].rolling(window=self.slow_sma).mean()
        return dataframe

    def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[(
            (dataframe['fast_sma'] > dataframe['slow_sma']) &
            (dataframe['fast_sma'].shift(1) < dataframe['slow_sma'].shift(1))
        ), 'enter_long'] = 1
        return dataframe

    def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
        dataframe.loc[(
            (dataframe['fast_sma'] < dataframe['slow_sma']) &
            (dataframe['fast_sma'].shift(1) > dataframe['slow_sma'].shift(1))
        ), 'exit_long'] = 1
        return dataframe

Explanation of the code:

In the populate_entry_trend method, dataframe.loc[(...), 'enter_long'] = 1 sets the enter_long column to 1 for candles where:

  • The fast SMA is above the slow SMA, indicating a bullish trend.
  • The previous candle had the fast SMA below the slow SMA, indicating a crossover.

In the populate_exit_trend method, dataframe.loc[(...), 'exit_long'] = 1 sets the exit_long column to 1 for candles where:

  • The fast SMA is below the slow SMA, indicating a bearish trend.
  • The previous candle had the fast SMA above the slow SMA, indicating a crossover.

So the .loc method simply modifies the values of specific rows in the dataframe.

The strategy determines entry signals based on two conditions:

  1. dataframe['fast_sma'] > dataframe['slow_sma']
    The fast SMA must be above the slow SMA.
  2. dataframe['fast_sma'].shift(1) < dataframe['slow_sma'].shift(1)
    The previous candle must have the fast SMA below the slow SMA.

The strategy determines exit signals based on two conditions:

  1. dataframe['fast_sma'] < dataframe['slow_sma']
    The fast SMA must be below the slow SMA.
  2. dataframe['fast_sma'].shift(1) > dataframe['slow_sma'].shift(1)
    The previous candle must have the fast SMA above the slow SMA.
Add Comment
Please, Sign In to add comment