input int InpRates=100;
//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
MqlRates rates[];
//--- 记住收到柱形图的开始时间
ulong start=GetMicrosecondCount();
//--- 在H1上请求最后100柱形图
if(CopyRates(Symbol(), PERIOD_H1, 1, InpRates, rates)<InpRates)
{
Print("CopyRates() failed,, Error ", GetLastError());
return;
}
else
{
//--- 接收多少柱形图,以及接收所花费的时间
PrintFormat("%s: CopyRates received %d bars in %d ms ",
_Symbol, ArraySize(rates), (GetMicrosecondCount()-start)/1000);
}
//--- 设置存储数据库的文件名称
string filename=_Symbol+"_"+EnumToString(PERIOD_H1)+"_"+TimeToString(TimeCurrent())+".sqlite";
StringReplace(filename, ":", "-"); // 文件名中不允许使用":"字符
//--- 在常规程序端文件夹中打开/创建数据库
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");
//--- 检查RATES表格是否存在
if(DatabaseTableExists(db, "RATES"))
{
//--- 移除RATES表格
if(!DatabaseExecute(db, "DROP TABLE IF EXISTS RATES"))
{
Print("Failed to drop the RATES table with code ", GetLastError());
DatabaseClose(db);
return;
}
}
//--- 创建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;
}
//--- 显示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;
}
//--- 创建参数化请求,将柱形图添加到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)"; // 请求参数
int request=DatabasePrepare(db, sql);
if(request==INVALID_HANDLE)
{
PrintFormat("DatabasePrepare() failed with code=%d", GetLastError());
Print("SQL request: ", sql);
DatabaseClose(db);
return;
}
//--- 设置第一个请求参数的值
DatabaseBind(request, 0, _Symbol);
//--- 记住将柱形图添加到RATES表格的开始时间
start=GetMicrosecondCount();
DatabaseTransactionBegin(db);
int total=ArraySize(rates);
bool request_error=false;
for(int i=0; i<total; i++)
{
//--- 在添加条目之前设置其余参数的值
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;
}
//--- 如果之前的DatabaseBind()调用成功,则设置下一个参数
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;
}
//--- 执行插入条目的请求并检查错误
if(!request_error && !DatabaseRead(request) && (GetLastError()!=ERR_DATABASE_NO_MORE_DATA))
{
PrintFormat("DatabaseRead() failed with code=%d", GetLastError());
DatabaseFinalize(request);
request_error=true;
break;
}
//--- 在下一次参数更新之前重置请求
if(!request_error && !DatabaseReset(request))
{
PrintFormat("DatabaseReset() failed with code=%d", GetLastError());
DatabaseFinalize(request);
request_error=true;
break;
}
} //--- 完成检查所有柱形图
//--- 交易事务状态
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);
}
//--- 将RATES表格保存为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());
//--- 关闭数据库文件并告知
DatabaseClose(db);
PrintFormat("Database: %s created and closed", filename);
|