TTM Squeeze Histogram Backtester For ThinkOrSwim

useThinkScript

Administrative
Staff member
Staff
VIP
Lifetime
TOS Indicators - Retina Logo Dark TTM Squeeze Histogram Backtester
https://tosindicators.com/backtesters/ttm-squeeze-histogram

Code:
input consecutiveHistogramBars = 7;
input RSIOverboughtVal = 60;
input RSIOversoldVal = 40;
input longEntries = yes;
input shortEntries = yes;

def histogram = TTM_Squeeze().histogram;

#Cyan, Blue, Yellow and Red Histogram Bar Definitions
def cyan = histogram > histogram[1] and histogram > 0;
def blue = histogram < histogram[1] and histogram > 0;
def red = histogram < histogram[1] and histogram < 0;
def yellow = histogram > histogram[1] and histogram < 0;

#Consecutive Momentum Histogram Bars
def minCyanBars = Sum(cyan, consecutiveHistogramBars) >= consecutiveHistogramBars;
def minRedBars = Sum(red, consecutiveHistogramBars) >= consecutiveHistogramBars;

#Momentum Shifts in Histogram
def momoDown = minCyanBars[1] and blue;
def momoUp = minRedBars[1] and yellow;

#RSI
def RSIOverbought = RSI() > RSIOverboughtVal;
def RSIOversold = RSI() < RSIOversoldVal;

AddOrder(OrderType.Buy_To_Open, longEntries and momoUp and RSIOversold, open[-1]);
AddOrder(OrderType.Sell_To_Close, histogram < histogram[1], open[-1]);

AddOrder(OrderType.Sell_To_Open, shortEntries and momoDown and RSIOverbought, open[-1]);
AddOrder(OrderType.Buy_To_Close, histogram > histogram[1], open[-1]);
 
So I have come across a back tester for TTM_Squeeze not my code but was wondering if the community can help tweak this below. This is were I found the code from the video link- https://tosindicators.com/indicators/ttm-squeeze-backtester

Original Code below:

Code:
input length = 20;
input nk = 1.5;
input nBB = 2.0;

def squeeze = TTM_Squeeze(close, length, nk, nBB).SqueezeAlert;

def EMA13 = ExpAverage(close, 13);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);

def bullish = EMA13 > EMA21 and EMA21 > EMA34;
def bearish = EMA13 < EMA21 and EMA21 < EMA34;

def fiveDotSqueeze = Sum(squeeze, 5) == 0;

def bullishSignal = if fiveDotSqueeze and bullish then 1 else 0;
def bearishSignal = if fiveDotSqueeze and bearish then 1 else 0;

def exitBullishTrade = if EMA13 < EMA21 and EMA13[1] >= EMA21[1] then 1 else 0;
def exitBearishTrade = if EMA13 > EMA21 and EMA13[1] <= EMA21[1] then 1 else 0;

AddOrder(OrderType.Buy_To_Open, BullishSignal, open[-1], 100);
AddOrder(OrderType.Sell_To_Close, exitBullishTrade,open[-1], 100);

AddOrder(OrderType.Sell_To_Open, bearishSignal,open[-1], 100);
AddOrder(OrderType.Buy_To_Close, exitBearishTrade,open[-1], 100);

Couple of things to insert below: need help with the code

Create:

1. alert when to buy to open is true
2. alert when to sell to close is true

Not sure how this can be done but the code below if true then override the alerts above ? In the image below you can see the purple trades means it was one or the codes below. I hope that makes sense if not let me know..

0Bm8E2U.png


Send Alerts: to phone/email or forward the message log to txt sms? Would it be possible to send when an alert- buy to open vice verse sell to close? Or
if someone can help me create it in marketwatch and create an alert?
 
Last edited by a moderator:
Try this:

Code:
# Alerts
Alert(BullishSignal, " ", Alert.Bar, Sound.Chimes);
Alert(exitBullishTrade, " ", Alert.Bar, Sound.Bell);
 
If this is true or vice verse if bearish
Code:
def bullishSignal = if fiveDotSqueeze and bullish then 1 else 0

I believe that is what makes an add order pop on the screen so in turn plot an arrow similar to what the add order does ? I am trying to replicate the strategy into a scan so I can make a watchlist, Then when a stock appears into scan watchlist I can send an txt to my phone. and can do the same when its a bearish signal to remove. Am i approaching this the right way @BenTen ?
 
@Squeeze I'm going to assume that you want to scan for the following:

Code:
def bullishSignal = if fiveDotSqueeze and bullish then 1 else 0;
def bearishSignal = if fiveDotSqueeze and bearish then 1 else 0;

Here is the new script that you need to add as an indicator:

Code:
input length = 20;
input nk = 1.5;
input nBB = 2.0;

def squeeze = TTM_Squeeze(close, length, nk, nBB).SqueezeAlert;

def EMA13 = ExpAverage(close, 13);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);

def bullish = EMA13 > EMA21 and EMA21 > EMA34;
def bearish = EMA13 < EMA21 and EMA21 < EMA34;

def fiveDotSqueeze = Sum(squeeze, 5) == 0;

def bullishSignal = if fiveDotSqueeze and bullish then 1 else 0;
def bearishSignal = if fiveDotSqueeze and bearish then 1 else 0;

plot bull = bullishSignal;
bull.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot bear = bearishSignal;
bear.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

This will plot up and down arrows on your chart. You can set up the scanner to something like this:

MhdSa9G.png


If you still need help with the scanner, watch the tutorial below:


In order to get alerts through your phone, you need to save your scanner as a watchlist and select the following option: Alerts when scan results change....

1VyQPrW.png
 
I could use fresh pair of eyes why my code is not working.

Thanks in advance for anyone willing to help!

1) I'm using the code from the top post.
2) I'm trying to convert it to chart indicator and scanner. For some reason, it appears the match logic of the RSI levels and histogram are not matching as they did in the back tester.
Here is my version to change the order logic to chart bubble.
Code:
input consecutiveHistogramBars = 7;
input RSIOverboughtVal = 60;
input RSIOversoldVal = 40;
input longEntries = yes;
input shortEntries = yes;

def histogram = TTM_Squeeze().histogram;

#Cyan, Blue, Yellow and Red Histogram Bar Definitions
def cyan = histogram > histogram[1] and histogram > 0;
def blue = histogram < histogram[1] and histogram > 0;
def red = histogram < histogram[1] and histogram < 0;
def yellow = histogram > histogram[1] and histogram < 0;

#Consecutive Momentum Histogram Bars
def minCyanBars = Sum(cyan, consecutiveHistogramBars) >= consecutiveHistogramBars;
def minRedBars = Sum(red, consecutiveHistogramBars) >= consecutiveHistogramBars;

#Momentum Shifts in Histogram
def momoDown = minCyanBars[1] and blue;
def momoUp = minRedBars[1] and yellow;

#RSI
def RSIOverbought = RSI() > RSIOverboughtVal;
def RSIOversold = RSI() < RSIOversoldVal;

# Plot Chart Bubbles Instead of Orders
AddChartBubble(longEntries and momoUp and RSIOversold, close, "Long", Color.GREEN, yes);
AddChartBubble(histogram < histogram[1], close, "Sell Close", Color.RED, no);
AddChartBubble(shortEntries and momoDown and RSIOverbought, close, "Short", Color.ORANGE, yes);
AddChartBubble(histogram > histogram[1], close, "Buy Close", Color.CYAN, no);

I place arrow on the screenshot where the only bubble should be located.

1740254779363.png
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
529 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top