Questions from Beginners MQL4 MT4 MetaTrader 4 - page 112

 
Николай Цепелев:

Hello. I wrote in my EA the calculation of the lot like this:

lot=NormalizeDouble(AccountBalance()*risk/(SL*MarketInfo(Symbol(),MODE_TICKVALUE)),2);

The purpose is to get the correct lot size regardless of the account currency and pair for the given risk and stop loss. Risk is set as a percentage of losses when Stop Loss is triggered. Stop Loss SL in pips (where pips is the smallest possible change in the price of the pair). I am going to use this EA in real account. But I have my doubts. If somebody from more experienced programmers could tell me what errors may occur, I would be very grateful.
Strategy Tester first. Then we have a demo account. And only then the real account. To check the formula, write a script. Or just calculate it on a piece of paper. I took balance=100, risk=0.1 SL=100, Tick=0.00001 Lot=100*0.1 / (100*0.00001) = 10 / 0.001 = 10000 lot more than the balance? Should we consider the leverage?
 
STARIJ:
First strategy tester. Then a demo account. And only then a real one. Write a script to check the formula. Or just calculate it on a piece of paper. I took balance=100, risk=0.1 SL=100, Tick=0.00001 Lot=100*0.1 / (100*0.00001) = 10 / 0.001 = 10000 lot more than the balance? Should we consider the leverage?

MarketInfo(Symbol(),MODE_TICKVALUE)) does not return the minimum tick size. It's written in the help:

Size of minimal change of instrument's price in the deposit currency.


But in fact, it returns the value of the minimal change of the symbol price in the deposit currency of 1 lot of the contract.


In your example,MarketInfo(Symbol(),MODE_TICKVALUE)) will return 1. And then everything is correct. I tried it on real dollar and ruble accounts and it works. Here is the script I used to check it:

#property copyright "Copyright 2016, MetaQuotes Software Corp.

#property link "https://www.mql5.com"

#property version "1.00"

#property strict

#property show_inputs

extern double risk=0.10;

extern int sl=1000;

//+------------------------------------------------------------------+

//| Script program start function |

//+------------------------------------------------------------------+

void OnStart()

{

Alert(NormalizeDouble(AccountBalance()*risk/(sl*MarketInfo(Symbol(),MODE_TICKVALUE),2))

}

//+------------------------------------------------------------------+

Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
Открой новые возможности в MetaTrader 5 с сообществом и сервисами MQL5
  • www.mql5.com
Задавайте вопросы по техническому анализу, обсуждайте торговые системы и улучшайте свои навыки программирования торговых стратегий на языке MQL5. Общайтесь и обменивайтесь опытом на форуме с трейдерами всего мира и помогайте ответами новичкам — наше сообщество развивается вместе с вами. Библиотеки: Black or White Text Black or White Text...
 

You are right. Checking with a script is faster and more reliable

//+------------------------------------------------------------------+ 
//| Расчет лота                                                      | 
//+------------------------------------------------------------------+ 
#property strict
#property show_inputs
extern double risk=0.10;
extern int    sl  =100;
void OnStart() 
{ 
   Alert(MarketInfo(Symbol(),MODE_TICKVALUE), "   ",NormalizeDouble(AccountBalance()*risk/(sl*MarketInfo(Symbol(),MODE_TICKVALUE)),2));
}
 
Николай Цепелев:
Your script takes up 19 cm in height on the screen, mine is 8.1 cm - half the height. And the colour is different
 

We also need to make the accuracy in NormaliseDouble function depend on the minimum allowed lot. If you pass a lot to the OrderSend function, for example, 0.11, and the tool is traded only with lots multiple of 0.1, there will be an error.

 

I can't understand why open trades are not deleted all at once, i.e. EA deletes some of them, but error orderdelete 4108, market order cannot be deleted several times, and only then deletes them.

  int start()                                     
  {
  
  while(!IsStopped())     
      {               

Refreshrates();

  
   if(cont==true&&NormalizeDouble(AccountProfit(),2)>=0)
    {
     for(i=1; i<=OrdersTotal(); i++)       
        { RefreshRates(); 
         if(OrderSelect(i-1,SELECT_BY_POS)==true
           {                                    
              while(OrderType()==OP_BUYLIMIT) 
              {
               res=false;
               res=OrderDelete(OrderTicket());
               if(res==true)
               break;
               }
              while(OrderType()==OP_BUY)
              {
               res=false;
               res=OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symb,MODE_BID),Slippage);  
               if(res==true)
               break;
              }
              while(OrderType()==OP_SELLLIMIT) 
              {
               res=false;
               res=OrderDelete(OrderTicket());
               if(res==true)
               break;
              }
              while(OrderType()==OP_SELL)
              {
               res=false;     
               res=OrderClose(OrderTicket(),OrderLots(),MarketInfo(Symb,MODE_ASK),Slippage);
               if(res==true)
               break;
              }     
           }  
         }
}
}  
 
Yevgen Butov:

I can't understand why open trades are not deleted all at once, i.e. EA deletes some of them, but error orderdelete 4108, market order cannot be deleted several times, and only then deletes them.

This page explains how to delete. You have 2 errors.

 
Good afternoon. I'm asking for help. I searched the NET and the forum. I am testing the Expert Advisor in standard mode, everything is OK... No errors or warnings. I downloaded the tick data and added. (I did everything as described here - How to test Expert Advisors with 99% quality - easy, free, legal) and here - "How to get 99% simulation quality in the Metatrader 4 Strategy Tester ") On startup constantly error 'array out of range in 'expert1.0.mq4' (69,25)... I understand that it is about an array. I don't know what to do. But since everything is OK in standard mode... Please help
 
qroner:
On startup there is a constant error array out of range in 'expert1.0.mq4' (69,25)... I understood that it's about the array. I don't know what to do.

Need to see the code, or a piece of it, handling this array.

Telepaths, have a rest today.

 
Konstantin Nikitin:

You need to look at the code or a piece of code that handles the array.

Telepaths, have a rest today.

void Uchet_Orderov_Function(string _Simvol, int _Magic, int &_Mas[8]){
ArrayInitialize(_Mas, 0);
int Ticket=-1;
for(int pos=OrdersTotal()-1; pos>=0; pos--){
if(OrderSelect(pos,SELECT_BY_POS,MODE_TRADES) && OrderSymbol()==_Simvol&&
OrderMagicNumber()==_Magic && OrderTicket()!=Ticket){
Ticket=OrderTicket();
switch(OrderType()){
case 0:{_Mas[0]++;_Mas[6]++;break;}
case 1:{_Mas[1]++;_Mas[6]++;break;}
case 2:{_Mas[2]++;_Mas[7]++;break;}
case 3:{_Mas[3]++;_Mas[7]++;break;}
case 4:{_Mas[4]++;_Mas[7]++;break;}
case 5:{_Mas[5]++;_Mas[7]++;break;}
}
}
}

}

Here's the function.

I just thought the problem wasn't in the code as there are no errors in standard mode.