Search code examples
structcompiler-errorsmql5

How to add a new item to the builtin struct in MQL5?


I've the following sample MQL5 code (to demonstrate the issue):

// PositionSelectByTicket is missing in older MQL5 builds.
#ifndef PositionSelectByTicket
#define PositionSelectByTicket(ticket) OrderSelect(ticket)
#endif 

void CheckOrder(const MqlTradeRequest &Request) {

  if (Request.action == TRADE_ACTION_SLTP) {
    ulong pos = PositionSelectByTicket(Request.position); // error 130: 'position' - struct member undefined
  }

}

void OnStart() { }

And it compiles fine on the latest build of Metaeditor 5 (build 1495), however in build 1162 (using older mql compiler) I've got the following errors:

$ wine mql /s /mql5 MQL_Test.mq5
MQL4/MQL5 Compiler build 1162 (02 Jul 2015)
MQL_Test.mq5 : information: Checking 'MQL_Test.mq5'
MQL_Test.mq5(9,17) : error 130: 'position' - struct member undefined
MQL_Test.mq5(9,17) : warning 60: possible use of uninitialized variable ''
 : information: Result 1 error(s), 1 warning(s)
Time: 11 ms

I'd like to improve the code to be backward compatible and I already did add define for PositionSelectByTicket as above to avoid undefined function.

Although how do I fix the errors above to work on both compilers?

In the documentation page of MqlTradeRequest struct the position struct item exists, so most likely it was added in the recent builds.

So, is there any way to add the missing variable item into the existing builtin struct, so the older compiler will pass the syntax checking?


Solution

  • Do you know that PositionBySelect() is a new feature, available since Build 1325 ( a new hedge mode )?

    new MetaTrader 5 Terminal release notes

    12: Added PositionSelectByTicket function — select an open position for further work by a specified ticket.

    bool  PositionSelectByTicket(ulong ticket );    // position ticket