Features of the mql4 language, subtleties and techniques

 
This topic will discuss undocumented ways of working with the mql4 language, examples of solving certain problems. It would be desirable that this branch would be closer in content to the FAQ, than to the discussion. I suggest that all experienced programmers share their solutions and programming techniques, especially the coverage of features not described in the help.
 
Well, let me start first.

To close an order, you don't have to define the order type and the price corresponding to that type at all. It is sufficient to write OrderClosePrice()

/********************Script program start function********************/
void OnStart()
{
   int i, total = OrdersTotal()-1;
    for(i = total; i >= 0; i--)
     {
      if(OrderType() < OP_SELLSTOP)
       {
        if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice(), 100))
        printf("***********");
       }
     }
}/********************************************************************/
 

There are many more inaccuracies in the help. TakeSetIndexBuffer(), for example.What is wrong in the help?

"Parameters

index

[in] Number of the indicator buffer. Numbering starts with 0. The number must be less than the value declared in #property indicator_buffers." (this statement is not true)

The #property indicator_buffers specifies the number of buffers to display. Perhaps what the help means, although I haven't checked this, is that, the buffers used for calculation don't need to be given a number by SetIndexBuffer(). But what I have checked is that if the buffer used for calculation is given a number with SetIndexBuffer(), its value can be retrieved via iCustom().

 
I don't get it.
 

Since the developers are visiting the thread - there is a question about "subtleties and tricks":

Example of a formatted print: PrintFormat("Bid=%.5f", Bid)

Question: is there any way, instead of a fixed precision (here5), to specify a condition in the format description so that the precision depends on _Digits? To do without 'preprocessing' like PrintFormat("Bid=%s", DoubleToString(Bid, _Digits))

 

In MQL4 the same code can give different results depending on the state of #property strict. And this is when compilation is successful in both cases. So if the execution is "rambling", you can look for the cause in the wrong place (strict) for a long time.

 
Alexander Puzanov:

Question: instead of a fixed precision (here5), is it possible to specify a condition in the format description so that the precision depends on _Digits?

No, examples are available at https://www.mql5.com/ru/docs/common/printformat
Документация по MQL5: Общие функции / PrintFormat
Документация по MQL5: Общие функции / PrintFormat
  • www.mql5.com
Общие функции / PrintFormat - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 

Описание проблемы

I am writing an indicator in MQL4. I faced an undocumented feature of the DRAW_HISTOGRAM style.

To display the values on the chart, I need two buffers: for the upper value of the histogram and for the lower one. When mapping the indicator array buffers, if the buffers for the style will have values 0 and 1, 2 and 3, 4 and 5, etc. everything is displayed normally. However, if the style buffers are assigned values starting with OTHER, i.e. 1 and 2, 3 and 4, 5 and 6, the histogram lines will not be displayed correctly in the graph, although the values in the data window will be correct.

Please add a mention of this peculiarity to the documentation, or correct it, because it's not always the case that the DRAW_HISTOGRAM style description will start with an even buffer!

From the SD.
 

Forum on trading, automated trading systems and trading strategy testing

Bugs, bugs, questions

-Aleks-, 2017.02.07 18:21

Can you please tell me if in MT4 Expert Advisor, if a custom indicator with more than one graphical buffer is consistently called, then recalculation happens at each call or all buffers are calculated at once and you can refer to the indicator in the code many times and not expect to waste resources. I am also interested to know what will happen if the code is not completed (runs longer than a tick), but the indicator value changes.


Forum on trading, automated trading systems and strategy testing

Bugs, bugs, questions

Slawa, 2017.02.08 08:20

All buffers are calculated at once. Quietly interrogate the values of the indicator - there will be no recalculation

In MT4 the indicator called from the Expert Advisor works in the flow of this EA and on a copy of its historical data. You can loop the Expert Advisor, but at the same time you will receive the indicator data calculated at the time of OnTick call. Until you call RefreshRates. RefreshRates updates historical data of the Expert Advisor, after that all its data will be recalculated at the first call of the indicator

 
Alexey Viktorov:
To close an order, you do not have to specify the order type and price corresponding to this type. It is enough to write close at price OrderClosePrice()

So, you can use OrderClosePrice only AFTER the corresponding OrderSelect. Because OrderSelect copies the data for Order(const)-functions once, and the same RefreshRates is not able to update them.

I.e. if, for example, OrderClosePrice fails to close, then you must do OrderSelect again before the next attempt (RefreshRates is not required).

ZS This thread is from 2005! Here are detailed arguments of developers.

 
Rashid Umarov:
No, there are examples at https://www.mql5.com/ru/docs/common/printformat

It's a shame...

---

So that the post is not useless:

instead of StringGetCharacter("a", 0) you can write just 'a'- often needed when parsing strings into parts with StringSplit