So I am building a study that will act as a scanner for horizontal channels. I have everything programmed up to it detecting whenever there is a high and low within a channel. It plots 1 when the price hits resistance and a 0 when it isn't. There is a separate plot that plots 1 when the price hits support and 0 when it isn't. I am trying to figure out how to go through each plot and add 1 to a separate variable each time the corresponding plot shows 1. So if the support plot hits one 6 times then the support accumulation variable would equal 6. And if the resistance plot hits one 4 times then the support accumulation variable would equal 4.
This is my code so far:
def offset = 0;
input length = 30;
input channelAccuracy = 3;
#Converting the accuracy input into usable data
def accuracy = (channelAccuracy / 2) * 0.01;
#Set Lower Line
def lowersma = SimpleMovingAvg(Lowest(low[1], length), 1, length);
rec lowerline = if IsNaN(lowersma) then lowerline[1] else lowersma[offset];
def lowerpriceline=if isnan(lowersma) then lowerline else double.nan;
#Lower Line Accuracy
def bottomloweraccuracy = lowerpriceline * (1 - accuracy);
def toploweraccuracy = lowerpriceline * (1 + accuracy);
#Set Upper Line
def uppersma = SimpleMovingAvg(Highest(high[1], length), 1, length);
rec upperline = if IsNaN(uppersma) then upperline[1] else uppersma[offset];
def upperpriceline=if isnan(uppersma) then upperline else double.nan;
#Upper Line Accuracy
def bottomupperaccuracy = upperpriceline * (1 - accuracy);
def topupperaccuracy = upperpriceline * (1 + accuracy);
#Is Stock Within Upper Accuracy Enough
plot NextHigh = fold i = 0 to length with price = Double.NaN while IsNaN(price) do if getValue(high, -i) >= bottomupperaccuracy && getValue(high, -i) <= topupperaccuracy then 1 else 0;
NextHigh.setdefaultcolor(color.green);
#Is Stock Within Lower Accuracy Enough
plot NextLow = fold m = 0 to length with price2 = Double.NaN while IsNaN(price2) do if getValue(low, -m) >= bottomloweraccuracy && getValue(low, -m) <= toploweraccuracy then 1 else 0;
NextLow.setdefaultcolor(color.red);
This is my code so far:
def offset = 0;
input length = 30;
input channelAccuracy = 3;
#Converting the accuracy input into usable data
def accuracy = (channelAccuracy / 2) * 0.01;
#Set Lower Line
def lowersma = SimpleMovingAvg(Lowest(low[1], length), 1, length);
rec lowerline = if IsNaN(lowersma) then lowerline[1] else lowersma[offset];
def lowerpriceline=if isnan(lowersma) then lowerline else double.nan;
#Lower Line Accuracy
def bottomloweraccuracy = lowerpriceline * (1 - accuracy);
def toploweraccuracy = lowerpriceline * (1 + accuracy);
#Set Upper Line
def uppersma = SimpleMovingAvg(Highest(high[1], length), 1, length);
rec upperline = if IsNaN(uppersma) then upperline[1] else uppersma[offset];
def upperpriceline=if isnan(uppersma) then upperline else double.nan;
#Upper Line Accuracy
def bottomupperaccuracy = upperpriceline * (1 - accuracy);
def topupperaccuracy = upperpriceline * (1 + accuracy);
#Is Stock Within Upper Accuracy Enough
plot NextHigh = fold i = 0 to length with price = Double.NaN while IsNaN(price) do if getValue(high, -i) >= bottomupperaccuracy && getValue(high, -i) <= topupperaccuracy then 1 else 0;
NextHigh.setdefaultcolor(color.green);
#Is Stock Within Lower Accuracy Enough
plot NextLow = fold m = 0 to length with price2 = Double.NaN while IsNaN(price2) do if getValue(low, -m) >= bottomloweraccuracy && getValue(low, -m) <= toploweraccuracy then 1 else 0;
NextLow.setdefaultcolor(color.red);