Help with read data from excel file in real time

 
Hi all, and thank you very much for keeping this awesome website.

I am learning about exchange data between some platforms and now i am with excel.

I am trying to download data of metatrader in real time (this step is succesfull)
I am trying to plot data of excel in mt4 (Not succesfull)

Maybe, this previous step i dont understood well. 
The data is saved in csv format, in folder "files" of mt4 broker. Then i use one indicator for read this data in real time. 
But if the file is closed, the data no is read for indicator, and if the file is open the data is not read for indicator. Then, how i can read the data in real time?

(When the file is not running in real time and with the file open, the indicator read the data)

//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Red
#property  indicator_width1  1

//---- indicator parameters
extern   string   FileName          = "Data";   // News file name

//---- indicator buffers
double     ind_buffer1[];

datetime   xDateTime[9999]; // Initialize
double     xValue[9999];  // Initialize

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   ArrayInitialize(ind_buffer1,EMPTY_VALUE);
   SetIndexBuffer(0,ind_buffer1);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   SetIndexDrawBegin(0,0);
   IndicatorShortName("External: "+FileName);

   return(0);
  }

//+------------------------------------------------------------------+
//| Principal                                                        |
//+------------------------------------------------------------------+

int start()
  {
   int handle = FileOpen(FileName, FILE_CSV|FILE_READ,';');  //Open file
   if(handle==0)
      Comment("File "+FileName+" not found.");

   for(int c=0; !FileIsEnding(handle); c++)
   {
      string xDate = FileReadString(handle);  // Read file
      string xTime = FileReadString(handle);
      if (FileIsEnding(handle)) break;
      xDateTime[c] = StrToTime(xDate+" "+xTime);
      xValue[c]    = StrToDouble(FileReadString(handle));

   }
   c--;
   for (int i=0; i<=Bars; i++)
     {
       while(Time[i] < xDateTime[c]) c--;
       if (c >= 0) ind_buffer1[i] = xValue[c];

     }
   FileClose(handle);  
    
//---- done
   return(0);
  }

 

Then, the indicator read well the data but not in real time because i think the logic in indicator is not correct.

If file is open and run in real time = the indicator not read data

If file is close and run in real time = the indicator not read data

if file is open and not run in real time = the indicator read data correctly. 

 

Any indication will be of great help.


Thank you very much in advance

 
It is not plotting real-time data because the "start" function is only exeuted when a new tick is sent from your broker. To have it real-time and real-time print, please refer to the OnTimer() function.
 
aakcaagac:
It is not plotting real-time data because the "start" function is only exeuted when a new tick is sent from your broker. To have it real-time and real-time print, please refer to the OnTimer() function.
thank you very much for your reply.

when you say refer to OnTimer function is for EA?. I still not create EA, but i try by this way:

 Any indication will be good. Thank you very much.

 

 

#property  indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Red
#property  indicator_width1  1

//---- indicator parameters
extern   string   FileName          = "Data";   // News file name

//---- indicator buffers
double     ind_buffer1[];

datetime   xDateTime[9999];
double     xValue[9999];


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
   
   ArrayInitialize(ind_buffer1,EMPTY_VALUE);
   SetIndexBuffer(0,ind_buffer1);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   SetIndexDrawBegin(0,0);
   IndicatorShortName("External: "+FileName);

      
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

int handle = FileOpen(FileName, FILE_CSV|FILE_READ,';');
   if(handle==0)
      Comment("File "+FileName+" not found.");
   
   for(int c=0; !FileIsEnding(handle); c++)
   {
      string xDate = FileReadString(handle);
      string xTime = FileReadString(handle);
      if (FileIsEnding(handle)) break;
      xDateTime[c] = StrToTime(xDate+" "+xTime);
      xValue[c]    = StrToDouble(FileReadString(handle));
//      
   }
   c--; // WHEN I COMPILE, THE PLATFORM SAY ME THAT THIS VARIABLE IS UNDECLARED. i dont understand, because "c" is declared in "for" bucle
   
   for (int i=0; i<=Bars; i++)
     {
       while(Time[i] < xDateTime[c]) c--;
       if (c >= 0) ind_buffer1[i] = xValue[c];

     }
   FileClose(handle);

   
  }
//+------------------------------------------------------------------+
 

The c--; variable issue is happening because int c = 0; is declared inside a block context and thus only valid within the block context.

Example:

for (int c = 0; .. ; ..) {

  // the variable c is only valid within the two curly brackets. NOT OUTSIDE!

}

Example:

int c = 0;

for (; .. ; ..) {

  // the c variable is valid inside and outside of the block context
  // because c has been declared and initialized outside the curly brackets
  // this is known as global variable.

}

I have not tested your code but this could help solving the problems. Simply remove the int c=0 within the for block and place it ouside:

int c=0;
for(c=0; !FileIsEnding(handle); c++)

or

int c=0;
for(; !FileIsEnding(handle); c++)
 

castann86: Thank you very much for your reply and sorry for delay.

I put your comment in code, but now always appear the same message when i compile :

"OnCalculate function not found in custom indicator"

 

int c=0;
   for(; !FileIsEnding(handle); c++)
   {
      string xDate = FileReadString(handle);
      string xTime = FileReadString(handle);
      if (FileIsEnding(handle)) break;
      xDateTime[c] = StrToTime(xDate+" "+xTime);
      xValue[c]    = StrToDouble(FileReadString(handle));

   }
   c--;

 Why this message?

Thank you very much in advance!! 


 

Why the message ?

That's because with MQL4 600+ or so, the things how MQL4 did has changed.

In former times you had these 3 key functions:

Init();

Start();

DeInit();

 

With MQL4 600+ these functions have changed (although the old naming still work to keep older code still operating). They changed e.g.

Init(); -> OnInit();

Start(); -> OnCalculate();   // in ea mode

DeInit(); -> OnDeinit();

 

New Methods are also:

e.g. OnChartEvent();, OnTimer(); etc... Please refer here for further information.

 

In your example it complains rightfully because the mandatory parts OnInit();, OnCalculate(); and DeInit(); are missing. In your case it's the OnCalculate();. This was the former Start(); function. So OnCalculate(); as well as Start();, only gets entered as soon as a Tick response comes form your Broker. So in some cases you might receive 10000 Ticks per seconds... in other cases you might receive 1 Tick in 2 minutes. Depending on the price changes (this is just a rough example).

Now you have been demanding some sort of "RealTime" functionality. E.g. you like to have your real time data from CVS being inported into your EA on real time. Therefore the OnCalculate(); (or Start();) is not the right place to put real time data (since your real time data has no effect because of the tick event handling that this method depends on. Therefore I suggested that you might use the OnTime(); function (which runs independantly) to OnCalculate();. You can init it by being triggered at all 1 Seconds (or even in Milliseconds (or was it Microseconds)) so you can evaluate your real time data and process these real time informations in some sort of real time within your EA. Nonetheless you need the OnCalculate(); function to be inside your EA (even if you don't use it).

 

Hi again,

when i attached the EA to chart, no occur nothing. Maybe i dont understand EA or logic, but i put the next EA to chart:

 

#property  indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Red
#property  indicator_width1  1

//---- indicator parameters
extern   string   FileName          = "PSI";   // News file name

//---- indicator buffers
double     ind_buffer1[];

datetime   xDateTime[9999];
double     xValue[9999];


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
   
   ArrayInitialize(ind_buffer1,EMPTY_VALUE);
   SetIndexBuffer(0,ind_buffer1);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   SetIndexDrawBegin(0,0);
   IndicatorShortName("External: "+FileName);

      
//---
   return(INIT_SUCCEEDED);
  }
  
  
  int OnCalculate (const int rates_total,      // size of input time series
                 const int prev_calculated,  // bars handled in previous call
                 const datetime& time[],     // Time
                 const double& open[],       // Open
                 const double& high[],       // High
                 const double& low[],        // Low
                 const double& close[],      // Close
                 const long& tick_volume[],  // Tick Volume
                 const long& volume[],       // Real Volume
                 const int& spread[]         // Spread
   )
   {
   return(rates_total);
   }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

int handle = FileOpen(FileName, FILE_CSV|FILE_READ,';');
   if(handle==0)
      Comment("File "+FileName+" not found.");
   
   int c=0;
   for(; !FileIsEnding(handle); c++)
   {
      string xDate = FileReadString(handle);
      string xTime = FileReadString(handle);
      if (FileIsEnding(handle)) break;
      xDateTime[c] = StrToTime(xDate+" "+xTime);
      xValue[c]    = StrToDouble(FileReadString(handle));

   }
   c--;
   
   for (int i=0; i<=Bars; i++)
     {
       while(Time[i] < xDateTime[c]) c--;
       if (c >= 0) ind_buffer1[i] = xValue[c];

     }
   FileClose(handle);

   
  }
//+------------------------------------------------------------------+

 Thank you very much 

 
int OnInit() {
   EventSetTimer(60);
}

Nothing happens ? Things happen after every 60 seconds. You initialized your timer that way. If you want that things happen more frequently then reduce the timer to e.g. 5 seconds or 2 seconds or whatever you like. You can even set it with Milli- and Microseconds if you wish. But note that the more open and close you do (file handling) the more your hard drives tear and your CPU is being loaded.

Please take your time studying the MQL4 API for a bit. I am willing to help you but I can't ride your whole road on my own.

 
aakcaagac:

Nothing happens ? Things happen after every 60 seconds. You initialized your timer that way. If you want that things happen more frequently then reduce the timer to e.g. 5 seconds or 2 seconds or whatever you like. You can even set it with Milli- and Microseconds if you wish. But note that the more open and close you do (file handling) the more your hard drives tear and your CPU is being loaded.

Please take your time studying the MQL4 API for a bit. I am willing to help you but I can't ride your whole road on my own.

I agree with you in this, i have to learn. 

i am going to prove with 5 sec of time!!

 

Thank you very much for your help!! 

 

Are new, i wonder if you can have in real time on a tick by tick excel line ,the prize, time , and volume  but if you can also timing how I feel most appropriate to do 5 seconds,

I state use the 2003 version of Excel if possible, or otherwise in 2010, the prize, time , and volume,, I see your last EA script ?? 

I do not know if it actually does this work ?? in when I can not figure out how to make it work .. would be so kind to give me that information ...

  I state of your kindness I translated a translator percu definitely what is written will be very bad, are Italian


Thank you very much for your help!!

marco

#property  indicator_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Red
#property  indicator_width1  1

//---- indicator parameters
extern   string   FileName          = "PSI";   // News file name

//---- indicator buffers
double     ind_buffer1[];

datetime   xDateTime[9999];
double     xValue[9999];


//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
   
   ArrayInitialize(ind_buffer1,EMPTY_VALUE);
   SetIndexBuffer(0,ind_buffer1);
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID);
   SetIndexDrawBegin(0,0);
   IndicatorShortName("External: "+FileName);

      
//---
   return(INIT_SUCCEEDED);
  }
  
  
  int OnCalculate (const int rates_total,      // size of input time series
                 const int prev_calculated,  // bars handled in previous call
                 const datetime& time[],     // Time
                 const double& open[],       // Open
                 const double& high[],       // High
                 const double& low[],        // Low
                 const double& close[],      // Close
                 const long& tick_volume[],  // Tick Volume
                 const long& volume[],       // Real Volume
                 const int& spread[]         // Spread
   )
   {
   return(rates_total);
   }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
      
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Timer function                                                   |
//+------------------------------------------------------------------+
void OnTimer()
  {
//---

int handle = FileOpen(FileName, FILE_CSV|FILE_READ,';');
   if(handle==0)
      Comment("File "+FileName+" not found.");
   
   int c=0;
   for(; !FileIsEnding(handle); c++)
   {
      string xDate = FileReadString(handle);
      string xTime = FileReadString(handle);
      if (FileIsEnding(handle)) break;
      xDateTime[c] = StrToTime(xDate+" "+xTime);
      xValue[c]    = StrToDouble(FileReadString(handle));

   }
   c--;
   
   for (int i=0; i<=Bars; i++)
     {
       while(Time[i] < xDateTime[c]) c--;
       if (c >= 0) ind_buffer1[i] = xValue[c];

     }
   FileClose(handle);

   
  }
//+--------------------------

Reason: