Trying to make FileWrite happen at a specific minute and second

 

I am trying to make my EA work to record an indicator (using stochastic for now) using the FileWrite() command.  The goal is for this to record the indicator values at every 10th minute and 30th second of every hour. This will be changed for future purposes. The file writes properly and saves as I want, opens in Excel properly, and is sets up in Excel the way I intended. 

The problem is that in backtesting over 5 days, I only managed to get 48 values, when I should be getting about 120 (whatever time markets close on Friday I will not get any new data points). I tested this on USDJPY from 1/5/2015 to 1/9/2015 (Monday through Friday)

Is this possible with "small" adjustments or must I do a much larger rework of this EA?  

Here is the code: 

//+------------------------------------------------------------------+
//|                                             Stochastic Stuff.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
extern int han;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
    han=FileOpen("Stoch check.txt",FILE_WRITE|FILE_READ|FILE_CSV,",");
    FileWrite(han,"Number","15M Val Stoch","15M Sig Stoch","Time"); 
     if(han<0) 
      return(0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
    
     
        FileClose(han);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   int hour=TimeHour(TimeCurrent());
   int minute=TimeMinute(TimeCurrent());
   int second=Seconds();
   static int count=1;
   double ValStoch = iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   double SignalStoch = iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   string val = DoubleToStr(ValStoch,5);
   string sig = DoubleToStr(SignalStoch,5);
   string counter = IntegerToString(count,1);
   

   if(minute==10 && second==30)
   {
    if(han>0)
    {  
     Print("The Val Stoch is ", val," and the signal is ",sig);
     FileSeek(han,0,SEEK_END);    
     FileWrite(han,count,ValStoch,SignalStoch,TimeToStr(TimeCurrent(),TIME_MINUTES|TIME_SECONDS|TIME_DATE) );
    }
    count++;
   } 
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

 Thank you in advance.

 
asham:

I am trying to make my EA work to record an indicator (using stochastic for now) using the FileWrite() command.  The goal is for this to record the indicator values at every 10th minute and 30th second of every hour. This will be changed for future purposes. The file writes properly and saves as I want, opens in Excel properly, and is sets up in Excel the way I intended. 

The problem is that in backtesting over 5 days, I only managed to get 48 values, when I should be getting about 120 (whatever time markets close on Friday I will not get any new data points). I tested this on USDJPY from 1/5/2015 to 1/9/2015 (Monday through Friday)

Is this possible with "small" adjustments or must I do a much larger rework of this EA?  

Here is the code: 

 Thank you in advance.

Hello!
One second may not have ticks to perform start() function.

But you can use the timer witch interval (for example) 0.5 seconds:
//+------------------------------------------------------------------+
//|                                             Stochastic Stuff.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
extern int han;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   EventSetMillisecondTimer(500);
//----
   han=FileOpen("Stoch check.txt",FILE_WRITE|FILE_READ|FILE_CSV,",");
   FileWrite(han,"Number","15M Val Stoch","15M Sig Stoch","Time");
   if(han<0)
      return(0);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

   FileClose(han);
//----
   return(0);
  }

void OnTimer()
  {
   int hour=TimeHour(TimeCurrent());
   int minute=TimeMinute(TimeCurrent());
   int second=Seconds();
   static int count=1;
   double ValStoch = iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   double SignalStoch=iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   string val = DoubleToStr(ValStoch,5);
   string sig = DoubleToStr(SignalStoch,5);
   string counter=IntegerToString(count,1);

   if(minute==10 && second==30)
     {
      if(han>0)
        {
         Print("The Val Stoch is ",val," and the signal is ",sig);
         FileSeek(han,0,SEEK_END);
         FileWrite(han,count,ValStoch,SignalStoch,TimeToStr(TimeCurrent(),TIME_MINUTES|TIME_SECONDS|TIME_DATE));
        }
      count++;
     }

  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+


Or change the condition, eg:

if(minute==10 && second>=30)

or:

if(minute==10 && (second>=30 && second<40))

etc

 

 
Better use TimeLocal instead of TimeCurrent due to its dependent on broker's tick to update the time. Localtime is always updated.
 

ENSED - You are correct that 1 second may not have a tick which gives the info.  However I am trying to collect my information at specific intervals to run my own observations.  I am struggling with the OnTimer function since this is the first time I have encountered it.  I tried copying exactly what you created into a new EA and had no data recorded.  Did I miss something in trying to implement and understand this new function?

 

deysmacro - You are right in pointing out that TimeLocal is better than TimeCurrent. Thank you. 

 

Hello!

Please, try this  code:

//+------------------------------------------------------------------+
//|                                             Stochastic Stuff.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"
extern int han;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  { 
//----
   han=FileOpen("Stoch check.txt",FILE_WRITE|FILE_READ|FILE_CSV,",");
   FileWrite(han,"Number","15M Val Stoch","15M Sig Stoch","Time");
   if(han<0)
      return(0);
   
   EventSetMillisecondTimer(500);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----

   FileClose(han);
//----
   return(0);
  }

void OnTimer()
  {
  
   int hour=TimeHour(TimeLocal());
   int minute=TimeMinute(TimeLocal());
   int second=TimeSeconds(TimeLocal());
   
   static datetime prevTime = 0;
   Comment("Current Local Time = ", hour, ":", minute, ":", second, "; prevTime = ", TimeToStr(prevTime, TIME_SECONDS));
   
   static int count=1;
   double ValStoch = iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_MAIN,0);
   double SignalStoch=iStochastic(NULL,PERIOD_M15,5,3,3,MODE_SMA,0,MODE_SIGNAL,0);
   string val = DoubleToStr(ValStoch,5);
   string sig = DoubleToStr(SignalStoch,5);
   string counter=IntegerToString(count,1);

   if(minute==10 && second==30 && prevTime != TimeLocal())
     {
      prevTime = TimeLocal();
      if(han>0)
        {
         Print("The Val Stoch is ",val," and the signal is ",sig);
         FileSeek(han,0,SEEK_END);
         FileWrite(han,count,ValStoch,SignalStoch,TimeToStr(TimeLocal(),TIME_MINUTES|TIME_SECONDS|TIME_DATE));
        }
      count++;
     }

  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----

//----
   return(0);
  }
//+------------------------------------------------------------------+

 

 I tested it and it seems to be working correctly.

Reason: