Missing something obvious/

 

I am using the code below to check a log file. Most of the tickets I would expect to have been closed sometime ago and I would therefore expect the code to take the 'else' route but the FileWrite suggestions otherwise

      while (FileIsEnding(My_FileClose_Handle) == FALSE )
      {  temp_string = FileReadString(My_FileClose_Handle);
         error = GetLastError();
         i1++;
         problem_string[i1]   =temp_string;
         problems[i1][0]      = StrToInteger( StringSubstr(temp_string,55, 6));      // Magic Number
         problems[i1][1]      = StrToInteger( StringSubstr(temp_string,33, 9));      // Ticket number
         ticket_nos           = StrToInteger( StringSubstr(temp_string,33, 9));      // Ticket number
         FileWrite(My_Handle,"IC2 "+" error: "+error+" "+ErrorDescription(error)+" My_FileClose_Handle ="+My_FileClose_Handle+
          " problems[i1][1] = "+problems[i1][1]+" problems[i1][0] = "+problems[i1][0]);
         if ( OrderSelect(ticket_nos,SELECT_BY_TICKET) ) 
         {  OrderClose(OrderTicket(), OrderLots(), OrderClosePrice() ,My_Slippage,Green);
            FileWrite(My_Handle,"CT ticket_nos = "+ticket_nos);
         } else
         {  problems[i1][1]=0;
            FileWrite(My_Handle,"P=0,ticket_nos = "+ticket_nos);
         }
      }


A section of the output is as below:-

CT ticket_nos = 282712970
IC2 error: 4108 invalid ticket My_FileClose_Handle =2 problems[i1][1] = 282712970 problems[i1][0] = 988519
CT ticket_nos = 282712970
IC2 error: 4108 invalid ticket My_FileClose_Handle =2 problems[i1][1] = 282765112 problems[i1][0] = 156253
CT ticket_nos = 282765112

I figure I am missing the obvious but why does the OrderSelect appear to succeed (goes down the CloseTicket route) and then obviously have an invalid ticket. The alternative route (i.e. the else) doesn't get selected.

 

I don't understand something . . . . how can you get the error that relates to the OrderSelect . . . before you perform the OrderSelect ? the first time through the while loop what error does error contain ? what about the last run through the loop ? what if there is an error with the final OrderSelect, how is that captured ?

The OrderSelect will work . . . why shouldn't it ? you can select a closed order just the same way as an open order when you use the ticket number . . . but you may have problems trying to close an order that is already closed.

I think I would do something like this . . . .

while (FileIsEnding(My_FileClose_Handle) == FALSE )
      {  temp_string = FileReadString(My_FileClose_Handle);
         
         i1++;
         problem_string[i1]   =temp_string;
         problems[i1][0]      = StrToInteger( StringSubstr(temp_string,55, 6));      // Magic Number
         problems[i1][1]      = StrToInteger( StringSubstr(temp_string,33, 9));      // Ticket number
         ticket_nos           = StrToInteger( StringSubstr(temp_string,33, 9));      // Ticket number
         
         if ( OrderSelect(ticket_nos,SELECT_BY_TICKET) ) 
            {  
             if(!OrderClose(OrderTicket(), OrderLots(), OrderClosePrice() ,My_Slippage,Green))
                {
                error = GetLastError();
                Print("OredrClose failed, Order: ", ticket_nos, " Error: ", error );
                FileWrite(My_Handle,"IC2 "+" error: "+error+" "+ErrorDescription(error)+" My_FileClose_Handle ="+My_FileClose_Handle+
                " problems[i1][1] = "+problems[i1][1]+" problems[i1][0] = "+problems[i1][0]);
                } 
            FileWrite(My_Handle,"CT ticket_nos = "+ticket_nos);
            } 
         else
            {  
            problems[i1][1]=0;
            error = GetLastError();
            Print("OrderSelect failed, Order: ", ticket_nos, " Error: ", error );
            FileWrite(My_Handle,"IC2 "+" error: "+error+" "+ErrorDescription(error)+" My_FileClose_Handle ="+My_FileClose_Handle+
            " problems[i1][1] = "+problems[i1][1]+" problems[i1][0] = "+problems[i1][0]);            
            FileWrite(My_Handle,"P=0,ticket_nos = "+ticket_nos);
            }
      }
 
 while (FileIsEnding(My_FileClose_Handle) == FALSE )
      {  temp_string = FileReadString(My_FileClose_Handle);
After reading the last real line in the file, FileIsEnding() is still false but FileReadString returns a null string. Add:
if (temp_string == "") break;
 
RaptorUK:

I don't understand something . . . . how can you get the error that relates to the OrderSelect . . . before you perform the OrderSelect ? the first time through the while loop what error does error contain ? what about the last run through the loop ? what if there is an error with the final OrderSelect, how is that captured ?

I think I would do something like this . . . .

the extract of results was a just a section - rather than start to finish - sorry if it provided the wrong information.


"....The OrderSelect will work . . . why shouldn't it ? you can select a closed order just the same way as an open order when you use the ticket number . . . but you may have problems trying to close an order that is already closed...."

I hadn't quite realised that -thank you. I have used the following in the past to read my current orders into a matrix:-


   for (int i=0; i<OrdersTotal(); i++) // For all orders in the terminal
   {  if((OrderSelect(i, SELECT_BY_POS)==true) && (OrderSymbol()==Symbol()) )

etc.

This only appears to 'list' current orders. If I want to access closed or past orders then I have used:

for (i=0; i< OrdersHistoryTotal(); i++) // For all orders in the terminal
   {  if((OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==true) && (OrderSymbol()==Symbol()) ) 
 
WHRoeder:
After reading the last real line in the file, FileIsEnding() is still false but FileReadString returns a null string. Add:

Thanks - got it


Thanks both ofyou I'll play with above.


I use the above in an 'integrity' routine to check any orders which may not have been closed correctly

 
peterhw1:

This only appears to 'list' current orders. If I want to access closed or past orders then I have used:

Correct . . . but SELECT_BY_TICKET reads from the trades pool and the history pool . . . SELECT_BY_POS reads from the orders pool by default or the history pool if MODE_HISTORY is specified . . . OrderSelect()
 
RaptorUK:
Correct . . . but SELECT_BY_TICKET reads from the trades pool and the history pool . . . SELECT_BY_POS reads from the orders pool by default or the history pool if MODE_HISTORY is specified . . . OrderSelect()


many thanks. It had never been a problem before so the fact I didn't understand correctly had not 'mattered too much"!! Thanks again
Reason: