MQL5-通过指示器作为参数

时间:2018-10-11 21:47:34

标签: mql5 forex metatrader5

我想知道是否有办法将指标定义或指标作为参数传递给类的构造函数。 关键是要创建一个类,该类接受指标并使用它来生成特定值,甚至初始化指标定义来获取值

1 个答案:

答案 0 :(得分:0)

MQL4和MQL5在指标方面有所不同。您可以在需要的地方直接使用指标,但这通常不方便,并且MQL4和MQL5需要不同的代码。

您可以创建指标的实例(当然,不同的参数表示不同的实例)。不同的指标可能具有不同数量的参数和缓冲区,因此有必要为所需的每个指标创建一个类。这种方法对MQL4和MQL5都很好。

#include <Indicators\Custom.mqh>
class CiMyCustomIndicator : public CiCustom
   {
       string  m_indicatorName;
    public:
       int     GetResult(const int index)const//as an example, buf0[0]>buf0[1] -> 1, buf0[0]<buf0[1] -> -1, else 0
          {
           double value=GetData(0,index),
                  prevValue=GetData(0,index+1);
           if(value>prevValue)return(1);
           if(value<prevValue)return(-1);
           return(0);
          }
   };

 int OnInit(){
    CiMyCustomIndicator *myMacd=new CiMyCustomIndicator();
    //lets assume myMacd gets two params:int InpPeriod=9; double InpLine=0.0001;
    MqlParams[] myMacdInputs;
    ArrayResize(myMacdInputs,2);
    myMacdInputs[0].type=TYPE_INT;
    myMacd[0].integer_value=InpPeriod;
    myMacdInputs[1].type=TYPE_DOUBLE;
    myMacdInputs[1].double_value=InpLine;

    myMacd.Create(_Symbol,_Period,IND_CUSTOM,ArraySize(myMacdInputs),myMacdInputs);
// you can now pass myMacd as pointer into another method
// other logic

}
void OnDeinit(const int reason){delete(myMacd);}
void OnTick(){
     double value=myMacd.GetData(bufferIndex, shift);
     int customResult=myMacd.GetResult(shift);
}