Skip to main content

TTM Squeeze Backtester for ThinkOrSwim (Free Download)

BacktesterFreeBeginner17 min video33 lines of codeJul 8, 2019Updated Oct 21, 2026

A free Squeeze backtester for ThinkOrSwim. It trades 5 red dots with stacked 13, 21 and 34 EMAs so you can compare length, nK and nBB.

TTM Squeeze Backtester on a ThinkOrSwim chart
  • Purple markers: each test trade the strategy takes
  • EMAs: 13, 21 and 34, drawn in cyan, purple and yellow

It enters after 5 red squeeze dots with the EMAs stacked, and exits at a 3% target or when the 13 EMA crosses the 21.

AAPL daily at 13:24 in the video. Watch this moment

On this page

What you'll build

TTM Squeeze Backtester in ThinkOrSwim: The backtester with the 13
The backtester with the 13, 21 and 34 EMAs on AAPL daily, at 13:54 in the video.
On real data
  • Entry (triangle) and exit (circle)
  • 13 EMA
  • 21 EMA
  • 34 EMA
625650675700725750775SepOctNovDecJanFebMarAprMayJunJulAugSepOct
The strategy's rules applied to SPY daily bars, Aug 27, 2025 to Oct 19, 2026: 4 completed trades. Hypothetical and for illustration only.
Type
Backtester
Runs as
Chart strategy
Inputs
3
Code
33 lines
Video
17 minutes
Level
Beginner

In this tutorial I build a Squeeze backtester for ThinkOrSwim as a strategy, so ThinkOrSwim can total the profit and loss for any settings I try. It goes long or short after 5 red Squeeze dots in a row when the 13, 21 and 34-period EMAs are stacked in one direction. It takes profit at 3% or exits when the 13 EMA crosses back through the 21.

I built it to test whether a ticker trades better with settings other than ThinkOrSwim's defaults of 20, 1.5 and 2.0. It's for traders who use the Squeeze and want to check its settings on the tickers and timeframes they trade before they put money behind them.

Key takeaways

  • The Squeeze backtester is a free ThinkOrSwim strategy for testing the Squeeze's 3 settings, length, nK and nBB, starting from the defaults of 20, 1.5 and 2.0.
  • It goes long after 5 red Squeeze dots in a row when the 13, 21 and 34-period EMAs are stacked bullish (13 above 21, 21 above 34), and goes short when they are stacked bearish.
  • Each trade is 100 shares at the signal bar's close. The backtester exits at a 3% profit target or when the 13-period EMA crosses back through the 21-period EMA.
  • In the video, the strategy lost $1,196.40 over 3 trades on Goldman Sachs and made $1,689 over 13 trades on Apple, both on daily charts. Both are small samples.
  • On a 4-hour chart in the video, length 20 with nK 2.0 and nBB 2.0 returned $1,368.70 over 12 trades, above the $927 I started from on that chart.

What it shows

What the Squeeze Backtester Does

The Squeeze backtester is a ThinkOrSwim strategy that places simulated trades on your chart when a Squeeze setup and a moving average trend line up. ThinkOrSwim then totals the profit and loss, so every combination of settings comes down to one dollar amount you can compare.

I built it after John Carter, who created the Squeeze, said in a webinar that he had found better settings than the defaults, a version he now calls the Squeeze Pro. The backtester lets you check whether a ticker you trade does better with settings other than the ones ThinkOrSwim gives you by default.

This was Episode 3 of our How to thinkScript series. For members, our Squeeze Course has more Squeeze backtesters with stats, and you can browse our free ThinkOrSwim backtesters for other strategies.

The Trading Rules

The Squeeze backtester trades both directions with one set of rules. The Squeeze decides when to trade, and the moving averages decide which way.

RuleLong sideShort side
Squeeze condition5 red dots in a row5 red dots in a row
Trend condition13 EMA above 21 EMA, 21 EMA above 34 EMA13 EMA below 21 EMA, 21 EMA below 34 EMA
EntryBuy to open 100 shares at the closeSell to open 100 shares at the close
Profit targetHigh reaches the entry price plus 3%Low reaches the entry price minus 3%
Trend exit13 EMA crosses below the 21 EMA13 EMA crosses above the 21 EMA

When the EMAs are stacked neither way, I treat the market as chop with no clear trend, and the backtester takes no trade. Our free Squeeze dots scan looks for the same kind of setup, 5 red dots in a row with stacked moving averages, across the whole market.

How to Read the Results

The floating P&L study shows the backtester's running profit and loss in a panel under your chart. Turn it on once, and it updates every time you change a setting.

  1. Click the Studies icon and choose Global Strategy Settings.
  2. Check the box to display the floating P&L study with strategies, and click OK.
  3. For the totals, right-click the strategy's name on the chart and choose Show report. The report gives the total P&L and the number of trades.

Before you compare, match the strategy's settings to the Squeeze study on your chart. In the video, the nK factors didn't match at first, so the strategy's squeeze and the dots on my chart disagreed until I changed the factor.

The backtester on real data

Drawn from real daily bars with the code on this page, through Oct 20, 2026. The backtester trades both directions; these are its long trades, one position at a time: buy 100 shares at the close of a bar with 5 squeeze dots in a row and the 13, 21 and 34 EMAs stacked up, sell at 3% above the entry or at the close when the 13 EMA crosses back below the 21. Dividends and commissions are left out.

SPY: the most recent trade

TTM Squeeze backtester on a SPY daily chart in ThinkOrSwim: a long trade on Jul 8, 2026 after 5 squeeze dots with the 13, 21 and 34 EMAs stacked
SPY bought Jul 8, 2026 at $745.40; the dashed line is the 3% target. The 13 EMA crossed below the 21 on Jul 24, 2026 before the target.

On Jul 8, 2026, SPY closed at $745.40 on its fifth squeeze dot in a row with the 13 EMA above the 21 and the 21 above the 34. The 13 EMA crossed below the 21 on Jul 24, 2026, 12 trading days later, and the trade closed at $738.93: -$647 on 100 shares.

Default settings on 14 ETFs since 2016

Length 20, nK 1.5 and nBB 2.0, from Jan 4, 2016 to Oct 20, 2026, 100 shares a trade.

TradesWonHit the 3% targetTotal
SPY1346%4-$41
QQQ1753%9$1,610
IWM2748%11-$2,266
DIA2264%14$10,088
XLK2045%9-$965
XLF2556%12$497
XLE1735%6-$854
XLV2232%7-$1,061
XLI2540%8-$1,287
XLY2162%13$2,133
XLP2737%10$326
XLU3941%16-$616
XLB2040%7-$363
XLRE2741%11$24

Across the 14 funds the long side made $7,226 in total on 322 trades, 137 of them at the 3% target. The typical trade lasted 8 trading days.

Which settings did best on SPY?

The reason the backtester exists: test the Squeeze's length and nK on your own ticker instead of trusting the defaults. Here are nine combinations on SPY, nBB held at 2.0.

LengthnKTradesWonTotal
1011050%$2,233
101.55659%$20,372
1027758%$24,063
20110%-$267
201.51346%-$41
2025052%$9,882
30100%$0
301.5425%-$74
3022250%$3,080

SPY, Jan 4, 2016 to Oct 20, 2026, long side. The defaults (20, 1.5) made -$41 on 13 trades; the best of the nine here, length 10 and nK 2, made $24,063 on 77 trades. One ticker and one stretch of years: a setting that won here can lose on the next ticker, which is why the tool lets you test your own.

Install it in ThinkOrSwim

TTM Squeeze Backtester

Download the Squeeze Backtester for ThinkOrSwim. The download is a .zip with one strategy file (.ts), the same code as the Code tab on this page. Import it as a strategy, then open its settings to change length, nK and nBB. If you haven't imported a file before, here's how to import into your ThinkOrSwim platform.

Download free

How to Create the Strategy in ThinkOrSwim

The Squeeze backtester is a strategy, so you create it from the Strategies tab instead of the Studies tab. That step matters: a strategy produces a profit and loss report, and a study doesn't.

  1. Click the Studies icon at the top right of your chart.
  2. Switch over to the Strategies tab and click Create.
  3. Give the strategy a unique name at the top of the editor.
  4. Paste in the code from the Code tab on this page, and click OK, then Apply.
  5. To see the trend condition, add the 13, 21 and 34-period exponential moving averages to the chart. I colored them cyan, purple and yellow.

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

How the code works

The Inputs

The Squeeze backtester has 3 inputs, the same settings the built-in Squeeze study uses. You change them in the strategy's settings window, and each change redraws the trades.

InputDefaultWhat it controls
length20The number of bars the Squeeze measures over
nk1.5The nK factor, the Keltner Channel multiplier
nBB2.0The nBB factor, the Bollinger Band multiplier

The built-in Squeeze function also takes a price, which I leave at close, and an alert line of 1.0, which I didn't need to change. Our multi-time frame Squeeze indicator rebuilds the Squeeze from source code, so you can see where the 2 factors enter the math: nK multiplies the average true range, and nBB multiplies the standard deviation.

How the Code Works

The Squeeze backtester code has 7 parts, and I wrote them in this order in the video. The first 5 define the setup and the exits, and the last 2 place the orders.

Step 1: The Inputs and the Squeeze

I make the 3 settings inputs, then call the built-in Squeeze with those inputs in place of fixed numbers.

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

The SqueezeAlert plot tells you whether the dot is red or green. It returns 0 on a red dot.

Step 2: The Trend Filter

Most people read the Squeeze's direction from its histogram. If you've read Mastering the Trade, you'll remember John Carter uses moving averages to define the trend and trades the Squeeze in that same direction. I used the 13, 21 and 34-period exponential moving averages.

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

Bullish means the 13 is above the 21 and the 21 is above the 34. Bearish is the reverse.

Our stacked moving averages indicator plots this kind of EMA stack on a chart and marks when it changes.

Step 3: Five Red Dots

John Carter's rule is to wait for at least 5 red Squeeze dots before getting into a trade, so the backtester checks the last 5 bars.

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

A red dot is 0, so a sum of 0 over 5 bars means all 5 dots were red.

Step 4: The Bullish and Bearish Signals

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

A bullish signal needs the 5-dot squeeze and a bullish EMA stack on the same bar. A bearish signal pairs the 5-dot squeeze with a bearish stack.

Step 5: The Trend Exits

To keep the first version simple, I exit when the trend breaks: the 13-period EMA crosses back through the 21-period EMA.

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

The [1] reads the previous bar, so each exit is true only on the bar where the cross happens.

Step 6: The Orders

AddOrder is the function that lets ThinkOrSwim place the simulated trades. Each order trades 100 shares at the closing price of the bar.

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

The short side mirrors the long side: sell to open on a bearish signal, buy to close on the bearish exit.

Step 7: The 3% Profit Target

In the video, one long trade was up 3.96% at one point and still closed at a loss, because the only exit was the EMA cross. So I added a profit target on both sides.

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

entryPrice() is built into ThinkOrSwim and returns the price of the open position. The long side fills at the entry price plus 3% once the high reaches it.

The short side fills at the entry price minus 3% once the low reaches it. Both orders close all 100 shares.

Full thinkScript code

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

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

Examples

Results From the Video

The Squeeze backtester gave different results on different tickers and timeframes in the video. These are historical results from the charts I had loaded when I recorded, for 100 shares per trade. The video doesn't state the date range, and every sample is small.

Test in the videoChartSettingsTradesTotal P&L
Goldman SachsDailyLength 20, factors unclear in the audio3-$1,196.40
AppleDailySame as the last Goldman Sachs test13+$1,689
Intraday test30-minuteOne factor at 2.0, then 1.5, then the defaultsNot statedNegative over the last 10 days, strongly negative at the defaults
Starting point4-hourNot statedNot stated$927
Final 4-hour run4-hourLength 20, nK 2.0, nBB 2.012+$1,368.70

The audio doesn't make clear which nK and nBB values were set for the Goldman Sachs and Apple runs. What it does show is the same rules losing money on one stock and making money on another.

On Goldman Sachs, the other combinations I tried made the result worse, and one was much worse. On Apple, changing one factor from 2.0 to 1.5 lowered the P&L. The defaults of 1.5 and 2.0 also looked good on Apple, but over only 2 trades.

On the 30-minute chart, every combination I tried was negative over the last 10 days, and the defaults of 20, 1.5 and 2.0 were strongly negative.

On the 4-hour chart, I tested the length. A length of 15 gave fewer trades without changing much else, a length of 10 changed the results, and a length of 30 took no trades at all. Length 20 with nK 2.0 and nBB 2.0 beat the $927 starting point with $1,368.70 over 12 trades.

What I took from the tests: the Squeeze behaves differently on each ticker and timeframe, so the settings need more testing and finessing for each one.

How I Use the Backtester

Backtesting the Squeeze settings this way is a manual, laborious process. What you get from it are patterns and tendencies for each ticker, and I make a note of them and study them.

  • Start with the defaults. Run 20, 1.5 and 2.0 first and write down the total P&L and the number of trades. That's the number to beat.
  • Change one input at a time. If you change length and nK together, you can't tell which one moved the result.
  • Repeat on your own tickers and timeframes. Settings that worked on Apple's daily chart in the video did the opposite on Goldman Sachs.
  • Count the trades. A strong total over 2 or 3 trades tells you little. Prefer settings that hold up over more trades and more symbols.

The backtester has no stop loss. A losing trade stays open until the 13 EMA crosses the 21 EMA. Before you trade a Squeeze setup, decide where your stop goes and size the position so a stop-out costs a small, fixed share of your account.

Customize and troubleshoot

Ways to Extend the Backtester

The Squeeze backtester is a starting block. You can tweak the code, add more settings and bring in more indicators as conditions.

Video: TTM Squeeze Backtester, built line by line

17 minutes on YouTube.

Watch on YouTube

Questions

What does the Squeeze backtester do?

It places simulated trades on a ThinkOrSwim chart so you can compare Squeeze settings by their total profit and loss. It goes long or short after 5 red Squeeze dots when the 13, 21 and 34-period EMAs are stacked, trades 100 shares at the close, and exits at a 3% profit target or when the 13 EMA crosses the 21 EMA.

How do I backtest the Squeeze in ThinkOrSwim?

Create a strategy instead of a study. Click the Studies icon, switch to the Strategies tab, click Create, and paste in the backtester code. Turn on the floating P&L study in Global Strategy Settings, then right-click the strategy name and choose Show report to see the total P&L and the number of trades for each set of settings.

What are the default Squeeze settings in ThinkOrSwim?

The defaults are a length of 20, an nK factor of 1.5 and an nBB factor of 2.0, with close as the price and an alert line of 1.0. The backtester starts from the same values, so its first run matches the built-in Squeeze study at its default settings.

How does the backtester decide whether to go long or short?

It uses the 13, 21 and 34-period exponential moving averages. When the 13 is above the 21 and the 21 is above the 34, it only takes long trades. When the order is reversed, it only takes short trades. When the EMAs are stacked neither way, it treats the market as chop and takes no trade.

Why does the backtester wait for 5 red Squeeze dots?

Waiting for at least 5 red dots before a trade is John Carter's rule, and the backtester follows it. The code adds up the Squeeze alert over the last 5 bars. A red dot returns 0, so a sum of 0 means all 5 dots were red.

How do I see the profit and loss report for a ThinkOrSwim strategy?

Click the Studies icon, open Global Strategy Settings, and check the box to display the floating P&L study with strategies. That draws the running P&L under your chart. For totals, right-click the strategy's name on the chart and choose Show report, which lists the total P&L and the number of trades.

What results did the backtester show in the video?

On daily charts, the strategy lost $1,196.40 over 3 trades on Goldman Sachs and made $1,689 over 13 trades on Apple. On a 4-hour chart, length 20 with nK 2.0 and nBB 2.0 made $1,368.70 over 12 trades. These are small historical samples for 100 shares per trade, and the video doesn't state the date range.

Does the Squeeze backtester use a stop loss?

No. The code exits only at the 3% profit target or when the 13-period EMA crosses back through the 21-period EMA, so a losing trade stays open until that cross. You can add your own stop with another AddOrder line that closes the position once price moves a set amount against the entry.

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.