MQL5 handles and Variables Question

To add comments, please log in or register
towelly03
35
towelly03  

I have spent quite a while trying to get my MQl5 up to scratch I have bought a few books and am stummped over a new feature in MQL5 CopyBuffer. I have tried using it ion my code and keep finding it a nusence. In my expert advisors I have the code inside the tick function where I am I going wrong I have checked and check and cant seem to get any clear explination on the reason my code isn't working can anyone help?

void onTick(){

     double array[];
     int handle = iMA(_Symbol, _Period, 0, -40, MODE_EMA, PRICE_MEDIAN);
     ArraySetAsSeries(array, true);
     CopyBuffer(handle, 0, 0, 1, array);
     Comment(DoubleToString(NormalizeDouble(array[0], 3)));


}

Seng Joo Thio
944
Seng Joo Thio  
towelly03:

I have spent quite a while trying to get my MQl5 up to scratch I have bought a few books and am stummped over a new feature in MQL5 CopyBuffer. I have tried using it ion my code and keep finding it a nusence. In my expert advisors I have the code inside the tick function where I am I going wrong I have checked and check and cant seem to get any clear explination on the reason my code isn't working can anyone help?

Check your parameters - you can't draw moving average with 0 ma_period, and if you want it shifted left by 40 bars, you're not going to get any value from bars 0 to 39.

Also, it's more efficient if you break down this line into two parts and move out of OnTick() (since this need not be executed every tick):

     int handle = iMA(_Symbol, _Period, 0, -40, MODE_EMA, PRICE_MEDIAN);

into

int handle;
int OnInit()
{
     handle = iMA(_Symbol, _Period, 0, -40, MODE_EMA, PRICE_MEDIAN);
}
towelly03
35
towelly03  
What would you do if the shift of a iMA was negative as far as the buffer??
William Roeder
19422
William Roeder  
Don't double post! You already had this thread open.
          General rules and best pratices of the Forum. - General - MQL5 programming forum
Seng Joo Thio
944
Seng Joo Thio  
towelly03:
What would you do if the shift of a iMA was negative as far as the buffer??

For your case, if you stick to -40, then change your CopyBuffer() to:

     CopyBuffer(handle, 0, 40, 1, array);
To add comments, please log in or register