发新话题
打印

[指标EA编写讨论] mt4编程代码讲解篇

本主题由 xfxyldj 于 2008-4-5 03:47 加入精华 本主题被作者加入到个人文集中 所需阅读权限 1
了解一些,但我个人专注于外汇方面。对mt4下的功夫比较多。
国内期货好像有个TradeBlazer可以自动交易编程基于VB的。你可以网上搜索下。

TOP

版主能讲解一下zigzag的代码吗?

看了版主讲解的代码受益非浅.版主能否接着讲解一下zigzag的代码?这个指标思路与其他标准差别很大,没看懂.

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 PaleTurquoise
//---- indicator parameters
extern int ExtDeviation=5;
extern int ExtBackstep=3;
extern int extdepth=12;
extern bool Automatic_Timeframe_Setting = true;
extern int M1_extdepth =180;
extern int M5_extdepth = 36;
extern int M15_extdepth =12;
extern int M30_extdepth = 6;
extern int H1_extdepth =  3;
extern int H4_extdepth = 12;
extern int D1_extdepth = 12;

//---- indicator buffers
double ZigzagBuffer[];
double HighMapBuffer[];
double LowMapBuffer[];
int level=3; // recounting's depth
bool downloadhistory=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(3);
//---- drawing settings
   SetIndexStyle(0,DRAW_SECTION);
//---- indicator buffers mapping
   SetIndexBuffer(0,ZigzagBuffer);
   SetIndexBuffer(1,HighMapBuffer);
   SetIndexBuffer(2,LowMapBuffer);
   SetIndexEmptyValue(0,0.0);
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int i, counted_bars = IndicatorCounted();
   int limit,counterZ,whatlookfor, ExtDepth;
   int shift,back,lasthighpos,lastlowpos;
   double val,res;
   double curlow,curhigh,lasthigh,lastlow;
   
   if (Automatic_Timeframe_Setting == true) {
      switch(Period()) {
         case 1: ExtDepth = M1_extdepth; break;
         case 5: ExtDepth = M5_extdepth; break;
         case 15: ExtDepth = M15_extdepth; break;
         case 30: ExtDepth = M30_extdepth; break;
         case 60: ExtDepth = H1_extdepth; break;
         case 240: ExtDepth = H4_extdepth; break;
         case 1440: ExtDepth = D1_extdepth; break;
      }
   }
   else {
      ExtDepth = extdepth;
   }

   if (counted_bars==0 && downloadhistory) // history was downloaded
     {
      ArrayInitialize(ZigzagBuffer,0.0);
      ArrayInitialize(HighMapBuffer,0.0);
      ArrayInitialize(LowMapBuffer,0.0);
     }
   if (counted_bars==0)
     {
      limit=Bars-ExtDepth;
      downloadhistory=true;
     }
   if (counted_bars>0)
     {
      while (counterZ<level && i<100)
        {
         res=ZigzagBuffer;
         if (res!=0) counterZ++;
         i++;
        }
      i--;
      limit=i;
      if (LowMapBuffer!=0)
        {
         curlow=LowMapBuffer;
         whatlookfor=1;
        }
      else
        {
         curhigh=HighMapBuffer;
         whatlookfor=-1;
        }
      for (i=limit-1;i>=0;i--)  
        {
         ZigzagBuffer=0.0;  
         LowMapBuffer=0.0;
         HighMapBuffer=0.0;
        }
     }
      
   for(shift=limit; shift>=0; shift--)
     {
      val=Low[iLowest(NULL,0,MODE_LOW,ExtDepth,shift)];
      if(val==lastlow) val=0.0;
      else
        {
         lastlow=val;
         if((Low[shift]-val)>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=LowMapBuffer[shift+back];
               if((res!=0)&&(res>val)) LowMapBuffer[shift+back]=0.0;
              }
           }
        }
      if (Low[shift]==val) LowMapBuffer[shift]=val; else LowMapBuffer[shift]=0.0;
      //--- high
      val=High[iHighest(NULL,0,MODE_HIGH,ExtDepth,shift)];
      if(val==lasthigh) val=0.0;
      else
        {
         lasthigh=val;
         if((val-High[shift])>(ExtDeviation*Point)) val=0.0;
         else
           {
            for(back=1; back<=ExtBackstep; back++)
              {
               res=HighMapBuffer[shift+back];
               if((res!=0)&&(res<val)) HighMapBuffer[shift+back]=0.0;
              }
           }
        }
      if (High[shift]==val) HighMapBuffer[shift]=val; else HighMapBuffer[shift]=0.0;
     }

   // final cutting
   if (whatlookfor==0)
     {
      lastlow=0;
      lasthigh=0;  
     }
   else
     {
      lastlow=curlow;
      lasthigh=curhigh;
     }
   for (shift=limit;shift>=0;shift--)
     {
      res=0.0;
      switch(whatlookfor)
        {
         case 0: // look for peak or lawn
            if (lastlow==0 && lasthigh==0)
              {
               if (HighMapBuffer[shift]!=0)
                 {
                  lasthigh=High[shift];
                  lasthighpos=shift;
                  whatlookfor=-1;
                  ZigzagBuffer[shift]=lasthigh;
                  res=1;
                 }
               if (LowMapBuffer[shift]!=0)
                 {
                  lastlow=Low[shift];
                  lastlowpos=shift;
                  whatlookfor=1;
                  ZigzagBuffer[shift]=lastlow;
                  res=1;
                 }
              }
             break;  
         case 1: // look for peak
            if (LowMapBuffer[shift]!=0.0 && LowMapBuffer[shift]<lastlow && HighMapBuffer[shift]==0.0)
              {
               ZigzagBuffer[lastlowpos]=0.0;
               lastlowpos=shift;
               lastlow=LowMapBuffer[shift];
               ZigzagBuffer[shift]=lastlow;
               res=1;
              }
            if (HighMapBuffer[shift]!=0.0 && LowMapBuffer[shift]==0.0)
              {
               lasthigh=HighMapBuffer[shift];
               lasthighpos=shift;
               ZigzagBuffer[shift]=lasthigh;
               whatlookfor=-1;
               res=1;
              }   
            break;               
         case -1: // look for lawn
            if (HighMapBuffer[shift]!=0.0 && HighMapBuffer[shift]>lasthigh && LowMapBuffer[shift]==0.0)
              {
               ZigzagBuffer[lasthighpos]=0.0;
               lasthighpos=shift;
               lasthigh=HighMapBuffer[shift];
               ZigzagBuffer[shift]=lasthigh;
              }
            if (LowMapBuffer[shift]!=0.0 && HighMapBuffer[shift]==0.0)
              {
               lastlow=LowMapBuffer[shift];
               lastlowpos=shift;
               ZigzagBuffer[shift]=lastlow;
               whatlookfor=1;
              }   
            break;               
         default: return;
        }
     }

   return(0);
  }
//+------------------------------------------------------------------+

TOP

等有时间的时候把。
最近比较忙。不好意思。

TOP

期待。。。。。。

TOP

引用:
原帖由 xfxyldj 于 2008-4-9 09:06 发表
了解一些,但我个人专注于外汇方面。对mt4下的功夫比较多。
国内期货好像有个TradeBlazer可以自动交易编程基于VB的。你可以网上搜索下。
谢谢版主!

TOP

//+------------------------------------------------------------------+
//|                                                  Custom MACD.mq4 |
//|                      Copyright ?2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property  copyright "Copyright ?2004, MetaQuotes Software Corp."
#property  link      "http://www.metaquotes.net/"
//---- indicator settings
#property  indicator_separate_window
#property  indicator_buffers 4
#property  indicator_color1  Blue
#property  indicator_color2  Yellow
#property  indicator_color3  Red
#property  indicator_color4  Green
//int indicator_color3;
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double     ind_buffer1[];
double     ind_buffer2[];
double     ind_buffer3[];
double     ind_buffer4[];
double     current,prev;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- drawing settings
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexDrawBegin(1,SignalSMA);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
//---- indicator buffers mapping
   if(!SetIndexBuffer(0,ind_buffer1) && !SetIndexBuffer(1,ind_buffer2)&& !SetIndexBuffer(2,ind_buffer3)&& !SetIndexBuffer(3,ind_buffer4))
      Print("cannot set indicator buffers!");
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"Signal");
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- macd counted in the 1-st buffer
   for(int i=0; i<limit; i++)
      ind_buffer1=iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i)-iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//---- signal line counted in the 2-nd buffer
   for(i=0; i<limit; i++)
      ind_buffer2=iMAOnArray(ind_buffer1,Bars,SignalSMA,0,MODE_EMA,i);
      
    bool up=true;
   for(i=limit-1; i>=0; i--)
     {
      current= ind_buffer1- ind_buffer2;
      prev= ind_buffer1[i+1]- ind_buffer2[i+1];
      if(current>prev) up=true;
      if(current<prev) up=false;
      if(!up)
        {
          ind_buffer3=current*2;
          ind_buffer4=0.0;
        }
      else
        {
          ind_buffer4=current*2;
          ind_buffer3=0.0;
        }
      }
      
//---- done
   return(0);
  }              这是双线MACD指标柱是变色的,请教版主为什么和你的MACD有的地方不一样,我是说里边有的英文字不一样,不好意思,我不懂英语,我总算找到了学习的好论坛。

TOP

SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);
   SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,1);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,1);
   SetIndexDrawBegin(1,SignalSMA);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS)+1);
这个STYLE_SOLID是什么意思,你的指标里没这个的,用0的。请指教。

[ 本帖最后由 fx-yama 于 2008-4-19 22:48 编辑 ]

TOP

回复 56楼 的帖子

简单看了下。
这个是调用了iMA(均线函数)来实现MACD指标。
我的是调用iMACD(MACD函数)来实现MACD指标。
对于MACD指标就是均线指标的一个简单应用。他的基础来源于均线。
殊途同归,目的都是一样的。

TOP

回复 57楼 的帖子

你可以看下这个指标数组函数的帮助。
SetIndexStyle()

void SetIndexStyle( int index, int type, int style=EMPTY, int width=EMPTY, color clr=CLR_NONE)
设置新型、样式、宽度和颜色为一条指定的显示线。
参量:
index   -   指数线。 必须在0到7 之间。
type   -   可以是一种风格形状 列表。
style   -   画出风格。应用单线。可以是一种风格形状列表。EMPTY 值意味着风格不能改变。
width   -   线的宽度。线的宽度可以是1,2,3,4,5。 EMPTY 值意味着风格不能改变。
clr   -   线的颜色。.现存的参数意味着颜色将不会改变。

例子:
  SetIndexStyle(3, DRAW_LINE, EMPTY, 2, Red);

TOP

多谢版主回复,您真是好人啊!这样我就知道了,我该按你的指标来学,大智慧软件我自学学会编指标,交易信号和条件选股指标,但MT4英语的就难了。谢谢您,我总算找到了一位好老师。

TOP

发新话题