Would appreciate help with this file reading code

 

Hello,


First of all, although I'm not totally new to programming, I am quite rustythough and very new to mql4. With the help of this site I've tried to piece together an ea that can take trades from an external source. That external source drops a txt file in the experts\files folder, this bit I got working (it's not in mql4).


Now what should happen with the ea: the file contains a "1" for long, or "2" for short. Before it can take a trade it checks to see if the maximum open orders are not exceeded or the minimum account balance is present (both user defined). I couldn't find the mql4 equivalent of AND, so I used 2 "if's" instead. Once the file is read it's deleted.


Anyway, the code makes it through the compiler and doesn't crash my mt4 (like my first attempt) but it doesn't do anything, even with the "order.txt" file sitting in the expert/files folder.


Any advice would be VERY appreciated!


extern double TakeProfit = 10;

extern double Lots = 0.01;
extern double StopLoss = 5;
extern int MaxOpenOrders = 100;
extern double MinBalance = 1500;

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
int ret, ticket, openorders, call, trade;
double margin;

call=FileOpen("order.txt", FILE_READ);
if (call > 0)
{
trade = FileReadInteger(call);
FileDelete("order.txt");
}


openorders = OrdersTotal();
if (openorders <= MaxOpenOrders)
{
if(AccountBalance() > MinBalance)
{
//go long
if (trade==1)
{
//in excel sheet1, cell A1 has symbol, B1 buy/sell,
ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,Ask-StopLoss*Point,Ask+TakeProfit*Point,"FirstGo",16384,0,Green);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
}
else Print("Error opening BUY order : ",GetLastError());
return(0);
}
// go short
if (trade == 2)
{
ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,Bid+StopLoss*Point,Bid-TakeProfit*Point,"FistGo",16384,0,Red);
if(ticket>0)
{
if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
}
else Print("Error opening SELL order : ",GetLastError());
return(0);

}

}
}
return(0);
}
 

Haven't had a chance to study your code yet but a few tips.

If you're running this in the strategy tester, the file need to be in tester/files rather than experts/files.

Add some error checking around the file read function. And use some print calls to check your logic flow and variables content.

Also, I wrote some file read/write functions for someone a while back:

https://www.mql5.com/en/forum/123129


CB

 

Thanks for the tips. I try to forward test it, so I think I need the file in experts/files. I will try to use the print calls you suggest.


MM

 

Yep - experts/files for forward testing.


CB

 

Thank you! Your advice with the print statements gave me more insight of what was going on.


I got most of it working apart from the part:

FileDelete("order.txt");


I get "cannot delete file". Perhaps I should open it in a different mode? In my current version I've opened it with:

call=FileOpen("order.txt", FILE_CSV|FILE_READ|FILE_WRITE, ";");


MM

 
MorningManiac:

Thank you! Your advice with the print statements gave me more insight of what was going on.


I got most of it working apart from the part:

FileDelete("order.txt");


I get "cannot delete file". Perhaps I should open it in a different mode? In my current version I've opened it with:

call=FileOpen("order.txt", FILE_CSV|FILE_READ|FILE_WRITE, ";");

Did u close the file properly? Is it open in any other windows application?

You should also check exactly what error u get, so u can pinpoint the problem:

  int error=GetLastError();   // clear error buffer
  FileDelete("order.txt");
  error=GetLastError();
  if(error!=ERR_NOERROR) Print("FileDelete() error ",error );
 

Thanks! I have added before the delete:

FileClose(call);

where call is the handle for the file.


I've added your piece of code and get this error message:

FileDelete() error 4100

I checked and it means "some file error", so I'm not sure what that's about.


MM

 
MorningManiac:

Thanks! I have added before the delete:

FileClose(call);

where call is the handle for the file.


I've added your piece of code and get this error message:

FileDelete() error 4100

I checked and it means "some file error", so I'm not sure what that's about.


MM

The first thing to do now is search the forums, but that leads nowhere in this case. The description in the documentation doesn't help either.

The only idea I have at the moment is that maybe some other windows process is using the file...?

 

Thanks Gordon and Cloudbraker. Perhaps the application that I use to create the file in the first place is the problem, though I don't have it opened for testing. If that isn't it then I'll see if I can work around having to delete the file.


MM

 
MorningManiac:

Thanks Gordon and Cloudbraker. Perhaps the application that I use to create the file in the first place is the problem, though I don't have it opened for testing. If that isn't it then I'll see if I can work around having to delete the file.


MM

Some windows applications don't release lock on files even when they are closed (they're just badly designed). You can use Process Explorer to verify if that's the case.

Here's instructions I've googled for ya: http://windowsxp.mvps.org/processlock.htm. If you find that the file is still locked by that program, then try switching to another. There are plenty of good text editors out there...

Reason: