Squeeze Backtest with Target and SL

All experts out there,

Whatta great community. Really helpful for newbies like me.
I just picked up this Squeeze pro from one of the communities here and thought of backtesting with defined target and stop loss. But not getting what I am looking for.

While the code works as expected....want a little tweaks on the below

1. Stop loss and target line upon signal be extended till price closes above/below the SL/target
2. LE "Buy_To_Open" or SE "Sell_To_Open" do not get closed upon meeting target/stop loss
3. How to close the position at the end of the day if no sl/target it met

Really appreciate your support

U9m2aKn.png


Code

Code:
# SqueezePro ##Assembled by TheBewb using existing Mobius Squeeze Momentum coding and "squeeze" concept made popular by John Carter.
input consecutiveHistogramBars = 5;
input price = close;
input length = 20;
input Num_Dev_Dn = -2.0;
input Num_Dev_up = 2.0;
input averageType = AverageType.SIMPLE;
input displace = 0;
def sDev = StDev(data = price[-displace], length = length);
def MidLineBB = MovingAverage(averageType, data = price[-displace], length = length);
def LowerBandBB = MidLineBB + Num_Dev_Dn * sDev;
def UpperBandBB = MidLineBB + Num_Dev_up * sDev;
input factorhigh = 1.0;
input factormid = 1.5;
input factorlow = 2.0;
input trueRangeAverageType = AverageType.SIMPLE;
def shifthigh = factorhigh * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def shiftMid = factormid * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def shiftlow = factorlow * MovingAverage(trueRangeAverageType, TrueRange(high, close, low), length);
def average = MovingAverage(averageType, price, length);

def Avg = average[-displace];

def UpperBandKCLow = average[-displace] + shiftlow[-displace];
def LowerBandKCLow = average[-displace] - shiftlow[-displace];

def UpperBandKCMid = average[-displace] + shiftMid[-displace];
def LowerBandKCMid = average[-displace] - shiftMid[-displace];

def UpperBandKCHigh = average[-displace] + shifthigh[-displace];
def LowerBandKCHigh = average[-displace] - shifthigh[-displace];

def K = (Highest(high, length) + Lowest(low, length)) /
2 + ExpAverage(close, length);
def momo = Inertia(price - K / 2, length);

def pos         = momo >= 0;
def neg         = momo < 0;
def up         = momo >= momo[1];
def dn         = momo < momo[1];


def presqueeze      = LowerBandBB > LowerBandKCLow and UpperBandBB < UpperBandKCLow;
def presqueezein    = LowerBandBB > LowerBandKCLow and UpperBandBB < UpperBandKCLow and LowerBandBB > LowerBandBB[1];
def presqueezeout   = LowerBandBB > LowerBandKCLow and UpperBandBB < UpperBandKCLow and LowerBandBB < LowerBandBB[1];

def originalSqueeze     = LowerBandBB > LowerBandKCMid and UpperBandBB < UpperBandKCMid;
def originalSqueezein   = LowerBandBB > LowerBandKCMid and UpperBandBB < UpperBandKCMid and LowerBandBB > LowerBandBB[1];
def originalSqueezeout  = LowerBandBB > LowerBandKCMid and UpperBandBB < UpperBandKCMid and LowerBandBB < LowerBandBB[1];


def ExtrSqueeze     = LowerBandBB > LowerBandKCHigh and UpperBandBB < UpperBandKCHigh;
def ExtrSqueezein   = LowerBandBB > LowerBandKCHigh and UpperBandBB < UpperBandKCHigh and LowerBandBB > LowerBandBB[1];
def ExtrSqueezeout  = LowerBandBB > LowerBandKCHigh and UpperBandBB < UpperBandKCHigh and LowerBandBB < LowerBandBB[1];



def PosUp = pos and up;
def PosDn = pos and dn;
def NegDn = neg and dn;
def NegUp = neg and up;

def minUpBars = Sum(PosUp, consecutiveHistogramBars) >= consecutiveHistogramBars;
def minDownBars = Sum(NegDn, consecutiveHistogramBars) >= consecutiveHistogramBars;
def Cyan = if PosDn and minUpBars[1] then high else Double.NaN;
def yellow = if NegUp and minDownBars[1] then low else Double.NaN;
def SqueezeLineGreen = !ExtrSqueezein and !ExtrSqueezeout and !originalSqueezein and !originalSqueezeout and !presqueezein and !presqueezeout;
def ReleaseUp = NegDn[1] and NegUp ;
def ReleaseDown = PosUp[1] and PosDn ;


plot UpSignal = if SqueezeLineGreen and ReleaseUp and
yellow then low else Double.NaN;
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpSignal.SetDefaultColor(Color.PINK);
UpSignal.SetLineWeight(3);

plot DownSignal = if SqueezeLineGreen and ReleaseDown and
Cyan then high else Double.NaN;
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownSignal.SetDefaultColor(Color.PINK);
DownSignal.SetLineWeight(3);



#==== code for backtest
input Limit = 1.0;
input SL_Lookback = 5;
input lot_Size = 100;

def EP = EntryPrice();
def SL_Long = Lowest(low, SL_Lookback);
def SL_Short = Highest(high, SL_Lookback);
def target_Long = close + ((close - SL_Long) * Limit);
def target_Short = close - ((SL_Short - close) * Limit);

plot slbar_Long = if UpSignal then SL_Long else Double.NaN;
slbar_Long.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
slbar_Long.SetDefaultColor(Color.WHITE);

plot slbar_Short = if DownSignal then SL_Short else Double.NaN;
slbar_Short.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
slbar_Short.SetDefaultColor(Color.WHITE);

plot Limitbar_Long = if UpSignal then target_Long else Double.NaN;
Limitbar_Long.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Limitbar_Long.SetDefaultColor(Color.WHITE);

plot Limitbar_Short = if DownSignal then target_Short else Double.NaN;
Limitbar_Short.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
Limitbar_Short.SetDefaultColor(Color.WHITE);


AddOrder(OrderType.BUY_TO_OPEN, UpSignal, close, lot_Size, Color.WHITE, Color.WHITE, "LE");
AddOrder(OrderType.SELL_To_Close, if DownSignal then close else high >= target_Long, close, 1, Color.WHITE, Color.WHITE, "LX");


AddOrder(OrderType.SELL_TO_OPEN, DownSignal[1], close[1], lot_Size, Color.CYAN, Color.CYAN, "SE");
AddOrder(OrderType.BUY_to_close, if UpSignal then close else low<= target_Short, close, 1, Color.CYAN, Color.CYAN, "SX" );
 

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
305 Online
Create Post

Similar threads

Similar threads

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