Help needed with script coding: monitor text file and execute trades

 
Hi,

I want to use Ninjatrader to generate trading signals and then pass the signal to MT4 to execute the trade.

Here is how I plan to do it:

1, I wrote a custom indicator in Ninjatrader that issue a buy signal when the conditions are met, this indicator will also create a "buy.csv" file in the mt4--expert--files folder.

2,I wrote a custom script in MT4 that check the existence of "buy.csv" file every 1 second, when it detect the "buy.csv" file, it will send a ordersend buy command, and then delete the "buy.csv" file, and continue to monitor the folder every 1 second.

The problem is, when I am testing my plan, the MT4 script will send and execute the first two buy signals and then stop working, can someone help me to figure out what's wrong with my code or logic?

Thanks!

This is the code from my Ninjatrader indicator:

protected override void OnBarUpdate()
{
System.IO.FileStream wFile;
byte[] byteData = null;
byteData = Encoding.ASCII.GetBytes("BUY");
if(Close[0]>Open[0] ) //This is the signal condition, when open>close, just an example
{
wFile = new FileStream("c:\\Program Files\\MetaTrader 4\\experts\\files\\buy.csv", FileMode.Append);
wFile.Write(byteData, 0, byteData.Length);
wFile.Close();
}

And this is the code for my MT4 script:


bool buyOrder = false;
int start()
{
int handle;
int i;
for(i=0;i<100000;i++)
{
handle=FileOpen("buy.csv",FILE_CSV|FILE_READ,';');
if(handle==1)
{
buyOrder=OrderSend(Symbol(),OP_BUY,1,Ask,10,Ask-200*Point,Ask+100*Point,"buy",0,0,CLR_NONE);
FileClose(handle);
FileDelete("buy.csv");
}
Sleep(1000);
}
return(0);
 

Just a thought, ... there may be an issue with Ninja writing to a file (or having it opened) while at the same time MetaTrader is trying to delete same file.

The first two trades may work, but eventually hang up as both programs are trying to access the same file.


I have Ninja but have never used it for coding. Perhaps MT4 could copy from file 1 to file 2 and delete off file 2, while Ninja performs the opposite.

I know that is not the easiest fix, but its a thought.


Another idea is to have Ninja write a #2 csv file BUT ONLY after performing vFile.Close(). Ninja will need to delete the #2 csv file when opening buy.csv file. Now you have MT4 look for # 2 csv file and if it exists, then that would mean Ninja is not accessing buy.csv. so its safe for MT4 to delete. Might also want to see if buy.csv has data inside before deleting.


Also... did you check your Terminal > Experts tab ... and in doing so, were there any errors with reading or writing to file? If yes, then above may be the problem.


Hopefully this helps. I do have a crazy way of trying to achieve stuff sometimes :) so I won't be surprised if it doesn't

 
JPS:

Just a thought, ... there may be an issue with Ninja writing to a file (or having it opened) while at the same time MetaTrader is trying to delete same file.

Think about having Ninja write a .tmp file and then rename it .csv. MT only looks for a complete .csv. Or have MT rename the .csv to .ord and open/delete that.

If the rename fails, wait and retry.

 

Hi JPS and WHRoeder,

Thank you both for your help.

Due to the limited programming skills, I can understand the suggestions you've made, but this is the most complicated code I can come up with. :(

I've added a "Print("1")" code before the OrderSend code, so it looks like this:

bool buyOrder = false;
int start()
{
int handle;
int i;
for(i=0;i<100000;i++)
{
handle=FileOpen("buy.csv",FILE_CSV|FILE_READ,';');
if(handle==1)
{
Print("1");
buyOrder=OrderSend(Symbol(),OP_BUY,1,Ask,10,Ask-200*Point,Ask+100*Point,"buy",0,0,CLR_NONE);
FileClose(handle);
FileDelete("buy.csv");
}
Sleep(1000);
}
return(0);


And when I run the script, the Print code will work everytime a "buy.csv" shows up in the folder, but not everytime the Ordersend code will work, the following is the experts log:

2010.12.21 12:34:38 A1220 EURUSD,M1: 1
2010.12.21 12:34:34 A1220 EURUSD,M1: 1
2010.12.21 12:34:31 A1220 EURUSD,M1: 1
2010.12.21 12:34:29 A1220 EURUSD,M1: open #49995354 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:34:29 A1220 EURUSD,M1: 1
2010.12.21 12:34:24 A1220 EURUSD,M1: 1
2010.12.21 12:34:21 A1220 EURUSD,M1: 1
2010.12.21 12:34:19 A1220 EURUSD,M1: 1
2010.12.21 12:34:16 A1220 EURUSD,M1: 1
2010.12.21 12:34:12 A1220 EURUSD,M1: open #49995292 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:34:12 A1220 EURUSD,M1: 1
2010.12.21 12:34:10 A1220 EURUSD,M1: open #49995288 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:34:10 A1220 EURUSD,M1: 1
2010.12.21 12:34:08 A1220 EURUSD,M1: open #49995282 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:34:07 A1220 EURUSD,M1: 1
2010.12.21 12:34:05 A1220 EURUSD,M1: open #49995278 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:34:05 A1220 EURUSD,M1: 1
2010.12.21 12:34:03 A1220 EURUSD,M1: open #49995271 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:34:03 A1220 EURUSD,M1: 1
2010.12.21 12:34:00 A1220 EURUSD,M1: open #49995261 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:34:00 A1220 EURUSD,M1: 1
2010.12.21 12:33:58 A1220 EURUSD,M1: open #49995254 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:33:58 A1220 EURUSD,M1: 1
2010.12.21 12:33:56 A1220 EURUSD,M1: open #49995251 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:33:55 A1220 EURUSD,M1: 1
2010.12.21 12:33:51 A1220 EURUSD,M1: open #49995243 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:33:51 A1220 EURUSD,M1: 1
2010.12.21 12:33:47 A1220 EURUSD,M1: open #49995237 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:33:47 A1220 EURUSD,M1: 1
2010.12.21 12:32:38 A1220 EURUSD,M1: 1
2010.12.21 12:32:35 A1220 EURUSD,M1: 1
2010.12.21 12:32:32 A1220 EURUSD,M1: 1
2010.12.21 12:32:28 A1220 EURUSD,M1: 1
2010.12.21 12:32:24 A1220 EURUSD,M1: open #49995174 buy 1.00 EURUSD at 1.31140 sl: 1.30940 tp: 1.31240 ok
2010.12.21 12:32:23 A1220 EURUSD,M1: 1
2010.12.21 12:32:14 A1220 EURUSD,M1: loaded successfully
2010.12.21 12:32:09 Compiling 'A1220'