Futures_Beginner
Member
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_T
pen" or SE "Sell_T
pen" 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
Code
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_T
3. How to close the position at the end of the day if no sl/target it met
Really appreciate your support
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" );