Which prevents the advisor from making money. - page 4

 
FAQ:

... please :

Most half of both global and local variables are uninitialized - I'm sure they contain a lot of rubbish at some point.

You never check if an order is selected by OrderSelect(), we are not even talking about checking if it is open or closed at all.

I haven't noticed any quotes update before trading functions...

GetLastError is not reset before opening/closing, but it is checked - again, there will be cases of rubbish in the variable.

You don't check and normalise prices when closing

Do you want to continue?

Go ahead.

Very useful.

 

Here https://www.mql5.com/ru/forum/137651/page3 I posted my closing function

Replace GetInfo() with print, Fun_Error() = ErrorDescription()

 
FAQ:

You never check if an order is selected by OrderSelect(),


Honestly, I don't quite understand what to do if a value - false - is returned when an order is selected.

Does false mean that it is 100% absent among MODE_TRADES?

I have redrawn order opening codes according to your recommendations, frankly, I have not touched the Expert Advisor's configuration, so I am curious, let's see... Will it sell out or will it take another profit?

 
valenok2003:


To be honest, I don't quite understand what to do if false is returned when an order is selected.

Does false mean its 100% absence among MODE_TRADES?


it means 100% that nothing can be done with the order because it is not selected.
 
PapaYozh:

it means 100% that nothing can be done with it [the order] as it is not selected.



Right... Oh, man... but if it's not selected, it's an error, isn't it? Something has to be done...

For example, see this function,

//+-------
//+------------------------------------------------------------------+
//|   valenok2003@mail.ru                                05.07.2011
//+------------------------------------------------------------------+
//| check_open_order() ПРОВЕРКА реального открытия рыночного ордера по тикету
//| Требует:
//| - тикет ордера 
//| Возвращает тикет открытого ордера или -1
//+------------------------------------------------------------------+
int check_open_order(int _Ticket)
{  string _Function = "check_open_order(): ";
//-----------------
   if(_Ticket > 0) {  
      OrderSelect(_Ticket,SELECT_BY_TICKET);             
      if(OrderType() <= 1) {  
         output_mov_string(order_type_in_txt(OrderType())+ " успешно открыт!", Clr_Report);
         output_sound(Name_Sound_Open);
         OrderPrint();
//-----------------         
         for(int _n = 0; _n < MAX_ORDERS; _n++)  {
            // Если место свободно
            if(Arr_Tickets[_n] == 0)   {
               // Создаём запись об ордере
               Arr_Tickets[_n] = OrderTicket();
               break;
            }
         }
      }
      else _Ticket = -1;
   }
//-----------------
   return(_Ticket);
}
//+-------------------------------------------------------------------
//+-------
If the order is not selected, I should return -1. And if it is not selected for any other reason - for example, a failure of some kind, but the order is open, then what should I do?
 
valenok2003:


Right... Oh, man... But if it's not selected, it's an error of some kind, isn't it? Something has to be done...


It may not be selected:

1) because of bugs in the Expert Advisor's code;

2) it was closed due to circumstances beyond the EA's control (triggered SL/TP, closed manually, closed by the EA)

 
valenok2003:


Right... Oh, man... but if it's not selected, there's an error, isn't there? Something has to be done...

Are you kidding me or are you serious?

Pause and select again. I have it organised that way, it works online:

// ------------------------------------------------Ищем наш ордер---------------------------------
   int orderType;
   for (int orderIndex = (OrdersTotal() - 1); orderIndex >= 0; orderIndex--)
   {
      if (!OrderSelect(orderIndex, SELECT_BY_POS))     continue;
      if ((OrderSymbol() != Symbol()) || (OrderMagicNumber() != MagicNumber)) continue;
      orderType = OrderType();
      if ((orderType != OP_BUY) && (orderType != OP_SELL)) continue;
          ticket = OrderTicket( );                         // Номер ордера
          orderLots = OrderLots();                         // Lots   
          orderProfit = OrderProfit() + OrderSwap();       // Profit
          Price = OrderOpenPrice();                        // Цена открытия рыночного ордера
          SL =  OrderStopLoss();                           // Значение StopLoss ордера
          TP = OrderTakeProfit();                          // Значение TakeProfit ордера          
          if (ticket>0)                                    // Если позиция открылась
              {
                while(OrderSelect(ticket,SELECT_BY_TICKET)==false)       // Если ордер выбран
                Sleep(100);                                 
                double OpenPrice=OrderOpenPrice();
       // Print("OrderTicket()=",OrderTicket(),  "OrderOpenTime()=",OrderOpenTime()); 
       // Print("TimeLocal()=",TimeLocal());                                                                    
                     //---------------------Запоминаем значения сл и тп ордера   
...
...
...
                  
 
Roman.:

Are you kidding me or are you serious?

Pause and select again. I have it organised this way, it works online:

I supplemented my post there.

How many times do I have to choose?

 
valenok2003:

I supplemented my post there.

And how many times to choose?

BEFORE. Until it is selected in a loop:

if (ticket>0)                                    // Если позиция открылась
              {
                while(OrderSelect(ticket,SELECT_BY_TICKET)==false)       // Если ордер выбран
                Sleep(100);                                 
                double OpenPrice=OrderOpenPrice();
       // Print("OrderTicket()=",OrderTicket(),  "OrderOpenTime()=",OrderOpenTime()); 
       // Print("TimeLocal()=",TimeLocal());                                                                    
                     //---------------------Запоминаем значения сл и тп ордера                     
                if (orderType == OP_BUY) 
                   {                
                     
                   }
            
                if (orderType == OP_SELL) 
                   {        
                   
                   }   
                            
              }
 
PapaYozh:


It may not be selected:

1) because of a bug in the EA code;

this is quite clear

2) it was closed due to circumstances beyond the EA's control (the SL/TP was triggered, manually closed, closed by the EA)

just these two reasons or could there be something else?
Reason: