Periodic bar export. Need expert critic and debugging

 

I'm trying to export each final formed bar on the M1 period so I developed this script


void start()

{

int i = 1;

datetime current;

string st, fn=Symbol()+"current.csv";

current = iTime( "EURUSD", PERIOD_M1, 1);

while(i == 1)

{

if( TimeMinute(current) < TimeMinute(iTime( "EURUSD", PERIOD_M1, 0)))


{

st=TimeToStr(Time[0], TIME_DATE)+","+

TimeToStr(Time[0], TIME_MINUTES)+","+

DoubleToStr(Open[0], Digits)+","+

DoubleToStr(High[0], Digits)+","+

DoubleToStr(Low[0], Digits)+","+

DoubleToStr(Close[0], Digits)+","+

DoubleToStr(Volume[0], 0);

scriere(fn, st);

st="";

Sleep(50000);

current = iTime( "EURUSD", PERIOD_M1, 1);

}

}

return(0);

}



The output however is quite not what i wanted:

i stopped the script at 10:40 . The outputs though are the same.





2009.10.13,10:16,1.4777,1.4777,1.4776,1.4777,5

2009.10.13,10:16,1.4777,1.4777,1.4776,1.4777,5

2009.10.13,10:16,1.4777,1.4777,1.4776,1.4777,5

 

What is scriere? Perhaps problem is in there.

 
Ruptor:

What is scriere? Perhaps problem is in there.


scriere is the writing function in the file, it just writes the st string containing the values, into the file with fn name.


void scriere(string FileName, string text)

{

int file_handle=FileOpen(FileName, FILE_READ|FILE_WRITE, " ");


if (file_handle>0)

{

FileSeek(file_handle, 0, SEEK_END);

FileWrite(file_handle, text);

FileClose(file_handle);

}

}


it's just a simple write to file function.


I also tried using i-Functions with the same result:

It keeps printing the same bar:

st= TimeToStr(iTime("EURUSD",PERIOD_M1,i),TIME_DATE)+", "+

TimeToStr(iTime("EURUSD",PERIOD_M1,i),TIME_MINUTES)+", "+

iOpen("EURUSD",PERIOD_M1,i)+", "+

iHigh("EURUSD",PERIOD_M1,i)+", "+

iLow("EURUSD",PERIOD_M1,i)+", "+

iClose("EURUSD",PERIOD_M1,i)+", "+

iVolume("EURUSD",PERIOD_M1,i);


result:

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

2009.10.13, 18:33, 1.48330000, 1.48350000, 1.48320000, 1.48340000, 6.00000000

 
Lascarica:

I'm trying to export each final formed bar on the M1 period so I developed this script


void start()

{

...

while(i == 1)

...

2009.10.13,10:16,1.4777,1.4777,1.4776,1.4777,5

Either close and reopen/seek each time in start() without a loop

or refresh your values inside the loop.

 
Make it an EA and you can test it on the back tester. Sleep command doesn't work in indicators maybe it is the same in scripts.I did this in an EA and only opened the file in init and closed it in deinit. The data collection is fine.
 
Ruptor:
Make it an EA and you can test it on the back tester. Sleep command doesn't work in indicators maybe it is the same in scripts.I did this in an EA and only opened the file in init and closed it in deinit. The data collection is fine.

here's the EA i came up with:


int file_handle;

//+------------------------------------------------------------------+

//| expert initialization function |

//+------------------------------------------------------------------+

int init()

{

//----

string fn="EURUSD_current.csv";

file_handle=FileOpen(fn, FILE_READ|FILE_WRITE, " ");

//----

if(file_handle<1)

{

Print("File EURUSD_current.csv not found, the last error is ", GetLastError());

return(false);

}

return(0);

}

//+------------------------------------------------------------------+

//| expert deinitialization function |

//+------------------------------------------------------------------+

int deinit()

{

//----

FileClose(file_handle);

//----

return(0);

}

//+------------------------------------------------------------------+

//| expert start function |

//+------------------------------------------------------------------+

int start()

{

//----

int i = 1;

datetime current;

string st;

// fn=Symbol()+"current.csv";

current = iTime( "EURUSD", PERIOD_M1, 1);

while(i == 1)

{

if( TimeMinute(current) < TimeMinute(iTime( "EURUSD", PERIOD_M1, 0)))


{

st= TimeToStr(iTime("EURUSD",1,i),TIME_DATE)+", "+

TimeToStr(iTime("EURUSD",1,i),TIME_MINUTES)+", "+

iOpen("EURUSD",1,i)+", "+

iHigh("EURUSD",1,i)+", "+

iLow("EURUSD",1,i)+", "+

iClose("EURUSD",1,i)+", "+

iVolume("EURUSD",1,i);

FileSeek(file_handle, 0, SEEK_END);

FileWrite(file_handle, st);

st="";

current = iTime( "EURUSD", PERIOD_M1, 1);

}

}

//----

return(0);

}


I'm having the same problem, it keeps writing the same values to the file, as if the variable stacks never refresh.


 

Put some comment statements in so it writes the data to the chart so you can check what path it follows and see the values of varibles then run it in back tester in visual mode and debug it.

See if you can debug this to do what you want. It ocurred to me the series might have problems in back tester if your history is not correct.

Files:
testdump.mq4  3 kb
 
Ruptor:

Put some comment statements in so it writes the data to the chart so you can check what path it follows and see the values of varibles then run it in back tester in visual mode and debug it.

See if you can debug this to do what you want. It ocurred to me the series might have problems in back tester if your history is not correct.

Thank you for your help. Like a noob that I am, I failed to check with the basics of the mt4 programming laguage so :

"

Syntax of MQL4 is much a C-like syntax, apart from some features:

  • no address arithmetic;

  • no operator do ... while;

  • no operator goto ...;

  • no operation of [condition]?[expression 1]:[expression 2];

  • no compound data types (structures);

  • complex assignments are impossible; for example, val1=val2=0; arr[i++]=val; cond=(cnt=OrdersTotal)>0; etc.;

  • calculation of a logical expression is always completed, never early terminated."

The above obviously conflicted with my functions. Now I made it write to file only the last formed bar (offset 1 implied) and since there's no sleep or if clause involved, I guess it has a predefined sleep function that keeps the Ea from flooding the system resources, since it increments the variable every 2 seconds or more .
Files:
 

Bear in mind MT4 looks like C but it doesn't have the rigorous type checking or error detection particularly with globals and externals. It also produces intermediate code for the platform to interpret so there is another point where the code checking might go wrong.

The EA only enters on a tick so if there are no tick for 5 minutes you will get no file write. You can put back the sleep command and search the forum on IsStopped() function for continuous EA looping that is time driven rather than tick driven.

Reason: