双线MT5指标自定义指标双线MACD指标

发布时间:2016-10-28 17:36 编辑:张志红 来源:福汇官网特约 浏览量:
 

双线MT5指标自定义指标双线MACD指标 - fxcm福汇宇汇国际图片 - yuhuifx.net

MT5 已经试用一段时间了,其指标在编程方面有很多改变,这里是源于网络的一个能用于MT5的MACD指标。

//+------------------------------------------------------------------+
//|                                                      MACD_DL.mq5 |
//|                                          Copyright 2009, Mr.Zhjp |
//|                                            mail://mrzhjp@163.com |
//+------------------------------------------------------------------+
#property copyright   "2009, Mr.Zhjp"
#property link        "mrzhjp@163.com"
#property description "Moving Average Convergence/Divergence"
#include
//--- indicator settings
#property indicator_separate_window
#property indicator_buffers 6
#property indicator_plots   4                         // New
#property indicator_type1   DRAW_LINE
#property indicator_type2   DRAW_LINE
#property indicator_type3   DRAW_HISTOGRAM
#property indicator_type4   DRAW_HISTOGRAM
#property indicator_color1  Red
#property indicator_color2  Green
#property indicator_color3  Red
#property indicator_color4  Green
#property indicator_width1  1
#property indicator_width2  1
#property indicator_width3  1
#property indicator_width4  1
#property indicator_label1  "DIFF"
#property indicator_label2  "DEA"
#property indicator_label3  "MACD"
#property indicator_label4  "MACD"
//--- input parameters
input int                InpFastEMA=12;               // Fast EMA period  // New
input int                InpSlowEMA=26;               // Slow EMA period
input int                InpSignalSMA=9;              // Signal SMA period
input ENUM_APPLIED_PRICE InpAppliedPrice=PRICE_CLOSE; // Applied price
//--- indicator buffers
double                   ExtDIFFBuffer[];
double                   ExtDEABuffer[];
double                   ExtMACDRBuffer[];
double                   ExtMACDGBuffer[];
double                   ExtFastMaBuffer[];
double                   ExtSlowMaBuffer[];
//--- MA handles
int                      ExtFastMaHandle;
int                      ExtSlowMaHandle;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtDIFFBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,ExtDEABuffer,INDICATOR_DATA);
   SetIndexBuffer(2,ExtMACDRBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,ExtMACDGBuffer,INDICATOR_DATA);
   SetIndexBuffer(4,ExtFastMaBuffer,INDICATOR_CALCULATIONS);
   SetIndexBuffer(5,ExtSlowMaBuffer,INDICATOR_CALCULATIONS);
//--- sets first bar from what index will be drawn
   PlotIndexSetInteger(1,PLOT_DRAW_BEGIN,InpSignalSMA-1);              // New
//--- name for Dindicator subwindow label
  IndicatorSetString(INDICATOR_SHORTNAME,"MACD_DL("+string(InpFastEMA)+","+string(InpSlowEMA)+","+string(InpSignalSMA)+")");
//--- get MA handles
   ExtFastMaHandle=iMA(NULL,0,InpFastEMA,0,MODE_EMA,InpAppliedPrice);
   ExtSlowMaHandle=iMA(NULL,0,InpSlowEMA,0,MODE_EMA,InpAppliedPrice);
//--- initialization done
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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 &TickVolume[],
                const long &Volume[],
                const int &Spread[])
  {
//--- check for data
   if(rates_total
      return(0);
//--- not all data may be calculated
   int calculated=BarsCalculated(ExtFastMaHandle);
   if(calculated
     {
      Print("Not all data of ExtFastMaHandle is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
   calculated=BarsCalculated(ExtSlowMaHandle);
   if(calculated
     {
      Print("Not all data of ExtSlowMaHandle is calculated (",calculated,"bars ). Error",GetLastError());
      return(0);
     }
//--- we can copy not all data
   int to_copy;
   if(prev_calculated>rates_total || prev_calculated<0) to_copy=rates_total;
   else
     {
      to_copy=rates_total-prev_calculated;
      if(prev_calculated>0) to_copy++;
     }
//--- get Fast EMA buffer
   if(CopyBuffer(ExtFastMaHandle,0,0,to_copy,ExtFastMaBuffer)<=0)
     {
      Print("Getting fast EMA is failed! Error",GetLastError());
      return(0);
     }
//--- get SlowSMA buffer
   if(CopyBuffer(ExtSlowMaHandle,0,0,to_copy,ExtSlowMaBuffer)<=0)
     {
      Print("Getting slow SMA is failed! Error",GetLastError());
      return(0);
     }
//---
   int limit;
   if(prev_calculated==0)
      limit=0;
   else limit=prev_calculated-1;
//--- calculate DIFF
   for(int i=limit;i
      ExtDIFFBuffer[i]=ExtFastMaBuffer[i]-ExtSlowMaBuffer[i];
//--- calculate DEA
   SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,ExtDIFFBuffer,ExtDEABuffer);
//--- calculate MACD
   for(int i=limit;i
   {
      if(ExtDIFFBuffer[i] > ExtDEABuffer[i]) 
      {
          ExtMACDRBuffer[i] = 2*(ExtDIFFBuffer[i]-ExtDEABuffer[i]); 
          ExtMACDGBuffer[i] = 0;
      }
      else
      {
          ExtMACDGBuffer[i] = 2*(ExtDIFFBuffer[i]-ExtDEABuffer[i]); 
          ExtMACDRBuffer[i] = 0;
      }
   }      
//--- OnCalculate done. Return new prev_calculated.
   return(rates_total);
  }
//+------------------------------------------------------------------+

不会操作的点击下载原文件:http://www.yuhuifx.net/ziyuan/FXCMMT5-zhibiao-4508.html.zip

已经打包用rat压缩软件进行解压


  • 咨询客服
  • 模拟账户
  • 立即开户

开设账户


真实开户


模拟开户


福汇赠金送金


福汇平台下载


申请福汇代理商IB


宇汇国际客服电话


随机文章

在线客服
QQ号:
872130197