Anyone know why I'm getting N/A in this code?

greco26

Active member
Im trying to get the bar number and ultimate the high of the last FractalUP. I can't seem to get the data though. I want to know when the current high crosses above the high of the last fractalUp.

The white label give me N/A.


Code:
def sequenceCount = 2;
def bn = barnumber();

def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
    if GetValue(high, -i1, -maxSideLength) > high or (GetValue(high, -i1, -maxSideLength) == high and count1 == 0) then -1
    else if GetValue(high, -i1, -maxSideLength) < high then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
    if GetValue(high, i2, maxSideLength) > high or (GetValue(high, i2, maxSideLength) == high and count2 >= 1) then -1
    else if GetValue(high, i2, maxSideLength) < high then count2 + 1 else count2;

def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
    if GetValue(low, -i3, -maxSideLength) < low or (GetValue(low, -i3, -maxSideLength) == low and count3 == 0) then -1
    else if GetValue(high, -i3, -maxSideLength) > low then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
    if GetValue(low, i4, maxSideLength) < low or (GetValue(low, i4, maxSideLength) == low and count4 >= 1) then -1
    else if GetValue(low, i4, maxSideLength) > low then count4 + 1 else count4;

def UpFractal = if upRightSide == sequenceCount and upLeftSide == sequenceCount then high else Double.NaN;
def DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then low else Double.NaN;


def UpFractalBN = if UpFractal then bn else UpFractalBN[1];
def UpHigh = if bn == UpFractalBN then high else UpHigh[1];
def CrosSUp = if high crosses above Uphigh then 1 else 0;
addlabel(yes, CrosSUp, color.white);
addchartbubble(UpHigh,high,UpHigh,color.white);
 
@greco26
def UpFractalBN = if !IsNaN(UpFractal) then bn else UpFractalBN[1];
This worked perfect. Can you tell me why my way wasn't working? I assumed UpFractal would return as the bar number so shouldn't I have got the same result? Obviously not but let me know so I learn for next time if possible. Thank you so much!!
 
@greco26
UpFractal is defined in your code as being the 'high value' or 'nothing'.
Your original code was checking UpFractal for a 'yes' or 'no'.

'Nothing' is not the same as 'No'
 
Last edited:
@Svanoy, I wanted to see if you can help me with something else on this code. Folds are my nemesis. I just can't get them. Even super simple ones, I can't wrap my brain around the mechanics. Anyway, In addition to capturing the first UpFractalBN, I wanted to capture the prior UpFractalBN as well as the one before that. I've tried to manipulate the code in the folds to try and get different results but, since folds and I don't get along, I just can't do it. Would you be able to give me some hints at how to get the historical UpFractalBN's by chance?
 
@greco26
You don't need folds, you have already defined the values.
You just need to carry on the previous values as the chart progresses.

Add this after the definition line of UpFractalBn.
Code:
def Prev1UpFractalBN = if UpFractalBN != UpFractalBN[1] then UpFractalBN[1] else Prev1UpFractalBN[1];
def Prev2UpFractalBN = if Prev1UpFractalBN != Prev1UpFractalBN[1] then Prev1UpFractalBN[1] else Prev2UpFractalBN[1];

addchartbubble(yes,low,UpFractalBN,color.White);
addchartbubble(yes,low,Prev1UpFractalBN,color.White);
addchartbubble(yes,low,Prev2UpFractalBN,color.White);

rGvNgWO.png
 
Strange, Its not returning the proper highs. I put it in a 30 minute chart and also in a 30 minute watchlist column. Both come back with different highs and neither are correct. I think this one may be beyond my skill set.

Code:
def sequenceCount = 2;
def bn = barnumber();

def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
    if GetValue(high, -i1, -maxSideLength) > high or (GetValue(high, -i1, -maxSideLength) == high and count1 == 0) then -1
    else if GetValue(high, -i1, -maxSideLength) < high then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
    if GetValue(high, i2, maxSideLength) > high or (GetValue(high, i2, maxSideLength) == high and count2 >= 1) then -1
    else if GetValue(high, i2, maxSideLength) < high then count2 + 1 else count2;

def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
    if GetValue(low, -i3, -maxSideLength) < low or (GetValue(low, -i3, -maxSideLength) == low and count3 == 0) then -1
    else if GetValue(high, -i3, -maxSideLength) > low then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
    if GetValue(low, i4, maxSideLength) < low or (GetValue(low, i4, maxSideLength) == low and count4 >= 1) then -1
    else if GetValue(low, i4, maxSideLength) > low then count4 + 1 else count4;

def UpFractal = if upRightSide == sequenceCount and upLeftSide == sequenceCount then high else Double.NaN;

def DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then low else Double.NaN;


def UpFractalBN = if !IsNaN(UpFractal) then bn else UpFractalBN[1];
def Prev1UpFractalBN = if UpFractalBN != UpFractalBN[1] then UpFractalBN[1] else Prev1UpFractalBN[1];
def Prev2UpFractalBN = if Prev1UpFractalBN != Prev1UpFractalBN[1] then Prev1UpFractalBN[1] else Prev2UpFractalBN[1];

def DnFractalBN = if !IsNaN(DownFractal) then bn else DNFractalBN[1];
def Prev1DnFractalBN = if DnFractalBN != DnFractalBN[1] then DnFractalBN[1] else Prev1DnFractalBN[1];
def Prev2DnFractalBN = if Prev1DnFractalBN != Prev1DnFractalBN[1] then Prev1DnFractalBN[1] else Prev2DnFractalBN[1];

def UpHigh = if bn == UpFractalBN then high else UpHigh[1];
def Prev1UpHigh = if bn == Prev1UpFractalBN then high else Prev1UpHigh[1];
def Prev2UpHigh = if bn == Prev2UpFractalBN then low else Prev2UpHigh[1];

def DnLow = if bn == DnFractalBN then low else DnLow[1];
def Prev1DnHigh = if bn == Prev1DnFractalBN then high else Prev1DnHigh[1];
def Prev2DnHigh = if bn == Prev2DnFractalBN then low else Prev2DnHigh[1];


#def NearTop2 = Close > UpHigh and close > UpHigh2;
def NearTop = close > UpHigh;
def NearBottom = close < DnLow;
def Prev1NearTop = close > Prev1UpHigh;
def Prev1NearBottom = close < Prev1DnHigh;
def Prev2NearTop = close > Prev2UpHigh;
def Prev2NearBottom = close < Prev2DnHigh;


addlabel(yes, Prev2UpHigh, color.white);
 
@greco26
You have to remember you are looking back at data.
So you just use GetValue and use the current barnumber and the barnumber values carried forward to set how many bars before to pull your desired data from.

This is an interesting script, I would like to see it when you finish.

Code:
def sequenceCount = 2;
def bn = barnumber();

def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
    if GetValue(high, -i1, -maxSideLength) > high or (GetValue(high, -i1, -maxSideLength) == high and count1 == 0) then -1
    else if GetValue(high, -i1, -maxSideLength) < high then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
    if GetValue(high, i2, maxSideLength) > high or (GetValue(high, i2, maxSideLength) == high and count2 >= 1) then -1
    else if GetValue(high, i2, maxSideLength) < high then count2 + 1 else count2;

def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
    if GetValue(low, -i3, -maxSideLength) < low or (GetValue(low, -i3, -maxSideLength) == low and count3 == 0) then -1
    else if GetValue(high, -i3, -maxSideLength) > low then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
    if GetValue(low, i4, maxSideLength) < low or (GetValue(low, i4, maxSideLength) == low and count4 >= 1) then -1
    else if GetValue(low, i4, maxSideLength) > low then count4 + 1 else count4;

def UpFractal = if upRightSide == sequenceCount and upLeftSide == sequenceCount then high else Double.NaN;

def DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then low else Double.NaN;


def UpFractalBN = if !IsNaN(UpFractal) then bn else UpFractalBN[1];
def Prev1UpFractalBN = if UpFractalBN != UpFractalBN[1] then UpFractalBN[1] else Prev1UpFractalBN[1];
def Prev2UpFractalBN = if Prev1UpFractalBN != Prev1UpFractalBN[1] then Prev1UpFractalBN[1] else Prev2UpFractalBN[1];

def DnFractalBN = if !IsNaN(DownFractal) then bn else DNFractalBN[1];
def Prev1DnFractalBN = if DnFractalBN != DnFractalBN[1] then DnFractalBN[1] else Prev1DnFractalBN[1];
def Prev2DnFractalBN = if Prev1DnFractalBN != Prev1DnFractalBN[1] then Prev1DnFractalBN[1] else Prev2DnFractalBN[1];

def UpHigh = GetValue(high,bn-UpFractalBN); #if bn == UpFractalBN then high else UpHigh[1];
def Prev1UpHigh = GetValue(high,bn-Prev1UpFractalBN); #if bn == Prev1UpFractalBN then high else Prev1UpHigh[1];
def Prev2UpHigh = GetValue(high,bn-Prev2UpFractalBN); #if bn == Prev2UpFractalBN then low else Prev2UpHigh[1];

def DnLow = GetValue(high,bn-DnFractalBN); #if bn == DnFractalBN then low else DnLow[1];
def Prev1DnHigh = GetValue(high,bn-Prev1DnFractalBN); #if bn == Prev1DnFractalBN then high else Prev1DnHigh[1];
def Prev2DnHigh = GetValue(high,bn-Prev2DnFractalBN); #if bn == Prev2DnFractalBN then low else Prev2DnHigh[1];


#def NearTop2 = Close > UpHigh and close > UpHigh2;
def NearTop = close > UpHigh;
def NearBottom = close < DnLow;
def Prev1NearTop = close > Prev1UpHigh;
def Prev1NearBottom = close < Prev1DnHigh;
def Prev2NearTop = close > Prev2UpHigh;
def Prev2NearBottom = close < Prev2DnHigh;


addchartbubble(yes,low,uphigh,color.White,no);
addchartbubble(yes,low,Prev1uphigh,color.White,no);
addchartbubble(yes,low,prev2uphigh,color.White,no);
 
This is great. I never did it this way using get value. this is so much easier than the way Ive do it in the past. THANK YOU FOR TEACHING ME!!!! ...and the code of course. :)
 
@Svanoy, I hope all is well on your side. I'm nearing the completion of this script. The only thing left is to get the plot lines to work properly so I was hoping you might know what I've got wrong here. The very last 2 sections on the code below are to identify the bar Prev1DnL and DnL bar numbers, calculate the rise and run then draw a diagonal between the two bars from their lows. I would do the same think on the high end as well. My code doesn't seem to be working for those lines though. Does anything jump out at you which looks wrong?

The end goal is the draw a megaphone shape on the chart and extend the lines beyond the last bar. I'll have to tweak one side of the megaphone as there won't be 2 points to connect on both sides. For example, I'll end up connecting the DnL to the previous higher low which will properly draw the wedge.

Let me know if you see my mistake in the code though. Thank you!!


Code:
def H = high ;
def L = low ;
def O = open ;
def C = close ;

# Calculate the length of the candle's wicks
def UpperWick = H - Max(O, C);
def LowerWick = Min(O, C) - L;

# Calculate the length of the candle's body
def CandleBody = BodyHeight();
def range = H - L;
def cond = range == Highest(range, 5);

# Compare the wicks to the body to ensure that one wick is 1.25x longer than the body
# also compare the other wick to ensure that it is a "small" wick
def Hammer = H < H[1] and (LowerWick / CandleBody >= 1.25) and (UpperWick / CandleBody <= 1);
def Shooter = L > L[1] and (UpperWick / CandleBody >= 1.25) and (LowerWick / CandleBody <= 1);
def inside = H < H[1] and L > L[1];
def twodn =  H < H[1];
def twoup =  L > L[1];


def sequenceCount = 2;
def bn = BarNumber();

def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
    if GetValue(H, -i1, -maxSideLength) > H or (GetValue(H, -i1, -maxSideLength) == H and count1 == 0) then -1
    else if GetValue(H, -i1, -maxSideLength) < H then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
    if GetValue(H, i2, maxSideLength) > H or (GetValue(H, i2, maxSideLength) == H and count2 >= 1) then -1
    else if GetValue(H, i2, maxSideLength) < H then count2 + 1 else count2;

def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
    if GetValue(L, -i3, -maxSideLength) < L or (GetValue(L, -i3, -maxSideLength) == L and count3 == 0) then -1
    else if GetValue(H, -i3, -maxSideLength) > L then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
    if GetValue(L, i4, maxSideLength) < L or (GetValue(L, i4, maxSideLength) == L and count4 >= 1) then -1
    else if GetValue(L, i4, maxSideLength) > L then count4 + 1 else count4;

def UpFractal = if  upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
def DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;



def UpFractalBN = if !IsNaN(UpFractal) then bn else UpFractalBN[1];
def Prev1UpFractalBN = if UpFractalBN != UpFractalBN[1] then UpFractalBN[1] else Prev1UpFractalBN[1];
#def Prev2UpFractalBN = if Prev1UpFractalBN != Prev1UpFractalBN[1] then Prev1UpFractalBN[1] else Prev2UpFractalBN[1];

def DnFractalBN = if !IsNaN(DownFractal) then bn else DnFractalBN[1];
def Prev1DnFractalBN = if DnFractalBN != DnFractalBN[1] then DnFractalBN[1] else Prev1DnFractalBN[1];
#def Prev2DnFractalBN = if Prev1DnFractalBN != Prev1DnFractalBN[1] then Prev1DnFractalBN[1] else Prev2DnFractalBN[1];

def UpH = GetValue(H, bn - UpFractalBN); #if bn == UpFractalBN then H else UpH[1];
def Prev1UpH = GetValue(H,bn-Prev1UpFractalBN); #if bn == Prev1UpFractalBN then H else Prev1UpH[1];
#def Prev2UpH = GetValue(H,bn-Prev2UpFractalBN); #if bn == Prev2UpFractalBN then L else Prev2UpH[1];

def DnL = GetValue(L, bn - DnFractalBN); #if bn == DnFractalBN then L else DnL[1];
def Prev1DnL = GetValue(L,bn-Prev1DnFractalBN); #if bn == Prev1DnFractalBN then H else Prev1DnH[1];
#def Prev2DnL = GetValue(L,bn-Prev2DnFractalBN); #if bn == Prev2DnFractalBN then L else Prev2DnH[1];


plot upfractalwedge = if upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
plot DownFractalwedge = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;

UpFractalwedge.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
UpFractalwedge.SetDefaultColor(GetColor(3));
UpFractalwedge.SetLineWeight(5);
DownFractalwedge.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
DownFractalwedge.SetDefaultColor(GetColor(4));
DownFractalwedge.SetLineWeight(5);

#def Prev2NearTop = C > Prev2UpH and C > Prev1UpH and C > UpH;
#def Prev2NearBottom = C < Prev2DnL and C < Prev1DnL and C < DnL;
#def Prev1NearTop = C > Prev1UpH and C > UpH;
#def Prev1NearBottom = C < Prev1DnL and C < DnL;
def NearTop = C > UpH;
def NearBottom = C < DnL;
def ReversaloffTop = NearTop and (Shooter or inside);
def ReversaloffBottom = NearBottom and (Hammer or inside);

def oldestbn = if UpFractalBN > DnFractalBN then UpFractalBN else DnFractalBN;
def BFdiff = if bn < Highestall(oldestbn) then double.nan else (UpH-DnL)/2 + dnL;

plot SSS = bfdiff;
sss.SetStyle(Curve.MEDIUM_DASH);
sss.SetDefaultColor(Color.gray);
sss.SetLineWeight(1);

input showcloud = yes;
input showbubbles = yes;
AddCloud(if showcloud and bn >= Highestall( oldestbn) then UpH else Double.NaN, DnL, createcolor(43,45,48  ), createcolor(50, 90, 59 ));
#AddCloud(if showcloud then UpH else Double.NaN, UpH, createcolor(110, 69, 60    ), createcolor(110, 69, 60    ));
addchartbubble(if showbubbles  and  bn == Highestall(bn-5) then 1 else 0 ,BFdiff,"M SSS", color.yellow);
#Alert(triggerlong or triggershort, getunderlyingsymbol() + " IS GOING OUTSIDE!!", Alert.TICK, Sound.Ding);
#plot triggerlong= L< L[1] and H < H[1] and C >= fiftypct;
#plot triggershort= H> H[1] and L > L[1] and C <= fiftypct;

def Rise = Prev1DnL - DnL;
def Run = DnFractalBN - Prev1DnFractalBN;
def IndeterminateSlope = if Run ==0 then yes else no;
def Slope = if !IndeterminateSlope then Rise / Run else double.nan;
def Line = if bn == 1 then double.nan else if bn == Prev1DnFractalBN then Prev1DnL else Line[1] - Slope;
Plot Drawline = Line;
DrawLine.setpaintingstrategy(paintingStrategy.points);
DrawLine.SetLineWeight(5);


def HRise = Prev1UpH - UpH;
def HRun = UpFractalBN - Prev1UpFractalBN;
def HIndeterminateSlope = if HRun ==0 then yes else no;
def HSlope = if !HIndeterminateSlope then HRise / HRun else double.nan;
def HLine = if bn == 1 then double.nan else if bn == Prev1UpFractalBN then Prev1UpH else HLine[1] - HSlope;
Plot HDrawline = HLine;
HDrawLine.setpaintingstrategy(paintingStrategy.points);
HDrawLine.SetLineWeight(5);






#
 
@Svanoy, I hope all is well on your side. I'm nearing the completion of this script. The only thing left is to get the plot lines to work properly so I was hoping you might know what I've got wrong here. The very last 2 sections on the code below are to identify the bar Prev1DnL and DnL bar numbers, calculate the rise and run then draw a diagonal between the two bars from their lows. I would do the same think on the high end as well. My code doesn't seem to be working for those lines though. Does anything jump out at you which looks wrong?

The end goal is the draw a megaphone shape on the chart and extend the lines beyond the last bar. I'll have to tweak one side of the megaphone as there won't be 2 points to connect on both sides. For example, I'll end up connecting the DnL to the previous higher low which will properly draw the wedge.

Let me know if you see my mistake in the code though. Thank you!!


Code:
def H = high ;
def L = low ;
def O = open ;
def C = close ;

# Calculate the length of the candle's wicks
def UpperWick = H - Max(O, C);
def LowerWick = Min(O, C) - L;

# Calculate the length of the candle's body
def CandleBody = BodyHeight();
def range = H - L;
def cond = range == Highest(range, 5);

# Compare the wicks to the body to ensure that one wick is 1.25x longer than the body
# also compare the other wick to ensure that it is a "small" wick
def Hammer = H < H[1] and (LowerWick / CandleBody >= 1.25) and (UpperWick / CandleBody <= 1);
def Shooter = L > L[1] and (UpperWick / CandleBody >= 1.25) and (LowerWick / CandleBody <= 1);
def inside = H < H[1] and L > L[1];
def twodn =  H < H[1];
def twoup =  L > L[1];


def sequenceCount = 2;
def bn = BarNumber();

def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
    if GetValue(H, -i1, -maxSideLength) > H or (GetValue(H, -i1, -maxSideLength) == H and count1 == 0) then -1
    else if GetValue(H, -i1, -maxSideLength) < H then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
    if GetValue(H, i2, maxSideLength) > H or (GetValue(H, i2, maxSideLength) == H and count2 >= 1) then -1
    else if GetValue(H, i2, maxSideLength) < H then count2 + 1 else count2;

def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
    if GetValue(L, -i3, -maxSideLength) < L or (GetValue(L, -i3, -maxSideLength) == L and count3 == 0) then -1
    else if GetValue(H, -i3, -maxSideLength) > L then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
    if GetValue(L, i4, maxSideLength) < L or (GetValue(L, i4, maxSideLength) == L and count4 >= 1) then -1
    else if GetValue(L, i4, maxSideLength) > L then count4 + 1 else count4;

def UpFractal = if  upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
def DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;



def UpFractalBN = if !IsNaN(UpFractal) then bn else UpFractalBN[1];
def Prev1UpFractalBN = if UpFractalBN != UpFractalBN[1] then UpFractalBN[1] else Prev1UpFractalBN[1];
#def Prev2UpFractalBN = if Prev1UpFractalBN != Prev1UpFractalBN[1] then Prev1UpFractalBN[1] else Prev2UpFractalBN[1];

def DnFractalBN = if !IsNaN(DownFractal) then bn else DnFractalBN[1];
def Prev1DnFractalBN = if DnFractalBN != DnFractalBN[1] then DnFractalBN[1] else Prev1DnFractalBN[1];
#def Prev2DnFractalBN = if Prev1DnFractalBN != Prev1DnFractalBN[1] then Prev1DnFractalBN[1] else Prev2DnFractalBN[1];

def UpH = GetValue(H, bn - UpFractalBN); #if bn == UpFractalBN then H else UpH[1];
def Prev1UpH = GetValue(H,bn-Prev1UpFractalBN); #if bn == Prev1UpFractalBN then H else Prev1UpH[1];
#def Prev2UpH = GetValue(H,bn-Prev2UpFractalBN); #if bn == Prev2UpFractalBN then L else Prev2UpH[1];

def DnL = GetValue(L, bn - DnFractalBN); #if bn == DnFractalBN then L else DnL[1];
def Prev1DnL = GetValue(L,bn-Prev1DnFractalBN); #if bn == Prev1DnFractalBN then H else Prev1DnH[1];
#def Prev2DnL = GetValue(L,bn-Prev2DnFractalBN); #if bn == Prev2DnFractalBN then L else Prev2DnH[1];


plot upfractalwedge = if upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
plot DownFractalwedge = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;

UpFractalwedge.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
UpFractalwedge.SetDefaultColor(GetColor(3));
UpFractalwedge.SetLineWeight(5);
DownFractalwedge.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
DownFractalwedge.SetDefaultColor(GetColor(4));
DownFractalwedge.SetLineWeight(5);

#def Prev2NearTop = C > Prev2UpH and C > Prev1UpH and C > UpH;
#def Prev2NearBottom = C < Prev2DnL and C < Prev1DnL and C < DnL;
#def Prev1NearTop = C > Prev1UpH and C > UpH;
#def Prev1NearBottom = C < Prev1DnL and C < DnL;
def NearTop = C > UpH;
def NearBottom = C < DnL;
def ReversaloffTop = NearTop and (Shooter or inside);
def ReversaloffBottom = NearBottom and (Hammer or inside);

def oldestbn = if UpFractalBN > DnFractalBN then UpFractalBN else DnFractalBN;
def BFdiff = if bn < Highestall(oldestbn) then double.nan else (UpH-DnL)/2 + dnL;

plot SSS = bfdiff;
sss.SetStyle(Curve.MEDIUM_DASH);
sss.SetDefaultColor(Color.gray);
sss.SetLineWeight(1);

input showcloud = yes;
input showbubbles = yes;
AddCloud(if showcloud and bn >= Highestall( oldestbn) then UpH else Double.NaN, DnL, createcolor(43,45,48  ), createcolor(50, 90, 59 ));
#AddCloud(if showcloud then UpH else Double.NaN, UpH, createcolor(110, 69, 60    ), createcolor(110, 69, 60    ));
addchartbubble(if showbubbles  and  bn == Highestall(bn-5) then 1 else 0 ,BFdiff,"M SSS", color.yellow);
#Alert(triggerlong or triggershort, getunderlyingsymbol() + " IS GOING OUTSIDE!!", Alert.TICK, Sound.Ding);
#plot triggerlong= L< L[1] and H < H[1] and C >= fiftypct;
#plot triggershort= H> H[1] and L > L[1] and C <= fiftypct;

def Rise = Prev1DnL - DnL;
def Run = DnFractalBN - Prev1DnFractalBN;
def IndeterminateSlope = if Run ==0 then yes else no;
def Slope = if !IndeterminateSlope then Rise / Run else double.nan;
def Line = if bn == 1 then double.nan else if bn == Prev1DnFractalBN then Prev1DnL else Line[1] - Slope;
Plot Drawline = Line;
DrawLine.setpaintingstrategy(paintingStrategy.points);
DrawLine.SetLineWeight(5);


def HRise = Prev1UpH - UpH;
def HRun = UpFractalBN - Prev1UpFractalBN;
def HIndeterminateSlope = if HRun ==0 then yes else no;
def HSlope = if !HIndeterminateSlope then HRise / HRun else double.nan;
def HLine = if bn == 1 then double.nan else if bn == Prev1UpFractalBN then Prev1UpH else HLine[1] - HSlope;
Plot HDrawline = HLine;
HDrawLine.setpaintingstrategy(paintingStrategy.points);
HDrawLine.SetLineWeight(5);
#

you have a nan value at bar #1 that is cascading through all the bars

this may not not be perfect, but it creates data that is plotted.

in the 2 similar lines, replace double.nan with close
def Line = if bn == 1 then double.nan else if bn == Prev1DnFractalBN then Prev1DnL else Line[1] - Slope;

def Line = if bn == 1 then close else if bn == Prev1DnFractalBN then Prev1DnL else Line[1] - Slope;
def HLine = if bn == 1 then close else if bn == Prev1UpFractalBN then Prev1UpH else HLine[1] - HSlope;


i put the variables from the def line = formula into a bubble , to see what was going on
line was always na

addchartbubble(1, low*0.996,
barnumber() + "\n" +
bn + "\n" +
Prev1DnFractalBN + "\n" +
Prev1DnL + "\n" +
Line[1]+ "\n" +
Slope + "\n" +
line
, color.yellow, no);
 
you have a nan value at bar #1 that is cascading through all the bars

this may not not be perfect, but it creates data that is plotted.

in the 2 similar lines, replace double.nan with close
def Line = if bn == 1 then double.nan else if bn == Prev1DnFractalBN then Prev1DnL else Line[1] - Slope;

def Line = if bn == 1 then close else if bn == Prev1DnFractalBN then Prev1DnL else Line[1] - Slope;
def HLine = if bn == 1 then close else if bn == Prev1UpFractalBN then Prev1UpH else HLine[1] - HSlope;


i put the variables from the def line = formula into a bubble , to see what was going on
line was always na

addchartbubble(1, low*0.996,
barnumber() + "\n" +
bn + "\n" +
Prev1DnFractalBN + "\n" +
Prev1DnL + "\n" +
Line[1]+ "\n" +
Slope + "\n" +
line
, color.yellow, no);
Thank you so much. I dont know why I didn’t catch that.…
 
Guys, Im going slightly mad here and I'm so sorry I'm dragging you guys into this. So all the data is calculating properly for the line plot however the lines just won't plot on the chart and I can't figure out why. I've spend hours trying to figure it out but no matter what, it won't plot. any help you can give would be awesome. these small things are what takes forever for me. crazy...
 
Lol, I figured it out. The lines were plotting. Just so far off the chart from where I was expecting them that I counldnt see them until I zoomed out. Well, at least I got that. Thanks again
 
Here's the most recent code if you want to take a look.
Code:
def H = high ;
def L = low ;
def O = open ;
def C = close ;

# Calculate the length of the candle's wicks
def UpperWick = H - Max(O, C);
def LowerWick = Min(O, C) - L;

# Calculate the length of the candle's body
def CandleBody = BodyHeight();
def range = H - L;
def cond = range == Highest(range, 5);

# Compare the wicks to the body to ensure that one wick is 1.25x longer than the body
# also compare the other wick to ensure that it is a "small" wick
def Hammer = H < H[1] and (LowerWick / CandleBody >= 1.25) and (UpperWick / CandleBody <= 1);
def Shooter = L > L[1] and (UpperWick / CandleBody >= 1.25) and (LowerWick / CandleBody <= 1);
def inside = H < H[1] and L > L[1];
def twodn =  H < H[1];
def twoup =  L > L[1];


def sequenceCount = 2;
def bn = BarNumber();

def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
    if GetValue(H, -i1, -maxSideLength) > H or (GetValue(H, -i1, -maxSideLength) == H and count1 == 0) then -1
    else if GetValue(H, -i1, -maxSideLength) < H then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
    if GetValue(H, i2, maxSideLength) > H or (GetValue(H, i2, maxSideLength) == H and count2 >= 1) then -1
    else if GetValue(H, i2, maxSideLength) < H then count2 + 1 else count2;

def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
    if GetValue(L, -i3, -maxSideLength) < L or (GetValue(L, -i3, -maxSideLength) == L and count3 == 0) then -1
    else if GetValue(H, -i3, -maxSideLength) > L then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
    if GetValue(L, i4, maxSideLength) < L or (GetValue(L, i4, maxSideLength) == L and count4 >= 1) then -1
    else if GetValue(L, i4, maxSideLength) > L then count4 + 1 else count4;

def UpFractal = if  upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
def DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;



def UpFractalBN = if !IsNaN(UpFractal) then bn else UpFractalBN[1];
def Prev1UpFractalBN = if UpFractalBN != UpFractalBN[1] then UpFractalBN[1] else Prev1UpFractalBN[1];
#def Prev2UpFractalBN = if Prev1UpFractalBN != Prev1UpFractalBN[1] then Prev1UpFractalBN[1] else Prev2UpFractalBN[1];

def DnFractalBN = if !IsNaN(DownFractal) then bn else DnFractalBN[1];
def Prev1DnFractalBN = if DnFractalBN != DnFractalBN[1] then DnFractalBN[1] else Prev1DnFractalBN[1];
#def Prev2DnFractalBN = if Prev1DnFractalBN != Prev1DnFractalBN[1] then Prev1DnFractalBN[1] else Prev2DnFractalBN[1];

def UpH = GetValue(H, bn - UpFractalBN); #if bn == UpFractalBN then H else UpH[1];
def Prev1UpH = GetValue(H,bn-Prev1UpFractalBN); #if bn == Prev1UpFractalBN then H else Prev1UpH[1];
#def Prev2UpH = GetValue(H,bn-Prev2UpFractalBN); #if bn == Prev2UpFractalBN then L else Prev2UpH[1];

def DnL = GetValue(L, bn - DnFractalBN); #if bn == DnFractalBN then L else DnL[1];
def Prev1DnL = GetValue(L,bn-Prev1DnFractalBN); #if bn == Prev1DnFractalBN then H else Prev1DnH[1];
#def Prev2DnL = GetValue(L,bn-Prev2DnFractalBN); #if bn == Prev2DnFractalBN then L else Prev2DnH[1];


plot upfractalwedge = if upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
plot DownFractalwedge = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;

UpFractalwedge.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
UpFractalwedge.SetDefaultColor(GetColor(3));
UpFractalwedge.SetLineWeight(5);
DownFractalwedge.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
DownFractalwedge.SetDefaultColor(GetColor(4));
DownFractalwedge.SetLineWeight(5);

#def Prev2NearTop = C > Prev2UpH and C > Prev1UpH and C > UpH;
#def Prev2NearBottom = C < Prev2DnL and C < Prev1DnL and C < DnL;
#def Prev1NearTop = C > Prev1UpH and C > UpH;
#def Prev1NearBottom = C < Prev1DnL and C < DnL;
def NearTop = C > UpH;
def NearBottom = C < DnL;
def ReversaloffTop = NearTop and (Shooter or inside);
def ReversaloffBottom = NearBottom and (Hammer or inside);

def oldestbn = if UpFractalBN > DnFractalBN then UpFractalBN else DnFractalBN;
def BFdiff = if bn < Highestall(oldestbn) then double.nan else (UpH-DnL)/2 + dnL;

plot SSS = bfdiff;
sss.SetStyle(Curve.MEDIUM_DASH);
sss.SetDefaultColor(Color.gray);
sss.SetLineWeight(1);

input showcloud = yes;
input showbubbles = yes;
AddCloud(if showcloud and bn >= Highestall( oldestbn) then UpH else Double.NaN, DnL, createcolor(43,45,48  ), createcolor(50, 90, 59 ));
#AddCloud(if showcloud then UpH else Double.NaN, UpH, createcolor(110, 69, 60    ), createcolor(110, 69, 60    ));
addchartbubble(if showbubbles  and  bn == Highestall(bn-5) then 1 else 0 ,BFdiff,"M SSS", color.yellow);
#Alert(triggerlong or triggershort, getunderlyingsymbol() + " IS GOING OUTSIDE!!", Alert.TICK, Sound.Ding);
#plot triggerlong= L< L[1] and H < H[1] and C >= fiftypct;
#plot triggershort= H> H[1] and L > L[1] and C <= fiftypct;


def HRise = Prev1UpH - UpH;
def HRun = UpFractalBN - Prev1UpFractalBN;
def HIndeterminateSlope = if HRun ==0 then yes else no;
#def HSlope = if !HIndeterminateSlope then HRise / HRun else double.nan;
def HSlope =HRise / HRun ;
#def HLine = if bn == 1 then double.nan else if bn == Prev1UpFractalBN then Prev1UpH else HLine[1] - HSlope;
def HLine = if bn == 1 then close else if bn == Prev1UpFractalBN then Prev1UpH else HLine[1]-Hslope;

Plot HDrawline = HLine;
HDrawLine.setpaintingstrategy(paintingStrategy.line);
HDrawLine.SetLineWeight(5);

def Rise = Prev1DnL - DnL;
def Run = DnFractalBN - Prev1DnFractalBN;
def IndeterminateSlope = if Run ==0 then yes else no;
def Slope = if !IndeterminateSlope then Rise / Run else double.nan;
#def Line = if bn == 1 then double.nan else if bn == Prev1DnFractalBN then Prev1DnL else Line[1] - Slope;
def Line = if bn == 1 then close else if bn == Prev1DnFractalBN then Prev1DnL else Line[1] - slope;

Plot Drawline = Line;
DrawLine.setpaintingstrategy(paintingStrategy.line);
DrawLine.SetLineWeight(5);





#addchartbubble(1, low*0.996,
#+ bn + "\n"
#+UpFractalBN + "\n"
#+UpH + "\n"
#+Prev1UpFractalBN + "\n"
#+Prev1UpH + "\n"

#+HIndeterminateSlope+ "\n"
#+HDrawline+ "\n"
#+HLine+ "\n"
#+HSlope + "\n"
#line
#, color.yellow, no);

#
 
@greco26
8oVZfwS.png

Code:
def H = high ;
def L = low ;
def O = open ;
def C = close ;

# Calculate the length of the candle's wicks
def UpperWick = H - Max(O, C);
def LowerWick = Min(O, C) - L;

# Calculate the length of the candle's body
def CandleBody = BodyHeight();
def range = H - L;
def cond = range == Highest(range, 5);

# Compare the wicks to the body to ensure that one wick is 1.25x longer than the body
# also compare the other wick to ensure that it is a "small" wick
def Hammer = H < H[1] and (LowerWick / CandleBody >= 1.25) and (UpperWick / CandleBody <= 1);
def Shooter = L > L[1] and (UpperWick / CandleBody >= 1.25) and (LowerWick / CandleBody <= 1);
def inside = H < H[1] and L > L[1];
def twodn =  H < H[1];
def twoup =  L > L[1];


def sequenceCount = 2;
def bn = BarNumber();

def maxSideLength = sequenceCount + 10;
def upRightSide = fold i1 = 1 to maxSideLength + 1 with count1 while count1 != sequenceCount and count1 != -1 do
    if GetValue(H, -i1, -maxSideLength) > H or (GetValue(H, -i1, -maxSideLength) == H and count1 == 0) then -1
    else if GetValue(H, -i1, -maxSideLength) < H then count1 + 1 else count1;
def upLeftSide = fold i2 = 1 to maxSideLength + 1 with count2 while count2 != sequenceCount and count2 != -1 do
    if GetValue(H, i2, maxSideLength) > H or (GetValue(H, i2, maxSideLength) == H and count2 >= 1) then -1
    else if GetValue(H, i2, maxSideLength) < H then count2 + 1 else count2;

def downRightSide = fold i3 = 1 to maxSideLength + 1 with count3 while count3 != sequenceCount and count3 != -1 do
    if GetValue(L, -i3, -maxSideLength) < L or (GetValue(L, -i3, -maxSideLength) == L and count3 == 0) then -1
    else if GetValue(H, -i3, -maxSideLength) > L then count3 + 1 else count3;
def downLeftSide = fold i4 = 1 to maxSideLength + 1 with count4 while count4 != sequenceCount and count4 != -1 do
    if GetValue(L, i4, maxSideLength) < L or (GetValue(L, i4, maxSideLength) == L and count4 >= 1) then -1
    else if GetValue(L, i4, maxSideLength) > L then count4 + 1 else count4;

def UpFractal = if  upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
def DownFractal = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;



def UpFractalBN = if !IsNaN(UpFractal) then bn else UpFractalBN[1];
def Prev1UpFractalBN = if UpFractalBN != UpFractalBN[1] then UpFractalBN[1] else Prev1UpFractalBN[1];
#def Prev2UpFractalBN = if Prev1UpFractalBN != Prev1UpFractalBN[1] then Prev1UpFractalBN[1] else Prev2UpFractalBN[1];

def DnFractalBN = if !IsNaN(DownFractal) then bn else DnFractalBN[1];
def Prev1DnFractalBN = if DnFractalBN != DnFractalBN[1] then DnFractalBN[1] else Prev1DnFractalBN[1];
#def Prev2DnFractalBN = if Prev1DnFractalBN != Prev1DnFractalBN[1] then Prev1DnFractalBN[1] else Prev2DnFractalBN[1];

def UpH = GetValue(H, bn - UpFractalBN); #if bn == UpFractalBN then H else UpH[1];
def Prev1UpH = GetValue(H, bn - Prev1UpFractalBN); #if bn == Prev1UpFractalBN then H else Prev1UpH[1];
#def Prev2UpH = GetValue(H,bn-Prev2UpFractalBN); #if bn == Prev2UpFractalBN then L else Prev2UpH[1];

def DnL = GetValue(L, bn - DnFractalBN); #if bn == DnFractalBN then L else DnL[1];
def Prev1DnL = GetValue(L, bn - Prev1DnFractalBN); #if bn == Prev1DnFractalBN then H else Prev1DnH[1];
#def Prev2DnL = GetValue(L,bn-Prev2DnFractalBN); #if bn == Prev2DnFractalBN then L else Prev2DnH[1];


plot upfractalwedge = if upRightSide == sequenceCount and upLeftSide == sequenceCount then H else Double.NaN;
plot DownFractalwedge = if downRightSide == sequenceCount and downLeftSide == sequenceCount then L else Double.NaN;

upfractalwedge.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
upfractalwedge.SetDefaultColor(GetColor(3));
upfractalwedge.SetLineWeight(5);
DownFractalwedge.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);
DownFractalwedge.SetDefaultColor(GetColor(4));
DownFractalwedge.SetLineWeight(5);

#def Prev2NearTop = C > Prev2UpH and C > Prev1UpH and C > UpH;
#def Prev2NearBottom = C < Prev2DnL and C < Prev1DnL and C < DnL;
#def Prev1NearTop = C > Prev1UpH and C > UpH;
#def Prev1NearBottom = C < Prev1DnL and C < DnL;
def NearTop = C > UpH;
def NearBottom = C < DnL;
def ReversaloffTop = NearTop and (Shooter or inside);
def ReversaloffBottom = NearBottom and (Hammer or inside);

def oldestbn = if UpFractalBN > DnFractalBN then UpFractalBN else DnFractalBN;
def BFdiff = if bn < HighestAll(oldestbn) then Double.NaN else (UpH - DnL) / 2 + DnL;

plot SSS = BFdiff;
SSS.SetStyle(Curve.MEDIUM_DASH);
SSS.SetDefaultColor(Color.GRAY);
SSS.SetLineWeight(1);

input showcloud = yes;
input showbubbles = yes;
AddCloud(if showcloud and bn >= HighestAll( oldestbn) then UpH else Double.NaN, DnL, CreateColor(43, 45, 48  ), CreateColor(50, 90, 59 ));
#AddCloud(if showcloud then UpH else Double.NaN, UpH, createcolor(110, 69, 60    ), createcolor(110, 69, 60    ));
AddChartBubble(if showbubbles  and  bn == HighestAll(bn - 5) then 1 else 0 , BFdiff, "M SSS", Color.YELLOW);
#Alert(triggerlong or triggershort, getunderlyingsymbol() + " IS GOING OUTSIDE!!", Alert.TICK, Sound.Ding);
#plot triggerlong= L< L[1] and H < H[1] and C >= fiftypct;
#plot triggershort= H> H[1] and L > L[1] and C <= fiftypct;

def FinalBar = fold b=0 to 1000 while !IsNaN(GetValue(close,-b)) do GetValue(BarNumber(),-b);

def HRise = Prev1UpH - UpH;
def HRun = UpFractalBN - Prev1UpFractalBN;
def HIndeterminateSlope = if HRun == 0 then yes else no;
def HSlope = if !HIndeterminateSlope then HRise / HRun else double.nan;
def FinalHSlope = If FinalBar!=0 then fold e=0 to 1000 while !IsNaN(GetValue(close,-e)) do GetValue(HSlope,-e) else FinalHSlope[1];

def HLine = if bn == 1 then 0 else if bn == Prev1UpFractalBN+(bn-Prev1UpFractalBN) and UpFractalBN!=UpFractalBN[1] then UpH else HLine[1] - FinalHSlope;
Plot HDrawline = if bn >= (FinalBar-bn)+prev1UpFractalBN[-1] then Hline else Double.NaN;
HDrawLine.setpaintingstrategy(paintingStrategy.line);
HDrawLine.SetLineWeight(5);

def Rise = Prev1DnL - DnL;
def Run = DnFractalBN - Prev1DnFractalBN;
def IndeterminateSlope = if Run == 0 then yes else no;
def LSlope = if !IndeterminateSlope then Rise / Run else Double.NaN;
def FinalLSlope = If FinalBar!=0 then fold f=0 to 1000 while !IsNaN(GetValue(close,-f)) do GetValue(LSlope,-f) else FinalLSlope[1];

def Line = if  bn == 1 then 0 else if bn == Prev1DnFractalBN+(bn-Prev1DnFractalBN) and DnFractalBN!=DnFractalBN[1] then DnL else Line[1] - FinalLSlope;
Plot LDrawLine = if bn >= (FinalBar-bn)+prev1DnFractalBN[-1] then Line else Double.NaN;
LDrawLine.setpaintingstrategy(paintingStrategy.line);
LDrawLine.SetLineWeight(5);
 
Nice, this worked. Im tweaking things now. I may come back to see if you can help me figure something else out on the study but I'm trying to work through it on my own. Thank you guys!!
 

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
318 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