Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 41

 
Vitaly Muzichenko:
Yes, you're right, it's just that the code is written in such a style that the previous speaker misinterpreted it.

And to check?

//+------------------------------------------------------------------+
//|                                                   TestLogics.mq4 |
//|              Copyright 2016, Artem A. Trishkin, Skype artmedia70 |
//|                       https://login.mql5.com/ru/users/artmedia70 |
//+------------------------------------------------------------------+
#property copyright "Copyright 2016, Artem A. Trishkin, Skype artmedia70"
#property link      "https://login.mql5.com/ru/users/artmedia70"
#property version   "1.00"
#property strict
#property script_show_inputs
//--- input parameters
input int      Value=0;    // Количество имеющихся ордеров
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//---
   if(Value>0) Alert("1. Количество ордеров=",Value);
   else Alert("2. Это код после else");
      {
      Alert("3. Ордера отсутствуют");
      }
  }
//+------------------------------------------------------------------+
The code in curly braces does not refer to the if-else condition, and will always be executed, regardless of the value in Value
 
Artyom Trishkin:

And check it?


The code in curly braces does not refer to the if-else condition, and will always be executed, regardless of the value in Value

Geez, really, now I've reviewed the original code again, and I was wrong. The writing style is kind of crooked - confused me.

 

Re-checked, removed the "extra".

division by zero didn't go anywhere

Files:
zero.mq4  5 kb
 
trader781:

Re-checked, removed the "extra".

division by zero didn't go anywhere

 
trader781:

Re-checked, removed the "extra".

The division by zero has not disappeared

Because there is no check if the divisor is different from 0. What is the point of executing further code when there are no orders and 0 lots?

 

Put a check on it

void ModifyOrders()
  {
   double avg_price=0;
   price=0;
   bool z=true;
   double orderlots=0;

   for(i=OrdersTotal()-1; i>=0; i--)
     {
      if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) && (OrderSymbol()==Symbol())
      && (OrderMagicNumber()==Magic) && (OrderType()==FindLastOType()))
        {
         price+=OrderOpenPrice()*OrderLots();
         orderlots+=OrderLots();
        }
     }
    
   if(orderlots==0) { return; } // если ничего нет - выходим
   avg_price=NormalizeDouble(price / orderlots,Digits);
  
   if(FindLastOType()==OP_BUY)     tp=NormalizeDouble(avg_price+TakeProfit*Point(),Digits);
   if(FindLastOType()==OP_SELL)    tp=NormalizeDouble(avg_price-TakeProfit*Point(),Digits);


   for(i=OrdersTotal()-1; i>=0; i--)
     {
      if((OrderSelect(i,SELECT_BY_POS,MODE_TRADES)) && (OrderSymbol()==Symbol())
      && (OrderMagicNumber()==Magic) &&(OrderType()==FindLastOType()))

         z=OrderModify(OrderTicket(),OrderOpenPrice(),0,tp,0);
         if (!z)
         Print("Ошибка функции модифицирования");
     }
  }
 
Vitaly Muzichenko:

Put a check on it


Vitalie Postolache:


Missed the lots though, thanks.
 
trader781:
But I missed the lots, thank you.
It's OK to miss it, it's not OK not to read the logbook, it says it all, and it says which line is wrong.
 
Vitaly Muzichenko:
It's OK to miss it, it's not OK not toread the magazine, it's all there.

I wasn't going to read it. It says 'to check in the market'. My products will never be on the market.

about dividing by zero.

what about the one above? It's supposed to takefrom there.

{
price+=OrderOpenPrice()*OrderLots();
orderlots+=OrderLots();
}
}

if(orderlots==0) {return; }// if there is nothing, exit
avg_price=NormalizeDouble(price / orderslots,Digits);

if(FindLastOType()==OP_BUY) tp=NormalizeDouble(avg_price+TakeProfit*Point(),Digits);
if(FindLastOType()==OP_SELL) tp=NormalizeDouble(avg_price-TakeProfit*Point(),Digits);

 
trader781:
I wasn't going to read it. It says "to test in the marketplace". My products will never be in the marketplace.

I'm talking about the magazine in the tester

Added: Assemble the code to its normal state, revise it, then run it in the tester and read the errors in the log. You're giving me scraps of code here.