//+------------------------------------------------------------------+
//| Demo_FileReadStruct.mq5 |
//| Copyright 2013, MetaQuotes Software Corp. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2013, MetaQuotes Software Corp."
#property link "https://www.mql5.com"
#property version "1.00"
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots 1
//---- plot Label1
#property indicator_label1 "Candles"
#property indicator_type1 DRAW_CANDLES
#property indicator_color1 clrOrange
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_separate_window
//--- parámetros para recibir datos
input string InpFileName="EURUSD.txt"; // nombre del archivo
input string InpDirectoryName="Data"; // nombre de la carpeta
//+------------------------------------------------------------------+
//| Estructura para almacenar los datos de la vela |
//+------------------------------------------------------------------+
struct candlesticks
{
double open; // precio de apertura
double close; // precio de cierre
double high; // precio máximo
double low; // precio mínimo
datetime date; // fecha
};
//--- búferes de indicadores
double open_buff[];
double close_buff[];
double high_buff[];
double low_buff[];
//--- variables globales
candlesticks cand_buff[];
int size=0;
int ind=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
int default_size=100;
ArrayResize(cand_buff,default_size);
//--- abrimos el archivo
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN|FILE_COMMON);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("Archivo %s abierto para la lectura",InpFileName);
PrintFormat("Ruta del archivo: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
//--- leemos los datos desde el archivo
while(!FileIsEnding(file_handle))
{
//--- escribimos los datos en el array
FileReadStruct(file_handle,cand_buff[size]);
size++;
//--- comprobamos si el array está sobrecargado
if(size==default_size)
{
//--- aumentar la dimensión del array
default_size+=100;
ArrayResize(cand_buff,default_size);
}
}
//--- cerramos el archivo
FileClose(file_handle);
PrintFormat("Datos leídos, archivo %s cerrado",InpFileName);
}
else
{
PrintFormat("Fallo al abrir el archivo %s, Código del error = %d",InpFileName,GetLastError());
return(INIT_FAILED);
}
//--- indicator buffers mapping
SetIndexBuffer(0,open_buff,INDICATOR_DATA);
SetIndexBuffer(1,high_buff,INDICATOR_DATA);
SetIndexBuffer(2,low_buff,INDICATOR_DATA);
SetIndexBuffer(3,close_buff,INDICATOR_DATA);
//--- valor vacío
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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[])
{
ArraySetAsSeries(time,false);
//--- ciclo para las velas no procesadas todavía
for(int i=prev_calculated;i<rates_total;i++)
{
//--- por defecto 0
open_buff[i]=0;
close_buff[i]=0;
high_buff[i]=0;
low_buff[i]=0;
//--- prueba de que si hay más datos
if(ind<size)
{
for(int j=ind;j<size;j++)
{
//--- si las fechas coinciden, utilizamos el valor desde el archivo
if(time[i]==cand_buff[j].date)
{
open_buff[i]=cand_buff[j].open;
close_buff[i]=cand_buff[j].close;
high_buff[i]=cand_buff[j].high;
low_buff[i]=cand_buff[j].low;
//--- aumentamos el contador
ind=j+1;
break;
}
}
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}
|