Discussion of article "The checks a trading robot must pass before publication in the Market"

 

New article The checks a trading robot must pass before publication in the Market has been published:

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.

The strategy tester integrated into the platform allows not only to backtest trading systems, but also to identify logical and algorithmic errors made at the development stage of the trading robot. During testing, all messages about trade operations and identified errors are output to the tester Journal. It is convenient to analyze those messages in a special log Viewer, which can be called using a context menu command.

Author: MetaQuotes Software Corp.

 

This is the part that raises questions:

bool CheckMoneyForTrade(string symb,double lots,ENUM_ORDER_TYPE type)
  {
//--- get the opening price
   MqlTick mqltick;
   SymbolInfoTick(symb,mqltick);
   double price=mqltick.ask;
   if(type==ORDER_TYPE_SELL)
      price=mqltick.bid;
//--- equity and required margin values
   double margin,equity=AccountInfoDouble(ACCOUNT_EQUITY);
   //--- call the test function
   if(!OrderCalcMargin(type,symb,lots,price,margin))
     {
      //--- something went wrong, report and return false
      Print("Error in ",__FUNCTION__," code=",GetLastError());
      return(false);
     }
   //--- if there are insufficient funds to perform the operation
   if(margin>equity)
     {
      //--- report an error and return false
      Print("Not enough money for ",EnumToString(type)," ",lots," ",symb," Error code=",GetLastError());
      return(false);
     }
//--- the test was successful
   return(true);
  }

Why is required margin compared to Equity and not FreeMargin?

 
Andrey Barinov:

This is the part that raises questions:

Why is required margin compared to Equity and not FreeMargin?

Reasonable - we'll fix it.
 

Thank you for the article, but so far I have not been able to get a working result with the

bool CheckMoneyForTrade(string symb, double lots,int type)
  {
   double free_margin=AccountFreeMarginCheck(symb,lots,type);
   //-- if there's not enough money
   if(free_margin<0)
     {
      string oper=(type==OP_BUY)? "Buy":"Sell";
      Print("Not enough money for ", oper," ",lots, " ", symb, " Error code=",GetLastError());
      return(false);
     }
   //-- the test was successful
   return(true);
  }

Any variations of AccountFreeMarginCheck give errors, it is impossible to set up the Expert Advisor for the market at all, I tried to find out from users, then the article appeared, no way, no matter what you do or don't do

If you add GetLastError()!=134 to the GetLastError()!=134 condition or use the above function, it just fails with errors.

The above function manages to throw errors like this.

2016.07.25 15:15:54.200 2016.01.04 19:59  RSI_Grabber.1.1 XAUUSD,H1: invalid lots amount for FreeMarginCheck function
2016.07.25 15:15:54.200 2016.01.04 19:59  RSI_Grabber.1.1 XAUUSD,H1: Not enough money for Buy 0.1 XAUUSD Error code=4051

I may be wrong, but 0.1 is a normal lot specified by hand. MODE_MINLOT for a currency pair is 0.01.

But to be honest, the further I dig, the less I understand what the problem(s) is.

 

Perhaps the lot should be normalised in this function so that there is no Disabled.

and then the function writes that there is no money, but it does not send a request to the server to open positions, which satisfies the rules of the market.

I have recently started to use such an instruction about 2 years ago:

double margin=EMPTY_VALUE;
      margin=AccountFreeMarginCheck(sy,op,ll);
      if(margin>0)
        {

        ticket=OrderSend(sy,op,ll,NormalizeDouble(pp,MarketInfo(sy,MODE_DIGITS)),Slippage,NormalizeDouble(sl,MarketInfo(sy,MODE_DIGITS)),NormalizeDouble(tp,MarketInfo(sy,MODE_DIGITS)),
coomment,mn,0,clOpen);
         
        }
      else Print("Not Enought Money Margin Required"+(string)margin);

Actually everything works.

When opening a trade, the Expert Advisor simply returns the Print("Not Enought Money Margin Required "+( string)margin) string;

The Expert Advisor is tested in the market, everyone is happy with it

 

I use this option for exact calculation, it is the only one that helped me to solve this problem. You can then compare them freely.

Laverage = AccountInfoInteger(ACCOUNT_LEVERAGE);  //Shoulder 
RazmerKontrakta=LotSize*MarketInfo(Symbol(),MODE_LOTSIZE);  //TradeVol*1 lot 
MargaB=(RazmerKontrakta/Laverage)*NormalizeDouble(MarketInfo(Symbol(),MODE_BID),Digits());
MargaS=(RazmerKontrakta/Laverage)*NormalizeDouble(MarketInfo(Symbol(),MODE_ASK),Digits());
FreeMargin  = AccountInfoDouble(ACCOUNT_MARGIN_FREE);
 

Half a year ago I tested one Expert Advisor from the Market, it was crashing in the tester on the second or third day with a division to zero error. Expert Advisor with several dozens of reviews, that is, it is bought. And several hundred comments.

And many write that there is this problem and advise each other to restart once a day!!!!

It turns out that the strictness of the rules in the Market is compensated by the optionality of their implementation?

SUS: I don't remember the name of the Expert Advisor, but it is a top and not cheap. Some kind of scalper.

 

Dear author, thank you for the article.

There is a small mistake in the code for MQL4, if I may. For the function CheckMoneyForTrade:

double free_margin=AccountFreeMarginCheck(symb,lots,type);

required:

double free_margin=AccountFreeMarginCheck(symb,type,lots);
 

In the function  

bool CheckMoneyForTrade(string symb, double lots,int type)

you have to change the free margin check function from,

double free_margin=AccountFreeMarginCheck(symb,lots,type);

to,

double free_margin=AccountFreeMarginCheck(symb,type,lots);

to let it work properly.

 
Article fixed on all languages, thank you.
 
Dart circus. Instead of programming all the necessary checks once on the terminal side, the developers decided not to bother and make each user write and debug the same codes, arranging "dances with tambourines" around one elementary command OrderSend. Thank you very much to the author of the article for spending time on describing the pitfalls, which should not be there at all if the terminal developers have a friendly approach to users. I personally have only contempt for terminal developers for such an approach. Because their laziness costs thousands of developers' hours lost in MQL.