Download MetaTrader 5

Obtaining the average high value of previous bars

To add comments, please log in or register
Morteza Khorasani
131
Morteza Khorasani  

Hello

I want to obtain the high price of the previous candles and calculate the average price.

double average1()
{//average1 start
double highfirst[];
CopyHigh(FirstSymbol,timeframe,0,50,highfirst);
sum=0;
for(int k=0;k<=noofbars;k++)
sum=sum+highfirst[k];

double ave=sum/noofbars;
return (ave);


}//average1 end

 but the function returns no value.

whroeder1
17050
whroeder1  
Morteza Khorasani but the function returns no value.
Of course it return a value. Print out your variables, and find out why it doesn't return the value you think it should.
Fernando Carreiro
3951
Fernando Carreiro  
Morteza Khorasani: I want to obtain the high price of the previous candles and calculate the average price but the function returns no value.

WHRoeder is correct, see the documentation on the function CopyHigh().

However why complicate things? Just use the iMA()!

AverageOfHighs = iMA( FirstSymbol, timeframe, noofbars, 0, MODE_SMA, PRICE_HIGH, 0 );

Also, please note that in your code your average calculation is incorrect because you are summing "noofbars + 1" and dividing the sum by "noofbars". The loop should probably be as follows:

for( int k = 0; k < noofbars; k++ ) // using "<" instead of "<="
CopyHigh - Timeseries and Indicators Access - MQL4 Reference
CopyHigh - Timeseries and Indicators Access - MQL4 Reference
  • docs.mql4.com
CopyHigh - Timeseries and Indicators Access - MQL4 Reference
Morteza Khorasani
131
Morteza Khorasani  
whroeder1:
Of course it return a value. Print out your variables, and find out why it doesn't return the value you think it should.

This message: 

2016.12.30 00:10:52.270 DMCS1 GBPJPY,H1: array out of range in 'DMCS1.mq4' (225,18)

Fernando Carreiro
3951
Fernando Carreiro  
Morteza Khorasani: 2016.12.30 00:10:52.270 DMCS1 GBPJPY,H1: array out of range in 'DMCS1.mq4' (225,18)
That is because you are only collecting "50" bars but then testing "noofbars" which is probably more than 50. Also, please take note of the "for" loop using "k < noofbars" instead of the "<=" which adds one more iteration.
Morteza Khorasani
131
Morteza Khorasani  
Fernando Carreiro:
That is because you are only collecting "50" bars but then testing "noofbars" which is probably more than 50. Also, please take note of the "for" loop using "k < noofbars" instead of the "<=" which adds one more iteration.
Thank you dear Fernando. I am editing the code to see the results.
Fernando Carreiro
3951
Fernando Carreiro  
Morteza Khorasani: Thank you dear Fernando. I am editing the code to see the results.
// Untested! Alternatively use the iMA solution (previously posted)
double HighAverage( string strSymbol, int intTimeFrame, int intBars )
{
   double dblHighs[], dblAverage = 0.0;
   int intCount = CopyHigh( strSymbol, intTimeFrame, 0, intBars, dblHighs );
      
   if( intCount > 0 )
   {
      for( int i = 0; i < intCount; i++ ) dblAverage += dblHighs[ i ];
      dblAverage /= double( intCount ); // Explicit typecasting just for good code practice
   }
  
   return( dblAverage );
}
To add comments, please log in or register