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

Squeeze Momentum Indicator #4166

Open
3 tasks done
jaredbroad opened this issue Mar 8, 2020 · 9 comments
Open
3 tasks done

Squeeze Momentum Indicator #4166

jaredbroad opened this issue Mar 8, 2020 · 9 comments
Labels
feature good first issue If you're looking to get started as a LEAN contributor, check out these starter issues! indicator

Comments

@jaredbroad
Copy link
Member

jaredbroad commented Mar 8, 2020

Expected Behavior

Supports "John Carter's TTM Squeeze Momentum Indicator"

Actual Behavior

No implementation of "Squeeze Momentum Indicator"

Potential Solution

Implement "Squeeze Momentum Indicator". Add unit tests

Checklist

  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
@jaredbroad jaredbroad added feature indicator good first issue If you're looking to get started as a LEAN contributor, check out these starter issues! labels Mar 8, 2020
@jdharmon
Copy link
Contributor

Is this John Carter's TTM Squeeze?

@jaredbroad
Copy link
Member Author

Yes sorry, edited for clarity

@steve-goswell
Copy link

Is there an ETA on this new indicator?

@jdharmon
Copy link
Contributor

jdharmon commented Dec 2, 2021

@steve-goswell I implemented this a while ago, but haven't had time to get the tests against external data working.

The exact formula for calculating momentum is not public. I based my implementation on what I could find. I think it is close enough to use as an indicator. If you compare my implementation with the indicator in your trade platform, the momentum histogram values will not match. However, increasing, decreasing, and whether you're above or below 0 should be consistent.

Feel free to take a look at Squeeze.cs and SqueezeMomentum.cs in feature-4166-squeeze-momentum.

@steve-goswell
Copy link

many thanks! I'll take a look at the code. are there any plans to add this to the list of indicators?

https://www.quantconnect.com/docs/algorithm-reference/indicators

@AlexCatarino
Copy link
Member

Hi @jdharmon, what is the source of the formulae you used and the data for comparison?

@jdharmon
Copy link
Contributor

@AlexCatarino Links to the formulas are in the comments.

/// <remarks>
/// Adapted formulas from
/// https://school.stockcharts.com/doku.php?id=technical_indicators:ttm_squeeze and
/// https://freethinkscript.blogspot.com/2009/05/ttm-like-squeeze-indicator-with.html
/// </remarks>

When I developed the indicators, I used data downloaded from AlphaVantage and cross-checked against thinkorswim. In my testing, my indicators matched the indicator in thinkorswim. I only picked a few places in recent history where the squeeze fired or the histogram crossed 0.

For unit testing, I used test data from one of the existing TestData files; spy_bollinger_bands.txt, I believe. I could never seem to find the time to go line-by-line in the spy_squeeze.csv file and validate that the data is correct. The bug may be in the test data or in my code. I'm not sure.

@LouisSzeto
Copy link
Collaborator

spy_smi.csv

@LouisSzeto
Copy link
Collaborator

LouisSzeto commented Oct 24, 2023

spy_smi.csv
Columns: "SPY high" "SPY low" "SPY close" "SMA" "BB high" "BB low" "KC high" "KC low" "Squeeze on?"

Script:

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

high_bb, sma, low_bb = talib.BBANDS(close, 20, 2)
bb = pd.concat([sma, high_bb, low_bb], axis=1).dropna()
bb.columns = ['sma', 'bb high', 'bb low']

tr = talib.TRANGE(high, low, close)
sma_tr = talib.SMA(tr, 20)
upper_kc = sma + sma_tr * 1.5
lower_kc = sma - sma_tr * 1.5
kc = pd.concat([upper_kc, lower_kc], axis=1).dropna()
kc.columns = ['kc high', 'kc low']

smi = pd.concat([bb, kc], axis=1).dropna()
smi['squeeze on'] = (smi['kc high'] > smi['bb high']) & (smi['kc low'] < smi['bb low'])
smi["high"] = high
smi["low"] = low
smi["close"] = close
smi = smi.iloc[:, [6, 7, 8, 0, 1, 2, 3, 4, 5]]
smi.to_csv("spy_smi.csv", header=False)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature good first issue If you're looking to get started as a LEAN contributor, check out these starter issues! indicator
Projects
None yet
Development

No branches or pull requests

5 participants