Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SqueezeMomentum (SM) indicator #8462

Conversation

JosueNina
Copy link
Contributor

Description

This pull request introduces the Squeeze Momentum Indicator (SM), which combines the Bollinger Bands and Keltner Channels to identify periods of low volatility in the market, signaling potential future price movements. The indicator highlights when the market is "squeezed" and a breakout may be imminent

Related Issue

Closes #4166

Motivation and Context

The Squeeze Momentum Indicator is important for traders who wish to identify periods of consolidation in the market. By identifying when volatility is low, it helps to predict possible breakouts, enhancing decision-making for entry and exit points.

Requires Documentation Change

Yes, the documentation should be updated to include usage instructions for the Squeeze Momentum Indicator and explain its parameters.

How Has This Been Tested?

The Squeeze Momentum Indicator has been tested using historical data from the SPY and compared against values generated using the Talib library.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

@JosueNina JosueNina changed the title Implement SqueezeMomentum (SM) indicator Feature 4166 squeeze momentum indicator Implement SqueezeMomentum (SM) indicator Dec 12, 2024
Copy link
Collaborator

@jhonabreul jhonabreul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thank you! Leaving a few minor comments

Indicators/SqueezeMomentum.cs Outdated Show resolved Hide resolved
Indicators/SqueezeMomentum.cs Outdated Show resolved Hide resolved
Algorithm/QCAlgorithm.Indicators.cs Outdated Show resolved Hide resolved
Indicators/SqueezeMomentum.cs Outdated Show resolved Hide resolved
@jhonabreul
Copy link
Collaborator

Pending:

Confirm which implementation of Keltner Channels is correct: using typical price (average of open, high, close) like Lean's KeltnerChannels indicator, or just using the close price as implemented in this new SqueezeMomentum indicator. Ideally we have a single implementation that is reused, like with BollingerBands

@JosueNina
Copy link
Contributor Author

@jhonabreul
The formula for Keltner Channels is EMA + keltnerMultiplier * ATR.
The ATR must use the SMA by default, while the EMA is applied to the Close price. However, it can also use the Typical price, which smooths out price noise by averaging the high, low, and close, making it more reliable in volatile markets.

The python script was updated:

import talib
import pandas as pd

history = pd.read_csv("https://github.com/QuantConnect/Lean/raw/master/Data/equity/usa/daily/spy.zip",
                       index_col=0, names=["open", "high", "low", "close", "volume"])

high = history.high
low = history.low
close = history.close
open = history.open

#BB
high_bb, mid_bb, low_bb = talib.BBANDS(close, 20, 2)
sma_test = talib.SMA(close, 20)
bb = pd.concat([mid_bb, high_bb, low_bb], axis=1)
bb.columns = ['mid_bb', 'bb high', 'bb low']


#KC
tr = talib.TRANGE(high, low, close)
sma_tr = talib.SMA(tr, 20)
t_price = (high + low + close) / 3
ema = talib.EMA(t_price, 20)
upper_kc = ema + sma_tr * 1.5
lower_kc = ema - sma_tr * 1.5
kc = pd.concat([upper_kc, lower_kc], axis=1)
kc.columns = ['kc high', 'kc low']

#SMI
smi = pd.concat([bb, kc], axis=1)
smi['squeeze on'] = ((smi['kc high'] > smi['bb high']) & (smi['kc low'] < smi['bb low'])).apply(lambda x: 1 if x else -1)
smi["open"] = open
smi["high"] = high
smi["low"] = low
smi["close"] = close
smi = smi.iloc[:, [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]]
smi.to_csv("spy_smi.csv", index_label="date")

And here are the references:
Squeeze momentum indicator
Keltner Channels
Keltner Channels with typical price

- Make the Bollinger Bands and Keltner Channels indicators public.
- In IndicatorBase -> Update, if T is IndicatorDataPoint, then create a new
  IndicatorDataPoint.
@JosueNina JosueNina force-pushed the feature-4166-squeeze-momentum-indicator branch from 861c2a4 to 87bb775 Compare December 20, 2024 18:07
@JosueNina JosueNina force-pushed the feature-4166-squeeze-momentum-indicator branch from 87bb775 to 5c4eb42 Compare December 20, 2024 18:09
Copy link
Member

@Martin-Molinero Martin-Molinero left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you 👍

@Martin-Molinero Martin-Molinero merged commit be4a34c into QuantConnect:master Dec 20, 2024
5 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Squeeze Momentum Indicator
3 participants