Trying to count the number of bars since the last time the 10 period avg crossed over the 20 period.
Sorry, tried to correct the title of this post, but not able?
What I have so far. Not working as expected. Plot value should be 0 while MovAvgFast < MovAvgSlow, but then grow by 1 for each bar since the last bar where MovAvgFast became > MovAvgSlow.
Sorry, tried to correct the title of this post, but not able?
What I have so far. Not working as expected. Plot value should be 0 while MovAvgFast < MovAvgSlow, but then grow by 1 for each bar since the last bar where MovAvgFast became > MovAvgSlow.
Code:
input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input lengthFast = 10;
input lengthSlow = 20;
input averageType = AverageType.SIMPLE;
def MovAvgFast = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod),lengthFast);
def MovAvgSlow = MovingAverage(averageType, Fundamental(price, period = aggregationPeriod),lengthSlow);
def Crossover = if (MovAvgFast > MovAvgSlow and MovAvgFast[1] < MovAvgSlow[1]) then 1 else 0;
# count number of bars since last time fast has crossed slow
def BarsSinceCrossUp = if (MovAvgFast > MovAvgSlow) then BarsSinceCrossUp[1] + Crossover else 0;
plot ShowBars = BarsSinceCrossUp;
Last edited: