Please tell me what is wrong with this simple code

 

I have an EA where I call a custom indicator and have some simple code to read shift 0 and shift 1 and do some comparisons with it and when a certain condition is true I generate an alert signal.

The problem is that instead of getting this one alert signal I get several signals and I cannot understand why.

Here is the simple code. I get an alert signal even when just the middle condition (e.g. EURafter < USDafter) is true and the other two conditions are false !


    CurrencyBuySignal = false;
    
    double USDbefore1 = iCustom(NULL,0,"CurrencySlopeStrength(2)",0,1); //reading value of USD shift 1
    double USDafter = iCustom(NULL,0,"CurrencySlopeStrength(2)",0,0);   //reading value of USD shift 0
    double EURbefore1 = iCustom(NULL,0,"CurrencySlopeStrength(2)",1,1); //reading value of EUR shift 1
    double EURafter = iCustom(NULL,0,"CurrencySlopeStrength(2)",1,0);

    string CurrPair = Symbol();
    string FirstCurr = StringSubstr(CurrPair,0,3);
    string SecondCurr = StringSubstr(CurrPair,3,3);

    if (FirstCurr == "EUR" && SecondCurr == "USD") 
   {
      Pair = "EURUSD";
      if ((EURbefore1 > USDbefore1) && (EURafter < USDafter) && (USDafter - EURafter >= Diff)) 
      CurrencySellSignal = true;
      if ((EURbefore1 < USDbefore1) && (EURafter > USDafter)&& (EURafter - USDafter >= Diff))
      CurrencyBuySignal = true;
   }
 

I don't see any alert in your code

You set CurrencyBuySignal to false, but do not set CurrencySellSignal to false, could this be causing your problem?

 
ernest02:

I have an EA where I call a custom indicator and have some simple code to read shift 0 and shift 1 and do some comparisons with it and when a certain condition is true I generate an alert signal.

The problem is that instead of getting this one alert signal I get several signals and I cannot understand why.

Here is the simple code. I get an alert signal even when just the middle condition (e.g. EURafter < USDafter) is true and the other two conditions are false !


Where are you setting CurrencySellSignal = false ?
 

Sorry guys I thought I copied both lines like this:

    CurrencySellSignal = false;
    CurrencyBuySignal = false;

So that cannot be the problem. I was thinking there could be something wrong with my "if" statements

 
GumRai:

I don't see any alert in your code

You set CurrencyBuySignal to false, but do not set CurrencySellSignal to false, could this be causing your problem?


Here is the alert code that I did not copy before:

if (Trade == true && Buy == true && BuyTrade == false && 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));
           
          /*  Signal++
            if (Signal == 3) BuySignal = false;*/
            
            PrevBuyTime = BuyTime;
            
                        } 
                        
        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 " + Pair);
           SendMail("SR System Alert", "SELL " + Pair + "  Timeframe = " + Period()+ "; " + " Date = " + TimeToStr(TimeLocal(),TIME_DATE) + "; " + " Time = " +  TimeToStr(TimeLocal(),TIME_MINUTES));
           PrevSellTime = SellTime;
           
 
          }             
 

Unless I am having a cross-eyed moment, I don't see how the Alert is connected to the previous code that you posted.

CurrencyBuySignal and CurrencySellSignal are not referred to

 
ernest02:

Sorry guys I thought I copied both lines like this:

Are thy placed in the position shown in your code above or somewhere else ?
 

Cannot find the way to edit my code after submitting. I left out this part at the beginning of the alert code:

 BuySignal = false;
 datetime BuyTime = iTime(NULL,0,0);
 if (BuyTime != PrevBuyTime)BuySignal = true;
 
 Print("BuySignal is ", BuySignal);
 
RaptorUK:
Are thy placed in the position shown in your code above or somewhere else ?


Like they are shown here. Same sequence.



 
ernest02:

Cannot find the way to edit my code after submitting.

Remove the code section and add the corrected code . . .
 
GumRai:

Unless I am having a cross-eyed moment, I don't see how the Alert is connected to the previous code that you posted.

CurrencyBuySignal and CurrencySellSignal are not referred to

Ernest, can you show the relationship between CurrencyBuySignal and CurrencySellSignal and these variables . . .

if (Trade == true && Buy == true && BuyTrade == false && BuySignal == true)
Reason: