My ATR-based spike alert needs some mods

 

Hello everyone. Much help is needed please :)

I programmed this very basic ATR-based spike alert. The premise is that it alerts you when there's a spike. Spike is defined when the ATR of 1 bar is x times the ATR or 60 bars. When that happens, the alert pops up and the sound goes off, also the results are printed in the experts journal. Some help is needed though please.

I want the file to be changed to an indicator. Right now it's an EA. Is this possible? Even with the sound, and the alert can it be just an indicator?

Also - I set up my mt4 to visually show me the spike as well. The EA runs fine on a blank chart, but I also set up ATR(1) and ATR(60) so that when a spike happens I can look at the two lines to see. Is there a way this can be converted to an indicator to work as designed, but also when the indicator is loaded, the 2 ATR lines show up at the bottom as well so we can see this visually. The point is to distribute this to people who can load this as easily as possible.

Thanks for any help!!!!!!

Files:
 
I would offer to help, but so far all of my MQL4 experience is with coding EAs as well. I'm curious though, how has this worked for you as a trading signal?
 
Anomalous:
I would offer to help, but so far all of my MQL4 experience is with coding EAs as well. I'm curious though, how has this worked for you as a trading signal?


Thanks tho :)

Basically it's there to let me know of any major moves, because if there's one on one pair, then there might be a few more coming soon on other pairs. Also it's nice to buy on dips, aka, silver the other day. That was a great ride up :)

Question about EAs vs Indicators though... since my alert isn't doing any live trading, can it be an indicator? What's the difference code-wise?

 
larperguy813:


Thanks tho :)

Basically it's there to let me know of any major moves, because if there's one on one pair, then there might be a few more coming soon on other pairs. Also it's nice to buy on dips, aka, silver the other day. That was a great ride up :)

Question about EAs vs Indicators though... since my alert isn't doing any live trading, can it be an indicator? What's the difference code-wise?


OK I made a small breakthrough. I edited the stock mt4 ATR indicator to show 2 lines, and the extern inputs for this indicator are what the periods are. I did this by doubling up the code, then changing variable names by adding _fast and _slow to the end of every variable etc...

The thing is though.... that one line works just perfectly, but the other one is stuck on period 1, and can't be changed! Can someone at least help me with this code?

Eventually I'll want an alert when the ATR(1) is x times the ATR(60). I will also want a visual line that is x times ATR(60), that way I can visually see the ATR(1) touching the (ATR(60) * x) line. Please someone help :) :) =)) =))

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!!

//+------------------------------------------------------------------+
//|                                                                  |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "xx"
#property link      "website"

#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);
  }
//+------------------------------------------------------------------+
 

LPG

Try

// for(int Count = Bars; Count >= 0; Count--)

for(int Count = 0; Count < CalculateBars ; Count++)
          {
              fastATR[Count] = iATR(NULL,0,fastATRperiod,Count);
              
              slowATR[Count] = iATR(NULL,0,slowATRperiod,Count);

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

-BB-

 
BarrowBoy:

LPG

Try

-BB-


Thanks BB!!

Actually, I was able to (somehow, I don't know) figure this out yesterday, however the only thing I changed was your second suggestion. I did not do the first one... it seems to be working so not sure if it's necessary, but maybe I'll give it a go.

I ran into 1 final issue though - look right here at this code:

        string Pair = Symbol();
   double ATRfast = iATR(NULL, 0, fastATRperiod, 0);
   double ATRslow = iATR(NULL, 0, slowATRperiod, 0);
   double MilliSex = (alertSleepMins * 60000);

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

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?

Thanks sooo much!

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);
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.
 

LPG

> 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?

Sleep() doesn't work at all in indicators (or EA's when backtesting)

Use something like

static bool IsNewBar;
static datetime LastBarOpenAt;


start()
 {

 if(LastBarOpenAt == Time[0]) // This tick is not in new bar
    {
     IsNewBar=false;
    }
  else
    {
      LastBarOpenAt = Time[0];
      IsNewBar = true;    
    }

// rest of indicator code where you can check value of IsNewBar
// ................................................................

// ................................................................

 }

-BB-

 

YOU are the man! However you're gonna think I'm really annoying because I was lucky in a google search and found this code to do the once per bar alert:

   static int LastAlert = 0;
  


            if(ATRfast >= (ATRslow * slowATRmultiplier))
                                        {
                                                // Sound Alert
                                        if( LastAlert == 0 || LastAlert < Bars )
                 				{
                			   Alert("Spike on " + Pair + "!");
               				   LastAlert = Bars;
                  				 }  
                                        Print("Fast = " + ATRfast + ", Slow = " + ATRslow);
                                        }

I'm pretty much done. I'd like to figure out how to modify the title of the separate indicator window... where the little white text is, in the top left. And also clean up values in the vertical axis on the right where it shows ATR value, because they are overlapping now... but that's just vanity :p

Thanks BB!

Files:
 

I don't recommend using Bars, as it is unreliable. You eventually reach a maximum number of bars; at this point the value of Bars will not change from one iteration to the next. This might be ok for coding an indicator, but for an EA you should be using Time:

static datetime LastAlert;

if( ATRfast >= ( ATRslow * slowATRmultiplier ))
{
// Sound Alert
   if( LastAlert != Time[0] )
   {                                            
      LastAlert = Time[0];
      Alert( "Spike on " + Pair + "!" );
   }                                                                             }  
   Print( "Fast = " + ATRfast + ", Slow = " + ATRslow );
}

This will still only alert once per bar, but does not have the shortcoming I mentioned above.

Reason: