有您的持续关注
我们会做得更好

主图绘制MACD背离的指标MACD_Histogram_3



MACD_Histogram_3指标用线段和箭头在主图中绘制MACD背离。背离可以参与行情分析,但在强势市场中背离可以重复发生,所以背离并不能单独成为开仓条件。

MACD_Histogram_3指标图表效果如下:绘制MACD背离指标MACD_Histogram_3

MACD_Histogram_3指标源码如下:

//+------------------------------------------------------------------+
//|                                             MACD_Histogram_3.mq4 |
//|                                     Copyright 2016, QQ:125808047 |
//+------------------------------------------------------------------+
#property copyright "www.125808047.com"
#property link      "http://www.125808047.com/"

#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 Turquoise
#property indicator_color2 Turquoise
#property indicator_color3 Turquoise
#property indicator_color4 Turquoise
#property indicator_color5 Turquoise
#property indicator_color6 Turquoise
#property indicator_color7 Red
#property indicator_color8 Red

extern int FindLen = 25;
extern int FastEMA = 12;
extern int SlowEMA = 26;
extern int SignalSMA = 9;
double g_ibuf_92[];
double g_ibuf_96[];
double g_ibuf_100[];
double g_ibuf_104[];
double g_ibuf_108[];
double g_ibuf_112[];
double g_ibuf_116[];
double g_ibuf_120[];
int gia_124[];
int gi_128 = 0;
int gia_132[];
int gi_136 = 0;
int gia_140[];
int gia_144[];
int gi_148 = 0;
int gia_152[];
int gia_156[];
int gi_160 = 0;

int init() {
   IndicatorBuffers(8);
   SetIndexStyle(0, DRAW_LINE);
   SetIndexBuffer(0, g_ibuf_92);
   SetIndexStyle(1, DRAW_LINE);
   SetIndexBuffer(1, g_ibuf_96);
   SetIndexStyle(2, DRAW_LINE);
   SetIndexBuffer(2, g_ibuf_100);
   SetIndexStyle(3, DRAW_LINE);
   SetIndexBuffer(3, g_ibuf_104);
   SetIndexStyle(4, DRAW_LINE);
   SetIndexBuffer(4, g_ibuf_108);
   SetIndexStyle(5, DRAW_LINE);
   SetIndexBuffer(5, g_ibuf_112);
   SetIndexStyle(6, DRAW_ARROW);
   SetIndexBuffer(6, g_ibuf_116);
   SetIndexArrow(6, 234);
   SetIndexStyle(7, DRAW_ARROW);
   SetIndexBuffer(7, g_ibuf_120);
   SetIndexArrow(7, 233);
   SetIndexEmptyValue(0, 0.0);
   SetIndexEmptyValue(1, 0.0);
   SetIndexEmptyValue(2, 0.0);
   SetIndexEmptyValue(3, 0.0);
   SetIndexEmptyValue(4, 0.0);
   SetIndexEmptyValue(5, 0.0);
   SetIndexEmptyValue(6, 0.0);
   SetIndexEmptyValue(7, 0.0);
   return (0);
}

int start() {
   int li_4;
   int li_8;
   int li_12;
   double ld_20;
   CalculateData();
   ArrayInitialize(g_ibuf_92, 0.0);
   ArrayInitialize(g_ibuf_96, 0.0);
   ArrayInitialize(g_ibuf_100, 0.0);
   ArrayInitialize(g_ibuf_104, 0.0);
   ArrayInitialize(g_ibuf_108, 0.0);
   ArrayInitialize(g_ibuf_112, 0.0);
   ArrayInitialize(g_ibuf_116, 0.0);
   ArrayInitialize(g_ibuf_120, 0.0);
   for (int index_0 = 0; index_0 < gi_148; index_0++) {
      li_4 = gia_140[index_0];
      li_8 = gia_144[index_0];
      li_12 = 1;
      for (int li_16 = li_4; li_16 >= li_8; li_16--) {
         if (li_12 == 1 && g_ibuf_92[li_16] != 0.0) li_12 = 2;
         if (li_12 == 2 && g_ibuf_96[li_16] != 0.0) li_12 = 3;
         if (li_12 == 3 && g_ibuf_100[li_16] != 0.0) li_12 = 4;
         if (li_12 == 4 && g_ibuf_104[li_16] != 0.0) li_12 = 5;
         if (li_12 == 5 && g_ibuf_108[li_16] != 0.0) li_12 = 6;
         if (li_12 == 6 && g_ibuf_112[li_16] != 0.0) li_12 = -1;
      }
      ld_20 = (High[li_8] - High[li_4]) / (li_4 - li_8);
      switch (li_12) {
      case 1:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_92[li_16] = High[li_4] + (li_4 - li_16) * ld_20;
         break;
      case 2:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_96[li_16] = High[li_4] + (li_4 - li_16) * ld_20;
         break;
      case 3:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_100[li_16] = High[li_4] + (li_4 - li_16) * ld_20;
         break;
      case 4:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_104[li_16] = High[li_4] + (li_4 - li_16) * ld_20;
         break;
      case 5:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_108[li_16] = High[li_4] + (li_4 - li_16) * ld_20;
         break;
      case 6:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_112[li_16] = High[li_4] + (li_4 - li_16) * ld_20;
      }
      g_ibuf_116[li_8] = High[li_8] + 5.0 * Point;
   }
   for (index_0 = 0; index_0 < gi_160; index_0++) {
      li_4 = gia_152[index_0];
      li_8 = gia_156[index_0];
      li_12 = 1;
      for (li_16 = li_4; li_16 >= li_8; li_16--) {
         if (li_12 == 1 && g_ibuf_92[li_16] != 0.0) li_12 = 2;
         if (li_12 == 2 && g_ibuf_96[li_16] != 0.0) li_12 = 3;
         if (li_12 == 3 && g_ibuf_100[li_16] != 0.0) li_12 = 4;
         if (li_12 == 4 && g_ibuf_104[li_16] != 0.0) li_12 = 5;
         if (li_12 == 5 && g_ibuf_108[li_16] != 0.0) li_12 = 6;
         if (li_12 == 6 && g_ibuf_112[li_16] != 0.0) li_12 = -1;
      }
      ld_20 = (Low[li_8] - Low[li_4]) / (li_4 - li_8);
      switch (li_12) {
      case 1:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_92[li_16] = Low[li_4] + (li_4 - li_16) * ld_20;
         break;
      case 2:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_96[li_16] = Low[li_4] + (li_4 - li_16) * ld_20;
         break;
      case 3:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_100[li_16] = Low[li_4] + (li_4 - li_16) * ld_20;
         break;
      case 4:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_104[li_16] = Low[li_4] + (li_4 - li_16) * ld_20;
         break;
      case 5:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_108[li_16] = Low[li_4] + (li_4 - li_16) * ld_20;
         break;
      case 6:
         for (li_16 = li_4; li_16 >= li_8; li_16--) g_ibuf_112[li_16] = Low[li_4] + (li_4 - li_16) * ld_20;
      }
      g_ibuf_120[li_8] = Low[li_8] - 5.0 * Point;
   }
   return (0);
}

void CalculateData() {
   int count_4;
   int lia_20[10];
   int lia_24[10];
   bool li_28;
   double imacd_32;
   double imacd_40;
   double imacd_48;
   int li_16 = 2;
   bool li_56 = FALSE;
   gi_128 = 0;
   gi_136 = 0;
   gi_148 = 0;
   gi_160 = 0;
   for (int shift_0 = Bars - 1; shift_0 >= -1; shift_0--) {
      imacd_40 = iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_0);
      if (imacd_32 * imacd_40 <= 0.0) {
         li_56 = FALSE;
         if (count_4 + 1 >= li_16 * 2 + 1 || shift_0 == -1) li_56 = TRUE;
         if (li_56 == TRUE) {
            if (imacd_32 > 0.0) {
               for (int shift_8 = shift_0 + count_4 + 1; shift_8 > shift_0; shift_8--) {
                  for (int shift_12 = 0; shift_12 < 10; shift_12++) {
                     lia_20[shift_12] = 1;
                     lia_24[shift_12] = 1;
                  }
                  li_28 = TRUE;
                  imacd_48 = iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_8);
                  for (shift_12 = shift_8 + 1; shift_12 <= shift_8 + li_16; shift_12++)
                     if (imacd_48 <= iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_12)) lia_20[shift_12 - shift_8 - 1] = 0;
                  for (shift_12 = shift_8 - 1; shift_12 >= shift_8 - li_16; shift_12--)
                     if (imacd_48 <= iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_12)) lia_24[StrToInteger(DoubleToStr(MathAbs(shift_12 - shift_8 + 1), 0))] = 0;
                  for (shift_12 = 0; shift_12 < 10; shift_12++) {
                     if (lia_20[shift_12] == 0 || lia_24[shift_12] == 0) {
                        li_28 = FALSE;
                        break;
                     }
                  }
                  if (li_28 == TRUE) {
                     gi_128++;
                     ArrayResize(gia_124, gi_128);
                     gia_124[gi_128 - 1] = shift_8;
                  }
               }
            } else {
               if (imacd_32 < 0.0) {
                  for (shift_8 = shift_0 + count_4 + 1; shift_8 > shift_0; shift_8--) {
                     for (shift_12 = 0; shift_12 < 10; shift_12++) {
                        lia_20[shift_12] = 1;
                        lia_24[shift_12] = 1;
                     }
                     li_28 = TRUE;
                     imacd_48 = iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_8);
                     for (shift_12 = shift_8 + 1; shift_12 <= shift_8 + li_16; shift_12++)
                        if (imacd_48 >= iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_12)) lia_20[shift_12 - shift_8 - 1] = 0;
                     for (shift_12 = shift_8 - 1; shift_12 >= shift_8 - li_16; shift_12--)
                        if (imacd_48 >= iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_12)) lia_24[StrToInteger(DoubleToStr(MathAbs(shift_12 - shift_8 + 1), 0))] = 0;
                     for (shift_12 = 0; shift_12 < 10; shift_12++) {
                        if (lia_20[shift_12] == 0 || lia_24[shift_12] == 0) {
                           li_28 = FALSE;
                           break;
                        }
                     }
                     if (li_28 == TRUE) {
                        gi_136++;
                        ArrayResize(gia_132, gi_136);
                        gia_132[gi_136 - 1] = shift_8;
                     }
                  }
               }
            }
         }
         imacd_32 = imacd_40;
         count_4 = 0;
      } else count_4++;
   }
   FindDeviation(FindLen);
}

void FindDeviation(int ai_0) {
   int shift_16;
   int shift_20;
   double ld_24;
   double ld_32;
   double ld_40;
   for (int index_4 = 0; index_4 < gi_128 - 1; index_4++) {
      shift_16 = gia_124[index_4];
      for (int li_8 = index_4 + 1; shift_16 - gia_124[li_8] <= ai_0 && li_8 < gi_128; li_8++) {
         shift_20 = gia_124[li_8];
         if (High[shift_20] > High[shift_16] && iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_20) < iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA,
            PRICE_CLOSE, MODE_SIGNAL, shift_16)) {
            ld_24 = (iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_20) - iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_16)) / (shift_16 - shift_20);
            ld_40 = 1;
            for (int shift_12 = shift_16 - 1; shift_12 >= shift_20 + 1; shift_12--) {
               ld_32 = iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_16) + (shift_16 - shift_12) * ld_24;
               if (ld_32 + 0.002 < iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_12)) ld_40 = 0;
            }
            if (ld_40 == 1.0) {
               gi_148++;
               ArrayResize(gia_140, gi_148);
               ArrayResize(gia_144, gi_148);
               gia_140[gi_148 - 1] = shift_16;
               gia_144[gi_148 - 1] = shift_20;
            }
         }
      }
   }
   for (index_4 = 0; index_4 < gi_136 - 1; index_4++) {
      shift_16 = gia_132[index_4];
      for (li_8 = index_4 + 1; shift_16 - gia_132[li_8] <= ai_0 && li_8 < gi_136; li_8++) {
         shift_20 = gia_132[li_8];
         if (Low[shift_20] < Low[shift_16] && iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_20) > iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA,
            PRICE_CLOSE, MODE_SIGNAL, shift_16)) {
            ld_24 = (iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_20) - iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_16)) / (shift_16 - shift_20);
            ld_40 = 1;
            for (shift_12 = shift_16 - 1; shift_12 >= shift_20 + 1; shift_12--) {
               ld_32 = iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_16) + (shift_16 - shift_12) * ld_24;
               if (ld_32 - 0.002 > iMACD(NULL, 0, FastEMA, SlowEMA, SignalSMA, PRICE_CLOSE, MODE_SIGNAL, shift_12)) ld_40 = 0;
            }
            if (ld_40 == 1.0) {
               gi_160++;
               ArrayResize(gia_152, gi_160);
               ArrayResize(gia_156, gi_160);
               gia_152[gi_160 - 1] = shift_16;
               gia_156[gi_160 - 1] = shift_20;
            }
         }
      }
   }
}

如您喜欢此文章请点下面分享按钮↴峰汇在线 » 主图绘制MACD背离的指标MACD_Histogram_3
上一篇:
下一篇:
分享到:更多 ()