Error 0 from OrderSelect

To add comments, please log in or register
Pableeto
22
Pableeto  
Hi guys, I am totally new to programming and I am getting crazy of this error. It always gives me Error 0 with "ticket_1". I have no freaking idea why
if(ticket_0==0 || ticket_1==0 || ticket_2==0)
{
            SL_0 = Ask - 200*_Point;
            TP_0 = Ask + 100*_Point;
            ticket_0 = OrderSend(symbol, OP_BUY, Vol, Ask, slippage, SL_0, TP_0, comment, magic, expiration, arrow_color);



            OP_1   = TP_0;                                                       
            OP_1   = NormalizeDouble(OP_1, 4);
            SL_1   = OP_1 - (200 * Point);
            TP_1   = OP_1 + (100 * Point);
            ticket_1      = OrderSend(symbol, cmd_1, Vol, OP_1, slippage, SL_1, TP_1, comment, magic, expiration, arrow_color);
            if(ticket_1>0)
            {
               Print("Wysłano pierwsze zlecenie ticket_1");
            }
            else
            {
               Print("Nie udało się wysłać pierwszego zlecenia ticket_1. Błąd = ",GetLastError());
            }
            
            
            OP_2   = SL_0; 
            OP_2   = NormalizeDouble(OP_2, 4);
            SL_2   = OP_2 + (200 * Point);
            TP_2   = OP_2 - (100 * Point);                                                
            ticket_2=OrderSend(symbol,cmd_2,Vol,OP_2,slippage,SL_2,TP_2,comment,magic,expiration,arrow_color);
            if(ticket_2>0)
            {
               Print("Wysłano pierwsze zlecenie ticket_2");
            }
            else
            {
               Print("Nie udało się wysłać pierwszego zlecenia ticket_2. Błąd = ",GetLastError());
            }
}
else
{
Print("Wysłano już pierwsze zlecenia ticket_0 ticket_1 i ticket_2");
}            
            
         
       
           if(ticket_0<=0)                
             {
             }
             else               
             {
                
                if(OrderSelect(ticket_0, SELECT_BY_TICKET)!=true)
                {
                   Print("Nie udało się pobrać zlecenie ticket_0 o numerze: ", ticket_0, "Błąd: ", GetLastError());
                }
                else
                {
                   
                   Vol_0       = OrderLots();                 
                   OCT_0       = OrderCloseTime();           
                   if(OCT_0<=0)                                      
                   {
                   }
                   else
                   {
                   
                     bool OS_1   = OrderSelect(ticket_1, SELECT_BY_TICKET);
                     if(OS_1 == false)
                     {
                        Print("Nie udało się pobrać zlecenia ticket_1 na etapie sprawdzania czy się uruchomiło. Ticket: ", ticket_1, "Błąd: ", GetLastError());
                     }
                     else
                     {
Pableeto
22
Pableeto  
Ok, I understand that there is no error. But why doesn't it selecting the bloody order?
William Roeder
19170
William Roeder  
  1. OrderSelect returns a bool (true or false) it does not return 0.
  2. Where did you declare ticket_x? Show all the relevant code.
  3. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.
Pableeto
22
Pableeto  
input  double       buy_price;                //pierwsza cena zakupu
input  double       sell_price;               //pierwsza cena sprzedaży               


//--- inicjacje zmiennych globlnych
double SL   = 2 * krok * _Point;           
double TP   = krok * _Point;              

int    ticket_01   = 0;                          
double SL_01       = buy_price - SL;             
double TP_01       = buy_price + TP;             
double OP_01       = buy_price;                 

int    ticket_02   = 0;                        
double SL_02       = sell_price + SL;
double TP_02       = sell_price - TP;
double OP_02       = sell_price;                    


int      ticket_0    = 0;                    //ticket number zlecenia otwartego
int      OT_0;                               //Order Type
double   Vol_0       = Vol;
datetime OCT_0;                              //Order Close Time
double   OP_0;                               
double   SL_0;                               
double   TP_0;




int    ticket_1    = 0;                    
int    OT_1;
double OP_1;
double SL_1        = OP_1 - SL;
double TP_1        = OP_1 + TP;
double Vol_1       = Vol;


int    ticket_2    = 0;                    
int    OT_2;                               // Order Type
double OP_2;                               //Open Price - cena otwarcia
double SL_2       = OP_2 + SL;
double TP_2       = OP_2 - TP;
double Vol_2      = Vol;
Pableeto
22
Pableeto  

These are my declarations.

Thank You very much for Your advices, I will try to follow them, and I really appreciate Your help with it.

Your logical approach that if(bool) is enough is very reasonable, but I just sow a lot of instances of using if(bool==true). But You are right, it's stupid.

William Roeder
19170
William Roeder  
  1. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. Why did you post your MT4 question in the Root / MT5 EA 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.

  3. double SL   = 2 * krok * _Point;           
    double TP   = krok * _Point;              
    ⋮ ^v order dependent
    double SL_01       = buy_price - SL;             
    double TP_01       = buy_price + TP;             
    double OP_01       = buy_price;                 
    Global and static variables work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.
    2. They don't update unless you assign to them.
    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5 (or MT4 with strict which you should always use.)

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and 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.
    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - Inflation - MQL4 programming forum

  4. Always use strict. Fixing the warnings will save you hours of debugging.

  5. Use the debugger or print out your variables, including _LastError and prices and find out why.

  6.             ticket_1      = OrderSend(symbol, cmd_1, Vol, OP_1, slippage, SL_1, TP_1, comment, magic, expiration, arrow_color);
                if(ticket_1>0)
                {
                   Print("Wysłano pierwsze zlecenie ticket_1");
                }
                else
                {
                   Print("Nie udało się wysłać pierwszego zlecenia ticket_1. Błąd = ",GetLastError());
                }
    
    In your other post you call GetLastError after multiple OrderSends which is useless as the second call invalidates the error of the first.
Pableeto
22
Pableeto  
William Roeder:
  1. Don't double post! You already had this thread open.
              General rules and best pratices of the Forum. - General - MQL5 programming forum

  2. Why did you post your MT4 question in the Root / MT5 EA 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.

  3. Global and static variables work exactly the same way in MT4/MT5/C/C++.
    1. They are initialized once on program load.
    2. They don't update unless you assign to them.
    3. In C/C++ you can only initialize them with constants, and they default to zero. In MTx you should only initialize them with constants. There is no default in MT5 (or MT4 with strict which you should always use.)

      MT4/MT5 actually compiles with non-constants, but the order that they are initialized is unspecified and 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.
    4. Unlike indicators, EAs are not reloaded on chart change so you must reinitialize them, if necessary.
                external static variable - Inflation - MQL4 programming forum
  4. OP_1 has no value. Always use strict. Fixing the warnings will save you hours of debugging.

  5. Use the debugger or print out your variables, including _LastError and prices and find out why.
I didn't want to double my post. I just added next comment. I will read all the rules not make suck a f...k up next time.

Thank very much for Your time and answers. I will work though all Your advices and see what happens
To add comments, please log in or register