The strategy generates a target signal for the LSTM model by combining technical indicators into a single score. The process follows these steps:
- Calculate Indicators: Use
ta (Technical Analysis library) to compute indicators like SMA, MACD, RSI, Bollinger Bands, etc. - Normalize Indicators: Convert indicators into z-scores (subtracting rolling mean and dividing by rolling standard deviation) so they are comparable and weightable.
- Apply Dynamic Weighting: Adjust weights based on market conditions (e.g., increasing momentum weight during strong trends).
- Calculate Aggregate Score (S): A weighted sum of all normalized indicators.
- Apply Market Regime Filter (R): Use Bollinger Bands or Moving Averages to determine if the market is trending or ranging (assigning values like 1, -1, or 0).
- Apply Volatility Adjustment (V): Scale the score based on volatility (e.g., using inverse Bollinger Band width or inverse ATR).
- Compute Final Target (T): Multiply the aggregate score by the regime filters and volatility adjustments.
The final value is assigned to the &-target column for the AI model to learn.
# Step 0: Calculate new indicators
dataframe['ma'] = ta.SMA(dataframe, timeperiod=10)
dataframe['roc'] = ta.ROC(dataframe, timeperiod=2)
dataframe['macd'], dataframe['macdsignal'], dataframe['macdhist'] = ta.MACD(dataframe['close'], slowperiod=12, fastperiod=26)
dataframe['momentum'] = ta.MOM(dataframe, timeperiod=4)
dataframe['rsi'] = ta.RSI(dataframe, timeperiod=10)
bollinger = ta.BBANDS(dataframe, timeperiod=20)
dataframe['bb_upperband'] = bollinger['upperband']
dataframe['bb_middleband'] = bollinger['middleband']
dataframe['bb_lowerband'] = bollinger['lowerband']
dataframe['cci'] = ta.CCI(dataframe, timeperiod=20)
dataframe['stoch'] = ta.STOCH(dataframe)['slowk']
dataframe['atr'] = ta.ATR(dataframe, timeperiod=14)
dataframe['obv'] = ta.OBV(dataframe)
# Step 1: Normalize Indicators (Z-Score)
dataframe['normalized_stoch'] = (dataframe['stoch'] - dataframe['stoch'].rolling(window=14).mean()) / dataframe['stoch'].rolling(window=14).std()
# ... (other normalizations) ...
# Step 2: Calculate aggregate score S
w = [self.w0.value, self.w1.value, self.w2.value, self.w3.value, self.w4.value, self.w5.value, self.w6.value, self.w7.value, self.w8.value]
dataframe['S'] = w[0] * dataframe['normalized_ma'] + ... + dataframe['w_momentum'] * dataframe['normalized_momentum']
# Step 3: Market Regime Filter R
dataframe['R'] = 0
dataframe.loc[(dataframe['close'] > dataframe['bb_middleband']) & (dataframe['close'] > dataframe['bb_upperband']), 'R'] = 1
dataframe.loc[(dataframe['close'] < dataframe['bb_middleband']) & (dataframe['close'] < dataframe['bb_lowerband']), 'R'] = -1
# Step 4: Volatility Adjustment V
bb_width = (dataframe['bb_upperband'] - dataframe['bb_lowerband']) / dataframe['bb_middleband']
dataframe['V'] = 1 / bb_width
# Final Target
dataframe['T'] = dataframe['S'] * dataframe['R'] * dataframe['V'] * dataframe['R2'] * dataframe['V2']
dataframe['&-target'] = dataframe['T']