input int InpRates=100;
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
MqlRates rates[];
//--- remember the start time before receiving bars
ulong start=GetMicrosecondCount();
//--- request the last 100 bars on H1
if(CopyRates(Symbol(), PERIOD_H1, 1, InpRates, rates)<InpRates)
{
Print("CopyRates() failed,, Error ", GetLastError());
return;
}
else
{
//--- how many bars were received and how much time it took to receive them
PrintFormat("%s: CopyRates received %d bars in %d ms ",
_Symbol, ArraySize(rates), (GetMicrosecondCount()-start)/1000);
}
//--- set the file name for storing the database
string filename=_Symbol+"_"+EnumToString(PERIOD_H1)+"_"+TimeToString(TimeCurrent())+".sqlite";
StringReplace(filename, ":", "-"); // ":" character is not allowed in file names
//--- open/create the database in the common terminal folder
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");
//--- check if the RATES table exists
if(DatabaseTableExists(db, "RATES"))
{
//--- remove the RATES table
if(!DatabaseExecute(db, "DROP TABLE IF EXISTS RATES"))
{
Print("Failed to drop the RATES table with code ", GetLastError());
DatabaseClose(db);
return;
}
}
//--- create the RATES table
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;
}
//--- display the list of all fields in the RATES table
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;
}
//--- create a parametrized request to add bars to the RATES table
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)"; // request parameters
int request=DatabasePrepare(db, sql);
if(request==INVALID_HANDLE)
{
PrintFormat("DatabasePrepare() failed with code=%d", GetLastError());
Print("SQL request: ", sql);
DatabaseClose(db);
return;
}
//--- set the value of the first request parameter
DatabaseBind(request, 0, _Symbol);
//--- remember the start time before adding bars to the RATES table
start=GetMicrosecondCount();
DatabaseTransactionBegin(db);
int total=ArraySize(rates);
bool request_error=false;
for(int i=0; i<total; i++)
{
//--- set the values of the remaining parameters before adding the entry
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;
}
//--- if the previous DatabaseBind() call was successful, set the next parameter
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;
}
//--- execute a request for inserting the entry and check for an error
if(!request_error && !DatabaseRead(request) && (GetLastError()!=ERR_DATABASE_NO_MORE_DATA))
{
PrintFormat("DatabaseRead() failed with code=%d", GetLastError());
DatabaseFinalize(request);
request_error=true;
break;
}
//--- reset the request before the next parameter update
if(!request_error && !DatabaseReset(request))
{
PrintFormat("DatabaseReset() failed with code=%d", GetLastError());
DatabaseFinalize(request);
request_error=true;
break;
}
} //--- done going through all the bars
//--- transactions status
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);
}
//--- save the RATES table to a CSV file
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());
//--- close the database file and inform of that
DatabaseClose(db);
PrintFormat("Database: %s created and closed", filename);
|