Is anything wrong with this snippet of code

 

I have written some code that is supposed to stop the same signals coming through multiple times, but it does not seem to work.

Can anybody see any errors in the code?

 BuySignal = false;
 datetime BuyTime = iTime(NULL,0,0);
 if (BuyTime != PrevBuyTime)BuySignal = true;
 
 Print("BuySignal is ", BuySignal);
 
 if (Trade == true && Buy == true && BuySignal == true)
 
         {

            PlaySound("Alert.wav");
            Alert("ALERT: BUY " + Pair);
            SendMail("SR System Alert ", "BUY " + Pair + "  Timeframe = " + Period()+ "; " + " Date = " + 
                  TimeToStr(TimeLocal(),TIME_DATE) + "; " + " Time = " +  TimeToStr(TimeLocal(),TIME_MINUTES));
          
            PrevBuyTime = BuyTime;
            
         } 
 
ernest02:

I have written some code that is supposed to stop the same signals coming through multiple times, but it does not seem to work.

Can anybody see any errors in the code?

Can't tell from your code because we don't know how your variables are declared, are they globally declared, locally declared, local statics, etc. ? For example, if PevBuyTime is a local then it will be declared and reset to zero for each call of this function and that will cause you an issue, if it's globally declared it will be OK.
 
ernest02:

I have written some code that is supposed to stop the same signals coming through multiple times, but it does not seem to work.

Can anybody see any errors in the code?

Is PrevBuyTime declared as global scope variable or as a static local variable? if neither, PrevBuyTime is probably loosing its value when start() is run on the next tick, which makes BuyTime != PrevBuyTime and thereby sets BuySignal to true.
 
RaptorUK:
Can't tell from your code because we don't know how your variables are declared, are they globally declared, locally declared, local statics, etc. ? For example, if PevBuyTime is a local then it will be declared and reset to zero for each call of this function and that will cause you an issue, if it's globally declared it will be OK.

RaptorUK that is interesting! Did not know there was a difference in a variable globally or locally declared. Show you how a beginner I am!

In this case it is globally declared. Should it be a static or just a normal datetime?

 
Thirteen:
Is PrevBuyTime declared as global scope variable or as a static local variable? if neither, PrevBuyTime is probably loosing its value when start() is run on the next tick, which makes BuyTime != PrevBuyTime and thereby sets BuySignal to true.

It is declared as a global variable. Is that enough or should it be a static?
 
ernest02:

RaptorUK that is interesting! Did not know there was a difference in a variable globally or locally declared. Show you how a beginner I am!

In this case it is globally declared. Should it be a static or just a normal datetime?

Globally declared are already static . . . it's locals that you have to decide if they need to be static or not.
 
ernest02:

I have written some code that is supposed to stop the same signals coming through multiple times, but it does not seem to work.

Can anybody see any errors in the code?

What you have should stop you getting the same signal more than once per bar, it won't stop you getting the same signal for different bars though . . . meaning on M1 you could get it once a minute.


Can you copy and paste from the log showing the issue . . .

 

Here is my test on a daily EURUSD chart (in strategy tester, since market isn't open at the moment):

void BuyTest1() {

   bool BuySignal = false, Trade = true, Buy = true;
   datetime BuyTime = iTime(NULL,0,0);
   static datetime PrevBuyTime = 0;
   if (BuyTime != PrevBuyTime) 
      BuySignal = true;
 
   if (Trade == true && Buy == true && BuySignal == true) {
      Print ("BUY ", Symbol(), " at ", TimeToStr(BuyTime));
      PrevBuyTime = BuyTime;
   }
}

int start() {
   BuyTest1();
   return(0);
}

Results:

2013.10.27 12:23:22 2013.10.18 00:00 TestingEA EURUSD,Daily: BUY EURUSD at 2013.10.18 00:00

2013.10.27 12:23:21 2013.10.17 00:00 TestingEA EURUSD,Daily: BUY EURUSD at 2013.10.17 00:00

2013.10.27 12:23:21 2013.10.16 00:00 TestingEA EURUSD,Daily: BUY EURUSD at 2013.10.16 00:00

2013.10.27 12:23:21 2013.10.15 00:00 TestingEA EURUSD,Daily: BUY EURUSD at 2013.10.15 00:00

2013.10.27 12:23:18 2013.10.14 00:00 TestingEA EURUSD,Daily: BUY EURUSD at 2013.10.14 00:00

 
Thirteen:

Here is my test on a daily EURUSD chart (in strategy tester, since market isn't open at the moment):

Results:


Exactly what happens to me! Except the signal/alert is activated every hour because I run it on the H1 time frame.

So what do i need to do to stop a signal every hour?

 

I think I need a counter somewhere and a test that will be false if the counter == 1??

But what happens if the signal becomes true again after a few hours?


 

When and how often do you want a buy signal? Once an hour?

If you are running on a H1 chart, the above code will generate one buy signal per hour.

Reason: