//+------------------------------------------------------------------+
//| Demo_FileReadNumber.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_chart_window
#property indicator_buffers 5
#property indicator_plots 1
//---- plot Label1
#property indicator_label1 "Overbought & Oversold"
#property indicator_type1 DRAW_COLOR_BARS
#property indicator_color1 clrRed, clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- parámetros para la lectura de datos
input string InpFileName="RSI.csv"; // nombre del archivo
input string InpDirectoryName="Data"; // nombre de la carpeta
//--- buferes de indicadores
double open_buff[];
double high_buff[];
double low_buff[];
double close_buff[];
double color_buff[];
//--- variables de sobrecompra
int ovb_ind=0;
int ovb_size=0;
datetime ovb_time[];
//--- variables de sobreventa
int ovs_ind=0;
int ovs_size=0;
datetime ovs_time[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit()
{
//--- variables de tamaños de arrays por defecto
int ovb_def_size=100;
int ovs_def_size=100;
//--- adjudicamos memoria para arrays
ArrayResize(ovb_time,ovb_def_size);
ArrayResize(ovs_time,ovs_def_size);
//--- abrimos el archivo
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_CSV|FILE_ANSI);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("Archivo %s abierto para la lectura",InpFileName);
PrintFormat("Ruta del archivo: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
double value;
//--- leemos los datos desde el archivo
while(!FileIsEnding(file_handle))
{
//--- leemos el primer valor en la cadena
value=FileReadNumber(file_handle);
//--- leemos a diferentes arrays dependiendo del resultado de la función
if(value>=70)
ReadData(file_handle,ovb_time,ovb_size,ovb_def_size);
else
ReadData(file_handle,ovs_time,ovs_size,ovs_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 arrays
SetIndexBuffer(0,open_buff,INDICATOR_DATA);
SetIndexBuffer(1,high_buff,INDICATOR_DATA);
SetIndexBuffer(2,low_buff,INDICATOR_DATA);
SetIndexBuffer(3,close_buff,INDICATOR_DATA);
SetIndexBuffer(4,color_buff,INDICATOR_COLOR_INDEX);
//---- establecimiento de valores del indicador que no van a ser visibles en el gráfico
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Lectura de datos de la cadena de caracteres del archivo |
//+------------------------------------------------------------------+
void ReadData(const int file_handle,datetime &arr[],int &size,int &def_size)
{
bool flag=false;
//--- leemos hasta alcanzar el fin de la cadena o archivo
while(!FileIsLineEnding(file_handle) && !FileIsEnding(file_handle))
{
//--- desplazamos el carro al leer el número
if(flag)
FileReadNumber(file_handle);
//--- recordamos la fecha actual
arr[size]=FileReadDatetime(file_handle);
size++;
//--- si hace falta aumentamos el tamaño del array
if(size==def_size)
{
def_size+=100;
ArrayResize(arr,def_size);
}
//--- pasamos de la primera iteración
flag=true;
}
}
//+------------------------------------------------------------------+
//| 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);
ArraySetAsSeries(open,false);
ArraySetAsSeries(high,false);
ArraySetAsSeries(low,false);
ArraySetAsSeries(close,false);
//--- ciclo para las barras todavía no procesadas
for(int i=prev_calculated;i<rates_total;i++)
{
//--- por defecto 0
open_buff[i]=0;
high_buff[i]=0;
low_buff[i]=0;
close_buff[i]=0;
color_buff[i]=0;
//--- prueba de que si hay más datos
if(ovb_ind<ovb_size)
for(int j=ovb_ind;j<ovb_size;j++)
{
//--- si las fechas coinciden, la barra se encuentra en la zona de sobrecompra
if(time[i]==ovb_time[j])
{
open_buff[i]=open[i];
high_buff[i]=high[i];
low_buff[i]=low[i];
close_buff[i]=close[i];
//--- 0 - color rojo
color_buff[i]=0;
//--- aumentamos el contador
ovb_ind=j+1;
break;
}
}
//--- prueba de que si hay más datos
if(ovs_ind<ovs_size)
for(int j=ovs_ind;j<ovs_size;j++)
{
//--- si las fechas coinciden, la barra se encuentra en la zona de sobreventa
if(time[i]==ovs_time[j])
{
open_buff[i]=open[i];
high_buff[i]=high[i];
low_buff[i]=low[i];
close_buff[i]=close[i];
//--- 1 - color azul
color_buff[i]=1;
//--- aumentamos el contador
ovs_ind=j+1;
break;
}
}
}
//--- return value of prev_calculated for next call
return(rates_total);
}
//+------------------------------------------------------------------+
//| Manejador del evento ChartEvent |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam
)
{
//--- variamos el grosor del indicador en función de la escala
if(ChartGetInteger(0,CHART_SCALE)>3)
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2);
else
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1);
}
|