# Creating a moving average

Hi all,
I am having difficulty with constructing this moving average.

1 If today’s close > today’s open, the volume is positive.
2 If today’s close < today’s open, volume is negative.
3 If today’s close = today’s open, volume is 0.
4 You then take the seven-day simple moving average of
the results.

I started off writing the if statements and came to a wall when these error messages came up saying I need to reference the array values. Do I need to use a do loop and access the array values individually make the comparison and make a new array before applying the moving average function ?
if( C > O )
{
v1 = V;
}
else {if (C < O) { v1= -V;} else {v1 = 0;}
};

``````/// @link https://forum.amibroker.com/t/creating-a-moving-average/19136

DC = C - O;
arr = sign(DC) * V;
ans = MA(arr, 7);

Filter = 1;
AddColumn(DC, "Daily Change (Points, C-O)", 1.2);
``````

Look also here: https://www.amibroker.com/guide/keyword/if.html

4 Likes

On a side note have you looked at OBV indicator, “ On Balance Volume”. Pretty sure this is exactly what your calculating.

Simply put a 7 MA on it and good to go.

2 Likes

@David1, you may want to look at the Immediate If statement "IIF" instead of the "IF" program flow control.

1 Like

@Metamega is not correct. My understanding of the OBV is that it compares the Close to previous Close and @david1 is looking to compare the today's Close to the today's Open.
https://www.amibroker.com/guide/t_obv.html

4 Likes

Good catch. Didn’t notice that small difference.

Small difference?

Even if OBV() would use Close vs. Open comparison end result would still not output the same thing and it would still be rather large difference in results if simply putting MA() on OBV(). (OBV applies cummulative sum.)

Long story short... IMO, instead of prematurely claiming something it would be a better idea to validate claim via test before.

3 Likes

Out of curiosity. Do you have a colour code for your chart background. I like that colour and tried to make something similar but it was always either obviously grey or black. Perhaps it’s way it shows on discourse but it’s almost got a black/blue/grey thing going on. Easy on the eyes.

I want to thank everyone for spending their time helping me.
I did it with a nested IIF
v1 = IIf(C > O, V, IIf(C < O, -V, 0));
v2 = MA(v1,7);

No, it is not because discourse. It uses a soft color gradient from top to bottom direction (see Tools-Preferences-Axes/Grids-Alternate background fill). It's kind of greenish. So being similar to chalkboard colors.

Anyway see screen color pickers to find out colors.

1 Like