//+------------------------------------------------------------------+
//| Demo_FileIsLineEnding.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 5
#property indicator_plots 1
//---- 图 Label1
#property indicator_label1 "Overbought & Oversold"
#property indicator_type1 DRAW_COLOR_BARS
#property indicator_color1 clrRed, clrBlue
#property indicator_style1 STYLE_SOLID
#property indicator_width1 2
//--- 读取数据的参数
input string InpFileName="RSI.csv"; // 文件名称
input string InpDirectoryName="Data"; // 目录名称
//--- 指标缓冲区
double open_buff[];
double high_buff[];
double low_buff[];
double close_buff[];
double color_buff[];
//--- 买超变量
int ovb_ind=0;
int ovb_size=0;
datetime ovb_time[];
//--- 卖超变量
int ovs_ind=0;
int ovs_size=0;
datetime ovs_time[];
//+------------------------------------------------------------------+
//| 自定义指标初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 默认数组大小的变量
int ovb_def_size=100;
int ovs_def_size=100;
//--- 为数组分配内存
ArrayResize(ovb_time,ovb_def_size);
ArrayResize(ovs_time,ovs_def_size);
//--- 打开文件
ResetLastError();
int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_CSV|FILE_ANSI);
if(file_handle!=INVALID_HANDLE)
{
PrintFormat("%s file is available for reading",InpFileName);
PrintFormat("File path: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
double value;
//--- 读取文件数据
while(!FileIsEnding(file_handle))
{
//--- 读取字符串的第一个值
value=FileReadNumber(file_handle);
//--- 根据函数结果读取不同的数组
if(value>=70)
ReadData(file_handle,ovb_time,ovb_size,ovb_def_size);
else
ReadData(file_handle,ovs_time,ovs_size,ovs_def_size);
}
//--- 关闭文件
FileClose(file_handle);
PrintFormat("Data is written, %s file is closed",InpFileName);
}
else
{
PrintFormat("Failed to open %s file, Error code = %d",InpFileName,GetLastError());
return(INIT_FAILED);
}
//--- 绑定数组
SetIndexBuffer(0,open_buff,INDICATOR_DATA);
SetIndexBuffer(1,high_buff,INDICATOR_DATA);
SetIndexBuffer(2,low_buff,INDICATOR_DATA);
SetIndexBuffer(3,close_buff,INDICATOR_DATA);
SetIndexBuffer(4,color_buff,INDICATOR_COLOR_INDEX);
//---- 设置图表上看不到的指标值
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| 读取文件的字符串数据 |
//+------------------------------------------------------------------+
void ReadData(const int file_handle,datetime &arr[],int &size,int &def_size)
{
bool flag=false;
//--- 阅读直至到达字符串或文件的末尾
while(!FileIsLineEnding(file_handle) && !FileIsEnding(file_handle))
{
//--- 阅读数量后转变这里
if(flag)
FileReadNumber(file_handle);
//--- 存储当前日期
arr[size]=FileReadDatetime(file_handle);
size++;
//--- 必要时增加数组大小
if(size==def_size)
{
def_size+=100;
ArrayResize(arr,def_size);
}
//--- 滑过第一迭代
flag=true;
}
}
//+------------------------------------------------------------------+
//| 自定义指标迭代函数 |
//+------------------------------------------------------------------+
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(open,false);
ArraySetAsSeries(high,false);
ArraySetAsSeries(low,false);
ArraySetAsSeries(close,false);
//--- 还未处理的柱的循环
for(int i=prev_calculated;i<rates_total;i++)
{
//--- 默认为0
open_buff[i]=0;
high_buff[i]=0;
low_buff[i]=0;
close_buff[i]=0;
color_buff[i]=0;
//--- 检查任何日期是否仍然存在
if(ovb_ind<ovb_size)
for(int j=ovb_ind;j<ovb_size;j++)
{
//--- 如果日期一致,柱形在超买区域
if(time[i]==ovb_time[j])
{
open_buff[i]=open[i];
high_buff[i]=high[i];
low_buff[i]=low[i];
close_buff[i]=close[i];
//--- 0 - 红色
color_buff[i]=0;
//--- 增加计数器
ovb_ind=j+1;
break;
}
}
//--- 检查是否任何数据仍然存在
if(ovs_ind<ovs_size)
for(int j=ovs_ind;j<ovs_size;j++)
{
//--- 如果日期一致,柱形在超卖区域
if(time[i]==ovs_time[j])
{
open_buff[i]=open[i];
high_buff[i]=high[i];
low_buff[i]=low[i];
close_buff[i]=close[i];
//--- 1 - 蓝色
color_buff[i]=1;
//--- 增加计数器
ovs_ind=j+1;
break;
}
}
}
//--- 返回prev_calculated值,以便下次调用
return(rates_total);
}
//+------------------------------------------------------------------+
//| ChartEvent 事件处理程序 |
//+------------------------------------------------------------------+
void OnChartEvent(const int id,
const long &lparam,
const double &dparam,
const string &sparam
)
{
//--- 根据比例更改指标宽度
if(ChartGetInteger(0,CHART_SCALE)>3)
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,2);
else
PlotIndexSetInteger(0,PLOT_LINE_WIDTH,1);
}
|