can mql5 EA coded with no OOP features?

MQL5 can be written with just global functions, ie without any OO constructs. But the advantage of OO encapsulation is that you can very easily use proven classes written by someone else - take a look at the CTrade class in include\trade\trade.mqh to use in your EA

CTrade is what is often termed a "wrapper", in order words it is a simple packaging of like functions into the one class, with a few checks. The documentation at the top of each method is sufficient to work out how the class is used.

To do the actions that you suggest using CTrade, I would write it like this.

CTrade trade;
MqlTick CurrentTick;
SymbolInfoTick(_Symbol,CurrentTick);

// open new position
double sl,tp; // set these to appropriate distance from CurrentTick.ask
if (!trade.PositionOpen(_Symbol,ORDER_TYPE_BUY,0.1,CurrentTick.ask,sl,tp,"comment"))
{
Print("Problem with CTrade::PositionOpen");
return;
}

// change sl and tp to new values
// sl=??????;
// tp=??????;
if (!trade.PositionModify(_Symbol,sl,tp))
{
Print("Problem with CTrade::PositionModify");
return;
}

No comments:

Post a Comment