Need some help with my simple alert indicator - page 3

 

It is because you need to use { } if you want to execute multiple lines with an if statement.

You can simplify your code by creating a function to check the lines. It saves you writing out the same code twice.

void CheckAlert(double alert_price)
  {
   if(alert_price==0) return;
   if(Open[1]<alert_price && Close[1]>alert_price)
     {
      Alert(Symbol() + " Bullish Breakout");
      PlaySound("alert.wav");
     }
   if (Open[1]>alert_price && Close[1]<alert_price)
     {
      Alert(Symbol() + " Bearish Breakout");
      PlaySound("alert.wav");
     }
  }  

And then your main body of code:

static datetime last_bar = 0;
datetime this_bar = Time[0];
if(last_bar != this_bar)
  {
   CheckAlert(AlertPrice);
   CheckAlert(AlertPrice2);
   last_bar=this_bar;
  }

You should also consider that if either Close[1] or Open[1] is exactly the same as your AlertPrice, you won't get an alert. It would be better to have >= (or <=) on one side of the if condition e.g.

if (Open[1]>=alert_price && Close[1]<alert_price)

The other thing to be aware of is that the Alert itself plays a sound (actually, the same sound you're trying to add manually!).

When you try to play 2 sounds together it generally doesn't work unless you build in a delay. Do you really want to override the manual sound of Alert?

 
honest_knave:

You can simplify your code by creating a function to check the lines. It saves you writing out the same code twice.

void CheckAlert(double alert_price)
  {
   if(alert_price==0) return;
   if(Open[1]<alert_price && Close[1]>alert_price)
     {
      Alert(Symbol() + " Bullish Breakout");
      PlaySound("alert.wav");
     }
   if (Open[1]>alert_price && Close[1]<alert_price)
     {
      Alert(Symbol() + " Bearish Breakout");
      PlaySound("alert.wav");
     }
  }  

And then your main body of code:

static datetime last_bar = 0;
datetime this_bar = Time[0];
if(last_bar != this_bar)
  {
   CheckAlert(AlertPrice);
   CheckAlert(AlertPrice2);
   last_bar=this_bar;
  }

You should also consider that if either Close[1] or Open[1] is exactly the same as your AlertPrice, you won't get an alert. It would be better to have >= (or <=) on one side of the if condition e.g.

if (Open[1]>=alert_price && Close[1]<alert_price)

The other thing to be aware of is that the Alert itself plays a sound (actually, the same sound you're trying to add manually!).

When you try to play 2 sounds together it generally doesn't work unless you build in a delay. Do you really want to override the manual sound of Alert?

I'm having a hard time deciphering exactly how your first and second examples work? Could you give me a quick explanation of how they work?

As for the alert itself, I only want to be alerted if we close ABOVE or BELOW the level ;)

I actually saved a different file as the default "alert.wav" file to something more suitable, that actually gets my attention, a ways back.... as I'm writing this, I just realized that if it already plays the alert.wav file, then having the PlaySound(alert.wav) is probably redundant...

 
gagebrk:

I'm having a hard time deciphering exactly how your first and second examples work? Could you give me a quick explanation of how they work?

The logic for testing AlertLine and AlertLine2 is exactly the same, except for the price you are checking. So rather than write out the same code twice in OnCalculate, you can create a custom function that you call.

Replace your entire OnCalculate() function with this:

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   static datetime last_bar = 0;
   datetime this_bar = Time[0];
   if(last_bar != this_bar)
     {
      CheckAlert(AlertPrice);
      CheckAlert(AlertPrice2);
      last_bar=this_bar;
     }
//--- return value of prev_calculated for next call
   return(rates_total);
  }

//+------------------------------------------------------------------+
//| Function to check an alert line                                  |
//+------------------------------------------------------------------+  
void CheckAlert(double alert_price)
  {
   if(alert_price==0) return;
   if(Open[1]<alert_price && Close[1]>alert_price)
     {
      Alert(Symbol() + " Bullish Breakout");
      PlaySound("alert.wav");
     }
   if (Open[1]>alert_price && Close[1]<alert_price)
     {
      Alert(Symbol() + " Bearish Breakout");
      PlaySound("alert.wav");
     }
  }  
gagebrk:


As for the alert itself, I only want to be alerted if we close ABOVE or BELOW the level ;)

OK, but imagine the following situation.

AlertLine = 1.1000

Open[1] = 1.1000

Close[1] = 1.1020

A bullish breakout? Nope, because Open[1] was not <AlertLine.

gagebrk:

I actually saved a different file as the default "alert.wav" file to something more suitable, that actually gets my attention, a ways back.... as I'm writing this, I just realized that if it already plays the alert.wav file, then having the PlaySound(alert.wav) is probably redundant...

Not wasted, because you learned about { } 
 

 
honest_knave:

The logic for testing AlertLine and AlertLine2 is exactly the same, except for the price you are checking. So rather than write out the same code twice in OnCalculate, you can create a custom function that you call.

OK, but imagine the following situation.

AlertLine = 1.1000

Open[1] = 1.1000

Close[1] = 1.1020

A bullish breakout? Nope, because Open[1] was not <AlertLine.

Not wasted, because you learned about { } 
 

I fixed that! You were right I didn't think about that :)