Simple Query....should be. But no luck with TOS. I want to AVERAGE the past 10 bear candles. This would be a label on a graph. I've come up with this
It Never seems to calculate correctly though. Any help is appreciated!!
here is another way to find the averages
hello and welcome
there are several things wrong with your post
1. your words say bear, but your formulas say bull.
2. i assume you want to average the close of the bull candles? you didn't specify.
3. your variable , bull , is equal to a boolean value, true or false, a series of 1's and 0's. using bull in the average formula, bulla , will create an average of a bunch of 1's and 0's , some fractional number.
4. minor thing, although you used parameter names to set the parameters in the average function, they are in the wrong order. it is good practice to put parameters in the correct order in functions, (then you dont need to use the names), average( data , length )
5. minor thing, no color defined in label.
-----------------------
not all bars are bull. there isn't going to be 10 bull candles in a row. looking back 10 bars won't find 10 bull candles.
you need a way to look back at an unknown quantity of bars and find 10 bull candles, add up
-----------------------
this will,
. ask for a quantity of bull bars and a quantity of bear bars, then find the average of each.
. 2 labels are drawn, for the bull average and bear average.
. can show wedges (tiny arrows) on bull bars and bear bars.
this study uses a reverse counter to determine when a group of bars, are desired.
increments a counter on each bull candle, and a separate counter for bears.
finds the highest counter value of both.
calculates a reverse counter for each. it counts down to 1, at the last desired candle.
the counter values are compared to the desired quantity, to know when a candle is to be summed for the average.
if you need help visualizing the counters, at the end is a test bubble. change show_bull_counters = no , to yes to have it draw bubbles with bull counter values.
Ruby:
def bn = barnumber();
input bull_qty = 10;
input bear_qty = 10;
def bull = close > open;
def bear = close < open;
def bullcnt =
if bn == 1 then 0
else if bull then bullcnt[1] + 1
else bullcnt[1];
def bullcntlast = highestall(bullcnt);
def bullrevcnt = bullcntlast - bullcnt + 1;
def bullsum =
if bn == 1 then 0
else if (bullrevcnt <= bull_qty and bull) then bullsum[1] + close
else bullsum[1];
def bullavg = round(bullsum / bull_qty, 2);
def bearcnt =
if bn == 1 then 0
else if bear then bearcnt[1] + 1
else bearcnt[1];
def bearcntlast = highestall(bearcnt);
def bearrevcnt = bearcntlast - bearcnt + 1;
def bearsum =
if bn == 1 then 0
else if (bearrevcnt <= bear_qty and bear) then bearsum[1] + close
else bearsum[1];
def bearavg = round(bearsum / bear_qty, 2);
addlabel(1, "averages of past " + bull_qty + " bull candles: " + bullavg, color.yellow);
addlabel(1, "averages of past " + bear_qty + " bear candles: " + bearavg, color.yellow);
input show_wedge_on_bull_candles = yes;
plot bullw = if (show_wedge_on_bull_candles and bullrevcnt <= bull_qty and bull) then 1 else 0;
bullw.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_up);
bullw.SetDefaultColor(Color.green);
bullw.setlineweight(1);
bullw.hidebubble();
input show_wedge_on_bear_candles = yes;
plot bearw = if (show_wedge_on_bear_candles and bearrevcnt <= bear_qty and bear) then 1 else 0;
bearw.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_down);
bearw.SetDefaultColor(Color.red);
bearw.setlineweight(1);
bearw.hidebubble();
input show_debug_counters = no;
Addchartbubble(show_debug_counters and bullrevcnt <= bull_qty and bull, high*1.005,
bullcnt + " cnt\n" +
bullrevcnt + " rev\n" +
bullcntlast + " last\n" +
close + " cls"
, color.green, yes);
Addchartbubble(show_debug_counters and bearrevcnt <= bear_qty and bear, low*0.995,
bearcnt + " cnt\n" +
bearrevcnt + " rev\n" +
bearcntlast + " last\n" +
close + " cls"
, color.red, no);