EA Handle Problem

 

Hi. So I coded a basic EA and want to add an MA filter to it to see if it performs better. I'm running into an issue, though. When it tries to copy the values from the indicator to the array, it fails, with error code 4807 for invalid handle. The weird thing is, I check to make sure the handle isn't invalid right after it's initialized, and have the OnInit function return INIT_FAILED if it's invalid. So, the handle is for some reason becoming invalid between the OnInit and OnTick functions. Anyway, here's the relevant code if anyone wants to see it:

int OnInit()
{
   m_Trade.SetExpertMagicNumber(OrderMagic);
   return(INIT_SUCCEEDED);
   maHandle = iMA(_Symbol,PERIOD_CURRENT,MALookback,0,MODE_EMA,PRICE_CLOSE);
   if(maHandle == INVALID_HANDLE)
   {
    Print("Error creating MA indicator handle");
    return (INIT_FAILED);
   }
}

and in the OnTick function:

if(CopyBuffer(maHandle,0,0,2,maValues) == -1)
      Print("Buffer copy failed, error code ", GetLastError());

Any help would be appreciated.

Documentation on MQL5: Other Constants / Constants, Enumerations and Structures
Documentation on MQL5: Other Constants / Constants, Enumerations and Structures
  • www.mql5.com
The CLR_NONE constant is used to outline the absence of color, it means that the graphical object or graphical series of an indicator will not be...
 
Raseri:

Hi. So I coded a basic EA and want to add an MA filter to it to see if it performs better. I'm running into an issue, though. When it tries to copy the values from the indicator to the array, it fails, with error code 4807 for invalid handle. The weird thing is, I check to make sure the handle isn't invalid right after it's initialized, and have the OnInit function return INIT_FAILED if it's invalid. So, the handle is for some reason becoming invalid between the OnInit and OnTick functions. Anyway, here's the relevant code if anyone wants to see it:

Try returning INIT_SUCCEEDED below your handle definition.
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Ryan L Johnson #:
Try returning INIT_SUCCEEDED below your handle definition.
*facepalm* Of course. I forgot to move it when I added in the MA. Thanks!
 
Raseri #:
*facepalm* Of course. I forgot to move it when I added in the MA. Thanks!

There's an emoji for that: 🤦‍♂️ (I've used it before.)

And you're welcome.