Need some help with my simple alert indicator - page 2

 
gagebrk:
So when the Alert is activated, it will pop up a message ? 

Yup. Can get very annoying if you don't restrict the number of alerts (you already have, by checking only once per bar)

gagebrk:
Also, I'd like to tie the horizontal line to the alert better, so that you can drag the line and it will drag the alert, also. I think I saw something about that in my searches :)
Also going to add inputs so they can change the color/type of the horizontal line.

All very do-able.

 ----

As I mentioned in my first post, there is the "old style" and the "new style" in MQL4. Quite often you can mix'n'match, but generally only the new style will also work with MQL5. At some point, you will probably want to make the transition to MQL5 so I recommend sticking to the new style MQL4 whenever possible. 

With that in mind, consider changing this (old style, not compatible with MQL5):

ObjectSet(alert_line,OBJPROP_COLOR,clrDarkOrchid);
ObjectSet(alert_line,OBJPROP_STYLE,STYLE_DASH);
ObjectSet(alert_line,OBJPROP_WIDTH,1);

To this (new style, compatible with MQL5):

ObjectSetInteger(0,alert_line,OBJPROP_COLOR,clrDarkOrchid);
ObjectSetInteger(0,alert_line,OBJPROP_STYLE,STYLE_DASH);
ObjectSetInteger(0,alert_line,OBJPROP_WIDTH,1);

The basic building blocks of working with objects are:

ObjectSetInteger / ObjectSetString / ObjectSetDouble for setting values

ObjectGetInteger / ObjectGetString / ObjectGetDouble for getting values
 

 
honest_knave:

...

To this (new style, compatible with MQL5):

ObjectSetInteger(alert_line,OBJPROP_COLOR,clrDarkOrchid);
ObjectSetInteger(alert_line,OBJPROP_STYLE,STYLE_DASH);
ObjectSetInteger(alert_line,OBJPROP_WIDTH,1);


You need to add the chart ID as first parameter if you want to compatible with mql5.
 
Alain Verleyen:
You need to add the chart ID as first parameter if you want to compatible with mql5.
Quite right Alain, missed that in my late-night stupor! Edited my previous post accordingly
 

You guys are really, truly awesome! Thanks for taking the time to help me out! :)

 Next thing I found:
When I put the alert on the chart, the horizontal line and the alert are placed perfectly. However, if I go and add a second alert to the same chart (say I wanted to straddle the market or something), the 2nd alert shows as there, but the horizontal line does not show? I haven't looked to see if the 2nd alert actually works, but it shows in the indicator window as being there...

Maybe I'll just have a "2nd alert" boolean input, and another input double (AlertPrice2) and if boolean is True, then the other alert price is plotted? I'm guessing there's a much easier way, though! What am I missing?

 
gagebrk:

You guys are really, truly awesome! Thanks for taking the time to help me out! :)

 Next thing I found:
When I put the alert on the chart, the horizontal line and the alert are placed perfectly. However, if I go and add a second alert to the same chart (say I wanted to straddle the market or something), the 2nd alert shows as there, but the horizontal line does not show? I haven't looked to see if the 2nd alert actually works, but it shows in the indicator window as being there...

Maybe I'll just have a "2nd alert" boolean input, and another input double (AlertPrice2) and if boolean is True, then the other alert price is plotted? I'm guessing there's a much easier way, though! What am I missing?

Do you mean you add the same indicator twice to the same chart?
 
honest_knave:
Do you mean you add the same indicator twice to the same chart?
Yes :)
 
gagebrk:
Yes :)

There can only be one object with any given name.

You have hard-coded a name for you HLINE, which means both indicators will be trying to create the same object.

You need to make the name of the HLINE unique to each instance of the indicator.

There are quite a few ways to do this. For example, you could generate a random number to prefix your object name.

 
honest_knave:

There can only be one object with any given name.

You have hard-coded a name for you HLINE, which means both indicators will be trying to create the same object.

You need to make the name of the HLINE unique to each instance of the indicator.

There are quite a few ways to do this. For example, you could generate a random number to prefix your object name.

Would what I suggested before work? Basically: 2nd alert, yes or no (True/false)? It's yes (true), Ok, draw HLINE_2 at AlertPrice2... (obviously a very simplified explanation LOL)

OR!

Would an IF statement work? Look for HLINE, IF HLINE is being used, then use HLINE_2 (pre-defined)? 

Really I only see this needing a max of two alerts. Those two options seem like the most obvious ways to solve this (for me)... Curious to pros/cons, or your thoughts to them? :)

Thanks for all the help! 

 

It'll be a matter of personal preference.

If you're sure you only want 2 alerts, build them both into one indicator.

AlertLine1 and AlertLine2

You can create functions to avoid duplication of code. 

To control whether they are both switched on, when you come to draw each line simply test if the input value > 0.  

 
static datetime last_bar = 0;
   datetime this_bar = Time[0];
   if(last_bar != this_bar)
     {
      if (Open[1]<AlertPrice && Close[1]>AlertPrice) Alert(Symbol() + " Bullish Breakout");
        
      if (Open[1]>AlertPrice && Close[1]<AlertPrice) Alert(Symbol() + " Bearish Breakout");
        
          
          if (AlertPrice2 != 0.0)
            {
               if (Open[1]<AlertPrice2 && Close[1]>AlertPrice2) Alert(Symbol() + " Bullish Breakout");
                  
               if (Open[1]>AlertPrice2 && Close[1]<AlertPrice2) Alert(Symbol() + " Bearish Breakout");
                  
            }
      last_bar = this_bar;
     }
    

Ok! So that's what I have now, and it looks like it works!

 

I went to add sound alerts to it, by doing this:  

static datetime last_bar = 0;
   datetime this_bar = Time[0];
   if(last_bar != this_bar)
     {
      if (Open[1]<AlertPrice && Close[1]>AlertPrice) Alert(Symbol() + " Bullish Breakout");
         PlaySound("alert.wav");
      if (Open[1]>AlertPrice && Close[1]<AlertPrice) Alert(Symbol() + " Bearish Breakout");
         PlaySound("alert.wav");
          
          if (AlertPrice2 != 0.0)
            {
               if (Open[1]<AlertPrice2 && Close[1]>AlertPrice2) Alert(Symbol() + " Bullish Breakout");
                  PlaySound("alert.wav");
               if (Open[1]>AlertPrice2 && Close[1]<AlertPrice2) Alert(Symbol() + " Bearish Breakout");
                  PlaySound("alert.wav");
            }
      last_bar = this_bar;
     }
    

 But that leads to the sound being played (seemingly) randomly...Not when the conditions above it are met...