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:
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:
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:
- 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.
- After a 5 dot squeeze, which EMA is best to enter on a pullback? This allows me to maximize my entry and minimize risk.
- 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.
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:
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);