KUO 发表于 2019-11-26 23:32:22

mql4如何自定义画图

mql4里很多自定义指标,将自定义指标拖入途中就能看到曲线图,俗话说,有图有真像,图是如何画的呢?我一直比较好奇,比如iMACD技术参数的图如下:

这个图是如何画的呢?问题1,银色的线垂直的线如何画,红色的曲线如何画
问题2, 两条线的数据是如何获取的 ?
带着这2个问题,我们看下这个源码
还好Mql开放了这个源代码,我们可以研究下, 源码如下//+------------------------------------------------------------------+
//|                                                Custom MACD.mq4 |
//|                      Copyright ?2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#propertycopyright "Copyright ?2004, MetaQuotes Software Corp."
#propertylink      "http://www.metaquotes.net/"
//---- indicator settings
#propertyindicator_separate_window
#propertyindicator_buffers 2
#propertyindicator_color1Silver
#propertyindicator_color2Red
#propertyindicator_width12
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
//---- indicator buffers
double   MacdBuffer[];
double   SignalBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
//---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexDrawBegin(1,SignalSMA);
   IndicatorDigits(Digits+1);
//---- indicator buffers mapping
   SetIndexBuffer(0,MacdBuffer);
   SetIndexBuffer(1,SignalBuffer);
//---- 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();
//---- 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++)
      MacdBuffer=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++)
      SignalBuffer=iMAOnArray(MacdBuffer,Bars,SignalSMA,0,MODE_SMA,i);
//---- done
   return(0);
}
//+------------------------------------------------------------------+


#propertyindicator_buffers 2
定义了有2条线

#propertyindicator_color1Silver
#propertyindicator_color2Red



定义颜色 ,银色和红色


#propertyindicator_width12

定义线的宽度。



extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalSMA=9;
iMACD的参数,我也不太明白,但是这对理解图的画法不影响,先不去管它。



//---- indicator buffers
double   MacdBuffer[];
double   SignalBuffer[];
这里是重点,存放数据的数组。





int init()
{
   //---- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);//设置第一条线的显示方式,这里就是垂直
   SetIndexStyle(1,DRAW_LINE);//水平线显示
   SetIndexDrawBegin(1,SignalSMA); //设置开始画的位置

   IndicatorDigits(Digits+1);
//---- indicator buffers mapping
   SetIndexBuffer(0,MacdBuffer);
   SetIndexBuffer(1,SignalBuffer);
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD("+FastEMA+","+SlowEMA+","+SignalSMA+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"Signal");
//---- initialization done
   return(0);
}


具体我就不一一解释了,后面有英文的注释,应该比较容易明白 ,因为系统没有ima的指标图,通过修改这个代码,我们可以画出iMA的指标图//+------------------------------------------------------------------+
//|                                                Custom MACD.mq4 |
//|                      Copyright ?2004, MetaQuotes Software Corp. |
//|                                       http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#propertycopyright "Copyright ?2004, MetaQuotes Software Corp."
#propertylink      "http://www.metaquotes.net/"
//---- indicator settings
#propertyindicator_separate_window

#propertyindicator_color1Red

#propertyindicator_width12

extern int SlowEMA=26;
extern int ma_shift=0;

double   MacdBuffer[];
double   SignalBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{

   SetIndexStyle(0,DRAW_HISTOGRAM);

   IndicatorDigits(Digits+1);

   SetIndexBuffer(0,MacdBuffer);

   SetIndexLabel(0,"iMA");

   return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
int start()
{
   int limit;
   int counted_bars=IndicatorCounted();
//---- 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++)
      MacdBuffer=iMA(NULL,0,SlowEMA,ma_shift,MODE_EMA,PRICE_CLOSE,i);
//---- done
   return(0);
}
//+------------------------------------------------------------------+
页: [1]
查看完整版本: mql4如何自定义画图