So, I've been trying to create a price action study based on Order Block logic -- it's based on this video which was sent to me by @OGOptionSlayer :
Basically, what I've been trying to create is "order blocks" or zones using the inefficiency logic talked about in the video, which is essentially:
But when I attempt to aggregate on the 30-minute time frame, all of the blocks just stack on top of each other:
Am I misusing BarNumber() here to try and and carry these values forward, or can it simply not be used on aggregate time frames? The first block seems to map just fine but all of the values from previous blocks that I want to hold no longer work the way I want. And if that is the case, are there any workarounds that would do something similar? I appreciate any input on this, there's still plenty that I don't know with TOS coding yet. Here is the code below if anyone wants to review it, thanks in advance for any help: http://tos.mx/Woc2wsV
Basically, what I've been trying to create is "order blocks" or zones using the inefficiency logic talked about in the video, which is essentially:
- Identify aggressive/larger candles moving in the same direction as trend (I'm using EMAs for this)
- Avoid zones that are directly above/below the next, as those newer zones generally get invalidated
- Add a signal for mitigation of zones and directional signals (haven't done this yet)
- Add MTF support
But when I attempt to aggregate on the 30-minute time frame, all of the blocks just stack on top of each other:
Am I misusing BarNumber() here to try and and carry these values forward, or can it simply not be used on aggregate time frames? The first block seems to map just fine but all of the values from previous blocks that I want to hold no longer work the way I want. And if that is the case, are there any workarounds that would do something similar? I appreciate any input on this, there's still plenty that I don't know with TOS coding yet. Here is the code below if anyone wants to review it, thanks in advance for any help: http://tos.mx/Woc2wsV
Code:
## Order Blocks Test Study
## Coded by Chemmy, direction by OGOptionSlayer
## Logic by Smart Risk: https://youtu.be/f18gazn0nYE
## Code inspired by Halcyonguy at usethinkscript.com
input test_labels = yes;
input usecharttime = no;
input agg = aggregationperiod.hour;
input length = 200;
input extend_mult = 1.5;
input num_zones = 3; # Max of 3
Assert(0 <= num_zones <= 3,"This study shows a max of 3 bullish and bearish zones");
input fast_type = AverageType.EXPONENTIAL;
input slow_type = AverageType.EXPONENTIAL;
input fastlen = 20;
input slowlen = 200;
def bn = BarNumber();
def na = Double.NaN;
def lastbn = HighestAll(if IsNaN(close) then 0 else bn);
def lastcls = if lastbn then close else lastcls[1];
def h;
def l;
def o;
def c;
def v;
if UseChartTime
then {
l = low;
h = high;
c = close;
o = open;
v = volume;
} else {
l = low(period = agg);
h = high(period = agg);
c = close(period = agg);
o = open(period = agg);
v = volume(period = agg);
}
def fastma = MovingAverage(fast_type, c, fastlen);
def slowma = MovingAverage(slow_type, c, slowlen);
def uptrend = fastma > slowma;
def dntrend = fastma < slowma;
def range = AbsValue(h - l);
def avgsize = Average(range, length);
AddLabel(test_labels, "Avg candle: " + Round(avgsize, 2), Color.YELLOW);
def gapup = l > h[2] and (range >= avgsize * extend_mult) and uptrend and v>=average(v,5);
def gapdn = h < l[2] and (range >= avgsize * extend_mult) and dntrend and v>=average(v,5);
#--------------------------
#valley section
#def Valley = low < Lowest(low[1], len) and low < Lowest(low[-len], len);
def major_valley = gapup and !gapup[1] and !gapup[2] and !gapup[3] and !gapup[4] and !gapup[5];
def major_valley_pr = if major_valley then l else na;
def major_valley_bn = if bn == 1 then 0 else if major_valley then bn else major_valley_bn[1];
def gapdnbar = if bn == 1 then 0 else if gapdn then bn else gapdnbar[1];
#---------------------------
#peak section
def major_peak = gapdn and !gapdn[1] and !gapdn[2] and !gapdn[3] and !gapdn[4] and !gapdn[5];
def major_peak_pr = if major_peak then h else na;
def major_peak_bn = if bn == 1 then 0 else if major_peak then bn else major_peak_bn[1];
def gapupbar = if bn == 1 then 0 else if gapup then bn else gapupbar[1];
# -------------------------
# does a bar have a peak and a valley? ignore them
def pvskip = (gapup and gapdn);
def major_peak2 = if pvskip then 0 else major_peak;
def major_valley2 = if pvskip then 0 else major_valley;
# prev signal
def major_prevpv = if bn == 1 then 0
else if major_peak2 then 1
else if major_valley2 then -1
else major_prevpv[1];
# -------------------------
# look ahead to next peak, if no valley in between, then ignore this current peak
def major_peak4 = if !major_peak2 then 0
else
(fold i = 1 to 200
with p = 1
while GetValue(major_peak2, -i) == 0 and !IsNaN(GetValue(c, -i))
# when next peak occurs, does the prev bar show a peak signal? if so then 0, ignore current peak
do if (GetValue(major_peak2, -(i + 1)) == 1 and GetValue(major_prevpv, -i) == 1) then 0 else 1);
# add code to fix the last peak being an error
def major_peak5 = if major_peak2 and IsNaN(major_peak4) then 1 else major_peak4;
#addverticalline(0 and peak5, "-", color.cyan);
# look ahead to next valley, if no peak in between, then ignore this current valley
def major_valley4 = if !major_valley2 then 0
else
(fold j = 1 to 200
with q = 1
while GetValue(major_valley2, -j) == 0 and !IsNaN(GetValue(c, -j))
# when next valley occurs, does the prev bar show a valley signal? if so then 0, ignore current valley
do if (GetValue(major_valley2, -(j + 1)) == 1 and GetValue(major_prevpv, -j) == -1) then 0 else 1);
# add code to fix the last valley being an error
def major_valley5 = if major_valley2 and IsNaN(major_valley4) then 1 else major_valley4;
#addverticalline(0 and valley5, "-", color.cyan);
#---------------
# valley-peak-valley
# look for next valley. is it higher than current valley?
# then draw a horizontal line from next peak
def major_v2ab = if major_valley5 then
(fold k1 = 1 to 100
with p1
while !IsNaN(GetValue(c, -k1))
do (if p1 != 0 then p1
else if GetValue(major_valley5, -k1) and GetValue(l, -k1) > l then (bn + k1)
else if GetValue(major_valley5, -k1) and GetValue(l, -k1) <= l then -1
else p1))
else major_v2ab[1];
# -------------------------
def major_valley2line = if major_v2ab > 0 and major_valley5 then h
else major_valley2line[1];
plot major_zv = major_valley2line;
major_zv.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
major_zv.SetDefaultColor(Color.GREEN);
major_zv.HideBubble();
#def major_valleyline_bn = if major_v2ab > 0 and major_valley5 then bn else major_valleyline_bn[1];
def major_valleyline_bn = if major_valley5 then bn else major_valleyline_bn[1];
def falseup_bn = HighestAll(if major_valley5 then bn else 0);
def major_valley2line2 = if major_v2ab > 0 and major_valley5 and bn < major_valleyline_bn then h
else major_valley2line2[1];
plot major_zv2 = major_valley2line2;
major_zv2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
major_zv2.SetDefaultColor(Color.GREEN);
major_zv2.HideBubble();
#def lowPointTwoclose = if bn == falseup_bn2 then median(c, false_len+1) else lowPointTwoclose[1];
def falseup_low = if bn == falseup_bn then low[2] else falseup_low[1];
def falseup_up = if bn == falseup_bn then Max(close[2], open[2]) else falseup_up[1];
plot up_low = falseup_low;
up_low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
up_low.Hide();
plot up_high = falseup_up;
up_high.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
up_high.Hide();
def falseup_bn2 = HighestAll(if major_valley5 and bn < falseup_bn then bn else 0);
#def lowPointTwoclose = if bn == falseup_bn2 then median(c, false_len+1) else lowPointTwoclose[1];
def falseup_low2 = if bn == falseup_bn2 then low[2] else falseup_low2[1];
def falseup_up2 = if bn == falseup_bn2 then Max(close[2], open[2]) else falseup_up2[1];
plot up_low2 = falseup_low2;
up_low2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
up_low2.Hide();
plot up_high2 = falseup_up2;
up_high2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
up_high2.Hide();
def falseup_bn3 = HighestAll(if major_valley5 and bn < falseup_bn2 then bn else 0);
#def lowPointTwoclose = if bn == falseup_bn2 then median(c, false_len+1) else lowPointTwoclose[1];
def falseup_low3 = if bn == falseup_bn3 then low[2] else falseup_low3[1];
def falseup_up3 = if bn == falseup_bn3 then Max(close[2], open[2]) else falseup_up3[1];
plot up_low3 = falseup_low3;
up_low3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
up_low3.Hide();
plot up_high3 = falseup_up3;
up_high3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
up_high3.Hide();
#------------------------------
# peak-valley-peak
# look for next peak. is it lower than current peak?
# then draw a horizontal line from next valley
def major_p2ab = if major_peak5 then
(fold k2 = 1 to 100
with p2
while !IsNaN(GetValue(c, -k2))
do (if p2 != 0 then p2
else if GetValue(major_peak5, -k2) and GetValue(h, -k2) < h then (bn + k2)
else if GetValue(major_peak5, -k2) and GetValue(h, -k2) >= h then -1
else p2))
else major_p2ab[1];
# -------------------------
def major_peak2line = if major_p2ab > 0 and major_peak5 then l
else major_peak2line[1];
plot major_zp = major_peak2line;
major_zp.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
major_zp.SetDefaultColor(Color.RED);
major_zp.HideBubble();
#def major_peakline_bn = if major_p2ab > 0 and major_peak5 then bn else major_peakline_bn[1];
def major_peakline_bn = if major_peak5 then bn else major_peakline_bn[1];
def major_peak2line2 = if major_peak5 and bn<major_peakline_bn then l
else major_peak2line2[1];
plot major_zp2 = major_peak2line2;
major_zp2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
major_zp2.SetDefaultColor(Color.RED);
major_zp2.HideBubble();
def falsedn_bn = HighestAll(if major_peak5 then bn else 0);
def falsedn_low = if bn == falsedn_bn then high[2] else falsedn_low[1];
def falsedn_up = if bn == falsedn_bn then min(close[2], open[2]) else falsedn_up[1];
plot dn_low = falsedn_low;
dn_low.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
dn_low.Hide();
plot dn_high = falsedn_up;
dn_high.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
dn_high.Hide();
def falsedn_bn2 = HighestAll(if major_peak5 and bn < falsedn_bn then bn else 0);
#def lowPointTwoclose = if bn == falseup_bn2 then median(c, false_len+1) else lowPointTwoclose[1];
def falsedn_low2 = if bn == falsedn_bn2 then high[2] else falsedn_low2[1];
def falsedn_up2 = if bn == falsedn_bn2 then min(close[2], open[2]) else falsedn_up2[1];
plot dn_low2 = falsedn_low2;
dn_low2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
dn_low2.Hide();
plot dn_high2 = falsedn_up2;
dn_high2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
dn_high2.Hide();
def falsedn_bn3 = HighestAll(if major_peak5 and bn < falsedn_bn2 then bn else 0);
#def lowPointTwoclose = if bn == falseup_bn2 then median(c, false_len+1) else lowPointTwoclose[1];
def falsedn_low3 = if bn == falsedn_bn3 then high[2] else falsedn_low3[1];
def falsedn_up3 = if bn == falsedn_bn3 then min(close[2], open[2]) else falsedn_up3[1];
plot dn_low3 = falsedn_low3;
dn_low3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
dn_low3.Hide();
plot dn_high3 = falsedn_up3;
dn_high3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
dn_high3.Hide();
#------------------------------
input majorline_sr = yes;
AddCloud(if majorline_sr and num_zones>=1 then up_low else na, up_high, Color.GREEN, Color.GREEN);
AddCloud(if majorline_sr and num_zones>=2 then up_low2 else na, up_high2, Color.yellow, Color.yellow);
AddCloud(if majorline_sr and num_zones>=3 then up_low3 else na, up_high3, Color.light_green, Color.light_green);
AddCloud(if majorline_sr and num_zones>=1 then dn_low else na, dn_high, Color.RED, Color.RED);
AddCloud(if majorline_sr and num_zones>=2 then dn_low2 else na, dn_high2, Color.light_red, Color.light_red);
AddCloud(if majorline_sr and num_zones>=3 then dn_low3 else na, dn_high3, Color.pink, Color.pink);
#input add_cloud = yes;
#AddCloud(if add_cloud then bulllow else Double.NaN, bullclose, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
#AddCloud(if add_cloud then bearclose else Double.NaN, bearhigh, Color.LIGHT_RED, Color.LIGHT_RED);
#def crossdn = (high[1] crosses above rtop and high[1] < rBot and close < rtop);
#def crossup = (low[1] crosses below stop and low[1] > sbot and close > stop);
#def crossdn = low crosses above up_high2;
#def crossup =low crosses above up_high;
## Signals and Coloring
#def crossupsig = if crossup then 1 else 0;
#def crossdnsig = if crossdn then 1 else 0;
#def holdup = if bn == major_valleyline_bn then 0
# else if holdup[1] == 1 then holdup[1]
# else if crossupsig then 1
# else holdup[1];
#def holddn = if bn == major_peakline_bn then 0
# else if holddn[1] == 1 then holddn[1]
# else if crossdnsig then 1
# else holddn[1];
#plot up = crossup and holdup!=holdup[1];
#plot dn = crossdn and holddn!=holddn[1];
addlabel(test_labels, "peak: " + major_peakline_bn + "valley: " + major_valleyline_bn, color.green);
#plot up = major_zp2;
#plot dn = major_zv2;
#up.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#dn.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);