# Simple calculation issue (Edit: Moving Average related)

103

The following snipett is meant to label bars as either "LONG" or "SHORT" when thier length is compared against a moving average of the previous 5 bars bodies. The labels work but the calculations are off.

I have got this to work in excel/vba though it is not much good to me there.

double sumSIZE=0;      //moving sum of i bars
double avgSIZE=0;      //moving average size of bars
for(int i=1;i<=avgPERIOD;i++)                    //loop through previous bars for avgPERIOD (5)
sumSIZE += MathAbs(iOpen(Symbol(),0,i)-iClose(Symbol(),0,i));      //sum bars contained within avgPERIOD
avgSIZE=sumSIZE/avgPERIOD;                                         //average of bars in avgPERIOD
if (MathAbs(O0-C0)>avgSIZE*1.3) RegisterPattern(p,PAT_LONG);         //if bar is > avgSIZE*1.3 then label as LONG
if (MathAbs(O0-C0)<avgSIZE*0.5) RegisterPattern(p,PAT_SHORT);        //if bar is < avgSIZE*0.5 then label as SHORT

It should be obvious from this diagram that the bars are being mislabelled. If avgPERIOD period is output to print at each bar the value is being updated. Though I'm just not sure what values it is being updated with
103

And the VBA script for anyone thats interested in it

LR = .Range("B" & Rows.Count).End(xlUp).Row
For i = 2 To LR
If .Range("B" & i).Value > 0 Then Set CellRef = .Range("B" & i) Else Set CellRef = .Range("Z4667")
If Not Application.Intersect(DateRange, CellRef) Is Nothing Then iSect = True Else iSect = False
If iSect = True Then Set avgRange = .Range("J" & (i - 1) & ":" & "J" & (i - 6))
If iSect = True Then CellRef.Offset(0, 9) = WorksheetFunction.Sum(avgRange) / 5
If iSect = True And CellRef.Offset(0, 8) > (CellRef.Offset(0, 9).Value * 1.3) Then CellRef.Offset(0, 9) = "LONG" Else _
If iSect = True And CellRef.Offset(0, 8) < (CellRef.Offset(0, 9).Value * 0.5) Then CellRef.Offset(0, 9) = "SHORT" Else _
If iSect = True Then CellRef.Offset(0, 9) = "VANILLA"

Next i
End With
15735

 SteepCurve: The labels work but the calculations are off. "Doesn't work" is meaningless - just like saying the car doesn't work. Doesn't start, won't go in gear, no electrical, missing the key, flat tires - meaningless. There are no mind readers here.Do you expect an answer where you use variables we have no idea their type, their value, how you are updating them? There are no mind readers here.
103

Apologies, the post has been updated. It was quite late here and had been a long day in the lab.

ResgisterPattern sends the outcome to the labeling function. I have created a similar fragment which labels the bars Bear or Bull (more as an excercise than anything actually useful) and it works as expected. The above script is based off of the Bear/Bull script

103

This question comes down to my inability to write a loop for a moving average in general.

The loop loops. The values are recaulculated at every loop. The loop is stepped by start(). I just have no idea what bar values the loop is actually using.

103

So after adding some print statements to find what time and prices the loop is using, it appears to only be looping through the most recent 5 bars. As all bars in the chart have been labelled I can only guess it is comparing all bars in the history to the average of the most recent 5 bars.

What is missing to make it look back through all bars?

103

I made some simple changes to ATR. The indicator now returns ATR of bar size (open/close only) and returns the value in pair points instead of true chart values.

Excuse me while I go scrub the stupid off myself haha :/

Files:
atroc.mq4 4 kb