ordersend -1 4105

 

I do not understand the -1 and 4105 error when opening an order.  Can someone help?


extern double Lots=0.01;                // Strictly set amount of lots

extern int    movingperiodfastm5=12;                    
extern int    movingperiodslowm5=26;                     
extern int    movingshiftm5=0;                     
extern int    movingperiodsignalm5=9;

bool Work=true;                               // EA will work
string Symb;                                  // Security name

//--------------------------------------------------------------- 2 --
int start()
  {
   int
   Number_of_open_orders,
   tick,                                    // Order number
   error=GetLastError(); 
 
   double
   Lots,
   SL,                // SL of a selected order
   TP,              // TP of a selected order
   macdmainm5,
   macdmainpreviousm5;
 
   bool
   Opn_B,                                // Criterion for opening Buy
   Opn_S;
  
//--------------------------------------------------------------- 3 --
   // Preliminary processing
   if(Bars < 100)                             // Not enough bars
     {
      Alert("Not enough bars in the window. EA does not work.");
      return;                                 // Exit start()
     }
   if(Work==false)                            // Critical error
     {
      Alert("Critical error. EA does not work.");
      return;                                 // Exit start()
     } 
//--------------------------------------------------------------- 4 --
   // Orders accounting
   Symb=Symbol();                               // Symbol EA is attached too
   Number_of_open_orders=OrdersTotal();         // Amount of open/pending orders

//--------------------------------------------------------------- 5 --
   // Trading criteria

   macdmainm5=iMACD(NULL,PERIOD_M5,movingperiodfastm5,movingperiodslowm5,movingperiodsignalm5,PRICE_CLOSE,MODE_MAIN,1);
   macdmainpreviousm5=iMACD(NULL,PERIOD_M5,movingperiodfastm5,movingperiodslowm5,movingperiodsignalm5,PRICE_CLOSE,MODE_MAIN,2);


//-----------------------------------------------------
//  Opening orders

//    Buy Orders                                                           
      if(
             Number_of_open_orders==0
          && macdmainm5>macdmainpreviousm5
        )            
           {
            Opn_B=true;                                // Criterion for opening Buy
           }
         
//    Sell orders
         if(
             Number_of_open_orders==0
          && macdmainm5<macdmainpreviousm5
           )
          {
             Opn_S=true;                                // Criterion for opening Sell
          }
 //--------------------------------------------------------------- 8 --
     //opening buy order
      if (Opn_B==true && OrdersTotal()==0)                                          // No open orders +
        {                                                                           // criterion for opening Buy is true
          SL=Bid-(50*Point);
          TP=Ask+(50*Point);                                                        // Calculating TP of opened
          Alert("Attempting to open Buy. Waiting for response..");
         tick=OrderSend(Symb,OP_BUY,Lots,Ask,2,SL,TP,NULL,0,0,Blue);//tp//Opening Buy
          Alert("tick ",tick);
         if (OrderTicket() >= 1)                                                      // Success :)
           {
            Alert ("Opened order Buy ",OrderTicket());
           }
         if (tick<= 0)                                                                // Success :)
           {
            Alert ("buy error ",error);
           }

         if (Fun_Error(GetLastError())==1)                                            // Processing errors
                {
                  Alert("Error - open buy");
                }
        }

     // opening sell order
      if (Opn_S==true && OrdersTotal()==0)                                          // No opened orders +
        {                                                                           // criterion for opening Sell
           SL=Ask+(50*Point);
           TP=Bid-(50*Point);                                                      // Calculating TP of opened
           Alert("Attempting to open Sell. Waiting for response..");
           tick=OrderSend(Symb,OP_SELL,Lots,Bid,2,SL,TP,NULL,0,0,Red);
           Alert("tick ",tick);
         if (OrderTicket() >= 1)                        // Success :)
           {
            Alert ("Opened order Sell ",OrderTicket());
           }
         if (tick<= 0)                                                                    // Success :)
           {
            error=GetLastError();
            Alert ("sell error ",error);
           }
         if (Fun_Error(GetLastError())==1)      // Processing errors
           {
            Alert("Error - open sell");
           }
        }

   return;                                      // Exit start()
  }
//-------------------------------------------------------------- 10 --
int Fun_Error(int Error)                        // Function of processing errors
  {
   switch(Error)
     {                                          // Not crucial errors           
      case 130:Alert(Symb,OrderSymbol(),"130-Invalid stops.");
         return(0);                             // Exit the function
      default: Alert("Error occurred: ",Error);  // Other variants  
         return(0);                             // Exit the function
     }
  }
//-------------------------------------------------------------- 11 --

 
spillstuff:

I do not understand the -1 and 4105 error when opening an order.  Can someone help?

...

No one will read and understand your code if you post it like that. Please edit your code using SRC button. You can begin editing after you click edit link on right top corner of your post

 

 
extern double Lots=0.01;                // Strictly set amount of lots

extern int    movingperiodfastm5=12;                     
extern int    movingperiodslowm5=26;                      
extern int    movingshiftm5=0;                      
extern int    movingperiodsignalm5=9;

bool Work=true;                               // EA will work
string Symb;                                  // Security name

//--------------------------------------------------------------- 2 --
int start()
  {
   int
   Number_of_open_orders,
   tick,                                    // Order number
   error=GetLastError();  
  
   double
   Lots,
   SL,                // SL of a selected order
   TP,              // TP of a selected order
   macdmainm5,
   macdmainpreviousm5;
 
   bool
   Opn_B,                                // Criterion for opening Buy
   Opn_S; 
   
//--------------------------------------------------------------- 3 --
   // Preliminary processing
   if(Bars < 100)                             // Not enough bars
     {
      Alert("Not enough bars in the window. EA does not work.");
      return;                                 // Exit start()
     }
   if(Work==false)                            // Critical error
     {
      Alert("Critical error. EA does not work.");
      return;                                 // Exit start()
     }  
//--------------------------------------------------------------- 4 --
   // Orders accounting
   Symb=Symbol();                               // Symbol EA is attached too
   Number_of_open_orders=OrdersTotal();         // Amount of open/pending orders

//--------------------------------------------------------------- 5 --
   // Trading criteria

   macdmainm5=iMACD(NULL,PERIOD_M5,movingperiodfastm5,movingperiodslowm5,movingperiodsignalm5,PRICE_CLOSE,MODE_MAIN,1);
   macdmainpreviousm5=iMACD(NULL,PERIOD_M5,movingperiodfastm5,movingperiodslowm5,movingperiodsignalm5,PRICE_CLOSE,MODE_MAIN,2);


//-----------------------------------------------------
//  Opening orders

//    Buy Orders                                                            
      if(
             Number_of_open_orders==0 
          && macdmainm5>macdmainpreviousm5
        )             
           { 
            Opn_B=true;                                // Criterion for opening Buy
           }
          
//    Sell orders
         if(
             Number_of_open_orders==0 
          && macdmainm5<macdmainpreviousm5
           )
          { 
             Opn_S=true;                                // Criterion for opening Sell
          } 
 //--------------------------------------------------------------- 8 --
     //opening buy order
      if (Opn_B==true && OrdersTotal()==0)                                          // No open orders +
        {                                                                           // criterion for opening Buy is true
          SL=Bid-(50*Point);
          TP=Ask+(50*Point);                                                        // Calculating TP of opened
          Alert("Attempting to open Buy. Waiting for response..");
         tick=OrderSend(Symb,OP_BUY,Lots,Ask,2,SL,TP,NULL,0,0,Blue);//tp//Opening Buy
          Alert("tick ",tick);
         if (OrderTicket() >= 1)                                                      // Success :)
           {
            Alert ("Opened order Buy ",OrderTicket());
           }
         if (tick<= 0)                                                                // Success :)
           {
            Alert ("buy error ",error);
           }

         if (Fun_Error(GetLastError())==1)                                            // Processing errors
                {
                  Alert("Error - open buy");
                }
        }

     // opening sell order
      if (Opn_S==true && OrdersTotal()==0)                                          // No opened orders +
        {                                                                           // criterion for opening Sell
           SL=Ask+(50*Point);
           TP=Bid-(50*Point);                                                      // Calculating TP of opened
           Alert("Attempting to open Sell. Waiting for response..");
           tick=OrderSend(Symb,OP_SELL,Lots,Bid,2,SL,TP,NULL,0,0,Red);
           Alert("tick ",tick);
         if (OrderTicket() >= 1)                        // Success :)
           {
            Alert ("Opened order Sell ",OrderTicket());
           }
         if (tick<= 0)                                                                    // Success :)
           {
            error=GetLastError();
            Alert ("sell error ",error);
           }
         if (Fun_Error(GetLastError())==1)      // Processing errors
           {
            Alert("Error - open sell");
           }
        }

   return;                                      // Exit start()
  }
//-------------------------------------------------------------- 10 --
int Fun_Error(int Error)                        // Function of processing errors
  {
   switch(Error)
     {                                          // Not crucial errors            
      case 130:Alert(Symb,OrderSymbol(),"130-Invalid stops.");
         return(0);                             // Exit the function
      default: Alert("Error occurred: ",Error);  // Other variants   
         return(0);                             // Exit the function
     }
  }
//-------------------------------------------------------------- 11 --
 
I simplified the code.

Will only work if SL and TP = 0.  Why?

Why the 4105 error?

Any help appreciated.


   extern string Symb;                                  // Security name  
//--------------------------------------------------------------- 2 --
int start()
  {
   int
   tick,                                    // Order number
   error=GetLastError();  

   Symb=Symbol();                               // Symbol EA is attached too
//--------------------------------------------------------------- 5 --
double    SL=0;
double    TP=0;
          tick=OrderSend(Symb,OP_BUY,0.01,Ask,5,SL,TP,NULL,0,0,Blue);

         Alert("tick ",tick);

         if (tick >= 1)                                                      // Success :)
           {
            Alert ("Opened order Buy ",OrderTicket());
           }
         if (tick< 0)                                                                // Success :)
           {
            Alert ("buy error ",error);
           }

         if (Fun_Error(GetLastError())==1)                                            // Processing errors
                {
                  Alert("Error - open buy ");
                }

   return;                                      // Exit start()
  }
//-------------------------------------------------------------- 10 --
int Fun_Error(int Error)                        // Function of processing errors
  {
   switch(Error)
     {                                          // Not crucial errors            
      case 130:Alert(Symb," 130-Invalid stops.");
         return(0);                             // Exit the function
      default: Alert("Error occurred: ",Error);  // Other variants   
         return(0);                             // Exit the function
     }
  }

 
spillstuff:

Will only work if SL and TP = 0.  Why?

Why the 4105 error?

Read this:  ECN

4105 error caused by which function ?  did you look up what error 4105 means ?


You need to sort out your error handling . . . 

         if (tick< 0)                                                                // Success :)
           {
            Alert ("buy error ",error);     //  where does the value of error come from ?
           }

 you will be reporting the wrong error . . .

 

Before you can use OrderTicket()  you must . . .   "Note: The order must be previously selected by the OrderSelect() function."

if (Fun_Error(GetLastError())==1)                                            // Processing errors
                {
                  Alert("Error - open buy ");
                }

 Your function  Fun_Error() can never return 1 so why on earth are you checking for that fact ?  if you are going to use other people's code you need to understand it before you try to use it . . .

 
          tick=OrderSend(Symb,OP_BUY,0.01,Ask,5,SL,TP,NULL,0,0,Blue);
         Alert("tick ",tick);
         if (tick >= 1)                                                      // Success :)
           {
            Alert ("Opened order Buy ",OrderTicket());
           }
         if (tick< 0)                                                                // Success :)
           {
            Alert ("buy error ",error);
  1. What is the value of error.
  2. No orderSelect before OrderTicket
  3. Not adjusting for 4/5 digit brokers (slippage)
  4. Not adjusting for ECN brokers
 

Thanks for the comments, especially the ecn clue.

The fun_error, and ordersend code are from the metatrader instruction book, so I'm learning.   

Reason: