Script that runs when a new bar is created in all timeframe

 

Hi,

I did search forums and was able to write this code. But I am stuck here.

I require a script/indicator something that would write to a file when a new bar is created in all time frame.

Can somebody help me improve this code please

#import "kernel32.dll"
int _lclose (int);
int _lopen (string,int);
int _llseek (int,int,int);
int _lread (int,string,int);
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
int sectorSize = 1936;
int fileHandle =_lopen(TerminalPath()+"\\history\\"+AccountServer()+"\\symbols.raw",0);
int symbolCount =_llseek(fileHandle,0,2)/sectorSize;
string symbolName = "123456789012";
double o,h,l,c;
int handle=FileOpen("mydat.csv",FILE_CSV|FILE_WRITE);
for(int i=0; i<symbolCount; i++)
{
_llseek(fileHandle,i*sectorSize,0);
_lread (fileHandle,symbolName,12);
o = iOpen(symbolName,PERIOD_M5,1);
h = iHigh(symbolName,PERIOD_M5,1);
l = iLow(symbolName,PERIOD_M5,1);
c = iClose(symbolName,PERIOD_M5,1);
FileWrite(handle,symbolName, o,h,l,c);
}
FileClose(handle);
_lclose(fileHandle);
return(0);
}

 
Please use this to post code . . . it makes it easier to read.

 
surisriram:

Hi,

I did search forums and was able to write this code. But I am stuck here.

I require a script/indicator something that would write to a file when a new bar is created in all time frame.

Can somebody help me improve this code please

#import "kernel32.dll"
int _lclose (int);
int _lopen (string,int);
int _llseek (int,int,int);
int _lread (int,string,int);
//+------------------------------------------------------------------+
//| script program start function |
//+------------------------------------------------------------------+
int start()
{
int sectorSize = 1936;
int fileHandle =_lopen(TerminalPath()+"\\history\\"+AccountServer()+"\\symbols.raw",0);
int symbolCount =_llseek(fileHandle,0,2)/sectorSize;
string symbolName = "123456789012";
double o,h,l,c;
int handle=FileOpen("mydat.csv",FILE_CSV|FILE_WRITE);
for(int i=0; i<symbolCount; i++)
{
_llseek(fileHandle,i*sectorSize,0);
_lread (fileHandle,symbolName,12);
o = iOpen(symbolName,PERIOD_M5,1);
h = iHigh(symbolName,PERIOD_M5,1);
l = iLow(symbolName,PERIOD_M5,1);
c = iClose(symbolName,PERIOD_M5,1);
FileWrite(handle,symbolName, o,h,l,c);
}
FileClose(handle);
_lclose(fileHandle);
return(0);
}
 

sir,

if you wrote that, you are a quite capable engineer.

>>Can somebody help me improve this code please?

u r funny.

but, one issue, when i learned how to write a dll for my ea, i thought that the #import "whatever.dll" needed a closing #import, like this:

#import "kernel32.dll"
int _lclose (int);
int _lopen (string,int);
int _llseek (int,int,int);
int _lread (int,string,int);

#import

it probably just isnt required.

happy coding sir,

zero/.

 
smoknfx:

sir,

if you wrote that, you are a quite capable engineer.

>>Can somebody help me improve this code please?

u r funny.

but, one issue, when i learned how to write a dll for my ea, i thought that the #import "whatever.dll" needed a closing #import, like this:

#import "kernel32.dll"
int _lclose (int);
int _lopen (string,int);
int _llseek (int,int,int);
int _lread (int,string,int);

#import

it probably just isnt required.

happy coding sir,

zero/.

I am java programmer but new to mql so need some help moving around

You are right abt closing #import i missed it but this seems to be working fine without it!!

 

yeah, i even removed the closing #import from my code that i run and i recompiled it and it seemed to recompile, but i did not test things to make sure that it was running..

just put it back in place, just for good luck.

now, that code that you wrote above, it is actually quite advanced code, omg, you are issuing direct function calls into kernel32.dll, lmfao..

that is not beginner level stuff.

quite nice, actually.

now, what is it that you are trying to do (that is actually causing you difficulty)?

edward/.

 
smoknfx:

yeah, i even removed the closing #import from my code that i run and i recompiled it and it seemed to recompile, but i did not test things to make sure that it was running..

just put it back in place, just for good luck.

now, that code that you wrote above, it is actually quite advanced code, omg, you are issuing direct function calls into kernel32.dll, lmfao..

that is not beginner level stuff.

quite nice, actually.

now, what is it that you are trying to do (that is actually causing you difficulty)?

edward/.


Frankly i did not write it, i was searching forums and found the code, modified it to suite me.

hmm any hu, is there a way in mql to run a script or EA when a new bar is added for a timeframes.

i.e a new bar is created once every 5 min on 5min time frame, i want to execute my code when that new bar is created.

 
static datetime cTime ;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   cTime=Time[0] ;
   return(0);
  }

int start()
  {
    
   if (cTime == Time[0]) 
      {       
        return(0) ;
      }
   else
      {
       cTime=Time[0] ;
      }  
// code for start of new bar follows
  }
 

This EA may also be of interest Its also an information collector

 
int init(){}
int deinit(){}

int run_once_perbar_function()
{
int cur_bartime;
int prev_bartime;

cur_bartime=Time[0];
prev_bartime=GlobalVariableGet("prev_bartime");

if(prev_bartime==0) {GlobalVariableSet("prev_bartime",cur_bartime); return(0);}

if(cur_bartime==prev_bartime) return(0);
GlobalVariableSet("prev_bartime",cur_bartime);

//code below this point will run once upon each new bar only..

Alert("whatever..."); return(0); }//run_once_perbar_function int start() { run_once_perbar_function(); return(0); }//end of start function
 
Wow! smoknfx finally answered a question! (:->
Reason: