I am trying to code the sequence of events: ST then UR then LS as seen below with horizontal lines. If the sequence does not occur nothing gets plot on the chart. I have post the script and looking for some tips to do this. Thanks!
Ruby:
def priceH = high;
def priceL = low;
def priceC = close;
def o = open;
def h = high;
def l = low;
def c = close;
input percentageReversal = 2;
input absoluteReversal = 0.0;
input atrLength = 2;
input atrReversal = 1;
input tickReversal = 0;
Assert(percentageReversal >= 0, "'percentage reversal' must not be negative: " + percentageReversal);
Assert(absoluteReversal >= 0, "'absolute reversal' must not be negative: " + absoluteReversal);
Assert(atrReversal >= 0, "'atr reversal' must not be negative: " + atrReversal);
Assert(tickReversal >= 0, "'ticks' must not be negative: " + tickReversal);
Assert(percentageReversal != 0 or absoluteReversal != 0 or atrReversal != 0 or tickReversal != 0, "Either 'percentage reversal' or 'absolute reversal' or 'atr reversal' or 'tick reversal' must not be zero");
def absReversal;
if (absoluteReversal != 0) {
absReversal = absoluteReversal;
} else {
absReversal = tickReversal * TickSize();
}
def hlPivot;
if (atrReversal != 0) {
hlPivot = percentageReversal / 100 + WildersAverage(TrueRange(high, close, low), atrLength) / close * atrReversal;
} else {
hlPivot = percentageReversal / 100;
}
def state = {default init, undefined, uptrend, downtrend};
def maxPriceH;
def minPriceL;
def newMax;
def newMin;
def prevMaxH = GetValue(maxPriceH, 1);
def prevMinL = GetValue(minPriceL, 1);
if GetValue(state, 1) == GetValue(state.init, 0) {
maxPriceH = priceH;
minPriceL = priceL;
newMax = yes;
newMin = yes;
state = state.undefined;
} else if GetValue(state, 1) == GetValue(state.undefined, 0) {
if priceH >= prevMaxH {
state = state.uptrend;
maxPriceH = priceH;
minPriceL = prevMinL;
newMax = yes;
newMin = no;
} else if priceL <= prevMinL {
state = state.downtrend;
maxPriceH = prevMaxH;
minPriceL = priceL;
newMax = no;
newMin = yes;
} else {
state = state.undefined;
maxPriceH = prevMaxH;
minPriceL = prevMinL;
newMax = no;
newMin = no;
}
} else if GetValue(state, 1) == GetValue(state.uptrend, 0) {
if priceL <= prevMaxH - prevMaxH * hlPivot - absReversal {
state = state.downtrend;
maxPriceH = prevMaxH;
minPriceL = priceL;
newMax = no;
newMin = yes;
} else {
state = state.uptrend;
if (priceH >= prevMaxH) {
maxPriceH = priceH;
newMax = yes;
} else {
maxPriceH = prevMaxH;
newMax = no;
}
minPriceL = prevMinL;
newMin = no;
}
} else {
if priceH >= prevMinL + prevMinL * hlPivot + absReversal {
state = state.uptrend;
maxPriceH = priceH;
minPriceL = prevMinL;
newMax = yes;
newMin = no;
} else {
state = state.downtrend;
maxPriceH = prevMaxH;
newMax = no;
if (priceL <= prevMinL) {
minPriceL = priceL;
newMin = yes;
} else {
minPriceL = prevMinL;
newMin = no;
}
}
}
def barNumber = BarNumber();
def barCount = HighestAll(If(IsNaN(priceH), 0, barNumber));
def newState = GetValue(state, 0) != GetValue(state, 1);
def offset = barCount - barNumber + 1;
def highPoint = state == state.uptrend and priceH == maxPriceH;
def lowPoint = state == state.downtrend and priceL == minPriceL;
def lastH;
if highPoint and offset > 1 {
lastH = fold iH = 1 to offset with tH = priceH while !IsNaN(tH) and !GetValue(newState, -iH) do if GetValue(newMax, -iH) or iH == offset - 1 and GetValue(priceH, -iH) == tH then Double.NaN else tH;
} else {
lastH = Double.NaN;
}
def lastL;
if lowPoint and offset > 1 {
lastL = fold iL = 1 to offset with tL = priceL while !IsNaN(tL) and !GetValue(newState, -iL) do if GetValue(newMin, -iL) or iL == offset - 1 and GetValue(priceL, -iL) == tL then Double.NaN else tL;
} else {
lastL = Double.NaN;
}
plot ZZ;
if barNumber == 1 {
ZZ = fold iF = 1 to offset with tP = Double.NaN while IsNaN(tP) do if GetValue(state, -iF) == GetValue(state.uptrend, 0) then priceL else if GetValue(state, -iF) == GetValue(state.downtrend, 0) then priceH else Double.NaN;
} else if barNumber == barCount {
ZZ = if highPoint or state == state.downtrend and priceL > minPriceL then priceH else if lowPoint or state == state.uptrend and priceH < maxPriceH then priceL else Double.NaN;
} else {
ZZ = if !IsNaN(lastH) then lastH else if !IsNaN(lastL) then lastL else Double.NaN;
}
ZZ.SetDefaultColor(GetColor(1));
ZZ.EnableApproximation();
input toggle = yes;
input reversalamount = 8.0;
def ATRSave = if !IsNaN(ZZ) then ZZ else GetValue(ATRSave, 1);
def ATRSave1 = if ATRSave != ATRSave[1] then ATRSave[1] else GetValue(ATRSave1, 1);
def ATRSave2 = if ATRSave != ATRSave then ATRSave else GetValue(ATRSave1, 1);
def ATRSave3 = if ATRSave2 != ATRSave2[1] then ATRSave2[1] else GetValue(ATRSave3, 1);
def ATRSave4 = if ATRSave3 != ATRSave3[1] then ATRSave3[1] else GetValue(ATRSave4, 1);
def ATRSave5 = if ATRSave4 != ATRSave4[1] then ATRSave4[1] else GetValue(ATRSave5, 1);
def ATRSave6 = if ATRSave5 != ATRSave5[1] then ATRSave5[1] else GetValue(ATRSave6, 1);
def ATRSave7 = if ATRSave6 != ATRSave6[1] then ATRSave6[1] else GetValue(ATRSave7, 1);
#########################################################################################
#Trend
#########################################################################################
rec fiveDaysSma = CompoundValue(5, Average(close, 5)[0], Double.NaN);
def LongTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 40)[0];
def MiddleTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 15)[0];
def ShortTermTrendSlope = LinearRegressionSlope(price = fiveDaysSma, length = 5)[0];
#########################################################################################
#Sequence 1
#########################################################################################
input length = 20;
input trendSetup = 2;
input error = .90;
def ErrMargin = error * Average(BodyHeight(), length);
def IsWhite = open < close;
def IsAlmostEqual = AbsValue(ATRSave - ATRSave2) <= ErrMargin and IsDescending(ATRSave, trendSetup);
def LR = isAlmostEqual;
plot Test = if LR then (low - 3 * TickSize()) else Double.NaN;
Test.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
Test.SetStyle(Curve.POINTS);
Test.SetDefaultColor(Color.white);
Test.HideBubble();
Test.HideTitle();
#########################################################################################
#Sequence 2
#########################################################################################
def Upper_ID = ATRSave > ATRSave1 && ATRSave > ATRSAve2 && ATRSave > ATRSave3 && ATRSave2 > ATRSave1 && ATRSave2 > ATRSave3 && (ATRSave1 > ATRSave3 or ATRSave3 > ATRSave1) ;
plot Upper_Resistance = if Upper_ID then (high + 3 * TickSize()) else Double.NaN;
Upper_Resistance.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
Upper_Resistance.SetStyle(Curve.POINTS);
Upper_Resistance.SetDefaultColor(Color.RED);
Upper_Resistance.HideBubble();
Upper_Resistance.HideTitle();
#########################################################################################
#Sequence 3
#########################################################################################
def Lower_ID = ATRSave1 > ATRSave && ATRSave1 > ATRSAve2 && ATRSave1 > ATRSave3 && ATRSave1 > ATRSave4 && ATRSave3 > ATRSave && ATRSave3 > ATRSave2 && ATRSave3 > ATRSave4 && ATRSave4 > ATRSave && (ATRSave4 > ATRSave2 or ATRSave2 > ATRSave4) && ATRSave2 > ATRSave;
plot Lower_Range = if Lower_ID then (low - 3 * TickSize()) else Double.NaN;
Lower_Range.SetPaintingStrategy(PaintingStrategy.LINE_VS_POINTS);
Lower_Range.SetStyle(Curve.POINTS);
Lower_Range.SetDefaultColor(Color.GREEN);
Lower_Range.HideBubble();
Lower_Range.HideTitle();
#########################################################################################
#Chartbubble
#########################################################################################
addchartBubble(toggle, Test, "ST", color.WHITE, no);
addchartBubble(toggle, Upper_Resistance, "UR", color.RED, yes);
addchartBubble(toggle, Lower_Range, "LS", color.GREEN, no);
#########################################################################################
#Horizontal Lines
#########################################################################################
input lineLength = 5;
def BN = BarNumber();
def barCount1 = Highestall(If(IsNaN(test), 0.0, BN));
def closeLine = if BN == 1 then Double.NaN
else if BN == barCount1 - linelength
then test[-lineLength]
else closeLine[1];
plot x = closeline;
def barCount2 = Highestall(If(IsNaN(Upper_Resistance), 0.0, BN));
def closeLine2 = if BN == 1 then Double.NaN
else if BN == barCount2 - linelength
then Upper_Resistance[-lineLength]
else closeLine2[1];
plot x2 = closeline2;
def barCount3 = Highestall(If(IsNaN(Lower_Range), 0.0, BN));
def closeLine3 = if BN == 1 then Double.NaN
else if BN == barCount3 - linelength
then Lower_Range[-lineLength]
else closeLine3[1];
plot x3 = closeline3;