HMA distinguishing colors

 

I need to pass the color of the Hull Moving Average to my EA. The HMA indicator only calcuates the value.

I have modified the code below to draw a histogram instead of a line for the HMA. I've added a variable named ColorBuffer[] to pass the value of the color. See last few lines of the code. When the indicator displays the Dntrend bar ColorBuffer is set to 0. When the indicator displays the Uptrend bar ColorBuffer is set to 1.

However, the ColorBuffer[] value does not always match the HMA histo color. Can someone explain why and how to fix it?


//+------------------------------------------------------------------+
//| HMA.mq4
//| Copyright © 2006 WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104
//| wizardserg@mail.ru
//| Revised by IgorAD,igorad2003@yahoo.co.uk |
//| https://www.forex-tsd.com/ |
//+------------------------------------------------------------------+
#property copyright "MT4 release WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104"
#property link "wizardserg@mail.ru"

//#property indicator_chart_window
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Aqua
#property indicator_color2 Tomato
#property indicator_color3 Black

//---- input parameters
extern int period=34;
extern int method=3; // MODE_LWMA
extern int price=0; // PRICE_CLOSE
//---- buffers
double Uptrend[];
double Dntrend[];
double ColorBuffer[];
double ExtMapBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(4);
SetIndexBuffer(0, Uptrend);
//ArraySetAsSeries(Uptrend, true);
SetIndexBuffer(1, Dntrend);
//ArraySetAsSeries(Dntrend, true);
SetIndexBuffer(2, ColorBuffer); //new variable added
ArraySetAsSeries(ColorBuffer, true);
SetIndexBuffer(3, ExtMapBuffer);
ArraySetAsSeries(ExtMapBuffer, true);

// SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
// SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
SetIndexStyle(0,DRAW_HISTOGRAM,DRAW_HISTOGRAM,2); //changed line to histo
SetIndexStyle(1,DRAW_HISTOGRAM,DRAW_HISTOGRAM,2);
SetIndexStyle(2,DRAW_ARROW);
SetIndexArrow(2,108);

IndicatorShortName("Hull Moving Average("+period+")");
return(0);
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}

//+------------------------------------------------------------------+
//| ?????????? ??????? |
//+------------------------------------------------------------------+
double WMA(int x, int p)
{
return(iMA(NULL, 0, p, 0, method, price, x));
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars = IndicatorCounted();

if(counted_bars < 0)
return(-1);

int x = 0;
int p = MathSqrt(period);
int e = Bars - counted_bars + period + 1;

double vect[], trend[];

if(e > Bars)
e = Bars;

ArrayResize(vect, e);
ArraySetAsSeries(vect, true);
ArrayResize(trend, e);
ArraySetAsSeries(trend, true);

for(x = 0; x < e; x++)
{
vect[x] = 2*WMA(x, period/2) - WMA(x, period);
// Print("Bar date/time: ", TimeToStr(Time[x]), " close: ", Close[x], " vect[", x, "] = ", vect[x], " 2*WMA(p/2) = ", 2*WMA(x, period/2), " WMA(p) = ", WMA(x, period));
}

for(x = 0; x < e-period; x++)

ExtMapBuffer[x] = iMAOnArray(vect, 0, p, 0, method, x);

for(x = e-period; x >= 0; x--)
{
trend[x] = trend[x+1];
if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1;
if (ExtMapBuffer[x]< ExtMapBuffer[x+1]) trend[x] =-1;

if (trend[x]>0)
{ Uptrend[x] = ExtMapBuffer[x];
if (trend[x+1]<0) Uptrend[x+1]=ExtMapBuffer[x+1];
Dntrend[x] = EMPTY_VALUE;
}
else
if (trend[x]<0)
{
Dntrend[x] = ExtMapBuffer[x];
if (trend[x+1]>0) Dntrend[x+1]=ExtMapBuffer[x+1];
Uptrend[x] = EMPTY_VALUE;
}
if (Uptrend[x] == EMPTY_VALUE) ColorBuffer[x]=0; //added ColorBuffer assignement to capture color
if (Dntrend[x] == EMPTY_VALUE) ColorBuffer[x]=1;
}

return(0);
}

 

SR

Sorry to say... HMA in all versions repaints sooo badly I wouldnt use it for anything :(

Good Luck

-BB-

 
BarrowBoy:

SR

Sorry to say... HMA in all versions repaints sooo badly I wouldnt use it for anything :(

Good Luck

-BB-

I understand. However, I still have a use for this indicator.

Any help would be appreciated.

 
SquareRoot:

I understand. However, I still have a use for this indicator.

Any help would be appreciated.

SquareRoot, (how aproppriate .. :))


I also use that HMA version as my leading indicator. I use it in M1 for checking best entry..

I'm very new at coding & programming but - correct me if i'm wrong - i think you should use the condition checking values instead of referring to it's color assignment. ie. the boolean trend checking itself is perfect for trade decision (or other operation perhaps).


==> if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1; ... if trend[x]==1 { is uptrend, do something ...}


< BarrowBoy wrote >>

Sorry to say... HMA in all versions repaints sooo badly I wouldnt use it for anything :( >


>> BB, Isn't repainting in HMA is a coloring algo issue only? (I have benefited a lot from your posts & have so much respect with your experience, so please enlighten me if i'm delusional here :) ). I know some indy is repainting - super signal or zigzag for example -but IMHO HMA is not the one.. this has been discussed & perceived as so too in other forum.

- HMA is updating in real time as any line indicator - watch it in M1 - it wobble with the updated value at each new tick.. but after bar is closed value is fixed and does not get re-positioned at the next n bar. of course we can set it to last closed bar if prefer non wobble but not real time. (i never tried it but maybe making it as DRAW_ARROW is a way to proof it definitively)

- Here is a pic thats showing it (hope the good folks at forex-tsd don't mind!). color repaints only, not values. there is also an HMA nrp (non [color]-repainting) by mladen here but only in ex4 so unfortunately cannot see it's color coding. The color coding by great Igorad is sufficient for me but maybe great coders here can help with new improved 'cross-coloring' code? that would be awsomme..



hma clr repaint

I hope this helps a little, good luck with your EA SquareRoot.. and if you wouldn't mind sharing the finished EA that would be great also.


best regards,

cameo

 
BarrowBoy wrote >>

SR

Sorry to say... HMA in all versions repaints sooo badly I wouldnt use it for anything :(

Good Luck

-BB-

what a nonsence

Reason: