# TTM SQUEEZE ALT BACKTEST W CHART v2
input price = CLOSE;
input ShortLength1 = 5;
input ShortLength2 = 14;
input ShortLength3 = 5;
input LongLength1 = 12;
input LongLength2 = 55;
input LongLength3 = 7;
# Momentum Oscillators
plot MS = Average(Average(price, ShortLength1) - Average(price, ShortLength2), ShortLength3);
plot MS2 = Average(Average(price, LongLength1) - Average(price, LongLength2), LongLength3);
# Wave A
def MSGreens = If (MS >= 0, MS, 0);
def MSReds = If (MS < 0, MS, 0);
# Wave C
def MS2Blues = If (MS2 >= 0, MS2, 0);
def MS2Yellows = If (MS2 < 0, MS2, 0);
plot MS_Pos = MSGreens;
MS_Pos.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
MS_Pos.AssignValueColor(if MSGreens < MSGreens[1] then Color.GREEN else Color.DARK_GREEN);
plot MS_Neg = MSReds;
MS_Neg.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
MS_Neg.AssignValueColor(if MSReds < MSReds[1] then CreateColor(255, 60, 60) else Color.DARK_RED);
plot MS2_Pos = MS2Blues;
MS2_Pos.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
MS2_Pos.AssignValueColor(if MS2Blues < MS2Blues[1] then Color.BLUE else Color.CYAN);
plot MS2_Neg = MS2Yellows;
MS2_Neg.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
MS2_Neg.AssignValueColor(if MS2Yellows < MS2Yellows[1] then Color.YELLOW else Color.LIGHT_RED);
# Squeeze Indicator
input length = 20;
input nK = 1.5;
input nBB = 2.0;
def BBHalfWidth = StDev(price, length);
def KCHalfWidth = nK * Average(TrueRange(high, close, low), length);
plot isSqueezed = nBB * BBHalfWidth / KCHalfWidth < 1;
isSqueezed.Hide();
plot BBS_Ind = If(isSqueezed, 0, Double.NaN);
BBS_Ind.SetPaintingStrategy(PaintingStrategy.SQUARES);
BBS_Ind.SetLineWeight(3);
BBS_Ind.AssignValueColor(Color.RED);
# Bollinger Resolution
def BBSMA = Average(price, length);
def BBSMAL = BBSMA + (-nBB * BBHalfWidth);
def BBSMAU = BBSMA + (nBB * BBHalfWidth);
def PerB = RoundUp((price - BBSMAL) / (BBSMAU - BBSMAL) * 100, 0);
#BELOW NOT NEEDED FOR BACKTEST.
#AddLabel(yes, Concat("%B: ", PerB), if PerB < 0 then Color.YELLOW else if PerB > 0 and PerB[1] < 0 then Color.GREEN else Color.WHITE);
# Parabolic SAR Signal
input accelerationFactor = 0.0275;
input accelerationLimit = 0.2;
def SAR = ParabolicSAR(accelerationFactor = accelerationFactor, accelerationLimit = accelerationLimit);
plot bearishCross = Crosses(SAR, price, CrossingDirection.ABOVE);
bearishCross.Hide();
# plot signalDown = If(bearishCross, 0, Double.NaN);
# signalDown.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
# signalDown.SetLineWeight(3);
# signalDown.AssignValueColor(Color.DOWNTICK);
def ema3 = ExpAverage(close, 3);
def ema21 = ExpAverage(close, 21);
# ADDED FOR EXITS
def BearishTrade = if ema21 > EMA3 and ema21[1] <= ema21[1] then 1 else 0;
plot bullishCross = Crosses(SAR, price, CrossingDirection.BELOW);
bullishCross.Hide();
plot signalUp = If(bullishCross, 0, Double.NaN);
signalUp.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
signalUp.SetLineWeight(3);
signalUp.AssignValueColor(Color.UPTICK);
AddOrder(OrderType.BUY_TO_OPEN, bullishCross, open[-1], 100);
# AddOrder(OrderType.Sell_To_Close, high>= entryPrice() + entryPrice()*0.03, entryPrice() + entryPrice()*0.03, 100);
# ORIFINAL BELOW
# AddOrder(OrderType.Sell_To_Close, bearishCross, open[-1], 100);
# ADDED FOR BETTER TRADE
AddOrder(OrderType.SELL_TO_CLOSE, BearishTrade, open[-1], 100);
#AddOrder(OrderType.Sell_To_Open, bearishCross, open[-1], 100);
#AddOrder(OrderType.Buy_To_Close, low <= entryPrice() - #(entryPrice()*0.03), entryPrice() - (entryPrice()*0.03), 100);
#AddOrder(OrderType.Buy_To_Close, bullishCross, open[-1], 100);