//+------------------------------------------------------------------+
//| Demo_FileReadBool.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 2
#property indicator_plots 2
//---- 图 Label1
#property indicator_label1 "UpSignal"
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 4
//---- 图 Label2
#property indicator_label2 "DownSignal"
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrRed
#property indicator_style2 STYLE_SOLID
#property indicator_width2 4
//--- 读取数据的参数
input string InpFileName="MACD.csv"; // 文件名称
input string InpDirectoryName="Data"; // 目录名称
//--- 全局变量
int ind=0; // 指数
double upbuff[]; // 向上箭头的指标缓冲区
double downbuff[]; // 向下箭头的指标缓冲区
bool sign_buff[]; // 信号数组 (true - 买入, false - 卖出)
datetime time_buff[]; // 信号到达时间数组
int size=0; // 信号数组大小
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 打开文件
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_CSV);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is open for reading",InpFileName);
//--- 首先,阅读信号的数量
size=(int)FileReadNumber(file_handle);
//--- 为数组分配内存
ArrayResize(sign_buff,size);
ArrayResize(time_buff,size);
//--- 读取文件数据
for(int i=0;i<size;i++)
{
//--- 信号时间
time_buff[i]=FileReadDatetime(file_handle);
//--- 信号价值
sign_buff[i]=FileReadBool(file_handle);
}
//--- 关闭文件
FileClose(file_handle);
}
else
{
PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
return(INIT_FAILED);
}
//--- 绑定数组
SetIndexBuffer(0,upbuff,INDICATOR_DATA);
SetIndexBuffer(1,downbuff,INDICATOR_DATA);
//--- 设置PLOT_ARROW中绘制的交易品种代码
PlotIndexSetInteger(0,PLOT_ARROW,241);
PlotIndexSetInteger(1,PLOT_ARROW,242);
//---- 设置图表上看不到的指标值
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 自定义指标迭代函数 |
//+------------------------------------------------------------------+
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(low,false);
ArraySetAsSeries(high,false);
//--- 还未处理的柱的循环
for(int i=prev_calculated;i<rates_total;i++)
{
//--- 默认为0
upbuff[i]=0;
downbuff[i]=0;
//--- 检查任何日期是否仍然存在
if(ind<size)
{
for(int j=ind;j<size;j++)
{
//--- 如果日期一致,使用文件的值
if(time[i]==time_buff[j])
{
//--- 根据信号绘制箭头
if(sign_buff[j])
upbuff[i]=high[i];
else
downbuff[i]=low[i];
//--- 增加计数器
ind=j+1;
break;
}
}
}
}
//--- 返回prev_calculated 值,以便下次调用
return(rates_total);
}
|