//+--------------------------------------------------------------------------------+
//| 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
//--- parametri per la lettura dei dati
input string InpFileName="RSI.csv"; // nome del file
input string InpDirectoryName="Data"; // nome della directory
//--- buffers indicatore
double open_buff[];
double high_buff[];
double low_buff[];
double close_buff[];
double color_buff[];
//--- variabili overbought
int ovb_ind=0;
int ovb_size=0;
datetime ovb_time[];
//--- variabili oversold
int ovs_ind=0;
int ovs_size=0;
datetime ovs_time[];
//+--------------------------------------------------------------------------------+
//| Funzione di inizializzazione Indicatore Personalizzato |
//+--------------------------------------------------------------------------------+
int OnInit()
{
//--- variabili di grandezze array per default
int ovb_def_size=100;
int ovs_def_size=100;
//--- alloca la memoria per gli array
ArrayResize(ovb_time,ovb_def_size);
ArrayResize(ovs_time,ovs_def_size);
//--- apre il file
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_CSV|FILE_ANSI);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s il file è disponibile per la lettura",InpFileName);
PrintFormat("Percorso file: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
double value;
//--- legge i dati dal file
while(!FileIsEnding(file_handle))
{
//--- legge i primi valori nella stringa
value=FileReadNumber(file_handle);
//--- legge da diversi arry secondo il risultato della funzione
if(value>=70)
ReadData(file_handle,ovb_time,ovb_size,ovb_def_size);
else
ReadData(file_handle,ovs_time,ovs_size,ovs_def_size);
}
//--- chiude il file
FileClose(file_handle);
PrintFormat("I dati vengono scritti, il file %s è chiuso",InpFileName);
}
else
{
PrintFormat("Fallimento nell'aprire il file %s, Codice errore = %d",InpFileName,GetLastError());
return(INIT_FAILED);
}
//--- lega gli array
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);
//---- imposta i valori dell'indicatore che non saranno visibili sul chart
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
return(INIT_SUCCEEDED);
}
//+--------------------------------------------------------------------------------+
//| Legge i dati stringa del file |
//+--------------------------------------------------------------------------------+
void ReadData(const int file_handle,datetime &arr[],int &size,int &def_size)
{
bool flag=false;
//--- legge fino alla che viene raggiunta la fine della stringa o del file
while(!FileIsLineEnding(file_handle) && !FileIsEnding(file_handle))
{
//--- slitta il carriage dopo aver letto il numero
if(flag)
FileReadNumber(file_handle);
//--- memorizza la data corrente
arr[size]=FileReadDatetime(file_handle);
size++;
//--- incrementa la grandezza dell'array se necessario
if(size==def_size)
{
def_size+=100;
ArrayResize(arr,def_size);
}
//--- va oltre la prima iterazione
flag=true;
}
}
//+--------------------------------------------------------------------------------+
//| Funzione di iterazione indicatore personalizato |
//+--------------------------------------------------------------------------------+
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);
//--- il loop per la barra che non è stata ancora maneggiata
for(int i=prev_calculated;i<rates_total;i++)
{
//--- 0 per default
open_buff[i]=0;
high_buff[i]=0;
low_buff[i]=0;
close_buff[i]=0;
color_buff[i]=0;
//--- controlla se qualunque data è ancora presente
if(ovb_ind<ovb_size)
for(int j=ovb_ind;j<ovb_size;j++)
{
//--- se le date coincidono, la barra è in area overbought
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 rosso
color_buff[i]=0;
//--- incrementa il contatore
ovb_ind=j+1;
break;
}
}
//--- controlla se esistono ancora dati
if(ovs_ind<ovs_size)
for(int j=ovs_ind;j<ovs_size;j++)
{
//--- se le date coincidono, la barra è in area oversold
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 - colore blu
color_buff[i]=1;
//--- incrementa il contatore
ovs_ind=j+1;
break;
}
}
}
//--- restituisce il valore di prev_calculated per la prossima chiamata
return(rates_total);
}
//+--------------------------------------------------------------------------------+
//| Event handler ChartEvent |
//+--------------------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam
)
{
//--- cambia lo spessore indicatore secondo la scala
if(ChartGetInteger(0,CHART_SCALE)>3)
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2);
else
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1);
}
|