error 4108 invalid ORDER_TICKET!!

 

it start here:  https://www.mql5.com/en/forum/143085/page3

and thise is the code:

#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
 
 
extern int Digits2Round = 2;  // Floating point rounding
 
extern int PercentOfFreeDepo = 1; // Risk Percent of depo
 
extern double MinLot = 0.10; // minimal lot for trading, if calculated lot is less than minimal (it depends on the equity)
 
extern int MagicNumber = 888; // This is magic number for the expert,
                              // It opens, modify and deletes orders with this MagicNumber
                              
extern double Threshold = 0.00050;  // Threshold for the pending order sending
 
extern double SL = 0.00050; // StopLoss
extern double TP = 0.00100; // TakeProfit
extern double P  = 0.00020; // Breakout points
double H; // <<<<<<<<<<<<< g  
double Spread;//<<<<<<<<<< l
double pend;//<<<<<<<<<<<< o <<<<<<<<<<<<<<<<<<<<<<
int  ticket2; //<<<<<<<<<<< 
  int ticket1;                     //  a
                        // l


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
 

 //bool BuyEntered  = false;
   //bool SellEntered = false;
 //int ByEntered;
 string symbol;


 //int ticket2;
//  int ticket;
  int err;
  int q=0;
 

double L=iLow(NULL,0,1);
 double H=iHigh(NULL,0,1);
double C=iClose(NULL,0,0);
double O=iOpen(NULL,0,0);
 double Spread=MarketInfo(Symbol(),MODE_SPREAD)*Point;
double FreeDepo=NormalizeDouble(AccountBalance()-AccountMargin(),Digits2Round);
double Risk=NormalizeDouble((FreeDepo*PercentOfFreeDepo/100),Digits2Round);
double Lot=NormalizeDouble(Risk/(SL/0.0001)*0.1,Digits2Round);
double Check1=H-C;
double Check2=C-L;

//===================== Lets determine lot size and risk ===================================
 
if ( Lot < MinLot )
 {
   Lot=MinLot;
 }
Comment( "\n","Acceptable risk is ",PercentOfFreeDepo, "% = ",Risk," of the free money ",FreeDepo," in lots = ",Lot);
 
//====================== checking for the orders opening
 for( q=0;q<OrdersTotal();q++)
 {
  if (OrderSelect(q, SELECT_BY_POS, MODE_TRADES) && OrderSymbol()==Symbol())
   {
// checking positions, if there are some opended orders, lets check them with the indicator
   if (OrderType()==OP_BUYSTOP)
     {
       return(0); 
     }
   if (OrderType()==OP_SELLSTOP)
     {
       return(0); 
     }
   }  
 }
 
 
//======================= condition for ORDER BUY ===============================

  

if  (Check1 >= Threshold && Check2 >= Threshold && O<H)     
  {    
    ticket1=OrderSend(Symbol(),OP_BUYSTOP,Lot,H+P+Spread,0,H+P-SL+Spread,H+P+TP+Spread,NULL,0,iTime( Symbol(), PERIOD_D1, 0 ) + 86400);
  
    if (ticket1==-1)
      {
        err=GetLastError();
        Print("error(",err,")");
      }
  }
else 
  {
    Comment("\n","Cannot set OP_BUYSTOP",
            "\n","The price is not satisfied to the market entry condition");
  }     
  
    
  
 

   
   
   //================================ condition for ORDER SELL ==================== 



if  (Check1 >= Threshold && Check2 >= Threshold && O>L)  
  {   
    
      
    ticket2=OrderSend(Symbol(),OP_SELLSTOP,Lot,L-P,0,L-P+SL,L-P-TP,NULL,0,iTime( Symbol(), PERIOD_D1, 0 ) + 86400);
  
    if (ticket2==-1)
      {
         err=GetLastError();
         Print("error(",err,")");
      }
  }
else 
  {
    Comment("\n","Cannot set OP_SELLSTOP",
            "\n","The price is not satisfied to the market entry condition");
  }     

   // pend =(H+P+Spread); // <<<<<<<<<<---------pend
 
      ClosePendingOrder( symbol);
//}
//+------------------------------------------------------------------+
// int total=OrdersTotal();
//int TotalOrderCount(string Symbol, int ticket)
//{
 }
  /* 
 bool ClosePendingOrder(string symbol )<<<<<<<<------func under comment
 {
    
     //int ticket2;
     OrderSelect(ticket, SELECT_BY_TICKET);
         
        
         if (ticket > 0 && OrderType()==OP_BUY)
             {
              OrderSelect(ticket2,SELECT_BY_POS);
             //  }
               //if (ticket2>0 && OrderType() !=OP_SELL) 
                  
             //  {    
                  bool orderx= OrderDelete(ticket2,Red);   
             } 
     return(0);<<<<<<<<<<<<<<<<<<<<<<<<<----------under comment
              
    }
 
 */
 /*
 void ClosePendingOrder(string symbol )
   OrderSelect(ticket, SELECT_BY_TICKET);
        
   if (ticket > 0 && OrderType()==OP_BUY)
      {
       OrderSelect(ticket2, SELECT_BY_TICKET);
         if (OrderType() == OP_SELLSTOP)
         {    
         bool test =OrderDelete(ticket2,Red);   
            if (test == false)
            {
            Print("OrderDelete failed, ticket ", ticket2 , " Error # ", GetLastError());
            }    
         } 
      }
   return;
   }
 
 */
 
 
 
 

 void ClosePendingOrder(string symbol )<<<<<<<<<<-------the function-----------------



   {
   {    OrderSelect(ticket1, SELECT_BY_TICKET);<<<<<<<<------the function-----------             if (ticket1 > 0 && OrderType()==OP_BUY)       {       if ( OrderSelect(ticket2, SELECT_BY_TICKET ) &&       OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP && ticket2 > 0 )          {              if( !OrderDelete(ticket2,Red) )                 {             Print("OrderDelete failed, ticket ", ticket2, " Error # ", GetLastError() );             }              }       }    return;    }

 thanks!

 
er007:

it start here:  https://www.mql5.com/en/forum/143085/page3

and thise is the code:

 thanks!

Could you please sort your code in way we can easily copy & paste it, or attach it as a file? You probably would get better help.
I remarked your <<<<< wiht // so I thought I can compile it.... I fixed your wrong braces and compiled it.

The error is pretty obvious, have you ever checked the log?

Read about OrderSelect() especially SELECT_BY_TICKET. Select by ticket does also select an order out of the history pool.
So, if you would read the log, you would have found this:

2013.01.20 22:36:41 2010.01.07 00:01  _help EURUSD,M15: OrderDelete failed, ticket 35 Error # 4108
2013.01.20 22:36:41 2010.01.07 00:01  _help EURUSD,M15: OrderDelete error 4108
2013.01.20 22:36:41 2010.01.07 00:01  _help EURUSD,M15: unknown ticket 35 for OrderDelete function
2013.01.20 22:36:41 2010.01.07 00:01  Tester: #35 deleted due expiration
2013.01.20 22:36:41 2010.01.06 15:05  Tester: take profit #34 at 1.43740 (1.43740 / 1.43780)
2013.01.20 22:36:41 2010.01.06 15:02  Tester: order #34, buy 1.73 EURUSD is opened at 1.43640

You probably would better select by pos and tell the function what order to delete (OP_XXX) 

 
kronin:

Could you please sort your code in way we can easily copy & paste it, or attach it as a file? You probably would get better help.
I remarked your <<<<< wiht // so I thought I can compile it.... I fixed your wrong braces and compiled it.

The error is pretty obvious, have you ever checked the log?

Read about OrderSelect() especially SELECT_BY_TICKET. Select by ticket does also select an order out of the history pool.
So, if you would read the log, you would have found this:

2013.01.20 22:36:41 2010.01.07 00:01  _help EURUSD,M15: OrderDelete failed, ticket 35 Error # 4108
2013.01.20 22:36:41 2010.01.07 00:01  _help EURUSD,M15: OrderDelete error 4108
2013.01.20 22:36:41 2010.01.07 00:01  _help EURUSD,M15: unknown ticket 35 for OrderDelete function
2013.01.20 22:36:41 2010.01.07 00:01  Tester: #35 deleted due expiration
2013.01.20 22:36:41 2010.01.06 15:05  Tester: take profit #34 at 1.43740 (1.43740 / 1.43780)
2013.01.20 22:36:41 2010.01.06 15:02  Tester: order #34, buy 1.73 EURUSD is opened at 1.43640

You probably would better select by pos and tell the function what order to delete (OP_XXX) 


the code:

#property copyright "Copyright © 2009, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
 
 
extern int Digits2Round = 2;  // Floating point rounding
 
extern int PercentOfFreeDepo = 1; // Risk Percent of depo
 
extern double MinLot = 0.10; // minimal lot for trading, if calculated lot is less than minimal (it depends on the equity)
 
extern int MagicNumber = 888; // This is magic number for the expert,
                              // It opens, modify and deletes orders with this MagicNumber
                              
extern double Threshold = 0.00050;  // Threshold for the pending order sending
 
extern double SL = 0.00050; // StopLoss
extern double TP = 0.00100; // TakeProfit
extern double P  = 0.00020; // Breakout points
double H; // <<<<<<<<<<<<< g  
double Spread;//<<<<<<<<<< l
double pend;//<<<<<<<<<<<< o <<<<<<<<<<<<<<<<<<<<<<
int  ticket2; //<<<<<<<<<<< 
  int ticket1;                     //  a
                        // l


//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
 

 //bool BuyEntered  = false;
   //bool SellEntered = false;
 //int ByEntered;
 string symbol;


 //int ticket2;
//  int ticket;
  int err;
  int q=0;
 

double L=iLow(NULL,0,1);
 double H=iHigh(NULL,0,1);
double C=iClose(NULL,0,0);
double O=iOpen(NULL,0,0);
 double Spread=MarketInfo(Symbol(),MODE_SPREAD)*Point;
double FreeDepo=NormalizeDouble(AccountBalance()-AccountMargin(),Digits2Round);
double Risk=NormalizeDouble((FreeDepo*PercentOfFreeDepo/100),Digits2Round);
double Lot=NormalizeDouble(Risk/(SL/0.0001)*0.1,Digits2Round);
double Check1=H-C;
double Check2=C-L;

//===================== Lets determine lot size and risk ===================================
 
if ( Lot < MinLot )
 {
   Lot=MinLot;
 }
Comment( "\n","Acceptable risk is ",PercentOfFreeDepo, "% = ",Risk," of the free money ",FreeDepo," in lots = ",Lot);
 
//====================== checking for the orders opening
 for( q=0;q<OrdersTotal();q++)
 {
  if (OrderSelect(q, SELECT_BY_POS, MODE_TRADES) && OrderSymbol()==Symbol())
   {
// checking positions, if there are some opended orders, lets check them with the indicator
   if (OrderType()==OP_BUYSTOP)
     {
       return(0); 
     }
   if (OrderType()==OP_SELLSTOP)
     {
       return(0); 
     }
   }  
 }
 
 
//======================= condition for ORDER BUY ===============================

  

if  (Check1 >= Threshold && Check2 >= Threshold && O<H)     
  {    
    ticket1=OrderSend(Symbol(),OP_BUYSTOP,Lot,H+P+Spread,0,H+P-SL+Spread,H+P+TP+Spread,NULL,0,iTime( Symbol(), PERIOD_D1, 0 ) + 86400);
  
    if (ticket1==-1)
      {
        err=GetLastError();
        Print("error(",err,")");
      }
  }
else 
  {
    Comment("\n","Cannot set OP_BUYSTOP",
            "\n","The price is not satisfied to the market entry condition");
  }     
  
    
  
 

   
   
   //================================ condition for ORDER SELL ==================== 



if  (Check1 >= Threshold && Check2 >= Threshold && O>L)  
  {   
    
      
    ticket2=OrderSend(Symbol(),OP_SELLSTOP,Lot,L-P,0,L-P+SL,L-P-TP,NULL,0,iTime( Symbol(), PERIOD_D1, 0 ) + 86400);
  
    if (ticket2==-1)
      {
         err=GetLastError();
         Print("error(",err,")");
      }
  }
else 
  {
    Comment("\n","Cannot set OP_SELLSTOP",
            "\n","The price is not satisfied to the market entry condition");
  }     

   // pend =(H+P+Spread); // <<<<<<<<<<---------pend
 
      ClosePendingOrder( symbol);
//}
//+------------------------------------------------------------------+
// int total=OrdersTotal();
//int TotalOrderCount(string Symbol, int ticket)
//{
 }

 
 
 
 
 void ClosePendingOrder(string symbol )
   {
   OrderSelect(ticket1, SELECT_BY_TICKET);
        
   if (ticket1 > 0 && OrderType()==OP_BUY)
      {
      if ( OrderSelect(ticket2, SELECT_BY_TICKET ) && 
      OrderType() == OP_SELLLIMIT || OrderType() == OP_SELLSTOP && ticket2 > 0 ) 
         {    
         if( !OrderDelete(ticket2,Red) )    
            {
            Print("OrderDelete failed, ticket ", ticket2, " Error # ", GetLastError() );
            }    
         } 
      }
   return;
   }
 

 do i need a loop for the op_xxx?

 
er007:


the expert open every new candle 2 pending orders one at the high of the previous candle , and one at low of the previous candle, no meter what time frame that is the basic logic ,every new candle 2 pending and pending from the last bar expire. and so on.

That may be what you intended but that isn't what your code will always produce.  You have the following conditions:

   if  (Check1 >= Threshold && Check2 >= Threshold && O < H)     
      {    
      ticket = OrderSend(Symbol(), OP_BUYSTOP, Lot, H + P + Spread, 0, H + P - SL + Spread, H + P + TP + Spread, NULL, 0, iTime( Symbol(), PERIOD_D1, 0 ) + 86400);


   
   //================================ condition for ORDER SELL ==================== 

   if(Check1 >= Threshold && Check2 >= Threshold && O > L)  
      {   
      ticket = OrderSend(Symbol(), OP_SELLSTOP, Lot, L - P, 0, L - P + SL, L - P - TP, NULL,0,iTime( Symbol(), PERIOD_D1, 0 ) + 86400);

The Check1 and Check2 tests are common to Buy and Sell pending orders  but you then have a test of Open[0] vs High[1] and Open[0] vs Low[1]  so, for example,  if Bar 1 closes at it's High and Bar 0 opens at that same price or higher you may get a OP_SELLSTOP but not an OP_BUYSTOP.  

 

Look at the code,  tell me I am wrong ? ?

 
er007:


the code:

 do i need a loop for the op_xxx?

 

er007:


the code:

 do i need a loop for the op_xxx?

thise is my fifth day of mql programming(i been doing programming a bit on c before but no more than that ,so i am very happy to get help from pro like you and RaptorUK and other)

 

many thanks !!! 

 

 
RaptorUK:

That may be what you intended but that isn't what your code will always produce.  You have the following conditions:

The Check1 and Check2 tests are common to Buy and Sell pending orders  but you then have a test of Open[0] vs High[1] and Open[0] vs Low[1]  so, for example,  if Bar 1 closes at it's High and Bar 0 opens at that same price or higher you may get a OP_SELLSTOP but not an OP_BUYSTOP.  

 

Look at the code,  tell me I am wrong ? ?

 


thise is true (uncommon but you absolutely correct ) in thise case i gess there will be no long trade. and if it form an outside bar which i do not what to trade,

thise is a problem.but i do not see how it effect the error 4108, one of the orders should be trigger and the func should close the other one.

 

i gess programming isn't a walking in the park , but its very interesting for shore (-;  and i am just in the beginning , mey by same day ill be pro like you and the other members here, until than ill try learn .

 many thank!!!

Reason: