Why "0" instead of OrderTicket() ?

 

Hi all, could you explain where is the error?

For the first order the expert returns "0" instead of the ticket.

total = OrdersTotal();  
    
  for(cnt=1;cnt<=total;cnt++)
     {    
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
     
     Print(OrderTicket());
          
     } 
 
for(cnt=0;cnt<total;cnt++)
 

Ok thank you, is this code correct? I have "abnormal termination" in the expert tab...

int Old_Ticket = "123" ;
            
             for(cnt=0;cnt<OrdersHistoryTotal();cnt++) 
                {
                OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY) ;
                if(OrderTicket()=="123")
                  {
                  int Old_Barre_Long_1 = GlobalVariableGet(StringConcatenate("Barre_Long_1_",OrderTicket()) ) ;
                  } 
                // else
                  // Print("OrderSelect returned the error of ",GetLastError());   
                } 
 
if(OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY) && (OrderTicket()==123))
 

I'm trying to recall a Global Variable from the ticket in the History related to the current ticket.

The problem is that every time I compile the platform crashes with "abnormal termination". I don't find the loop in the code!

// find the old ticket "from"
int Old_Ticket = StringSubstr(OrderComment(),StringLen(OrderComment())-8,8) ; 

// search for the ticker in the History
for(cnt=0;cnt<OrdersHistoryTotal();cnt++) 
   {
   if(OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY)  && OrderTicket()==Old_Ticket)
     {
     int Old_Barre_Long_1 = GlobalVariableGet(StringConcatenate("Barre_Long_1_",OrderTicket()) )       
     } 
   } 
 
  1. There is no point in going through history to find a ticket when you know the ticket number. Just select by ticket.
  2. There is no point selecting by ticket when all you want to do is add a prefix to it and do a GV get. Just do it.
    int Old_Ticket = StringSubstr(OrderComment(),StringLen(OrderComment())-8,8) ; 
    int Old_Barre_Long_1 = GlobalVariableGet(StringConcatenate("Barre_Long_1_",Old_Ticket) );
    
  3. Don't hard code numbers (8). Your code breaks when the broker's ticket numbers roll over to a billion (9.)
  4. Not a good idea to use comments, brokers can change comments, including complete replacement. Not all brokers put old ticket numbers on partial close. You already know the ticket number, remember it before the partial close.

  5. Don't double post! Don't triple post!
              General rules and best pratices of the Forum. - General - MQL5 programming forum
 

Sorry for post duplication, I will pay more attention.

I don't know the ticket number.

I need to find the "old" ticket starting from the opened positions.

When I trade is modified, broker changes the comment in "from #...." and the Global Variable is lost because I create it with OrderTicket() function (one global variable for every position).

So I need to search for the old ticket in the comment and then I need to recall the Global Variable in the below way, or do you have another way to do this?

I'm sure the "Old_Ticket" int is correct, I verified it with Print() function. The problem is in the for(cnt=0;cnt<OrdersHistoryTotal();cnt++) loop. 

Thank you! 


total = OrdersTotal();
    
for(cnt=0;cnt<total;cnt++)
     {
     OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES) ;
     
   
             if (GlobalVariableGet(StringConcatenate("Barre_Long_1_",OrderTicket()))==0 && OrderComment()!="Long 1")
                {
                int Old_Ticket = StringSubstr(OrderComment(),StringLen(OrderComment())-8,8) ; 
                
                for(cnt=0;cnt<OrdersHistoryTotal();cnt++) 
                  {
                  if(OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY) && OrderTicket()==Old_Ticket)
                    {
                    int Old_Barre_Long_1 = GlobalVariableGet(StringConcatenate("Barre_Long_1_",OrderTicket()) ) ;
                    } 
                  else
                    Print("OrderSelect returned the error of ",GetLastError());   
                  }
                } 
   }
 

May be I got it.

In the history tab I need to select "all history", not only today.

I seem in this way the for loop works properly.

 

I need to create a Global Variable using OrderTicket() in this way.

When is the right time to write this code?

I think that if I write this code just after the order send, the ticket is not known so the Global Variable doesn't work.

I tried but I have abnormal termination.

Take_Profit_Short_1   = Entry_Price - (Distanza_Short*Ratio_1) ;

string Profit_Short_1 = StringConcatenate("Take_Profit_Short_1_",OrderTicket());

GlobalVariableSet(Profit_Short_1,Take_Profit_Short_1);  

 
Alberto Tortella:

I need to create a Global Variable using OrderTicket() in this way.

When is the right time to write this code?

I think that if I write this code just after the order send, the ticket is not known so the Global Variable doesn't work.

I tried but I have abnormal termination.

  1. Global variables are for interprocess communication (EA to EA.) Also as a quickie persistent storage. Why do you think you need a GV?

  2. When you need it.

  3. Perhaps you should read the manual. What does OrderSend return? Why aren't you checking it?
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  4. Do you really expect an answer? We can't see your broken code. There are no mind readers here and our crystal balls are cracked.
 
whroeder1:
  1. Global variables are for interprocess communication (EA to EA.) Also as a quickie persistent storage. Why do you think you need a GV


If I turn the platform off during the weekend I will loose the value of my variables.

I seem the only way to solve this is to use the GV.

Reason: