
Hi Everyone,
I seek help to fix a code. I am trying to find fresh demand and supply zones for forex trading. I have used some code from community members(and I really already appreciate the help and support) to find candles on the chart going 300 bars back from last closed candle to find legout candles(orange candles) of demand and supply zones. I record candle numbers of the last 3 zones within +/- 2% of the current price. I then try to go from last closed candle to the 1st legout candle(orange).
If this originally a red candle ( open > close) then i check for highs of all candles starting from the current price to see if any high is above the open of this red candle. If this is the case the FOLD loop is exited and we know this is not a fresh zone.
If 1st orange candle is originally a green candle(close>open) then i check for lows of all candles starting from the current price to see if any low is below the open of this green candle. If this is the case the FOLD loop is exited and we know this is not a fresh zone.
I want to put an arrow under/above the legout candle of a fresh zone for the orange candles in the 1st 300 candles from the 1st closed candle.
I get L1 ( 1st orange candle) bar number right but then it seems the cold finds a nan and gives random results for L2 and L3. It also shows "Folding: Integer 'from' is expected. "
Code is attached. Seek help of members to guide me in the right direction to fix this code.
Thanks in advance!

Code:
# // Find a supply/demand zone of high quality
input Sensitivity = 2.25;
def Candle_Range = high - low;
def Body_Range = AbsValue(close - open);
def Bar_Lookback = 100;
def Volatility = ATR(Bar_Lookback);
def Strength = Volatility * Sensitivity;
def Exciting = (Body_Range > Candle_Range * 0.75) and (Candle_Range >= Strength);
def C = Exciting;
#def A = Exciting2 and lunch;
#def B = Exciting2 and lunch[5];
# // Assign color orange to legout of the demand/supply zone
AssignPriceColor(if C then Color.ORANGE else Color.CURRENT);
#def lastBar = if IsNaN(close[-1]) && !IsNaN(close) then BarNumber() else lastBar[1];
#// Go back 300 candles from last bar on chart to find the 1st legout candle(orange candle) of demand/supply zone
def L1 = fold k = 1 to 300 with CValue = 0 while CValue <= 0 do
if AbsValue (getvalue(close,k) - getvalue(open,k)) > ((getvalue(high,k) - getvalue(low,k)) * .75) and ((getvalue(high,k) - getvalue(low,k)) >= ATR(Bar_Lookback) * 2.25) then CValue + k else CValue ;
#// Go back 300 candles from 1st orange candle to next orange candle on chart to find the 2nd orange candle
def L2 = fold j = L1+1 to 300 with CValue2 = 0 while Cvalue2 <=0 do
if AbsValue (getvalue(close,j) - getvalue(open,j)) > ((getvalue(high,j) - getvalue(low,j)) * .75) and ((getvalue(high,j) - getvalue(low,j)) >= ATR(Bar_Lookback) * 2.25) then CValue2 + j else CValue2 ;
#// Go back 300 candles from 2nd orange candle to next orange candle on chart to find the 3rd orange candle
def L3 = fold m = L2+1 to 300 with CValue3 = 0 while CValue3 <= 0 do
if AbsValue (getvalue(close,m) - getvalue(open,m)) > ((getvalue(high,m) - getvalue(low,m)) * .75) and ((getvalue(high,m) - getvalue(low,m)) >= ATR(Bar_Lookback) * 2.25) then CValue3 + m else CValue3 ;
#// check if 1st orange candle is within 2% of current price and is red candle (open> close)
def L1_Red_Candle_Check = if GetValue(close, L1) < GetValue(close, 1) * 1.02 and GetValue(close, L1) > GetValue(close, 1) * 0.8 and GetValue(open, L1) >= GetValue(close, L1) then 1 else 0;
#// check if highs of all candles from current price to 1st orange candle are below the close of the 1st orange candle. If yes come out of the loop. This shows the supply zone is not fresh and price has cut it at least once
def L1_Fresh_Above = fold n = 1 to 200 with Highvalue = 0 while L1_Red_Candle_Check and
Highvalue<=0 do
if (n < L1 and GetValue(high, n) > GetValue(open, L1)) then Highvalue + n else Highvalue;
#// check if 1st orange candle is within 2% of current price and is green candle (close > open)
def L1_Green_Candle_Check = if GetValue(close, L1) < GetValue(close, 1) * 1.02 and GetValue(close, L1) > GetValue(close, 1) * 0.8 and GetValue(open, L1) < GetValue(close, L1) then 1 else 0;
#// check if lows of all candles from current price to 1st orange candle are above the open price of the 1st orange candle. If yes come out of the loop. This shows the demand zone is not fresh and price has cut it at least once
def L1_Fresh_Below = fold s = 1 to 200 with Lowvalue = 0 while L1_Green_Candle_Check and Lowvalue<=0 do
if (s < L1 and GetValue(low, s) < GetValue(open, L1)) then Lowvalue + s else Lowvalue;
def L2_Red_Candle_Check = if GetValue(close, L2) < GetValue(close, 1) * 1.02 and GetValue(close, L2) > GetValue(close, 1) * 0.8 and GetValue(open, L2) >= GetValue(close, L2) then 1 else 0;
def L2_Fresh_Above = fold t = 1 to 200 with Highvalue2 = 0 while L2_Red_Candle_Check and Highvalue2<=0 do
if (t < L2 and GetValue(high, t) > GetValue(open, L2)) then Highvalue2 + t else Highvalue2;
def L2_Green_Candle_Check = if GetValue(close, L2) < GetValue(close, 1) * 1.02 and GetValue(close, L2) > GetValue(close, 1)* 0.8 and GetValue(open, L2) < GetValue(close, L2) then 1 else 0;
def L2_Fresh_Below = fold u = 1 to 200 with Lowvalue2 = 0 while L2_Green_Candle_Check and Lowvalue2<=0 do
if (u < L2 and GetValue(low, u) < GetValue(open, L2)) then Lowvalue2 + u else Lowvalue2;
#def L1_Fresh_Above = fold n = 1 to 200 with Highvalue = 0 while n < L1 do
#if (getvalue(close, L1) < close[1]*1.02 and getvalue(close, L1) > close[1]*.08 and high[n] #> getvalue(open, L1)) then HighValue + n else HighValue;
AddLabel(1, " L1 " + L1 + " L2 " + L2 + " L3 " + L3 + " L1_Fresh_Above " + L1_Fresh_Above + " L1_Fresh_Below " + L1_Fresh_Below + " L2_Fresh_Above " + L2_Fresh_Above + " L2_Fresh_Below " + L2_Fresh_Below);
Last edited by a moderator: