0
votes

I was wondering if there is a way to pass an Indicator Definition or the Indicator as a parameter to a constructor of a class. The point is to create a class, that accept the indicator and use it to generate specific values, or even initialized indicator definition to get values

1
What would be the use to pass a random indicator to a class? Since all indicators have different values wouldn't it make more sense to pass a signal into a class? e.g. MyDecider decider(signal);nicholishen
Update, sorry to not specify, i mean MQL5Ramzy
nicholishen, that's something i didn't thought off xD but to think, i have a class for each indicator, the input parameters change, and the buffer number change, i was going to make a loop that take argument from the constructor of how may buffers, didn't thought about the varied parametersRamzy
varied number of parameters and varied buffer numbers are evil, why didn't they make an array of values to be passed, or something like MqlTradeRequest and MqlTradeResult for the indicatorsRamzy

1 Answers

0
votes

MQL4 and MQL5 are different in terms of indicators. You can use indicator directly where you need it, but that is usually not convenient and requires different code for MQL4 and MQL5.

You can create an instance of an indicator (of course different parameters means different instances). Different indicators may have different number of parameters and buffers, so it makes sense to create a class for each indicator you need. This approach is both good for MQL4 and 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);
}