In typical bloke style I've been going round in circles on this one for ages trying to solve it myself but I've finally given up and need some help.
I'm trying to use the attached Heiken Ashi smoothed indicator within an EA as trend direction (based on the last closed candle). I've got it working using an iCustom call but I'd like to incorporate it into the EA as a function - I've tried to do it as an include and couldn't get that working either! The idea is that an internal function call will be less cpu intensive as it doesn't have to calculate all bars and I will only need the opens and closes and not the highs and lows. It also makes it neat and portable without having to move the indicator with the EA each time.
My attempt is here - with comments
Code: Select all
//+------------------------------------------------------------------+
//| HAS_Trend |
//+------------------------------------------------------------------+
int HAS_Trend(int _period1,int _method1,int _period2,int _method2)
{
// variables:
double maOpen,maClose,maLow,maHigh,haOpen,haHigh,haLow,haClose;
int trend;
// Define Arrays: (I've kept the names the same as those in the indicator)
//double ExtMapBuffer1[]; // don't need the high and low as just open and close is sufficient to give trend
//double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];
// define the "limit" // don't need to do all bars - a sample of 200 plus the max period length will be sufficient
int pos=200+MathMax(_period1,_period2);
// size the arrays
ArrayResize(ExtMapBuffer5,pos+2); // to allow poss + 1 to be accessed and including the zero.
ArrayResize(ExtMapBuffer6,pos+2);
ArrayResize(ExtMapBuffer7,pos+2);
ArrayResize(ExtMapBuffer8,pos+2);
ArrayResize(ExtMapBuffer3,6); // open
ArrayResize(ExtMapBuffer4,6); // close
// really not sure these are right!!
ArraySetAsSeries(ExtMapBuffer3,false);
ArraySetAsSeries(ExtMapBuffer4,false);
ArraySetAsSeries(ExtMapBuffer5,false);
ArraySetAsSeries(ExtMapBuffer6,false);
ArraySetAsSeries(ExtMapBuffer7,true);
ArraySetAsSeries(ExtMapBuffer8,true);
// calculate the intial HA values and populate buffers
while(pos>=0)
{
maOpen=iMA(NULL,0,_period1,0,_method1,PRICE_OPEN,pos);
maClose=iMA(NULL,0,_period1,0,_method1,PRICE_CLOSE,pos);
maLow=iMA(NULL,0,_period1,0,_method1,PRICE_LOW,pos);
maHigh=iMA(NULL,0,_period1,0,_method1,PRICE_HIGH,pos);
//----
// populated the first record to prevent a divide by zero - this value won't be used in the smoothed calcs
ExtMapBuffer5[pos+1]=1;
ExtMapBuffer6[pos+1]=1;
// populate as per the indicator
haOpen=(ExtMapBuffer5[pos+1]+ExtMapBuffer6[pos+1])/2;
haClose=(maOpen+maHigh+maLow+maClose)/4;
// may not need the highs and lows - remove once it's working!
haHigh=MathMax(maHigh,MathMax(haOpen,haClose));
haLow=MathMin(maLow,MathMin(haOpen,haClose));
// populate buffers according to trend
if(haOpen<haClose)
{
ExtMapBuffer7[pos]=haLow;
ExtMapBuffer8[pos]=haHigh;
}
else
{
ExtMapBuffer7[pos]=haHigh;
ExtMapBuffer8[pos]=haLow;
}
// populate the open and close
ExtMapBuffer5[pos]=haOpen;
ExtMapBuffer6[pos]=haClose;
pos--; // next down to zero
}
// initial HA values are calculated into buffers, now smooth them:
// only need to do a few because we're only looking at the trend direction on the most recent closed candle
// I checked this on the indicator and it just draws the last 4 bars as expected
// for(int i=0; i<limit; i++) {
for(int i=0; i<5; i++) {
// only need the open and close
ExtMapBuffer3[i]=iMAOnArray(ExtMapBuffer5,0,_period2,0,_method2,i); //open
ExtMapBuffer4[i]=iMAOnArray(ExtMapBuffer6,0,_period2,0,_method2,i); //close
}
if(ExtMapBuffer3[1]<ExtMapBuffer4[1])trend=1;
else trend=-1;
return(trend);
}I think the issue might be with the arrays as series but I can't find a combination that works. I'm sure this should be done as an include file in OOP but that's well beyond me at the moment.
If anyone can help point me in the right direction that would be great!
Thanks
Bruce