Features of the mql5 language, subtleties and tricks - page 18

 
Dennis Kirichenko:
And may I ask why static?

Because it is taken out of context - see the original post.
 

undocumented feature of the OnInit() function, this function can be run in any part of the program. Test code.

//+------------------------------------------------------------------+
//|                                                       test10.mq5 |
//|                                                   Sergey Gritsay |
//|                         https://www.mql5.com/ru/users/sergey1294 |
//+------------------------------------------------------------------+
#property copyright "Sergey Gritsay"
#property link      "https://www.mql5.com/ru/users/sergey1294"
#property version   "1.00"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class CNevBar
  {
private:
   datetime          curbar;
   datetime          lastbar;
public:
                     CNevBar();
                    ~CNevBar();
   bool              bar(string symbol,ENUM_TIMEFRAMES period);
  };
//+------------------------------------------------------------------+
void CNevBar::CNevBar()
  {

  }
//+------------------------------------------------------------------+
void CNevBar::~CNevBar(void)
  {

  }
//+------------------------------------------------------------------+
bool CNevBar:: bar(string symbol,ENUM_TIMEFRAMES period)
  {
   curbar=(datetime) SeriesInfoInteger(symbol,period,SERIES_LASTBAR_DATE);
   if(lastbar==0)lastbar=(datetime)SeriesInfoInteger(symbol,period,SERIES_LASTBAR_DATE);
   if(lastbar!=curbar)
     {
      lastbar=curbar;
      return(true);
     }
   return(false);
  }

int test=0;
CNevBar newbar;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

   test=5;
   Print(__FUNCTION__,": test = ",test);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---

  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(newbar.bar(_Symbol,PERIOD_CURRENT))
     {
      test=10;
      Print(__FUNCTION__,": test = ",test);
      OnInit();
     }
  }
//+------------------------------------------------------------------+

Result

2017.03.10 19:26:02.977 test10 (EURUSD,M1)      OnInit: test = 5
2017.03.10 19:27:03.041 test10 (EURUSD,M1)      OnTick: test = 10
2017.03.10 19:27:03.042 test10 (EURUSD,M1)      OnInit: test = 5
2017.03.10 19:28:03.245 test10 (EURUSD,M1)      OnTick: test = 10
2017.03.10 19:28:03.245 test10 (EURUSD,M1)      OnInit: test = 5
2017.03.10 19:29:02.995 test10 (EURUSD,M1)      OnTick: test = 10
2017.03.10 19:29:02.995 test10 (EURUSD,M1)      OnInit: test = 5


...

 
Sergey Gritsay:

undocumented feature of the OnInit() function, this function can be run in any part of the program. Test code.


All On-functions are normal functions and, accordingly, they can be called whenever you want. Either OnCalculate or OnTradeTransaction.
 
fxsaber:

All On-functions are normal functions, and accordingly they can be called whenever you want. Either OnCalculate or OnTradeTransaction.


But it's not written in the documentation

OnInit

The OnInit() function is the OnInit event handler. It can be of typevoid orint, and has no parameters:

voidOnInit();

The Init event is generated immediately after loading an Expert Advisor or indicator, this event is not generated for scripts. The OnInit() function is used for initialization. If OnInit() has the int type return value, the non-zero return code means unsuccessful initialization, and it generates theDeinit event with the deinitialization reason codeREASON_INITFAILED.

To optimize the input parameters of an Expert Advisor it is recommended to use values from enumENUM_INIT_RETCODE as the return code. These values are used to control the course of optimization, including the selection of the most appropriatetesting agents. During initialization of an Expert Advisor before the start of testing you can request information about the configuration and resources of an agent (the number of cores, amount of free memory, etc.) using theTerminalInfoInteger() function. Based on the information obtained, you can either allow to use this testing agent, or reject using it during the optimization of this Expert Advisor.

ENUM_INIT_RETCODE

Identifier

Description

INIT_SUCCEEDED.

Successful initialization, testing of the Expert Advisor can be continued.

This code means the same as zero - initialization of the Expert Advisor in the tester was successful.

INIT_FAILED

Unsuccessful initialization, testing is meaningless to continue due to unrecoverable errors. For example, failed to create an indicator that is required for the work of the Expert Advisor.

Return of this value means the same as the return of a value other than zero - initialization of the Expert Advisor in the tester was unsuccessful.

INIT_PARAMETERS_INCORRECT

Designed to indicate an incorrect set of input parameters to the programmer. The result string containing this return code will be highlighted in red in the general optimization table.

Testing for this set of parameters of the Expert Advisor will not be executed, the agent is free to get a new task.

When receiving this value, the strategy tester is guaranteed not to pass this task to other agents for repeated execution.

INIT_AGENT_NOT_SUITABLE

No errors during initialization, but for some reason the agent is not suitable for testing. For example, there is insufficient RAM, there is noOpenCL supportand so on.

After returning this code, the agent will not receive tasks until the end of thisoptimization.

The OnInit() function of the void type always means successful initialization.

Документация по MQL5: Программы MQL5 / События клиентского терминала
Документация по MQL5: Программы MQL5 / События клиентского терминала
  • www.mql5.com
Программы MQL5 / События клиентского терминала - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
When I write code on weekends I insert OnTick in OnInit. And I get everything fine, even if there are no new ticks coming in
 
Sergey Gritsay:


But it's not written in the documentation

OnInit

The OnInit()function is the OnInit event handler. It can be of typevoid orint, and has no parameters:

voidOnInit();

The Init event is generated immediately after loading an Expert Advisor or indicator

It says that it is a function.

It's not written in documentation, it's not mentioned here. It's called before OnInit and after OnDeinit - constructor/destructor calls of global objects (inaccuracy in Help). You can declare the indicator buffers without OnInit. The rudiments are as follows.

 
Searching FullOnCalculate leads immediately to a lot of completely undocumented features of working with indicator buffers in MT4/MT5 - in some places they differ seriously.
 
Here is my final vision of the solution to the problem:
iCustom(... inputs, PRICE_CLOSE,
         color &colors[], int &widths[], int &styles[] );

iAlligator(... params, PRICE_CLOSE,
         color &colors[], int &widths[], int &styles[] );
Suppose an indicator has these parameters:
#property indicator_plots 5
#property  indicator_color1 clrAquamarine, clrBeige, clrBurlyWood
#property  indicator_color2 clrRed, clrGreen

#property  indicator_color5 clrGold, clrCrimson, clrCadetBlue
then the array of colors for it will be as follows:
color colors[] = {
   clrAquamarine, clrBeige, clrBurlyWood,  // 1 plot
   clrRed, clrGreen,                       // 2 plot
   clrBlack,                               // 3 plot
   clrWhite,                               // 4 plot
   clrGold, clrCrimson, clrCadetBlue,      // 5 plot
   clrBurlyWood, clrBlue                   // ignore
};

If the array is smaller, only part of the colors are transferred. If the array is larger, it is trimmed.

For width and style arrays, the principle is the same.

 
fxsaber:

The overloaded (see above) HistoryDealSelect is called in OrderSendSync.

How can I determine the time of opening the last position and the time of closing the last position?

And in general, I would like to know the general principles and algorithms for obtaining information on open/closed positions on mql5.

What should be taken into account, what peculiarities exist at the moment?

 
Artyom Trishkin:

How can I determine the time of opening the last position and the time of closing the last position?

In general, I would like to know the general principles and algorithms for obtaining information on open/closed positions in mql5.

What should be considered, what are the peculiarities at the moment?


This is all determined by selecting the history of orders and deals and then reading the properties of orders.

bool  HistorySelectByPosition( 
   long   position_id     // идентификатор позиции - POSITION_IDENTIFIER 
   );

POSITION_IDENTIFIER

The position identifier is a unique number, which is assigned to each newly opened position and does not change throughout its lifetime. It corresponds to the ticket of the order with which the position was opened.

The position identifier is specified in each order (ORDER_POSITION_ID) and each trade (DEAL_POSITION_ID) that opened, changed or closed it. Use this property to search for orders and trades associated with the position.

When a position is reversed in netting mode (a single in/out trade), the POSITION_IDENTIFIER position identifier is not changed. However, POSITION_TICKET is changed to the order ticket which resulted in the reversal. In the hedging mode, there is no position reversal.

long


And using the properties from the enumeration ENUM_DEAL_ENTRY

DEAL_ENTRY_IN

Entering the market

DEAL_ENTRY_OUT

Market exit

DEAL_ENTRY_INOUT

U-turn

DEAL_ENTRY_OUT_BY

Closing of counter position