Need help with ATR-based spike alert :)

 

Hi everyone....

Been stressing out big time, and spent way too many hours trying to code this.

First, I'll tell you the goal and what finished product I want. Second, I'll tell u my progress and what I've done, and attach the 2 files I've been able to create.

1st - the goal

I want 3 ATR lines in a separate chart window (#property indicator_separate_window). One will be ATR(x), another will be ATR(y) and the third will be ATR(y*z). Variable z will be a multiplier, lets say 3. So when ATR(x) is 3 times the ATR of ATR(y), I'll see a spike in ATR(x) and it will be touching the third line which is ATR(y*3). When ATR(x) is at that level of ATR(y*z), I also want an alert, saying the that there's a spike on [pair]. Basically I want the visual indicator, and the built in alert as well.

2nd - my progress so far on this

I was able to create an EA (but I want an indicator) that did this successfully. I called it volatility_alert_1.5.mq4. This will do the alert, the message etc.. However, I need this converted to an indicator that also shows it visually. I also want it all in 1 file... that way I don't need a template etc..

[ATTACH]8711[/ATTACH]

I was also able to modify the stock mt4 ATR.mq4 indicator to show 2 lines. However, one line works great, and will adjust to whatever period I want. The other is stuck at ATR(1) and I can't figure out why. This file is called spike_alert_v0.3.mq4. Basically I want that fixed, and combined with the first file.

[ATTACH]8710[/ATTACH]

I hope I've been very clear... This is soooo frustrating as I am just beginning my programming education. I'm reading as much as i can in the mq4 book, however these little issues keep holding me back... Thanks everyone!

Brendon

Files:
 

Made a breakthrough... sorta.

OK... since I have already programmed an EA to give me a popup and alert noise when the parameters are hit, I focused on creating the indicator that combines fast ATR (ATR(1)), slow ATR (ATR(60)), and also the slow ATR multiplied by a variable, probably 3 (ATR(60*3)).

I am semi successful with this indicator. However.... they are 3 flat lines! (Look at picture) I know there's something missing and it is minor.... I think it has to do with the count/countedbars, etc. Can someone fix this for me?? Thank you!!

[ATTACH]8716[/ATTACH]



#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 DeepSkyBlue
#property indicator_color2 Yellow
#property indicator_color3 Green

extern int fastATRperiod = 1;
extern int slowATRperiod = 80;
extern int slowATRmultiplier = 3;
extern int alertSleepMins = 0;


//---- buffers
double fastATR[];
double slowATR[];
double multipliedATR[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,fastATR);
SetIndexLabel(0,"fastATR");

SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,slowATR);
SetIndexLabel(1,"slowATR");

SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,multipliedATR);
SetIndexLabel(2,"multipliedATR");

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

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

int CalculateBars = Bars - counted_bars;

for(int Count = Bars; Count >= 0; Count--)
{
fastATR[Count] = iATR(NULL,0,fastATRperiod,0);

slowATR[Count] = iATR(NULL,0,slowATRperiod,0);

multipliedATR[Count] = ((iATR(NULL,0,slowATRperiod,0)) * slowATRmultiplier);

}

//----

//----
return(0);
}
//+------------------------------------------------------------------+

Files:
 

Thanks for the mounds of help flowing in

I was able to make it work! I got it done.... However there's still this issue:

Basically - in the externs, the user specifies how many minutes the indicator should stop sending alerts before resuming again (right now every tick it sends the alert until a new bar forms and it's very annoying). The code above translates the minutes into milliseconds and then below, once the alert function is called, it's supposed to sleep for that many milliseconds. But it's not working! It worked when this was first an EA, but not as an indicator. Any suggestions on how to get this to pause, or only to alert once per bar?


double MilliSex = (alertSleepMins * 60000);

if(ATRfast >= (ATRslow * slowATRmultiplier))
{
// Sound Alert
Alert("Spike on " + Pair + "!");
Print("Fast = " + ATRfast + ", Slow = " + ATRslow);
Sleep(MilliSex);

Also... this is minor, but I can't get one of the lines to be thickness of 2.

Before

SetIndexStyle(2,DRAW_LINE);

After:

This isn't working. It won't even do STYLE_DASH if I wanted it to, which I don't, but curious why it doesn't work.

SetIndexStyle(2,DRAW_LINE,STYLE_SOLID,2);

All help is greatly appreciated

 

Oooops forgot to attach the most recent version

Files:
 

N/M about the line thickness.... I just had to re-load the indicator... I thought compiling it in metaeditor would update the line thickness right there, but not I had to remove the indicator then re-load it.