Why am I getting an Error 4051?

 
I'm somewhat frustrated but I'm getting the a. m. error but I can't find the souce. Proboblyblind to from staring so long. Here is the code that is causing it:

//+   Select long positions     +

   for ( i=OrdersTotal()-1;i>=0;i--)
      {
      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
         Print ("Real Ticket: ",OrderTicket());
         if (OrderMagicNumber()==MagicNumberLong)
            if (OrderSymbol()==Symbol())
               if (OrderType()==OP_BUY)
               {
                  if(Ask-OrderTakeProfit()>Trail*Point)
                     TrailingStop(OrderTicket());
                    
                  oldticketnumber = OrderTicket();
                  if (oldticketnumber > buyticketnumber)
                     buyticketnumber = oldticketnumber;     //Sets buyticketnumber to newest order
               }
      }

//+   Check for Martingale condition    +
                  
   if (LongCount <= MaxTradeBuy)
      if (UseMartingale)
    {    Print("Ticket: ",buyticketnumber);
        if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES))
        {
        Print("M-Ticket: ",OrderTicket(),"OOP: ",OrderOpenPrice());
           Print( "Martingale error ",GetLastError()," occurred");}
        else
           if (Ask<OrderOpenPrice()-MartLimit*Point)
              Martingale(0);
    }
I added the "Print" statements to help find where it's stopping the log doesn't make sense. It's jumping the "M-Ticket:" It also doesn't call the Martingale function even though it should. Here is the log:

2017.02.20 12:03:41.845 2017.02.01 01:07:00  TheDescisionTaker EURUSDmicro,M1: Martingale error 4051 occurred
2017.02.20 12:03:41.845 2017.02.01 01:07:00  TheDescisionTaker EURUSDmicro,M1: Ticket: 1
2017.02.20 12:03:41.845 2017.02.01 01:07:00  TheDescisionTaker EURUSDmicro,M1: Real Ticket: 1
2017.02.20 12:03:41.098 2017.02.01 01:06:00  TheDescisionTaker EURUSDmicro,M1: Martingale error 4051 occurred
2017.02.20 12:03:41.098 2017.02.01 01:06:00  TheDescisionTaker EURUSDmicro,M1: Ticket: 1
2017.02.20 12:03:41.098 2017.02.01 01:06:00  TheDescisionTaker EURUSDmicro,M1: Real Ticket: 1
2017.02.20 12:03:40.380 2017.02.01 01:05:00  TheDescisionTaker EURUSDmicro,M1: Martingale error 4051 occurred
2017.02.20 12:03:40.380 2017.02.01 01:05:00  TheDescisionTaker EURUSDmicro,M1: Ticket: 1

What it should be doing is getting the ticket number of the last order, checking if the martingale option is true (user input) and checking if a new order price level was reached. if all are true  send "0" (similar for sell but "1") to the Martigale(). It doesn't seem to reach the final "if" to check the price.


I hope someone sees my mistake because sure don't.



Thanks
 
 
Your code
Print("Ticket: ",buyticketnumber);
if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES)){
   Print("M-Ticket: ",OrderTicket(),"OOP: ",OrderOpenPrice());
   Print( "Martingale error ",GetLastError()," occurred");
}
Your output2017.02.20 12:03:41.098 2017.02.01 01:06:00  TheDescisionTaker EURUSDmicro,M1: Martingale error 4051 occurred
Where is M-Ticket? Code and output do not match!
2017.02.20 12:03:41.098 2017.02.01 01:06:00  TheDescisionTaker EURUSDmicro,M1: Ticket: 1
 
whroeder1:
Your code
Print("Ticket: ",buyticketnumber);
if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES)){
   Print("M-Ticket: ",OrderTicket(),"OOP: ",OrderOpenPrice());
   Print( "Martingale error ",GetLastError()," occurred");
}
Your output2017.02.20 12:03:41.098 2017.02.01 01:06:00  TheDescisionTaker EURUSDmicro,M1: Martingale error 4051 occurred
Where is M-Ticket? Code and output do not match!
2017.02.20 12:03:41.098 2017.02.01 01:06:00  TheDescisionTaker EURUSDmicro,M1: Ticket: 1
There is no "M-Ticket". I just use that string together with the order ticket number so I can see if the line is reached by looking at the journal. If it does it should print "M-Ticket: xxxxxx OOP: yyyyyyyy" The odd thing is that the line "Martingale..." is printed so there is no logical reason why the line preceeding is ignored.



My main question though is why I'm getting that 4051? There are no data type mismatches that I can see. I don't see what function parameter is causing the problem.
 

Try following to narrow down cause of the error:

  • save or print GetLastError() value as the first thing after OrderSelect()
  • Orderxxx() functions results are undefined if OrderSelect() fails, so you probably shouldn't call OrderTicket() and OrderOpenPrice() at that place
  • ticket number 1 may happen only as the first order in the strategy tester. Otherwise, you have a bug in the code that determines the ticket number.
 

If OrderSelect fails because the ticket number (or index) doesn't exist, you get Error 4051.

Additionally, some print statements get skipped in the Experts log. Try right-clicking the Experts log and selecting "view"

 
datas_brother: There is no "M-Ticket".
Print("M-Ticket: ", OrderTicket(), "OOP: ", OrderOpenPrice());
Where is the output from the print statement containing "M-Ticket?" Your code and the output does not match.
 
Drazen Penic:

Try following to narrow down cause of the error:

  • save or print GetLastError() value as the first thing after OrderSelect()
  • Orderxxx() functions results are undefined if OrderSelect() fails, so you probably shouldn't call OrderTicket() and OrderOpenPrice() at that place
  • ticket number 1 may happen only as the first order in the strategy tester. Otherwise, you have a bug in the code that determines the ticket number.
Thanks for the suggestion but it unfortunately didn't work. The OrderSelect() doesn't cause an error.

The OrderTicket() and OrderOpenPrice() were put in after the error started to be able to follow the flow in the journal.

You are right, it is the first ticket in the tester.
 

OK, it's not OrderSelect().

Anyway, you should not call OrderTicket() and OrderOpenPrice() when OrderSelect() fails. Behavior is undefined. 


Are you absolutely sure that the order with the ticket number 1 exists?

Is it possible that it was a pending order that was deleted or expired at some point?

 
datas_brother:
My main question though is why I'm getting that 4051? There are no data type mismatches that I can see. I don't see what function parameter is causing the problem.

 

honest_knave:

If OrderSelect fails because the ticket number (or index) doesn't exist, you get Error 4051.

Additionally, some print statements get skipped in the Experts log. Try right-clicking the Experts log and selecting "view"

So try adding this: 

if (LongCount <= MaxTradeBuy)
      if (UseMartingale)
    {    Print("Ticket: ",buyticketnumber);
        if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES))
        {
         for(int i=OrdersTotal()-1; i>=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS)) Print(OrderTicket());
           }
        Print("M-Ticket: ",OrderTicket(),"OOP: ",OrderOpenPrice());
           Print( "Martingale error ",GetLastError()," occurred");}
        else
           if (Ask<OrderOpenPrice()-MartLimit*Point)
              Martingale(0);
    }

I'm fairly confident that you won't get a match between buyticketnumber and the printed ticket numbers. 

There is no point printing the value of OrderTicket() or OrderOpenPrice() if OrderSelect() has failed. All the values of Orderxxxxx() are irrelevant if the OrderSelect failed.

 
honest_knave:

 

So try adding this: 

if (LongCount <= MaxTradeBuy)
      if (UseMartingale)
    {    Print("Ticket: ",buyticketnumber);
        if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES))
        {
         for(int i=OrdersTotal()-1; i>=0; i--)
           {
            if(OrderSelect(i,SELECT_BY_POS)) Print(OrderTicket());
           }
        Print("M-Ticket: ",OrderTicket(),"OOP: ",OrderOpenPrice());
           Print( "Martingale error ",GetLastError()," occurred");}
        else
           if (Ask<OrderOpenPrice()-MartLimit*Point)
              Martingale(0);
    }

I'm fairly confident that you won't get a match between buyticketnumber and the printed ticket numbers. 

There is no point printing the value of OrderTicket() or OrderOpenPrice() if OrderSelect() has failed. All the values of Orderxxxxx() are irrelevant if the OrderSelect failed.

I was up til 1:00 am trying to find the problem. First, adding that for-loop gave me identical ticket numbers. I also threw out all of the print statements and the problem remained. I then followed the logic painstakingly all the way to the end. The problem was with the structure. The code in my OP is repeated for sell within the same function. If there is only a buy order open the "Check for martingale" part was also being run for short positions. Since there were none open it threw taht error. The solution was checking if buyticketnumber and sellticketnumber != 0.

//+   Check for Martingale condition    +
  
   if (LongCount <= MaxTradeBuy)
      if (UseMartingale)
      if (buyticketnumber>0)
        if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES))
           Print( "Martingale error ",GetLastError()," occurred");
        else
           if (Ask<OrderOpenPrice()-MartLimit*Point)
              Martingale(0);
 There are still a number of errors 130 that are likely caused by the SL and TP being calculated from candel history but I think I can find a solution to that. It is now running on a demo account so thank you very much for the help.
 
datas_brother:
I was up til 1:00 am trying to find the problem. First, adding that for-loop gave me identical ticket numbers. I also threw out all of the print statements and the problem remained. I then followed the logic painstakingly all the way to the end. The problem was with the structure. The code in my OP is repeated for sell within the same function. If there is only a buy order open the "Check for martingale" part was also being run for short positions. Since there were none open it threw taht error. The solution was checking if buyticketnumber and sellticketnumber != 0.

//+   Check for Martingale condition    +
  
   if (LongCount <= MaxTradeBuy)
      if (UseMartingale)
      if (buyticketnumber>0)
        if(!OrderSelect (buyticketnumber,SELECT_BY_TICKET,MODE_TRADES))
           Print( "Martingale error ",GetLastError()," occurred");
        else
           if (Ask<OrderOpenPrice()-MartLimit*Point)
              Martingale(0);
 There are still a number of errors 130 that are likely caused by the SL and TP being calculated from candel history but I think I can find a solution to that. It is now running on a demo account so thank you very much for the help.

Error 130 may be due to a lack of braces after OrderSelect()

      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
        {
         Print ("Real Ticket: ",OrderTicket());
         if (OrderMagicNumber()==MagicNumberLong)
            if (OrderSymbol()==Symbol())
               if (OrderType()==OP_BUY)
               {
                  if(Ask-OrderTakeProfit()>Trail*Point)
                     TrailingStop(OrderTicket());
                    
                  oldticketnumber = OrderTicket();
                  if (oldticketnumber > buyticketnumber)
                     buyticketnumber = oldticketnumber;     //Sets buyticketnumber to newest order
               }
         }
      }
Reason: