The SMAOffsetProtectOpt strategy is designed to backtest trading decisions based on various indicators. Here's a breakdown of what the strategy does:
In the populate_indicators method:
Calculates exponential moving averages (EMAs) for different time periods (base_nb_candles_buy and base_nb_candles_sell) and adds them as columns to the input dataframe. Computes the Elder's Force Index (EWO) using the EWO function and adds it as a column to the dataframe.
Calculates the Relative Strength Index (RSI) with a time period of 14 and adds it as a column to the dataframe.
Returns the updated dataframe with the added indicators.
In the populate_buy_trend method:
Initializes an empty list called conditions. Computes the lower offset moving average (ma_buy) by multiplying the specified EMA (ma_buy_{self.base_nb_candles_buy.value}) with the low offset value and adds it as a column to the dataframe. Appends a condition to the conditions list:
Checks if the closing price is below the ma_buy. Checks if the EWO value is above a certain threshold (ewo_high). Checks if the RSI value is below a certain threshold (rsi_buy). Checks if the trading volume is greater than zero. Appends another condition to the conditions list:
Checks if the closing price is below the ma_buy. Checks if the EWO value is below a certain threshold (ewo_low). Checks if the trading volume is greater than zero. If any of the conditions in the conditions list are met, assigns a value of 1 to the "buy" column in the dataframe. Returns the updated dataframe with the "buy" column. In the populate_sell_trend method:
Initializes an empty list called conditions. Computes the upper offset moving average (ma_sell) by multiplying the specified EMA (ma_sell_{self.base_nb_candles_sell.value}) with the high offset value and adds it as a column to the dataframe. Appends a condition to the conditions list:
Uses the qtpylib.crossed_below function to check if the closing price crossed below the ma_sell. Checks if the trading volume is greater than zero. If the condition in the conditions list is met, assigns a value of 1 to the "sell" column in the dataframe. Returns the updated dataframe with the "sell" column. Overall, this strategy identifies buy signals based on the crossing of the price below a lower offset moving average (ma_buy), along with additional conditions related to EWO, RSI, and volume. It also identifies sell signals based on the crossing of the price below an upper offset moving average (ma_sell) and volume condition.