How to call a protected/private method from another class in MQL

Hi there! I’ve been trying to create my first EA without much success. I’m not an expert in programming and my almost forgotten php programming skills doesn’t help either because I have the feeling that I’m trying to program my EA the way I would program a php so apologies for the mistakes both in the code and in the way I think about this.

What I managed to put together: My EA opens a limit or stop order if there is an active buy or sell order and the magic number equals to the open position’s ticket id.

What I’m struggling to put together: If I close my position, I’d like my EA to cancel the stop/limit order that it has opened. To make this happen, I thought that the EA should check if any of the active orders’ magic number matches a position id in the order history. If it does, then it means that the position has been closed already and the order should be cancelled. If not, then it should do nothing.

There are many things I’m struggling to understand and I can’t find an answer for, even though I’ve been reading the manual like there’s no tomorrow.

  • If I create a class with some public, private/protected functions, then how do I call one of them from another class or from another function that’s not in any class?

  • I may still don’t get how this works and maybe the whole code gets executed automatically and I don’t have to call functions like in php. I have rebuilt my whole EA with classes because the first version that opened the pending orders did not check the history for some reason and therefore did not close the pending order when it would had to. I thought that it may be because I had multiple OrderSelect with for cycles and maybe that was the problem so I thought that I’m going to re-write the EA with classes to separate various functions from each other.

Here is how it starts but needless to say the compiler is complaining but I have no idea how to call a function to start my EA.

int APPositions[];


void start(){

   if(OrdersTotal()>0){
   
      ModifyPosition::Launch();
      CheckIfClosed::CheckHistory();
   
   }

}

Structures, Classes and Interfaces

This should do the trick:

int APPositions[];

ModifyPosition *mp;
CheckIfClosed *cic;

int OnInit()
  {
   mp=new ModifyPosition;
   cic=new CheckIfClosed;
  }

void OnDeinit(const int reason)
  {
   delete mp;
   delete cic;
  }

void start()
  {
   if(OrdersTotal()>0)
     {
      mp.Launch();
      cic.CheckHistory();
     }
  }

It looks like you are using class methods – which in MQL is the equivalent of creating a namespace. Nothing wrong with that, but just know that you will need to define your methods as static and you will not be able to use them with instance data.

class Foo {
public:
   static void class_method() { 
      Print("Hello from Foo's class method");
   }
};

class Bar {
public:
   static void class_method() { 
      Print("Hello from Bar's class method");
   }
};
void OnStart(){
   Foo::class_method();
   Bar::class_method();
}