This should be easy to solve

 

I am sending an Alert when certain conditions are true. I do not want this Alert to be repeated again and again while the same bar is still open, so I have written a little code to prevent this.

The problem is it should be easy for me to see why it is not working, but I am blind so the Alerts keep going.

Please tell me why the following code does not activate the Alert only once! (My variables say "time", because I started first with iTime and then Time[0] and eventually tried iOPen to see if it makes any difference)

Here is the snippet of code:

   
   double SellTime = iOpen(NULL,0,0);                 //this is where I first tried iTime and Time[0]
   if (SellTime != PrevSellTime) SellSignal = true;
   
   if (Trade == true && Sell == true && SellTrade == false && SellSignal == true)
       {
        
           PlaySound("Alert.wav");
           Alert("ALERT: SELL " + Symbol());
           
           PrevSellTime = SellTime;
           

          }     
 
ernest02:

I am sending an Alert when certain conditions are true. I do not want this Alert to be repeated again and again while the same bar is still open, so I have written a little code to prevent this.

The problem is it should be easy for me to see why it is not working, but I am blind so the Alerts keep going.

Please tell me why the following code does not activate the Alert only once! (My variables say "time", because I started first with iTime and then Time[0] and eventually tried iOPen to see if it makes any difference)

Here is the snippet of code:

If you use price you will get into problems where the next bar can actually have the same Open price and of course you will have to tackle the issue of comparing doubles, usse time and you don't have these issue.

How and where are you setting SellSignal back to false ? is PrevSellTime a static or global scope ?

 
RaptorUK:

If you use price you will get into problems where the next bar can actually have the same Open price and of course you will have to tackle the issue of comparing doubles, usse time and you don't have these issue.

How and where are you setting SellSignal back to false ? is PrevSellTime a static or global scope ?


As I said I used time before (iTime & Time[0]), but got the same result. PrevSellTime is a Global variable. Should it be static rather?

Ah! I should set SellSignal to false! That may be the problem. Now where to do it!


 

Well, I'm not really a programer but you might try this:

int cBar;

if (Bars >cBar)

{

if (Trade == true && Sell == true && SellTrade == false && SellSignal == true)

       {
        
           PlaySound("Alert.wav");
           Alert("ALERT: SELL " + Symbol());
           
           cBar = Bars;
           

          }   

}

 
xrafix:

Well, I'm not really a programer but you might try this:

Bars is not reliable . . . use time
 
ernest02:

As I said I used time before (iTime & Time[0]), but got the same result. PrevSellTime is a Global variable. Should it be static rather?

Ah! I should set SellSignal to false! That may be the problem. Now where to do it!

Here:

PrevSellTime = SellTime;
                          //  <------ here
 

RaptorUK,

I changed the code and it is working! The problem was not setting the SellSignal false.

Thanks for your invaluable help!!

This is what the code looks like now:

        
        SellSignal = false;

        datetime SellTime = iTime(NULL,0,0);
        if (SellTime != PrevSellTime)SellSignal = true;
   
        if (Trade == true && Sell == true && SellTrade == false && SellSignal == true)
               {
               
       
           PlaySound("Alert.wav");
           Alert("ALERT: SELL " + Symbol());
           
           PrevSellTime = SellTime;
           

          }