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
0
votes
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);
}
signal
into a class? e.g.MyDecider decider(signal);
– nicholishen