Question with CopyBuffer not working as expected.

 

Hi Guys

Im having my first crack at an EA but am getting some strange results. If someone could have a look and point me in the right direction Id appreciate it.

I am calling the function "GetThreeMAValues" with the following line "GetThreeMAValues(_Symbol, PERIOD_CURRENT);". The three arrays are declared in the Global Scope.

Anyway the issue is when the following print statements are run 

Print("maArray1[1] = " + maArray1[1]);
Print("maArray2[1] = " + maArray2[1]);
Print("maArray3[1] = " + maArray3[1]);

My understanding is that it should be printing out the three MA's on the bar prior to the current one (Index 1). However, the MA's being displayed are 8 bars earlier and I cant work out why.

void GetThreeMAValues( string symbol, ENUM_TIMEFRAMES timeframe )
{
Print("GetThreeMAValues(_Symbol, PERIOD_CURRENT);");
    // Create the MA handles
    int handle1 = iMA(_Symbol, PERIOD_CURRENT, 8,  0, MODE_EMA, PRICE_CLOSE);
    int handle2 = iMA(_Symbol, PERIOD_CURRENT, 13, 0, MODE_EMA, PRICE_CLOSE);
    int handle3 = iMA(_Symbol, PERIOD_CURRENT, 55, 0, MODE_EMA, PRICE_CLOSE);
   
    if(handle1 == INVALID_HANDLE || handle2 == INVALID_HANDLE || handle3 == INVALID_HANDLE)
    {
      Print("Error: Unable to create one or more MA indicator handles");
      return;
    }
    // Copy MA values from the indicator buffers
if(CopyBuffer(handle1, 0, 0, 1000, maArray1) <= 0)
    {
      Print("Error: Unable to copy MA data for handle1");
      return;
    }
    if(CopyBuffer(handle2, 0, 0, 1000, maArray2) <= 0)
    {
      Print("Error: Unable to copy MA data for handle2");
      return;
    }
    if(CopyBuffer(handle3, 0, 0, 1000, maArray3) <= 0)
    {
      Print("Error: Unable to copy MA data for handle3");
      return;
    }
    Print("maArray1[1] = " + maArray1[1]);
    Print("maArray2[1] = " + maArray2[1]);
    Print("maArray3[1] = " + maArray3[1]);
} 
 

If you want that the index 1 array value would return you the previous bar you have to turn your MA arrays to series first, otherwise (by default) it will return you the 2nd oldest bar.

Put it into OnInit() function:

ArraySetAsSeries(maArray1, true);
ArraySetAsSeries(maArray2, true);
ArraySetAsSeries(maArray3, true);


 
Kristian Kafarov #:

If you want that the index 1 array value would return you the previous bar you have to turn your MA arrays to series first, otherwise (by default) it will return you the 2nd oldest bar.

Put it into OnInit() function:

Thanks for the response Kristian, Ive just tried it and it worked. Thank you.

Steve

 
dawsonsg: . If someone could have a look and point me in the right direction Id appreciate it.
  1. Please edit your (original) post and use the CODE button (or Alt+S)! (For large amounts of code, attach it.)
          General rules and best pratices of the Forum. - General - MQL5 programming forum #25 (2019)
              Messages Editor
          Forum rules and recommendations - General - MQL5 programming forum (2023)

  2. Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              MQL5 for Newbies: Guide to Using Technical Indicators in Expert Advisors - MQL5 Articles (2010)
              How to call indicators in MQL5 - MQL5 Articles (2010)

  3. In MT5, you must set the direction.

    To define the indexing direction in the time[], open[], high[], low[], close[], tick_volume[], volume[] and spread[] arrays, call the ArrayGetAsSeries() function. In order not to depend on defaults, call the ArraySetAsSeries() function for the arrays to work with.
              Event Handling / OnCalculate - Reference on algorithmic/automated trading language for MetaTrader 5
 

I've a problem with a custom indicator in MT5 EA.

It's working fine up to the date that starts testing (october 11th 2024).

Attached a screenshot that gives good dots on october 10th. But then???

Anyone that can give me a clue? 

Files:
241014Fout.PNG  26 kb
 
bertpost #:

I've a problem with a custom indicator in MT5 EA.

It's working fine up to the date that starts testing (october 11th 2024).

Attached a screenshot that gives good dots on october 10th. But then???

Anyone that can give me a clue? 

do you really expect an answer?  

no code, no error message, a custom indicator that nobody knows what it should be doing, etc.....

 

I thought somebody recocnizes what's happening. But now attached the source codes.

In the mean time I've seen that not every copybuffer gives results. Sometimes up to 10 times the result is 0.

I hope you can catch me on my errors!!

Thanks.

 
i believe that 0 means that there is no change.
 
Copybuffer has been buggy since the last two versions 4585 and 4620. It can return 0. You have to deal with it
 
Gerard Willia G J B M Dinh Sy #:
Copybuffer has been buggy since the last two versions 4585 and 4620. It can return 0. You have to deal with it
Explain 
 
Gerard Willia G J B M Dinh Sy #: Copybuffer has been buggy since the last two versions 4585 and 4620. It can return 0. You have to deal with it

I have seen no such bug, and I have used both builds, currently using 4620. You must be using the function incorrectly.

Can you please describe the details, preferably with sample code and log output so that the issue can be reproduced by others?