Skip to main content

TTM Squeeze Histogram Backtester for ThinkOrSwim (Free Download)

BacktesterFreeBeginner33 mins video37 lines of codeSep 13, 2022Updated Oct 21, 2026

A free Squeeze histogram backtester for ThinkOrSwim: it trades the first yellow bar after 7 red, or the first blue after 7 cyan, with an RSI filter.

TTM Squeeze Histogram Backtester on a SPY chart in ThinkOrSwim
On this page

Overview

In this tutorial I build a Squeeze histogram backtester for ThinkOrSwim to answer one question: is the histogram useful for catching momentum swings? It goes long on the first yellow bar after 7 red histogram bars in a row, goes short on the first blue bar after 7 cyan bars, and exits at the first sign the momentum is fading.

It's a strategy, so ThinkOrSwim totals the profit and loss on whatever chart and timeframe you load. An RSI filter cuts some of the noise, and inputs change the bar count, the RSI levels and which sides it trades. It's for Squeeze traders who read the histogram and want to test it first.

Key Takeaways

  • The Squeeze histogram backtester is a free ThinkOrSwim strategy that tests whether a change in histogram color times a momentum shift.
  • It goes long on the first yellow bar after 7 red histogram bars in a row when the RSI is below 40, and short on the first blue bar after 7 cyan bars when the RSI is above 60.
  • A long trade exits at the close of the first bar where the histogram is lower than the bar before. A short trade exits at the close of the first bar where it's higher.
  • It has 5 inputs: consecutiveHistogramBars (7), RSIOverboughtVal (60), RSIOversoldVal (40), longEntries (yes) and shortEntries (yes).
  • In the video, the members' version showed 12 winners and 32 losers on the long side and 17 winners and 23 losers on the short side, on a 5-minute S&P chart over 30 days.

The Four Colors of the Squeeze Histogram

The Squeeze histogram is the bar plot of ThinkOrSwim's free built-in Squeeze study, and each bar is one of 4 colors. The color comes from two checks: is the bar above or below zero, and is it higher or lower than the bar before it?

The study's other output, the dots, tells you whether there's a squeeze. This backtester ignores the dots and works only with the histogram. Our Squeeze hub covers the dots, and the MTF Squeeze indicator shows them for many timeframes at once.

ColorZero lineCompared with the previous barWhat it says about momentum
CyanAbove 0HigherBullish momentum is increasing
BlueAbove 0LowerBullish momentum is fading
RedBelow 0LowerBearish momentum is increasing
YellowBelow 0HigherBearish momentum is fading
ThinkOrSwim chart with rising candles above a Squeeze histogram of consecutive cyan bars climbing above zero, then blue bars
Consecutive cyan histogram bars under rising price: bullish pressure. The blue bars that follow show the momentum fading.
Daily ThinkOrSwim chart from December to February with falling candles above a run of red Squeeze histogram bars, followed by yellow bars
Consecutive red histogram bars under bearish price action. The first yellow bar marks the selling momentum starting to fade.

What a Momentum Shift Looks Like

A momentum shift on the Squeeze histogram is the first bar of a new color after a long run of one color. The goal of this tutorial is to test whether those turning points are useful for catching momentum swings.

  • Bullish shift. 7 red bars in a row say momentum is declining. The first yellow bar after them is the sign of at least a temporary bullish shift.
  • Bearish shift. 7 cyan bars in a row mark a bullish move. The first blue bar says momentum is shifting into at least a temporary pause, if not a shorter-term pullback.

The first example in the video is a bullish trade on the S&P 500. The histogram printed more than 7 red bars, then its first yellow bar, and the backtester bought there. It rode the move as the bars kept rising through zero and sold on the first blue bar, the first sign the momentum was stalling.

The Trading Rules

The Squeeze histogram backtester has two entry conditions, one bullish and one bearish, and each depends on the run of bars that came before. Every entry and exit uses the closing price of the bar where its condition is true.

RuleLong tradeShort trade
Setup7 red bars in a row, ending on the previous bar7 cyan bars in a row, ending on the previous bar
Trigger barYellowBlue
RSI filterRSI below 40 (oversold)RSI above 60 (overbought)
EntryBuy to open at the closeSell to open at the close
ExitSell to close at the close of the first bar where the histogram is lower than the bar beforeBuy to close at the close of the first bar where the histogram is higher than the bar before
On/off switchlongEntriesshortEntries

The exit tries to ride the momentum for as long as possible. Read the code closely, though: the long exit fires on the first lower bar of any color. If a yellow bar is followed by a red one, the trade closes there, below zero, without waiting for a blue bar.

How to Create the Backtester in ThinkOrSwim

The Squeeze histogram backtester is a strategy, so you create it from the Strategies tab. That step is very important: only a strategy can place the simulated orders that produce a profit and loss report.

  1. Click the Studies icon at the top of your chart.
  2. Switch over to the Strategies tab and click Create.
  3. Give the backtester a name. I called mine TI_SqueezeHistogram_Backtester, the name the download uses.
  4. Leave the default AddOrder line in the editor while you build, so you don't get an error right away.
  5. Write the code in the steps below, or paste the finished code from the Code tab, then click OK and Apply.

The download is the same code as a .ts strategy file. If you'd rather import it, our guide on how to import a file into ThinkOrSwim covers the steps.

The Inputs

The Squeeze histogram backtester has 5 inputs. Double-click the backtester on your chart to change any of them from the studies menu instead of editing the code.

This step’s code is free with an account. Create one or sign in.

The table explains what each one controls.

InputDefaultWhat it controls
consecutiveHistogramBars7How many cyan or red bars in a row come before the shift
RSIOverboughtVal60The RSI has to be above this for a short entry
RSIOversoldVal40The RSI has to be below this for a long entry
longEntriesyesTurns long trades on or off
shortEntriesyesTurns short trades on or off

How the Code Works

The Squeeze histogram backtester code has 6 parts, written in the order below. I tested each one on the chart before moving on, and the snippets are copied from the download.

Step 1: Reference the Histogram

The most basic piece is the histogram itself, brought over from the built-in Squeeze study.

This step’s code is free with an account. Create one or sign in.

The name before the dot is the study, and histogram after it picks the plot. One line, and the strategy can read every histogram bar.

Step 2: Define the Four Colors

Splitting the histogram into its 4 colors makes the rest of the code much easier to write.

This step’s code is free with an account. Create one or sign in.

Each line is the color table above written in thinkScript. histogram[1] is the previous bar's value.

To test the color definitions, I added a label that prints one of them. On a chart whose last bar was red, it showed 1. After I swapped red for cyan, it showed 0. That's a test line from the video and isn't in the download:

This step’s code is free with an account. Create one or sign in.

Our AddLabel tutorial covers labels in more depth.

Step 3: Count the Consecutive Bars

The setup needs a run of cyan bars or red bars, and the Sum function counts them.

This step’s code is free with an account. Create one or sign in.

A cyan bar is 1 and any other bar is 0. If the sum over the last 7 bars is at least 7, all 7 were cyan.

I tested it with a yellow chart bubble. The first bubble appeared on the 7th cyan bar in a row, and bubbles kept plotting on every bar after it until the first blue bar.

Step 4: Find the Momentum Shift

This step’s code is free with an account. Create one or sign in.

momoDown needs 7 cyan bars that were true on the previous bar and a blue bar now. momoUp needs 7 red bars on the previous bar and a yellow bar now.

The bubbles landed on the first bar of the new color, as expected. One of them was a false signal: momentum turned back up about 2 bars later. Filtering signals like that is the next step.

Step 5: Add an RSI Filter

The RSI filter tries to cut false signals by asking whether the market is overbought or oversold. You can use any indicator you like here. I used the built-in RSI to keep things simple.

This step’s code is free with an account. Create one or sign in.

RSI() with empty parentheses uses the built-in study's default settings, a 14-period RSI. The 60 and 40 thresholds sit closer to 50 than the 70 and 30 lines the RSI study draws.

On a daily SPY chart in the video, adding the oversold filter to the bullish shifts removed a few bubbles on the left and quite a few on the right. Then I turned the hard-coded 60, 40 and 7 into inputs.

Step 6: Place the Orders

AddOrder is the function that places the simulated trades, and it's the bulk of how a backtester works.

This step’s code is free with an account. Create one or sign in.

The buy order fires on a bullish shift in oversold territory, and the sell-to-close fires on the first lower histogram bar. The short side mirrors it with sell to open and buy to close.

The longEntries and shortEntries inputs went in last, so you can see which side contributes to the P&L. These AddOrder lines don't set a trade size, so ThinkOrSwim uses the default trade size from Global Strategy Settings.

How to Read the Results

ThinkOrSwim marks each of the backtester's orders on the chart (pink text in the video) and draws its running profit and loss in a floating P&L graph under the price. If you don't see the graph, open Global Strategy Settings and turn on Display floating P&L with strategies.

For the trade list, right-click the strategy on the chart and choose Show report. In the video, I checked the most recent trade that way. It was a short opened on a blue bar, the histogram rose on the very next bar, and the report listed it as a loser, as expected.

The report gets harder to use as the sample grows. On 5 days of data it's manageable, but 30 days gives many more trades to comb through for the win and loss ratio and the average gain and loss.

Then, first on the SPY daily chart and later on a 5-minute chart over 5 days, I changed one thing at a time and read the P&L after each change:

  • Turned off the short entries, then the long entries, to see which side drove the result.
  • Moved consecutiveHistogramBars from 7 to 10, then down to 5.
  • Moved the RSI levels from 60 and 40 to 70 and 30.

On the SPY daily chart over about 5 years, the short entries caused more of the losses, which makes sense with a bull run inside that window. I read out only P&L totals for the free version, without trade counts, so I don't compare them here. Before you compare two totals of your own, check how many trades each report holds.

Free Version vs Member Version

The Squeeze histogram backtester in this tutorial measures P&L, and the member version adds the statistics. Volatility Box members have a version that puts the trade stats in labels on the chart. It's included with the membership, with a tutorial on how it's built, as part of the Squeeze Course. The course has a Histogram Pro Backtester module.

FeatureFree download (this tutorial)Member version (Squeeze Course)
Entries and exitsMomentum shifts on the histogramMomentum shifts on the histogram
Long and short switchesYesYes
Where the stats areFloating P&L and Show reportLabels and strategy bubbles on the chart
Stats shownTotal P&L, trade listWinners, losers, average win %, average loss %, average trade duration, per side

Results From the Video

The Squeeze histogram results in the video come from the member version's labels, which give winners and losers for each side. These are historical results from the charts loaded when I recorded, not a forecast, and most of the samples are small.

Chart in the videoPeriodLong sideShort side
S&P, 5-minute30 days12 winners, 32 losers17 winners, 23 losers
S&P, 5-minute5 daysCounts not read out9 winners, 1 loser
SPY, 5-minute5 days2 winners, 2 losers10 winners, 1 loser
Apple, 30-minute, extended hours off90 days4 winners, 4 losers1 winner, 10 losers

On the SPY 5-day run, long trades lasted about 9.5 five-minute bars on average, about 50 minutes per trade. The short trades lasted a little longer, and the average short winner was much greater than the average short loser.

Apple's 30-minute chart was basically the reverse of the S&P: the short side, the strongest side on the S&P, won 1 trade out of 11. The same rules behave differently on each market and timeframe, so test the one you trade.

How I Use the Backtester

The Squeeze histogram backtester is a starting point for research. With the free version, the work is manual: study the reports and look for the common patterns and tendencies. How many winners, how many losers, and is it the long or the short side driving the P&L?

  • Test one side at a time. Turn longEntries or shortEntries off and compare.
  • Change one input at a time. Bar count first, then the RSI levels, so you know what moved the result.
  • Swap the filter. Replace the RSI with your own condition if another indicator fits your trading better.

The code has no stop loss. A trade stays open until the histogram turns, so before you trade a histogram shift, decide where your stop goes and size the position so a stop-out costs a small, fixed share of your account.

Common Problems

Most problems with the Squeeze histogram backtester come from where it was created or how strict the filters are.

  • An error that the strategy should contain AddOrder. The editor shows it while no order lines exist. It goes away once the AddOrder lines are in.
  • No orders on the chart. Check that you created it under the Strategies tab. AddOrder works only in a strategy.
  • No floating P&L graph. Turn on Display floating P&L with strategies in Global Strategy Settings.
  • Very few trades. Each entry needs a full run of bars and an RSI reading on the right side of the threshold. Lower consecutiveHistogramBars or move the RSI levels closer to 50 for more trades, and check the report to see what changed.

The backtester on real data

Drawn from real daily bars with the code on this page, through Oct 20, 2026. Long on the first yellow bar after 7 red bars when the RSI is under 40, short on the first blue bar after 7 cyan bars when it is over 60, out at the close of the first bar that turns the other way. 100 shares, one position at a time, dividends and commissions left out.

The most recent long: XLRE, Oct 2, 2026

TTM Squeeze histogram backtester on a XLRE daily chart in ThinkOrSwim: a long on the first yellow bar after 7 red bars, Oct 2, 2026
XLRE, Oct 2, 2026: the white box marks the 7 red bars and the first yellow one. RSI was 21.6.

XLRE printed its first yellow histogram bar after 7 red ones on Oct 2, 2026, with the RSI at 21.6, and the backtester bought at $40.81. The histogram dipped again on Oct 5, 2026, 1 trading day later, and it sold at $40.66: -$15 on 100 shares. The exit is quick by design: the first lower bar ends the trade.

14 ETFs since 2016

Every signal on SPY, QQQ, IWM, DIA and 10 sector ETFs, Jan 4, 2016 to Oct 20, 2026. Funds with no signal on a side are left out.

SideTradesWonTotal
SPYLong743%$728
SPYShort2910%-$10,633
QQQLong425%-$3,229
QQQShort3013%-$7,603
IWMLong1136%$766
IWMShort2223%-$3,255
DIALong633%$2,295
DIAShort2818%-$6,117
XLKLong1100%$141
XLKShort2615%-$2,591
XLFLong944%$191
XLFShort2412%-$1,544
XLELong1331%$256
XLEShort1937%$1,010
XLVLong862%$1,069
XLVShort2832%-$301
XLILong1164%$2,073
XLIShort2030%-$299
XLYLong650%$1,636
XLYShort2627%-$689
XLPLong967%$412
XLPShort1932%$599
XLULong1060%$620
XLUShort1638%-$41
XLBLong1145%$728
XLBShort2025%-$387
XLRELong1136%$666
XLREShort2030%-$449

Across the 14 funds in ten years, the 117 longs won 47% of the time and made $8,352; the 327 shorts won 23% and lost $32,299. SPY went from $201.02 to $769.05 over the same years, so the short side spent most of them fighting the trend. Turning shorts off is one input in the settings.

Full thinkScript code

TTM Squeeze Histogram Backtester.tsthinkScript · 37 lines
The full code is free with an account

Create a free account or sign in to copy all 37 lines and download the file.

Download

TTM Squeeze Histogram Backtester

Download the Squeeze Histogram Backtester for ThinkOrSwim. The download is a .zip with one strategy file, TI_SqueezeHistogram_BacktesterSTRATEGY.ts, the same code as the Code tab on this page. Import it as a strategy, then open its settings to change the bar count, the RSI levels and the long and short switches. If you haven't imported a file before, here's how to import into your ThinkOrSwim platform.

Download free

Video: TTM Squeeze Histogram Backtester, built line by line

33 mins on YouTube.

Watch on YouTube

Questions

What do the colors of the Squeeze histogram mean?

Cyan is above zero and higher than the bar before, so bullish momentum is increasing. Blue is above zero and lower, so it's fading. Red is below zero and lower, so bearish momentum is increasing. Yellow is below zero and higher, so the selling momentum is fading.

How does the Squeeze histogram backtester decide when to enter?

It looks for a momentum shift after a run of one color. A long trade needs 7 red bars in a row, then a yellow bar, with the RSI below 40. A short trade needs 7 cyan bars in a row, then a blue bar, with the RSI above 60. Both enter at the bar's close.

When does the backtester exit a trade?

A long trade exits at the close of the first bar where the histogram is lower than the bar before, and a short trade exits on the first bar where it's higher. The exit doesn't wait for a particular color, so a long can close on a red bar below zero.

How do I backtest the Squeeze histogram in ThinkOrSwim?

Create a strategy: click the Studies icon, switch to the Strategies tab, click Create and paste in the code. Apply it to a chart, then right-click the strategy and choose Show report for the trade list. Turn on the floating P&L graph in Global Strategy Settings if you don't see it.

Why does the histogram backtester use the RSI?

The RSI filters out some false signals. A long entry needs the RSI below 40 and a short entry needs it above 60, so the backtester only takes shifts after the market has moved one way. On a daily SPY chart in the video, the filter removed a number of the signals.

What results did the histogram backtester show in the video?

The member version's labels showed 12 winners and 32 losers long and 17 winners and 23 losers short on a 5-minute S&P chart over 30 days. On Apple's 30-minute chart over 90 days, longs had 4 winners and 4 losers and shorts had 1 winner and 10 losers. These are small historical samples.

What is the difference between the free and member versions?

Both trade the same histogram momentum shifts. The free version gives you the floating P&L and the Show report trade list. The member version adds labels with winners, losers, average win and loss percentages and average trade duration for each side. It's part of the Squeeze Course, with a tutorial on how it's built.

Can I test only long or only short trades?

Yes. The longEntries and shortEntries inputs both default to yes. Set either one to no in the strategy's settings and the P&L shows only the other side. In the video I used them to see which side was causing more of the losses.

Goes with

Get each new indicator by email

One email on Sundays with the week's new tool and its code. A free account also unlocks every .txt download.

No credit card. Unsubscribe anytime.