//+------------------------------------------------------------------+
//| Demo_FileReadFloat.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 2
#property indicator_plots 1
//---- plot Label1
#property indicator_label1 "CloseLine"
#property indicator_type1 DRAW_COLOR_LINE
#property indicator_color1 clrRed,clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- parámetros para la lectura de datos
input string InpFileName="Close.bin"; // nombre del archivo
input string InpDirectoryName="Data"; // nombre de la carpeta
//--- variables globales
int ind=0;
int size=0;
double close_buff[];
datetime time_buff[];
//--- indicator buffers
double buff[];
double color_buff[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
int def_size=100;
//--- adjudicamos la memoria para los arrays
ArrayResize(close_buff,def_size);
ArrayResize(time_buff,def_size);
//--- abrimos el archivo
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("Archivo %s abierto para la lectura",InpFileName);
PrintFormat("Ruta del archivo: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
//--- leemos los datos desde el archivo
while(!FileIsEnding(file_handle))
{
//--- leemos los valores de la hora y el precio
time_buff[size]=(datetime)FileReadDouble(file_handle);
close_buff[size]=(double)FileReadFloat(file_handle);
size++;
//--- aumentamos el tamaño de los arrays si están sobrecargados
if(size==def_size)
{
def_size+=100;
ArrayResize(close_buff,def_size);
ArrayResize(time_buff,def_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);
}
//--- enlace de los arrays a los búferes de indicadores
SetIndexBuffer(0,buff,INDICATOR_DATA);
SetIndexBuffer(1,color_buff,INDICATOR_COLOR_INDEX);
//---- establecimiento de valores del indicador que no van a mostrarse en el gráfico
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 barras no procesadas todavía
for(int i=prev_calculated;i<rates_total;i++)
{
//--- por defecto 0
buff[i]=0;
color_buff[i]=0; // el color rojo por defecto
//--- 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]==time_buff[j])
{
//--- obtenemos el precio
buff[i]=close_buff[j];
//--- si el precio actual supera el anterior, el color es azul
if(buff[i-1]>buff[i])
color_buff[i]=1;
//--- aumentamos el contador
ind=j+1;
break;
}
}
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}
|