Help me to debug my code of Checking a ticket in orders file CSV

 

Hi Everyone!

I'm trying to make my EA checking order exist or not by order's ticket as the code below:

bool orderexist = false;	// Global variable
bool BotStatus = true;		// Global variable

void OnTick()
{   
if(BotStatus)
     {
      if(CheckOrderExist("orders.csv",351851389))
         printf("Order is opened already...");
      else
         printf("not found...");
     } //End of Slave Working Mode
  } // End of OnTick


//+------------------------------------------------------------------+
//|Checking ticket in file                                           |
//+------------------------------------------------------------------+
bool CheckOrderExist(string _filename,int _ticket)
  {
   bool orderexist=false;
   int handle = FileOpen(_filename,FILE_COMMON|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_READ|FILE_CSV);
   if(handle!=INVALID_HANDLE)
     {
      while(!FileIsEnding(handle))
        {
        int a = FileReadInteger(handle);
         if(_ticket==a)
           {
            orderexist=true;
            FileClose(handle);
            break;
           }
         else
            return orderexist;
        }
     }
   else
      printf("Can not read the file for checking. Error: ",GetLastError());
   return orderexist;
  }

Result: No error in compiling, no error by GetLastError(), and no result as i want. Its seems to be loop or stuck somewhere i dont know, it makes MT4 working slow nearly crashed.

Please anyone tell me what wrong i did.

Thank you so much!

p/s: The orders file attached.

Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
  • www.mql5.com
order_calc_margin - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Files:
orders.csv  1 kb
 
If the first integer read is not the search parameter, you return false instead of continuing to search.
 
William Roeder #:
If the first integer read is not the search parameter, you return false instead of continuing to search.

But i've put the search parameter - "ticket" number at the begin of file and the line also.

I just modify the code a little bit like below:

bool CheckOrderExist(string _filename,int _ticket)
  {
   bool orderexist=false;
   int handle = FileOpen(_filename,FILE_COMMON|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_READ|FILE_CSV);
   if(handle!=INVALID_HANDLE)
     {
      while(!FileIsEnding(handle))
        {
         int a = FileReadInteger(handle);
         if(_ticket==a)
           {
            orderexist=true;
            FileClose(handle);
            return orderexist;
           }
         else
            orderexist=false;
         printf(a); // >>>Check the value of a but nothing
         break;
        }
     }
   else
      printf("Can not read the file for checking. Error: ",GetLastError());
   return orderexist;
  }

But the result the same: ***

Can you clarify more for me pls!
 
What part of “continuing to search”  was unclear to you?
 
William Roeder #:
What part of “continuing to search”  was unclear to you?

Yes please. I modified my code as what i understood, not sure that's what you mean.

 
What part of “If the first integer read is not the search parameter, you return false instead of continuing to search”  is unclear to you? Run in the debugger and find out.
 
Tran Ngoc Quang # :

Yes please. I modified my code as what i understood, not sure that's what you mean.

You can insert a picture into a post (the button Image) or attach a picture to your message (button Attach file))

 
bool CheckOrderExist(string _filename,int _ticket)
  {
   bool orderexist=false;
   int handle = FileOpen(_filename,FILE_COMMON|FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_READ|FILE_CSV);
   if(handle!=INVALID_HANDLE)
     {
      while(!FileIsEnding(handle))
        {
         int a = FileReadInteger(handle);
         if(_ticket==a)
           {
            orderexist=true;
            FileClose(handle);
            return orderexist;
           }
        }
     }
   else
      printf("Can not read the file for checking. Error: ",GetLastError());
   FileClose(handle);
   return orderexist;
  }
 
Alexandre Borela #:

Hi Bro!

This code's result is still the same. Can you make clear for me please!

Thanks!

 
Vladimir Karputov #:

You can insert a picture into a post (the button ) or attach a picture to your message (button )

Yes. Noted.

Thanks!

 
William Roeder #:
What part of “If the first integer read is not the search parameter, you return false instead of continuing to search”  is unclear to you? Run in the debugger and find out.

Yes bro. I run in debug mode a saw that, the function is can not get the value by the FileReadInteger() so can not meet the condition of If(). But i down know why.

Can you help me this bro?

Reason: