关于iCustom调用的问题,测试的时候不断开指标图

要添加评论,请登录注册
xiaohouzi88
18
xiaohouzi88  

自己写了一个指标(MACD图),通过iCustom函数调用内部数据,

在回测调试时,不断的在图中打开MACD指标.

请问该是哪里出现了错误

=========调用代码=======EA中

void OnTick()

  {

   double macdz[];

   double macdf[];

   double dea[];

   double dif[];  

   

   zb.MACD(macdz,macdf,dif,dea,3);

========CLASS代码===========

   void MACD(double &macdz[],double &macdf[],double &dif[],double &dea[],int count)

     {

      int macd_h=iCustom(Symbol(),PERIOD_CURRENT,"MACD_DL",12,26,9,PRICE_CLOSE);

      ArraySetAsSeries(macdz,true);

      ArraySetAsSeries(macdf,true);

      ArraySetAsSeries(dif,true);

      ArraySetAsSeries(dea,true);

      CopyBuffer(macd_h,0,0,8,dif);

      CopyBuffer(macd_h,1,0,8,dea);

      CopyBuffer(macd_h,2,0,8,macdz);

      CopyBuffer(macd_h,3,0,8,macdf);

      IndicatorRelease(macd_h);

     }

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 <MovingAverages.mqh>

//--- 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 3

#property indicator_width4 3

#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<InpSignalSMA)

      return(0);

//--- not all data may be calculated

   int calculated=BarsCalculated(ExtFastMaHandle);

   if(calculated<rates_total)

     {

      Print("Not all data of ExtFastMaHandle is calculated (",calculated,"bars ). Error",GetLastError());

      return(0);

     }

   calculated=BarsCalculated(ExtSlowMaHandle);

   if(calculated<rates_total)

     {

      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<rates_total;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<rates_total;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);

  }

//+------------------------------------------------------------------+

Sergey Golubev
版主
115923
Sergey Golubev  

Forum on trading, automated trading systems and testing trading strategies

When you post code please use the CODE button (Alt-S)!

Use the CODE button

Xiangdong Guo
3997
Xiangdong Guo  
xiaohouzi88:

自己写了一个指标(MACD图),通过iCustom函数调用内部数据,

在回测调试时,不断的在图中打开MACD指标.

请问该是哪里出现了错误

=========调用代码=======EA中

void OnTick()

  {

   zb.MACD(macdz,macdf,dif,dea,3);

========CLASS代码===========

   void MACD(double &macdz[],double &macdf[],double &dif[],double &dea[],int count)

     {

      int macd_h=iCustom(Symbol(),PERIOD_CURRENT,"MACD_DL",12,26,9,PRICE_CLOSE);

      ......

     }

你在 MACD 函数中用 iCustom 创建新的指标句柄,那么在 OnTick() 里执行 zb.MACD() 时当然就会有新的指标出现。

xiaohouzi88
18
xiaohouzi88  

请问该如何处理  


我想调用自定义指标中的四个值 在EA中使用  

该如何处理 

是使用新的函数 

还是对MACD自定义指标  代码进行处理  

Ziheng Zhuang
30793
Ziheng Zhuang  

指标句柄在MT5是比较特殊的存在。

定义一个全局的句柄handle,然后在OnInit()中创建,在OnTick()中调用。

xiaohouzi88
18
xiaohouzi88  
Ziheng Zhuang:

指标句柄在MT5是比较特殊的存在。

定义一个全局的句柄handle,然后在OnInit()中创建,在OnTick()中调用。

不是十分了解

能不能请帮忙修改下~~做一个示范

万分感谢

Ziheng Zhuang
30793
Ziheng Zhuang  
//+------------------------------------------------------------------+
//|                                                    test_macd.mq5 |
//|                        Copyright 2018, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"


int handle=-1;
double macdz[];
double macdf[];
double dea[];
double dif[];
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   handle=iCustom(Symbol(),PERIOD_CURRENT,"MACD_DL",12,26,9,PRICE_CLOSE);

   if(handle==INVALID_HANDLE)
     {
      return(INIT_FAILED);
     }
   ArraySetAsSeries(macdz,true);
   ArraySetAsSeries(macdf,true);
   ArraySetAsSeries(dif,true);
   ArraySetAsSeries(dea,true);
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool MACD(int count)
  {
   if(CopyBuffer(handle,0,0,count,dif)!=count)return(false);
   if(CopyBuffer(handle,1,0,count,dea)!=count)return(false);
   if(CopyBuffer(handle,2,0,count,macdz)!=count)return(false);
   if(CopyBuffer(handle,3,0,count,macdf)!=count)return(false);

   return(true);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   IndicatorRelease(handle);
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   if(MACD(3))
     {
      //--- 你的代码
     }
  }
//+------------------------------------------------------------------+
xiaohouzi88
18
xiaohouzi88  

上午想了一个上午,想将MACD写进一个类里面,但是还是出了错误.

还请老师指点~~~~

错误代码为

      for(int i=kaiswei;i<shuliang;i++)
         ExtDIFFBuffer[i]=ExtFastMaBuffer[i]-ExtSlowMaBuffer[i];

         ExtFastMaBuffer[0]和ExtSlowMaBuffer[0]的数据能够获取


但是  ExtDIFFBuffer[0]不能获取  出现报错

critical error while running

array out of range




//全部代码

//+------------------------------------------------------------------+
//|                                                      ProjectName |
//|                                      Copyright 2012, CompanyName |
//|                                       http://www.companyname.net |
//+------------------------------------------------------------------+
#include <MovingAverages.mqh>
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
class Macdzb
  {
public:
   void macd(double &ExtDIFFBuffer[],double &ExtDEABuffer[],double &ExtMACDBuffer[],int kaiswei,int shuliang)
     {
      int ExtFastMaHandle;
      int ExtSlowMaHandle;
      double ExtFastMaBuffer[];
      double ExtSlowMaBuffer[];

      ArraySetAsSeries(ExtDIFFBuffer,true);
      ArraySetAsSeries(ExtDEABuffer,true);
      ArraySetAsSeries(ExtMACDBuffer,true);

      ArraySetAsSeries(ExtFastMaBuffer,true);
      ArraySetAsSeries(ExtSlowMaBuffer,true);
      
      ExtFastMaHandle=iMA(NULL,0,12,0,MODE_EMA,PRICE_CLOSE);
      ExtSlowMaHandle=iMA(NULL,0,26,0,MODE_EMA,PRICE_CLOSE);

//--- get Fast EMA buffer
   if(CopyBuffer(ExtFastMaHandle,0,0,shuliang,ExtFastMaBuffer)<=0)
     {
      Print("Getting fast EMA is failed! Error",GetLastError());
     }
//--- get SlowSMA buffer
   if(CopyBuffer(ExtSlowMaHandle,0,0,shuliang,ExtSlowMaBuffer)<=0)
     {
      Print("Getting slow SMA is failed! Error",GetLastError());
     }
      //dif   
      int i=kaiswei;
      for(int i=kaiswei;i<shuliang;i++)
         ExtDIFFBuffer[i]=ExtFastMaBuffer[i]-ExtSlowMaBuffer[i];
      //--- calculate DEA
      SimpleMAOnBuffer(1,0,0,9,ExtDIFFBuffer,ExtDEABuffer);//shuliang
      //--- calculate MACD
      for(int i=kaiswei;i<shuliang;i++)
            ExtMACDBuffer[i] = 2*(ExtDIFFBuffer[i]-ExtDEABuffer[i]);
            
            IndicatorRelease(ExtFastMaHandle);
            IndicatorRelease(ExtSlowMaHandle);
     }
  };
//+------------------------------------------------------------------+
xiaohouzi88
18
xiaohouzi88  
Ziheng Zhuang:
十分感谢老师的回复  先研修下
要添加评论,请登录注册