# Average Price Calculation

Just curious if anyone else has seen this. I wanted to use the average price for my BuyPrice. I tried this formula first but it wouldn't calculate properly:

``````AveragePrice=(Open + High + Low + Close)/4;
BuyPrice = SellPrice = ShortPrice = CoverPrice = AveragePrice;
``````

Then I just made a small change and put Open last in my calculation and then the calculation would come out correctly:

``````AveragePrice=(High + Low + Close + Open)/4;//doesn't work right if you put open first in equation
BuyPrice = SellPrice = ShortPrice = CoverPrice = AveragePrice;
``````

Does anyone have any idea why that might be? It shouldn't make any difference what order you put the four prices in the calculation but it does.

Hi @Marcel. I have used that code in my templates for years and never had a problem with it. Send me email directly if you want me to help you investigate the issue.

2 Likes

@Marcel, very strange. I do not see the issue.

``````AveragePrice1 = ( Open + High + Low + Close ) / 4;
_TRACE( "Average 1 = " + LastValue(AveragePrice1) );
AveragePrice2 = ( High + Low + Close + Open ) / 4; //doesn't work right if you put open first in equation
_TRACE( "Average 2 = " + LastValue(AveragePrice2) );
AveragePrice1 = ( Open + High + Low + Close ) / 4;
_TRACE( "Average 1 = " + Ref(AveragePrice1, -1) );
AveragePrice2 = ( High + Low + Close + Open ) / 4; //doesn't work right if you put open first in equation
_TRACE( "Average 2 = " + Ref(AveragePrice2, -1) );
AveragePrice1 = ( Open + High + Low + Close ) / 4;
_TRACE( "Average 1 = " + Ref(AveragePrice1, -2) );
AveragePrice2 = ( High + Low + Close + Open ) / 4; //doesn't work right if you put open first in equation
_TRACE( "Average 2 = " + Ref(AveragePrice2, -2) );
`````` (tested both 6.10 32-bit - 6.28.0 64-bit - I checked also )

As a side note, in any AmiBroker formulas it is also possible to use "Avg" (similar to Close, Open, etc.- but there is no abbreviation) to represent an average price array.
It is calculated as (High+Low+Close)/3 - corresponding to the so called "typical price" as defined in other trading applications.

``````Plot(Avg, "Average", colorYellow);
``````
2 Likes

I just ran a scan on all days for all major US markets using Norgate data with version 6.27 x64.

All calculations agree within 2 ulps. Many days are within 1 ulp with most days working out to the same answer. It seems to be working properly here.

1 Like