The YoyoActionStrategy is a trading strategy that uses various indicators to determine buy and sell signals. Here's a breakdown of what the strategy does:
It calculates several indicators based on the input dataframe, including:
'ohlc4': Average of the open, high, low, and close prices. 'ema_fast': Exponential Moving Average with a user-defined period (self.emaFast).
'ema_slow': Exponential Moving Average with a user-defined period (self.emaSlow).
'rsi': Relative Strength Index with a user-defined period (self.rsiPeriod).
'macd': Moving Average Convergence Divergence calculated as the difference between 'ema_fast' and 'ema_slow'. 'bullish': True if 'macd' is greater than 0, indicating a bullish signal. 'bearish': True if 'macd' is less than 0, indicating a bearish signal. 'sl1': Stop Loss calculated as a multiple (self.atrFM) of the Average True Range (ATR) using a user-defined period (self.atrFast). 'sl2': Stop Loss calculated as a multiple (self.atrSM) of the ATR using a user-defined period (self.atrSlow). It initializes additional columns in the dataframe for intermediate calculations and signals. It iterates over each row in the dataframe to populate the intermediate calculations and signals based on the indicator values and previous row values. Based on certain conditions, it calculates the 'trail2' value, which is used in determining the trailing stop loss. It determines various states and signals, such as 'green', 'blue', 'aqua', 'yellow', 'brown', 'red', 'long', 'preBuy', 'short', 'preSell', 'over_sole', 'over_bought', 'greenLine', and 'hold_state', based on the indicator values and previous row values. It drops any rows with missing values (NaN). It calculates the 'signal_buy' and 'signal_sell' columns based on specific conditions related to the 'green', 'red', and 'greenLine' signals. It returns the populated dataframe. Additionally, there are two helper functions:
populate_buy_trend: Sets the 'buy' column to 1 for rows where 'signal_buy' is True. populate_sell_trend: Sets the 'sell' column to 1 for rows where 'signal_sell' is True. Overall, the YoyoActionStrategy backtests a trading strategy by populating indicator values, calculating intermediate values, determining buy and sell signals, and generating buy and sell trends based on those signals.