//+--------------------------------------------------------------------------------+
//| 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 "Candele"
#property indicator_type1 DRAW_CANDLES
#property indicator_color1 clrOrange
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
#property indicator_separate_window
//--- parametri per la ricezione dei dati
input string InpFileName="EURUSD.txt"; // nome del file
input string InpDirectoryName="Data"; // nome directory
//+--------------------------------------------------------------------------------+
//| Struttura per memorizzare i dati della candela |
//+--------------------------------------------------------------------------------+
struct candlesticks
{
double open; // prezzo apertura
double close; // prezzo chiusura
double high; // prezzo più alto
double low; // prezzo più basso
datetime date; // data
};
//--- buffers indicatore
double open_buff[];
double close_buff[];
double high_buff[];
double low_buff[];
//--- variabili globali
candlesticks cand_buff[];
int size=0;
int ind=0;
//+--------------------------------------------------------------------------------+
//| Funzione di inizializzazione Indicatore Personalizzato |
//+--------------------------------------------------------------------------------+
int OnInit()
{
int default_size=100;
ArrayResize(cand_buff,default_size);
//--- apre il file
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_BIN|FILE_COMMON);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s il file è disponibile per la lettura",InpFileName);
PrintFormat("Percorso file: %s\\Files\\",TerminalInfoString(TERMINAL_COMMONDATA_PATH));
//--- legge i dati dal file
while(!FileIsEnding(file_handle))
{
//--- scrive i dati sull' array
FileReadStruct(file_handle,cand_buff[size]);
size++;
//--- controlla se l'array è stato riempito
if(size==default_size)
{
//--- incrementa le dimensioni dell'array
default_size+=100;
ArrayResize(cand_buff,default_size);
}
}
//--- chiude il file
FileClose(file_handle);
PrintFormat("I dati vengono letti, il file %s è chiuso",InpFileName);
}
else
{
PrintFormat("Fallimento nell'aprire il file %s, Codice errore = %d",InpFileName,GetLastError());
return(INIT_FAILED);
}
//--- mappatura buffers indicatore
SetIndexBuffer(0,open_buff,INDICATOR_DATA);
SetIndexBuffer(1,high_buff,INDICATOR_DATA);
SetIndexBuffer(2,low_buff,INDICATOR_DATA);
SetIndexBuffer(3,close_buff,INDICATOR_DATA);
//--- valore vuoto
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
return(INIT_SUCCEEDED);
}
//+--------------------------------------------------------------------------------+
//| 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);
//--- il ciclo per le candele che non sono state ancora trattate
for(int i=prev_calculated;i<rates_total;i++)
{
//--- 0 per default
open_buff[i]=0;
close_buff[i]=0;
high_buff[i]=0;
low_buff[i]=0;
//--- controlla se ci sono ancora dati presenti
if(ind<size)
{
for(int j=ind;j<size;j++)
{
//--- se le date coincidono, viene usato il valore dal file
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;
//--- incrementa il contatore
ind=j+1;
break;
}
}
}
}
//--- restituisce il valore di prev_calculated per la prossima chiamata
return(rates_total);
}
|