input int InpRates=100;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
MqlRates rates[];
//--- ricordare l'ora di inizio prima della ricezione delle barre
ulong start=GetMicrosecondCount();
//--- richiedere le ultime 100 barre su H1
if(CopyRates(Symbol(), PERIOD_H1, 1, InpRates, rates)<InpRates)
{
Print("CopyRates() failed,, Error ", GetLastError());
return;
}
else
{
//---quante barre sono state ricevute e quanto tempo ci è voluto per riceverle
PrintFormat("%s: CopyRates received %d bars in %d ms ",
_Symbol, ArraySize(rates), (GetMicrosecondCount()-start)/1000);
}
//--- impostare il nome del file per la memorizzazione del database
string filename=_Symbol+"_"+EnumToString(PERIOD_H1)+"_"+TimeToString(TimeCurrent())+".sqlite";
StringReplace(filename, ":", "-"); // ":" character is not allowed in file names
//--- aprire/creare il database nella cartella common del terminale
int db=DatabaseOpen(filename, DATABASE_OPEN_READWRITE|DATABASE_OPEN_CREATE|DATABASE_OPEN_COMMON);
if(db==INVALID_HANDLE)
{
Print("Database: ", filename, " open failed with code ", GetLastError());
return;
}
else
Print("Database: ", filename, " opened successfully");
//--- controlla se la tabella RATES esiste
if(DatabaseTableExists(db, "RATES"))
{
//--- rimuove la tabella RATES
if(!DatabaseExecute(db, "DROP TABLE IF EXISTS RATES"))
{
Print("Failed to drop the RATES table with code ", GetLastError());
DatabaseClose(db);
return;
}
}
//--- crea la tabella RATES
if(!DatabaseExecute(db, "CREATE TABLE RATES("
"SYMBOL CHAR(10),"
"TIME INT NOT NULL,"
"OPEN REAL,"
"HIGH REAL,"
"LOW REAL,"
"CLOSE REAL,"
"TICK_VOLUME INT,"
"SPREAD INT,"
"REAL_VOLUME INT);"))
{
Print("DB: ", filename, " create table RATES with code ", GetLastError());
DatabaseClose(db);
return;
}
//--- visualizza l'elenco di tutti i campi della tabella RATES
if(DatabasePrint(db, "PRAGMA TABLE_INFO(RATES)", 0)<0)
{
PrintFormat("DatabasePrint(\"PRAGMA TABLE_INFO(RATES)\") failed, error code=%d at line %d", GetLastError(), __LINE__);
DatabaseClose(db);
return;
}
//--- creare una richiesta parametrizzata per aggiungere barre alla tabella RATES
string sql="INSERT INTO RATES (SYMBOL,TIME,OPEN,HIGH,LOW,CLOSE,TICK_VOLUME,SPREAD,REAL_VOLUME)"
" VALUES (?1,?2,?3,?4,?5,?6,?7,?8,?9)"; // parametri richiesti
int request=DatabasePrepare(db, sql);
if(request==INVALID_HANDLE)
{
PrintFormat("DatabasePrepare() failed with code=%d", GetLastError());
Print("SQL request: ", sql);
DatabaseClose(db);
return;
}
//--- impostare il valore del primo parametro di richiesta
DatabaseBind(request, 0, _Symbol);
//--- ricordare l'ora di inizio prima dell'aggiunta delle barre
start=GetMicrosecondCount();
DatabaseTransactionBegin(db);
int total=ArraySize(rates);
bool request_error=false;
for(int i=0; i<total; i++)
{
//--- impostare i valori dei restanti parametri prima di aggiungere la voce
ResetLastError();
if(!DatabaseBind(request, 1, rates[i].time))
{
PrintFormat("DatabaseBind() failed with code=%d", GetLastError());
PrintFormat("Bar #%d line=%d", i+1, __LINE__);
request_error=true;
break;
}
//--- se la precedente chiamata a DatabaseBind() ha avuto esito positivo, impostare il parametro successivo
if(!request_error && !DatabaseBind(request, 2, rates[i].open))
{
PrintFormat("DatabaseBind() failed with code=%d", GetLastError());
PrintFormat("Bar #%d line=%d", i+1, __LINE__);
request_error=true;
break;
}
if(!request_error && !DatabaseBind(request, 3, rates[i].high))
{
PrintFormat("DatabaseBind() failed with code=%d", GetLastError());
PrintFormat("Bar #%d line=%d", i+1, __LINE__);
request_error=true;
break;
}
if(!request_error && !DatabaseBind(request, 4, rates[i].low))
{
PrintFormat("DatabaseBind() failed with code=%d", GetLastError());
PrintFormat("Bar #%d line=%d", i+1, __LINE__);
request_error=true;
break;
}
if(!request_error && !DatabaseBind(request, 5, rates[i].close))
{
PrintFormat("DatabaseBind() failed with code=%d", GetLastError());
PrintFormat("Bar #%d line=%d", i+1, __LINE__);
request_error=true;
break;
}
if(!request_error && !DatabaseBind(request, 6, rates[i].tick_volume))
{
PrintFormat("DatabaseBind() failed with code=%d", GetLastError());
PrintFormat("Bar #%d line=%d", i+1, __LINE__);
request_error=true;
break;
}
if(!request_error && !DatabaseBind(request, 7, rates[i].spread))
{
PrintFormat("DatabaseBind() failed with code=%d", GetLastError());
PrintFormat("Bar #%d line=%d", i+1, __LINE__);
request_error=true;
break;
}
if(!request_error && !DatabaseBind(request, 8, rates[i].real_volume))
{
PrintFormat("DatabaseBind() failed with code=%d", GetLastError());
PrintFormat("Bar #%d line=%d", i+1, __LINE__);
request_error=true;
break;
}
//--- eseguire una richiesta di inserimento della voce e verificare la presenza di un errore
if(!request_error && !DatabaseRead(request) && (GetLastError()!=ERR_DATABASE_NO_MORE_DATA))
{
PrintFormat("DatabaseRead() failed with code=%d", GetLastError());
DatabaseFinalize(request);
request_error=true;
break;
}
//--- resettare la richiesta prima del successivo aggiornamento del parametro
if(!request_error && !DatabaseReset(request))
{
PrintFormat("DatabaseReset() failed with code=%d", GetLastError());
DatabaseFinalize(request);
request_error=true;
break;
}
} //--- finito tutte le barre attraversate
//--- stato delle transazioni
if(request_error)
{
PrintFormat("Table RATES: failed to add %d bars ", ArraySize(rates));
DatabaseTransactionRollback(db);
DatabaseClose(db);
return;
}
else
{
DatabaseTransactionCommit(db);
PrintFormat("Table RATES: added %d bars in %d ms",
ArraySize(rates), (GetMicrosecondCount()-start)/1000);
}
//--- salva la tabella RATES in un file CSV
string csv_filename=Symbol()+".csv";
long saved=DatabaseExport(db, "SELECT * FROM RATES", csv_filename, DATABASE_EXPORT_HEADER|DATABASE_EXPORT_INDEX|DATABASE_EXPORT_COMMON_FOLDER, ";");
if(saved>0)
Print("Table RATES saved in ", Symbol(), ".csv");
else
Print("DatabaseExport() failed. Error ", GetLastError());
//--- chiude il file del database e lo comunica
DatabaseClose(db);
PrintFormat("Database: %s created and closed", filename);
|