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

 
fxsaber:
for (int i =::ArrayResize(TmpSort, ::ArraySize(ARRAY)) - 1; i >= 0; i--)

I do not advise you to write it this way. If the system fails to allocate the required amount of memory to the array, the program will continue to run quietly, leading to unpredictable consequences.

 
Alexey Navoykov:

I do not advise you to write it this way. If the system fails to allocate the required amount of memory for the array, the program will continue to run quietly, leading to unpredictable consequences.

I necessarily put it for the real account.

Forum on trading, automated trading systems and testing trading strategies

Libraries: HistoryTicks

fxsaber, 2019.09.10 18:58

Forum on trading, automated trading systems and trading strategy testing

Libraries: HistoryTicks

fxsaber, 2018.12.10 13:55

If you write the following line in the beginning of any MT4/5 Expert Advisor
#include <fxsaber\HistoryTicks\ArrayResize.mqh> // https://www.mql5.com/ru/code/20298

then get a detailed notification in case there is an error when running ArrayResize or ArrayCopy.

This allows you to see hard to identify problems in passing information through arrays, but even more importantly, get a message that the EA has stopped due to an array out of range(not the EA's fault). For example, because of a lack of memory or some other failure. In other words, you will know right away that an abnormal stopping of the EA took place, rather than accidentally notice it a few hours later.


The HistoryTicks library already contains this line (no need to add it), because the main purpose of the tools - fighting Expert Advisors on real accounts.


In the Terminal journal, there was only such a record in another project.

MemoryException 4471871064 bytes not available, 0 heapmin result


Thanks to the inserted line, the details of what has happened can be seen in the user log

Alert: ThirdPartyTicks - ERROR in int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int)! - see the logs.
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 64: ::MQLInfoString(MQL_PROGRAM_PATH) = C:\Program Files\ICMarkets - MetaTrader 5\MQL5\Scripts\fxsaber\ThirdPartyTicks.ex5
PROGRAM_SCRIPT
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 66: typename(T) = MqlTick
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 67: sizeof(T) = 60
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 68: Res = -1
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 69: NewSize = 129140
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 70: Reserve = 74402041
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 71: ::ArraySize(Array) = 129139
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 72: PrevSize = 129139
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 73: NewSize*sizeof(T) = 7748400
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 74: PrevSize*sizeof(T) = 7748340
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 75: Reserve*sizeof(T) = 169155164
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 76: ::GetLastError() = 4004
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 77: DuringTime = 1195
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 79: ::TerminalInfoInteger(TERMINAL_BUILD) = 2138
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 80: ::TerminalInfoInteger(TERMINAL_DISK_SPACE) = 6344
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 81: ::TerminalInfoInteger(TERMINAL_MEMORY_PHYSICAL) = 16301
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 82: ::TerminalInfoInteger(TERMINAL_MEMORY_AVAILABLE) = 31530
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 83: ::TerminalInfoInteger(TERMINAL_MEMORY_TOTAL) = 32602
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 84: ::TerminalInfoInteger(TERMINAL_MEMORY_USED) = 1072
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 86: ::ChartID() = 132088214536525342
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 87: ::PeriodSeconds() = 60
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 88: ::Symbol() = EURUSD
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 89: ::TimeCurrent() = 2019.09.10 19:49:15
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 90: ::TimeLocal() = 2019.09.10 19:49:12
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 94: ::SymbolInfoTick(_Symbol,Tick) = true
 time = 2019.09.10 19:49:13.178 bid = 1.10436 ask = 1.10439 last = 0.00000 volume = 0 2 TICK_FLAG_BID
int ARRAYRESIZE::ArrayResize_Real<MqlTick>(MqlTick&[],const int,const int), Line = 97: ::ChartSetInteger(0,CHART_BRING_TO_TOP,true) = true
array out of range in 'Array.mqh' (11,10)
 

Please suggest an analog of MQL4 function MarketInfo(asymbol, MODE_MARGIN_REQUIRED) in MQL5.

I need to get the amount of free funds needed to open 1 lot to buy.

I tried different variants of SymbolInfoDouble(symbol, ...) function in MQL5, I can't find the right variant, it returns 0 everywhere.

 
Elena Baranova:

Please suggest an analog of MQL4 function MarketInfo(asymbol, MODE_MARGIN_REQUIRED) in MQL5.

I need to get the amount of free funds needed to open 1 lot to buy.

I tried different variants of SymbolInfoDouble(symbol, ...) function in MQL5, I can't find the right variant, it returns 0 everywhere.

https://www.mql5.com/ru/articles/2555#not_enough_money

Какие проверки должен пройти торговый робот перед публикацией в Маркете
Какие проверки должен пройти торговый робот перед публикацией в Маркете
  • www.mql5.com
Все продукты Маркета перед публикацией проходят обязательную предварительную проверку, так как небольшая ошибка в логике советника или индикатора может привести к убыткам на торговом счете. Именно поэтому нами разработана серия базовых проверок, призванных обеспечить необходимый уровень качества продуктов Маркета. Если в процессе проверки...
 

I'm sorry, I didn't understand why I needed this link. I didn't see the answer to my question there.

 
Elena Baranova:

I'm sorry, I didn't understand why I needed this link. I didn't see the answer to my question there.

What exactly didn't you see in the article?

Examples of checking:

MQL5

bool CheckMoneyForTrade(string symb,double lots,ENUM_ORDER_TYPE type)
  {
//--- получим цену открытия
   MqlTick mqltick;
   SymbolInfoTick(symb,mqltick);
   double price=mqltick.ask;
   if(type==ORDER_TYPE_SELL)
      price=mqltick.bid;
//--- значения необходимой и свободной маржи
   double margin,free_margin=AccountInfoDouble(ACCOUNT_MARGIN_FREE);
   //--- вызовем функцию проверки
   if(!OrderCalcMargin(type,symb,lots,price,margin))
     {
      //--- что-то пошло не так, сообщим и вернем false
      Print("Error in ",__FUNCTION__," code=",GetLastError());
      return(false);
     }
   //--- если не хватает средств на проведение операции
   if(margin>free_margin)
     {
      //--- сообщим об ошибке и вернем false
      Print("Not enough money for ",EnumToString(type)," ",lots," ",symb," Error code=",GetLastError());
      return(false);
     }
//--- проверка прошла успешно
   return(true);
  }
 
Vladimir Karputov:

What exactly didn't you see in the article?

Examples of checking:

MQL5

You need to getthe amount of free funds needed to open 1 lot to buy.

What does the check in the article have to do with it?

 
Elena Baranova:

Need to getthe amount of available funds needed to open 1 lot to buy.

What does the check from the article have to do with it?

Because the article teaches diligent study of the material.

The example even gives you the full function to check before sending the trade order.


One:

//--- значения необходимой и свободной маржи
   double margin,free_margin=AccountInfoDouble(ACCOUNT_MARGIN_FREE);

and immediately the reference:

ACCOUNT_MARGIN_FREE

Size of free funds in the account in the deposit currency, available for position opening

AccountInfoDouble


and two is the minimum check:

   //--- вызовем функцию проверки
   if(!OrderCalcMargin(type,symb,lots,price,margin))
     {
      //--- что-то пошло не так, сообщим и вернем false
      Print("Error in ",__FUNCTION__," code=",GetLastError());
      return(false);
     }
   //--- если не хватает средств на проведение операции
   if(margin>free_margin)
     {
      //--- сообщим об ошибке и вернем false
      Print("Not enough money for ",EnumToString(type)," ",lots," ",symb," Error code=",GetLastError());
      return(false);
     }
Документация по MQL5: Информация о счете / AccountInfoDouble
Документация по MQL5: Информация о счете / AccountInfoDouble
  • www.mql5.com
Информация о счете / AccountInfoDouble - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
Elena Baranova:

Need to getthe amount of available funds needed to open 1 lot to buy.

What does the check from the article have to do with it?

didn't think you would have trouble reading the short code, then read the helphttps://www.mql5.com/ru/docs/trading/ordercalcmargin

 
Vladimir Karputov:

While the article teaches diligent study of the material.

The example even gives the full function of checking before sending a trade order.


One:

and right away the reference:

ACCOUNT_MARGIN_FREE

Size of free funds in the account in the deposit currency, available for position opening

AccountInfoDouble


and two - minimum check:

Size of free funds in the account in the currency of deposit available to open the position I do not need

I calculate the lot size as % of the balance.

I need to get the amount ofavailable funds needed to open 1 lot to buy.

Reason: