MQL5教程 05 指标开发实战:双色线、双线变色MACD、跨时间周期均线

一、双色线指标

在这里插入图片描述
这里的类型中,Color开头的,是可以选择多个颜色的。


#property indicator_chart_window
#property indicator_buffers 18
#property indicator_plots   7
//--- plot xian
#property indicator_label1  "xian"
#property indicator_type1   DRAW_COLOR_LINE
#property indicator_color1  clrRed,clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  3
//--- plot zhu
#property indicator_label2  "zhu"
#property indicator_type2   DRAW_COLOR_HISTOGRAM
#property indicator_color2  clrRed,clrYellow
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot zhu2
#property indicator_label3  "zhu2"
#property indicator_type3   DRAW_COLOR_HISTOGRAM2
#property indicator_color3  clrRed,clrYellow
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot up
#property indicator_label4  "up"
#property indicator_type4   DRAW_COLOR_ARROW
#property indicator_color4  clrFuchsia,clrRed
#property indicator_style4  STYLE_SOLID
#property indicator_width4  3
//--- plot down
#property indicator_label5  "down"
#property indicator_type5   DRAW_COLOR_ARROW
#property indicator_color5  clrPowderBlue,clrWhite
#property indicator_style5  STYLE_SOLID
#property indicator_width5  3
//--- plot lazhu
#property indicator_label6  "lazhu"
#property indicator_type6   DRAW_COLOR_CANDLES
#property indicator_color6  clrAqua,clrBlue
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1
//--- plot xian2
#property indicator_label7  "xian2"
#property indicator_type7   DRAW_COLOR_LINE
#property indicator_color7  clrRed,clrYellow
#property indicator_style7  STYLE_SOLID
#property indicator_width7  3
//--- indicator buffers
double         xianBuffer[];
double         xianColors[];
double         zhuBuffer[];
double         zhuColors[];
double         zhu2Buffer1[];
double         zhu2Buffer2[];
double         zhu2Colors[];
double         upBuffer[];
double         upColors[];
double         downBuffer[];
double         downColors[];
double         lazhuBuffer1[];
double         lazhuBuffer2[];
double         lazhuBuffer3[];
double         lazhuBuffer4[];
double         lazhuColors[];
double         xian2Buffer[];
double         xian2Colors[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
// 指标句柄
int ma5_h;
int ma10_h;

int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,xianBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,xianColors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(2,zhuBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,zhuColors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(4,zhu2Buffer1,INDICATOR_DATA);
   SetIndexBuffer(5,zhu2Buffer2,INDICATOR_DATA);
   SetIndexBuffer(6,zhu2Colors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(7,upBuffer,INDICATOR_DATA);
   SetIndexBuffer(8,upColors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(9,downBuffer,INDICATOR_DATA);
   SetIndexBuffer(10,downColors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(11,lazhuBuffer1,INDICATOR_DATA);
   SetIndexBuffer(12,lazhuBuffer2,INDICATOR_DATA);
   SetIndexBuffer(13,lazhuBuffer3,INDICATOR_DATA);
   SetIndexBuffer(14,lazhuBuffer4,INDICATOR_DATA);
   SetIndexBuffer(15,lazhuColors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(16,xian2Buffer,INDICATOR_DATA);
   SetIndexBuffer(17,xian2Colors,INDICATOR_COLOR_INDEX);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(3,PLOT_ARROW,225);
   PlotIndexSetInteger(4,PLOT_ARROW,226);
   
   ma5_h = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE);
   ma10_h = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE);
   
   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[])
  {
   double ma5[];
   CopyBuffer(ma5_h, 0, 0, rates_total, ma5);
   double ma10[];
   CopyBuffer(ma10_h, 0, 0, rates_total, ma10);
   
   int start = 100;  // 如果写0,会下标越界
   
   if(prev_calculated > 0)
     {
      start = prev_calculated - 1;
     }
     
   for(int i=start; i<rates_total; i++)
     {
      xianBuffer[i] = ma5[i];
      xian2Buffer[i] = ma10[i];
      
      if(close[i] > ma5[i])
        {
         xianColors[i] = 0;   // indicator_color1中索引为0的颜色
        }
      else
        {
         xianColors[i] = 1;   // indicator_color1中索引为1的颜色
        }
        
      if(close[i] > ma10[i])
        {
         xian2Colors[i] = 0;
        }
      else
        {
         xian2Colors[0] = 1;
        }
        
      // 金叉
      if(xianBuffer[i-1]<xian2Buffer[i-1] && xianBuffer[i]>=xian2Buffer[i])
        {
         upBuffer[i] = xian2Buffer[i] - 100*Point();
         if((xianBuffer[i]-xian2Buffer[i]) < 50*Point())
           {
            upColors[i] = 0;
           }
         else
           {
            upColors[i] = 1;
           }
        }
      // 死叉
      if(xianBuffer[i-1]>xian2Buffer[i-1] && xianBuffer[i]<=xian2Buffer[i])
        {
         downBuffer[i] = xian2Buffer[i] + 100*Point();
         if((xian2Buffer[i]-xianBuffer[i]) < 50*Point())
           {
            downColors[i] = 0;
           }
         else
           {
            downColors[i] = 1;
           }
        }
      // HISTOGRAM是从上界画到0
      //zhuBuffer[i] = ma5[i];
      
      // HISTOGRAM2是从上界画到下界,所以有两个数值缓冲区
      zhu2Buffer1[i] = ma5[i];
      zhu2Buffer2[i] = ma10[i];
      if(xianBuffer[i] >= xian2Buffer[i])
        {
         zhu2Colors[i] = 0;
        }
      else
        {
         zhu2Colors[i] = 1;
        }
        
      lazhuBuffer1[i] = open[i] + 100*Point();
      lazhuBuffer2[i] = high[i] + 100*Point();
      lazhuBuffer3[i] = low[i] + 100*Point();
      lazhuBuffer4[i] = close[i] + 100*Point();
      if(lazhuBuffer1[i]>lazhuBuffer4[i])
        {
         lazhuColors[i] = 0;
        }
      else
        {
         lazhuColors[i] = 1;
        }
     }
   
   return(rates_total);
  }

二、双线变色MACD指标

在这里插入图片描述


#property indicator_separate_window
#property indicator_buffers 8
#property indicator_plots   6
//--- plot macd
#property indicator_label1  "macd"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot signal
#property indicator_label2  "signal"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrYellow
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- plot cha
#property indicator_label3  "cha"
#property indicator_type3   DRAW_COLOR_HISTOGRAM
#property indicator_color3  clrWhite,clrAqua,clrDarkGray
#property indicator_style3  STYLE_SOLID
#property indicator_width3  1
//--- plot up
#property indicator_label4  "up"
#property indicator_type4   DRAW_ARROW
#property indicator_color4  clrLimeGreen
#property indicator_style4  STYLE_SOLID
#property indicator_width4  1
//--- plot down
#property indicator_label5  "down"
#property indicator_type5   DRAW_ARROW
#property indicator_color5  clrTomato
#property indicator_style5  STYLE_SOLID
#property indicator_width5  1
//--- plot tianchong
#property indicator_label6  "tianchong"
#property indicator_type6   DRAW_FILLING
#property indicator_color6  clrRed,clrYellow
#property indicator_style6  STYLE_SOLID
#property indicator_width6  1

input int InpFastEMA=12;   // Fast EMA period
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         macdBuffer[];
double         signalBuffer[];
double         chaBuffer[];
double         chaColors[];
double         upBuffer[];
double         downBuffer[];
double         tianchongBuffer1[];
double         tianchongBuffer2[];

int macd_h; // 指标句柄
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,macdBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,signalBuffer,INDICATOR_DATA);
   SetIndexBuffer(2,chaBuffer,INDICATOR_DATA);
   SetIndexBuffer(3,chaColors,INDICATOR_COLOR_INDEX);
   SetIndexBuffer(4,upBuffer,INDICATOR_DATA);
   SetIndexBuffer(5,downBuffer,INDICATOR_DATA);
   SetIndexBuffer(6,tianchongBuffer1,INDICATOR_DATA);
   SetIndexBuffer(7,tianchongBuffer2,INDICATOR_DATA);
//--- setting a code from the Wingdings charset as the property of PLOT_ARROW
   PlotIndexSetInteger(3,PLOT_ARROW,225); // 这里的索引号为绘图索引,不是缓冲区索引
   PlotIndexSetInteger(4,PLOT_ARROW,226);
   PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,0); // 将0设置为空值,空值不在图表上绘制
   PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,0);
   
   macd_h = iMACD(NULL,0,InpFastEMA,InpSlowEMA,InpSignalSMA,InpAppliedPrice);
   
   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[])
  {
   double macd[];
   double signal[];
   CopyBuffer(macd_h, 0, 0, rates_total, macd);
   CopyBuffer(macd_h, 1, 0, rates_total, signal);
   int start = 100;
   if(prev_calculated>0)
     {
      start = prev_calculated - 1;
     }
   
   for(int i=start; i<rates_total; i++)
     {
      macdBuffer[i] = macd[i];
      signalBuffer[i] = signal[i];
      chaBuffer[i] = macd[i] - signal[i];
      if(chaBuffer[i]>0)
        {
         if(chaBuffer[i]>chaBuffer[i-1])
           {
            chaColors[i] = 0;
           }
         else
           {
            chaColors[i] = 2;
           }
        }
      else
        {
         if(chaBuffer[i]<chaBuffer[i-1])
           {
            chaColors[i] = 1;
           }
         else
           {
            chaColors[i] = 2;
           }
        }
      
      // 金叉
      if(macdBuffer[i]>signalBuffer[i] && macdBuffer[i-1]<signalBuffer[i-1])
        {
         upBuffer[i] = signalBuffer[i] - 50*Point();
        }
      // 死叉
      if(macdBuffer[i]<signalBuffer[i] && macdBuffer[i-1]>signalBuffer[i-1])
        {
         downBuffer[i] = signalBuffer[i] + 50*Point();
        }
      
      // 上界 - 下界:大于0,填充第一种颜色;小于0,填充第二种颜色
      tianchongBuffer1[i] = macdBuffer[i];   // 上界
      tianchongBuffer2[i] = signalBuffer[i]; // 下界
     }
     
   return(rates_total);
  }

三、跨时间周期均线

在当前图表上显示不同时间周期(比当前周期要大的周期)的均线。

在这里插入图片描述


在这里插入图片描述


#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots   2
//--- plot maxiao
#property indicator_label1  "maxiao"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrRed
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- plot mada
#property indicator_label2  "mada"
#property indicator_type2   DRAW_LINE
#property indicator_color2  clrYellow
#property indicator_style2  STYLE_SOLID
#property indicator_width2  1
//--- input parameters
input ENUM_TIMEFRAMES TimeFrame = PERIOD_H4;
input int maxiao=5;
input int mada=10;
input int KCount=3000;  // 显示K线根数
//--- indicator buffers
double         maxiaoBuffer[];
double         madaBuffer[];

int maxiao_h;
int mada_h;

int OnInit()
  {
   ArraySetAsSeries(maxiaoBuffer, true);
   ArraySetAsSeries(madaBuffer, true);
   SetIndexBuffer(0,maxiaoBuffer,INDICATOR_DATA);
   SetIndexBuffer(1,madaBuffer,INDICATOR_DATA);
   maxiao_h = iMA(NULL, TimeFrame, maxiao, 0, MODE_SMA, PRICE_CLOSE);
   mada_h = iMA(NULL, TimeFrame, mada, 0, MODE_SMA, PRICE_CLOSE);

   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[])
  {
   double maxiaob[];
   ArraySetAsSeries(maxiaob, true);
   CopyBuffer(maxiao_h,0,0,rates_total,maxiaob);
   
   double madab[];
   ArraySetAsSeries(madab, true);
   CopyBuffer(mada_h,0,0,rates_total,madab);
   
   datetime timeda[];
   ArraySetAsSeries(timeda, true);
   CopyTime(NULL, TimeFrame, 0, rates_total, timeda);
   
   ArraySetAsSeries(time, true);
   
   int y = 0;
   for(int i=0; i<KCount; i++)
     {
      if(time[i] < timeda[y]) y++;
      maxiaoBuffer[i] = maxiaob[y];
      madaBuffer[i] = madab[y];
     }
   
   return(rates_total);
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值