请观看如何免费下载自动交易
请在Telegram上找到我们!
加入我们粉丝页
有趣的脚本?
因此发布一个链接 -
让其他人评价
喜欢这个脚本? 在MetaTrader 5客户端尝试它
显示:
1695
等级:
(16)
已发布:
2013.10.24 16:27
已更新:
2016.11.22 07:33
需要基于此代码的EA交易或指标吗?请在自由职业者服务中订购 进入自由职业者服务

指标从文件中读取买、卖信号,文件位于客户端本地文件夹下子目录中,之后用上、下方向箭头显示信号。为得到文件,您要首先运行 演示_FileWrite 脚本。客户端的本地文件夹位置包含在 TerminalInfoString() 函数。

PrintFormat("客户端本地文件夹路径: %s\\Files\\",TerminalInfoString(TERMINAL_DATA_PATH));
读取数据不光用 FileReadDatetime() 函数, 指标还用到 FileReadNumber() 和 FileReadBool() 函数。

代码:

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//---- 绘制标签1
#property indicator_label1  "UpSignal"
#property indicator_type1   DRAW_ARROW
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  4
//---- 绘制标签2
#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"; // 目录名
//--- global variables
int      ind=0;       // 索引
double   upbuff[];    // 指标上箭头的缓存区
double   downbuff[];  // 指标下箭头的缓存区
bool     sign_buff[]; // 信号数组 (true - 买, false - 卖)
datetime time_buff[]; // 信号产生时间
int      size=0;      // 信号数组尺寸
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- 打开文件
   ResetLastError();
   int file_handle=FileOpen(InpDirectoryName+"//"+InpFileName,FILE_READ|FILE_CSV);
   if(file_handle!=INVALID_HANDLE)
     {
      PrintFormat("%s 打开读文件",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("打开失败 %s 文件, 错误代码 = %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);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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(high,false);
   ArraySetAsSeries(low,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);
  }

由MetaQuotes Ltd译自俄语
原代码: https://www.mql5.com/ru/code/1627

演示_FileWrite 演示_FileWrite

这段脚本简单示例如何使用 FileWrite() 函数

演示_FileReadArray 演示_FileReadArray

这段脚本简单示例如何使用 FileReadArray() 函数

演示_FileWriteDouble 演示_FileWriteDouble

这段脚本简单示例如何使用 FileWriteDouble() 函数

演示_FileReadDouble 演示_FileReadDouble

这段脚本简单示例如何使用 FileReadDouble() 函数