Squeeze Backtester - Find Best Pullback Entry/Stop/Target For ThinkOrSwim

SealTrades

New member
I really like the Squeeze strategy and I know it's pretty popular. I took two Backtesting strategies created by TOS Indicators (EMA Pullback and TTM Squeeze) that he provides for free on his website. I combined some features, made it a little more user friendly (customizable without getting into the code), and added all three types of squeezes. NOTE: This backtester is predicated on waiting for 5 squeeze dots before entering and EMAs stacked (8, 21, 34, 55, 89) either bullishly or bearishly.

This hasn't been thoroughly tested so if anyone can suggest improvements, then I'm all about it. I'm very new to coding and don't even consider it a hobby yet...but I've enjoyed doing it to potentially enhance my trading. If anyone knows how to add an 'either/or' condition to the closing orders I think it would be beneficial to have an option in the settings where trades either reach it's profit ATR "or" a certain amount of days (if that's even possible).

Every ticker has it's own personality so I use the backtester to answer the following for each symbol:
  1. Does the stock respect the squeeze setup? Does it respect the black dot or normal squeeze only? This can keep me out of bad trades.
  2. After a 5 dot squeeze, which EMA is best to enter on a pullback? This allows me to maximize my entry and minimize risk.
  3. What ATR stop loss/profit target combo is most profitable? Some stocks need more room to move while others tend to bounce right off the entry.
Obviously the backtester is limited, in that it would require pretty mechanical trading for the P/L to have been true in the long run. But I think it provides some statistically relevant criteria for placing a high probability trade given a setup on a specific ticker.

CAVEAT: If you make your Profit ATR too large, you'll notice some trades just run forever (not good if you're buying options). Ideally I try to find a profitable balance of stop loss/profit that creates quicker trades (1 extra ATR profit isn't worth it if it takes 15 extra days to achieve). Therefore I don't always use total profit on the floating P/L as an indicator of high probability without also going through and looking at the average trade durations. If someone knows how to create labels for "Avg Trade Duration" and "Win:Loss Ratio" I think that would be helpful.

You can view the P/L at the bottom of your chart studies by going to "Edit Studies" > "Global Strategy Settings" and check "Turn on the "Display floating PL with strategies".

Here are the settings in the Backtester, followed by the code:

Squeeze-Backtest.png


Code:
input SqueezeType = {default BlackDotSqueeze, RedDotSqueeze, OrangeDotSqueeze};
input LongEntries = yes;
input ShortEntries = no;
input EntryMA = {default EMA8, EMA21, EMA34, EMA55, EMA89};
input TargetMultiplier = 2.5;
input StopMultiplier = 1.5;
input Quantity = 100;

def Squeeze;
switch (SqueezeType){
case BlackDotSqueeze:
      Squeeze = TTM_Squeeze(close, 20, 2.0, 2.0, 1.0).SqueezeAlert;
case RedDotSqueeze:
     Squeeze = TTM_Squeeze(close, 20, 1.5, 2.0, 1.0).SqueezeAlert;
case OrangeDotSqueeze:
     Squeeze = TTM_Squeeze(close, 20, 1.0, 2.0, 1.0).SqueezeAlert;
}

def EMA8 = ExpAverage(close, 8);
def EMA21 = ExpAverage(close, 21);
def EMA34 = ExpAverage(close, 34);
def EMA55 = ExpAverage(close, 55);
def EMA89 = ExpAverage(close, 89);

def entryPullbackMA;
switch (entryMA){
case EMA8:
    entryPullbackMA = EMA8;
case EMA21:
    entryPullbackMA = EMA21;
case EMA34:
    entryPullbackMA = EMA34;
case EMA55:
    entryPullbackMA = EMA55;
case EMA89:
    entryPullbackMA = EMA89;
}


def bullishStackedMAs = EMA8 > EMA21 and EMA21 > EMA34 and EMA34 > EMA55 and EMA55 > EMA89;
def bearishStackedMAs = EMA8 < EMA21 and EMA21 < EMA34 and EMA34 < EMA55 and EMA55 < EMA89;

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


def bullEntrySignal = fiveDotSqueeze and bullishStackedMAs and low <= entryPullbackMA;

def bearEntrySignal = fiveDotSqueeze and bearishStackedMAs and high >= entryPullbackMA;

def bullEntryPrice = entryPullbackMA;

def bearEntryPrice = entryPullbackMA;

def ATRvalue = if (bullEntrySignal and !bullEntrySignal[1]) or (bearEntrySignal and !bearEntrySignal[1]) then ATR(14) else ATRvalue[1];


#Long-Side Trades
AddOrder(OrderType.BUY_TO_OPEN, longEntries and bullEntrySignal, bullEntryPrice, quantity);
AddOrder(OrderType.SELL_TO_CLOSE, high >= entryPrice() + targetMultiplier * ATRValue, entryPrice() + targetMultiplier * ATRValue, quantity);
AddOrder(OrderType.SELL_TO_CLOSE, low <= entryPrice() - stopMultiplier * ATRValue, entryPrice() - stopMultiplier * ATRValue, quantity);

#Short-Side Trades
AddOrder(OrderType.SELL_TO_OPEN, shortEntries and bearEntrySignal, bearEntryPrice, quantity);
AddOrder(OrderType.BUY_TO_CLOSE, low <= entryPrice() - targetMultiplier * ATRValue, entryPrice() - targetMultiplier * ATRValue, quantity);
AddOrder(OrderType.BUY_TO_CLOSE, high >= entryPrice() + stopMultiplier * ATRValue, entryPrice() + stopMultiplier * ATRValue, quantity);
 

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

I installed your code but am seeing nothing on my screen
Have you turned on "Display Floating PL with Strategies" in your Global Strategy settings in the Edit Studies window? I had this problem for awhile and it took me forever to figure out I actually had to enable the floating P/L.
 

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
347 Online
Create Post

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