EA recovery (reupdate after removal) not working, maybe I should try Gloval Variables?

 

Hello, 

I've been struggling with this for a quite a while, and I get some error that just don't make sense to me, Hopefully some of you pros can help me out and find what I'm missing. I want to assign the ticket number to each open order after removal and then also get the open price of the oldest order. In my point of view I have this programmed EXACTLY the same for buys and sells, but if works properly for sells, and not for buys. Last week it was the opposite, it worked for buys and not for sells, so this drives me mad!!

int orders[10];  //array to store sell tickets 
int tickets[10]; //array to store buy tickets


int OnInit()
  {   
        buypositions = CountBuyPositions();
        sellpositions = CountSellPositions();

if (buypositions > 0)
{
   for(int i =buypositions-1; i>=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(_Symbol==OrderSymbol())
           {
            if(OrderType()==OP_BUY)
              {
               tickets[i] = OrderTicket();
               Print("ticket ", i, " ", tickets[i]);
              }
           }
        }
      else
        {
         Print("OrderSelect failed error # ", GetLastError());
        };
     }
}

   if(buypositions > 0)
     {
      if(OrderSelect(tickets[0], SELECT_BY_TICKET==true))
        {
         if(_Symbol==OrderSymbol())
           {
            if(OrderType()==OP_BUY)
              {
                   signalbuystartprice=OrderOpenPrice();
              }
           }
        }
      else
        {
         Print("OrderSelect buy failed error # ", GetLastError());
        };

      //Print("tickets 0 = ", tickets[0]);
     }
if (sellpositions > 0 )
{
   for(int i =sellpositions-1; i>=0; i--)
     {
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
        {
         if(_Symbol==OrderSymbol())
           {
            if(OrderType()==OP_SELL)
              {
               orders[i] = OrderTicket();
               Print("order ", i, " ", orders[i]);
              }
           }
        }
      else
        {
         Print("OrderSelect failed error # ", GetLastError());
        };
     }
 }


   if(sellpositions > 0)
     {
      if(OrderSelect(orders[0], SELECT_BY_TICKET==true))
        {
         if(_Symbol==OrderSymbol())
           {
            if(OrderType()==OP_SELL)
              {
                 signalsellstartprice=OrderOpenPrice();
              }
           }
         }
      else
        {
         Print("OrderSelect sell failed error # ", GetLastError());
        };

      //Print("order 0 = ", orders[0]);
     }



   Print("Signal Buy Start Price ", signalbuystartprice);
   Print("Signal Sell Start Price ", signalsellstartprice);

   Print("Opened buy positions: ", buypositions, " || ", "Opened Sell Positions: ", sellpositions);



   return(INIT_SUCCEEDED);

}

this is my output when I print it now. At the moment of testing this I have 2 sells and 2 buys open, I should see Signal buy start price "opening price of the buy order", but its just returning 0. 

Also, I might think the problem is in the loops as tickets doesn't reach index 0?



I search for orderselect error #4051, but could not find any useful information about how to fix that. Any help here plz? 

 
Skester96:

Hello, 

I've been struggling with this for a quite a while, and I get some error that just don't make sense to me, Hopefully some of you pros can help me out and find what I'm missing. I want to assign the ticket number to each open order after removal and then also get the open price of the oldest order. In my point of view I have this programmed EXACTLY the same for buys and sells, but if works properly for sells, and not for buys. Last week it was the opposite, it worked for buys and not for sells, so this drives me mad!!

this is my output when I print it now. At the moment of testing this I have 2 sells and 2 buys open, I should see Signal buy start price "opening price of the buy order", but its just returning 0. 

Also, I might think the problem is in the loops as tickets doesn't reach index 0?



I search for orderselect error #4051, but could not find any useful information about how to fix that. Any help here plz? 

I wonder, why do you want to assign a ticket number to the open order?
 
Ahmad Zuhairdi Noh:
I wonder, why do you want to assign a ticket number to the open order?

This is in the situation of a crash of MT4, electricity falls or any situacion in which the EA is removed, and have to initialize again and the EA has to know which open orders belong where in the EA algorithm to continue its operations. 

 
Skester96:

This is in the situation of a crash of MT4, electricity falls or any situacion in which the EA is removed, and have to initialize again and the EA has to know which open orders belong where in the EA algorithm to continue its operations. 

There is no need to put ordertickets in an array. The terminal already gives you access to open orders. Just do an orderselect loop to check the situation.

 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  3. if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
             ⋮
                   tickets[i] = OrderTicket();
    You are using your index i for two separate things which will not work. You need i for the position list index and two others for the buy/sell arrays.

  4. if(OrderSelect(orders[0], SELECT_BY_TICKET==true))
    The second argument is SELECT_BY_TICKET. It is not a bool.
MQL5 forum: General
MQL5 forum: General
  • www.mql5.com
Release notes, news, and general questions not discussed in other forum sections
 
Skester96:

This is in the situation of a crash of MT4, electricity falls or any situacion in which the EA is removed, and have to initialize again and the EA has to know which open orders belong where in the EA algorithm to continue its operations. 

Is there any reason why Magic number cannot be used?
OrderMagicNumber - Trade Functions - MQL4 Reference
OrderMagicNumber - Trade Functions - MQL4 Reference
  • docs.mql4.com
OrderMagicNumber - Trade Functions - MQL4 Reference
 
William Roeder:
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  3. You are using your index i for two separate things which will not work. You need i for the position list index and two others for the buy/sell arrays.

  4. The second argument is SELECT_BY_TICKET. It is not a bool.

Hello, thank you for answer, sorry about the post in the wrong group, will post in the correct place next time. 

So you recomend I make this removal update on the ontick funcion just once? So that I avoind making the update in OnInit?

"You need i for the position list index and two others for the buy/sell arrays. " I think this might be the problem, I added 1 more sell and one more buy. and I get this as a result 

Index i is indeed skipping through the orders. What do you mean with using two others? How can I approach that?


Thank you!

 
Ahmad Zuhairdi Noh:
Is there any reason why Magic number cannot be used?

How can I use a magic number for this situation? 

 
I will move this topic to the MQL4 and Metatrader 4 section.

 
William Roeder:
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum?
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:

    1. Terminal starts.
    2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
    3. OnInit is called.
    4. For indicators OnCalculate is called with any existing history.
    5. Human may have to enter password, connection to server begins.
    6. New history is received, OnCalculate called again.
    7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent, account information and prices are valid.
  3. You are using your index i for two separate things which will not work. You need i for the position list index and two others for the buy/sell arrays.

  4. The second argument is SELECT_BY_TICKET. It is not a bool.

I fixed it thank you!!

 
Skester96:

How can I use a magic number for this situation? 

When the EA open the trade using order send,

static int MAGIC_NUMBER_GOES_HERE = 12345;

OrderSend(Symbol(),OP_BUY,1,price,3,stoploss,takeprofit,"My order",MAGIC_NUMBER_GOES_HERE,0,clrGreen);

if the Magic Number was included, the EA will recognize the trade by check for OrderMagicNumber().

For example:

if (buypositions > 0){
   for(int i =buypositions-1; i>=0; i--){
      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
         if(_Symbol==OrderSymbol()){
            if(OrderType()==OP_BUY && OrderMagicNumber == MAGIC_NUMBER_GOES_HERE){ //<<---------- check for magic number according to the one set earlier
                tickets[i] = OrderTicket();
                Print("ticket ", i, " ", tickets[i]);
              }
           }
        }
      else{
         Print("OrderSelect failed error # ", GetLastError());
        };
     }
}

So, each time the EA was reinit, it will check only the trade opened with magic number.

Reason: