Questions from Beginners MQL4 MT4 MetaTrader 4 - page 61

 
Renat Akhtyamov:
I hope there is a value assigned to the Percent variable in OnInit()?
The Percent variable is user defined. I've dropped OnInit().
 
Michail_David:
My Percent variable is user defined. OnInit() I've dropped it.
If Lots=0, the tester won't open anything
 
Renat Akhtyamov:
if Lots=0, nothing will be opened by the tester
I have lots calculated relative to Percent. The tester calculates the number of lots, but no trades.

 Margin_Percent=AccountFreeMargin()*Percent/100; 

Lots=Margin_Percent/MarketInfo(Symbol(),MODE_MARGINREQUIRED);

      Lots_Volume=(MathFloor(Lots)+MarketInfo(Symbol(),MODE_LOTSTEP));

if(Lots>Lots_Volume)
        {
         Lots=Lots_Volume;
        }
      else if(Lots<Lots_Volume)
         Lots=MathFloor(Lots);
      Print("Количество лотов : ",Lots);
      return;
 
Michail_David:
I have lots calculated relative to Percent. The tester calculates the number of lots, but there are no trades.

And still, where in the code is set Percent

and what does the following command print("Lots : ",Lots); ?

 
Renat Akhtyamov:

Still, where in the code is Percent

and what does the following command print("Number of lots : ",Lots); ?

#property link      ""
#property version   "1.00"
#property strict
//--- input parameters
input int      Percent=50;
input int      Fast_EMA_Period=12;
input int      Slow_EMA_Period=25;
input int      Signal_Period=9;
input double   Step_PSAR=0.02;
input double   Maximum_PSAR=0.2;
input int      MAGICNUMBER=413;
static datetime New_Time;// Время текущего бара
double Margin_Percent;  //Используемые средства для открытия ордеров
double Lots;//Определение общего количества лотов
double Lots_Volume;
double MacdCurrent;//Параметры MACD основной линии текущего бара
double MacdPrevious1;//Параметры MACD основной линии предыдущего бара
double MacdPrevious2;//Параметры MACD основной линии со смещением на 2 бара
double StopLoss;//Параметры Трейлинг стоп по параметрам ParabolicSAR текущего бара
double Previous_StopLoss;//Параметры СтопЛосс по параметрам ParabolicSAR предыдущего бара
double CurrentPSAR;//Параметры СтопЛосс по параметрам ParabolicSAR предыдущего бара
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

   bool New_Bar=false;                    // Флаг нового бара
   int ticket,total,cnt;
//---------------------------------------------------------------------------
   New_Time=TimeCurrent();
   if(Time[0]==New_Time) // Сравниваем время
      New_Bar=true;      // Поймался новый бар
   else if(New_Bar==false) // Если бар не новый..
The fifth line specifies Percent. The log prints: "Amount of lots: 90.0100000001". Maybe we should add NormalizeDouble()?
 
Michail_David:
In the fifth line Percent is set. The log shows: "The number of lots is 90.0100000001". Maybe we should add NormalizeDouble()?

There is nothing wrong with the lot.

Look for an error in the order opening logic.

In such cases, I usually set an explicit condition that will open the order

 
Renat Akhtyamov:

There is nothing wrong with the lot.

Look for an error in the order opening logic.

I usually in such cases set an explicit condition for the order to open

Please give us an example with an explicit condition. If I understand correctly, there should be one condition in the loop to open. Is this correct?
 
Michail_David:
Please give an example with an explicit condition. If I understand correctly, there should be one condition in the loop for opening. Right?
Give a command to open an order at the beginning of the code, right after calculation of lot and make sure that the tester works properly. Then you can put everything back.
 
Renat Akhtyamov:
Give a command to open an order at the beginning of the code, just after calculating the lot, and make sure the tester is working properly. Then you can put everything back in place.
Thank you. I found a mistake. I had return; after calculation of lots. That's why the lots didn't open
 
Michail_David:
The fifth line specifies Percent. The log displays: "Number of lots 90.0100000001". Maybe we should add NormalizeDouble()?
Yes, you should. And not only lots, but all the other values which are passed to the server. Lots are also useful to check the minimum and maximum allowed in the account value, also on the step change, there were cases where the lot can be changed in increments of 0.03, so 90.01 will not work, at least overwritten.