Download MetaTrader 5
To add comments, please log in or register
MQL5.community allows users to store their source codes in MQL5 Storage. Try it!
Bradford Hall
183
Bradford Hall 2016.09.26 22:37 
I am struggling to get this to work.  I want to be able to just plot data values associated with a csv file.

I've read others using external text files and have been going through the reference manual trying to learn how to do this and have not been able to glean any results.

I want to be able to plot values from a csv file as a histogram for the associated date-time.  I can not get anything to show up when I fill in the csv file with values.

Could anybody please take a look at this code and show me the changes you need to properly import the data into the indicator window??  

This seems like such a trivial thing but I can not figure out what I am overlooking.

//+------------------------------------------------------------------+
//|                                                             .mq4 |
//|                                                 Copyright © 2016 |
//|                                                      https://www |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2016"
#property link      "https://www"
#property version   "1.00"
#property strict
#property indicator_separate_window
#property indicator_buffers 1

double ExtMapBuffer[];

int handle;

int OnInit(){
   string file_name = "Test.csv";
   if (!FileIsExist(file_name)){
      Alert("This file doesn't exist. It is created now by the indicator. Please, fullfill it. It is in \\MQL4\\Files\\Test.csv");
      handle = FileOpen(file_name,FILE_WRITE|FILE_CSV,";");
      FileWrite(handle,"Datetime (YEAR.MONTH.DAY HOUR:MINUTE)","Value (Number)");
      FileClose(handle);
      return(INIT_FAILED);
   }
   else handle = FileOpen(file_name,FILE_READ|FILE_CSV,";");
   
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,2,clrRed);
   SetIndexBuffer(0,ExtMapBuffer);
   SetIndexEmptyValue(0,0);

   return(INIT_SUCCEEDED);
}



int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[]){
   static int flag = 0;
   if (flag == 0){
      FileSeek(handle,0,SEEK_SET);
      while (!FileIsEnding(handle)){
         datetime t = FileReadDatetime(handle);
         double data = FileReadNumber(handle);
         ExtMapBuffer[iBarShift(Symbol(),Period(),t)] = data;
      }
      flag = 1;
   }
   return(rates_total);
}
Frederic Metraux
503
Frederic Metraux 2016.09.26 22:42  
Bradford Hall:
I am struggling to get this to work.  I want to be able to just plot data values associated with a csv file.

I've read others using external text files and have been going through the reference manual trying to learn how to do this and have not been able to glean any results.

I want to be able to plot values from a csv file as a histogram for the associated date-time.  I can not get anything to show up when I fill in the csv file with values.

Could anybody please take a look at this code and show me the changes you need to properly import the data into the indicator window??  

This seems like such a trivial thing but I can not figure out what I am overlooking.

Hi! I think it is missing FILE_ANSI option. And you don't need this:

FileSeek(handle,0,SEEK_SET);

A good idea is to debug the code. My guess is that t and data is rubbish. Maybe it is not finding the file. Add some Alert() Print() or Comment() to make it clear what is happening.

Here is a snippet that works. filename points to a file in ...\AppData\Roaming\MetaQuotes\Terminal\Common\Files

   int filehandle=FileOpen(filename,FILE_READ|FILE_CSV|FILE_ANSI|FILE_COMMON,separator);
   if(filehandle!=INVALID_HANDLE)  
   {
      while(!FileIsEnding(filehandle))
        {
         //--- read the string
         CreationTime=FileReadString(filehandle,-1);
         symbol=FileReadString(filehandle,-1);
         Direction=FileReadString(filehandle,-1);
         Probability=FileReadString(filehandle,-1);
         Interval=FileReadString(filehandle,-1);
         Pattern=FileReadString(filehandle,-1);
         BreakoutPrice=FileReadString(filehandle,-1);
         ForecastPrice=FileReadString(filehandle,-1);
         if(OpenTrade())
         {
            Alert(symbol+" trade opening success");
         }
         else
         {
            Alert(symbol+" trade opening failure");
         }
        }
      FileClose(filehandle);
   }
   else
   {
       string err= "File cannot Be Opened: Error= "+GetLastError();
       Alert(err);
   } 
  }
/
To add comments, please log in or register