Download MetaTrader 5

Why does this not work? :(

To add comments, please log in or register
WTM
82
WTM  
void OnTick()
  {
   datetime candletime=0;
   datetime currenttime=0;
   currenttime=Time[0];
   double K_line=iStochastic(NULL,0,20,3,5,2,0,MODE_MAIN,1);
   double D_line=iStochastic(NULL,0,20,3,5,2,0,MODE_SIGNAL,1);
   double pK_line=iStochastic(NULL,0,20,3,5,2,0,MODE_MAIN,2);
   double pD_line=iStochastic(NULL,0,20,3,5,2,0,MODE_MAIN,2);

   if(pD_line>80 && D_line<80)
      if(currenttime!=candletime)
         SendNotification((string)Period()+Symbol()+"80 Cross, Sell");
      candletime=Time[0];
   if(pD_line<20 && D_line>20)
      if(currenttime!=candletime)
         SendNotification((string)Period()+Symbol()+"20 Cross, Buy");
      candletime=Time[0]
  ;}
//+------------------------------------------------------------------+

I'm new to coding, and not sure why this doesn't work. Need it so send me push notifications when conditions hit.

whroeder1
18212
whroeder1  
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2.  if(pD_line>80 && D_line<80)
          if(currenttime!=candletime)
             SendNotification((string)Period()+Symbol()+"80 Cross, Sell");
          candletime=Time[0];
    The assignment is not part of any if statement. It always executes. Which means the next if fails. Add proper braces.

  3.    datetime candletime=0;
       datetime currenttime=Time[0];
    :
          if(currenttime!=candletime)
    Since candletime isn't static, the first if always succeeds.

  4. SendNotification((string)Period()+Symbol()+"80 Cross, Sell");
    Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

WTM
82
WTM  
whroeder1:
  1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. The assignment is not part of any if statement. It always executes. Which means the next if fails. Add proper braces.

  3. Since candletime isn't static, the first if always succeeds.

  4. Check your return codes for errors, report them and you would know why. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

I don't get any error messages now, but I still don't get notifications.

I do still get the indicator window property warning though.

whroeder1
18212
whroeder1  
WTM:

I don't get any error messages now, but I still don't get notifications.

I do still get the indicator window property warning though.

  1. Asked and answered. Fix your broken code.
  2. It has OnTick not OnCalculate. Therefor it is an EA not an indicator. Fix your broken code.
Matthew Colter
1165
Matthew Colter  
whroeder1:
  1. Asked and answered. Fix your broken code.
  2. It has OnTick not OnCalculate. Therefor it is an EA not an indicator. Fix your broken code.

;) I think the poster is confused about how braces work and would like you to decorate the code. The snippet provided looks very web scripty... I mean the missing semicolon! I will try suggesting something without the context of knowing what the intended functionality of the code is... like... maybe...

   datetime candletime=0;
   datetime currenttime=0;
   currenttime=Time[0];
   double K_line=iStochastic(NULL,0,20,3,5,2,0,MODE_MAIN,1);
   double D_line=iStochastic(NULL,0,20,3,5,2,0,MODE_SIGNAL,1);
   double pK_line=iStochastic(NULL,0,20,3,5,2,0,MODE_MAIN,2);
   double pD_line=iStochastic(NULL,0,20,3,5,2,0,MODE_MAIN,2);

   if(pD_line>80 && D_line<80) {
       if(currenttime!=candletime) {
          SendNotification((string)Period()+Symbol()+"80 Cross, Sell");
          if(pD_line<20 && D_line>20) {
             if(currenttime!=candletime)
             SendNotification((string)Period()+Symbol()+"20 Cross, Buy");
          }
          candletime=Time[0];
      }
  }

Maybe this helps? It needs full volume though, braces don't make sense without full volume.

 

Barring that, maybe the terminal is low on compiler fluid?

WTM
82
WTM  
whroeder1:
  1. Asked and answered. Fix your broken code.
  2. It has OnTick not OnCalculate. Therefor it is an EA not an indicator. Fix your broken code.
Why does it not work as an EA then? Do you have to code it completely differently if it's one or the other given what I want it to do?
WTM
82
WTM  
Matthew Colter:

;) I think the poster is confused about how braces work and would like you to decorate the code. The snippet provided looks very web scripty... I mean the missing semicolon! I will try suggesting something without the context of knowing what the intended functionality of the code is... like... maybe...

Maybe this helps? It needs full volume though, braces don't make sense without full volume.

 

Barring that, maybe the terminal is low on compiler fluid?

I think that's helped - but then again I thought it was fixed before too :(

I just want it to send me a notification whenever the D line crosses from below 20, and from above 80.

Matthew Colter
1165
Matthew Colter  
WTM:

I think that's helped - but then again I thought it was fixed before too :(

I just want it to send me a notification whenever the D line crosses from below 20, and from above 80.

Then my solution will not work. Take your original code and add braces around all of the lines you want executed for each of your "if" statements. Without braces the "if" statements only affect the next line. That is why your original code keeps setting the "candle time" and "current time" to the same value, both places where you are setting the candle time get executed every time your method runs. I think you might have been attempting to only change the candle time after a notification was sent.

As a general rule, always use braces to enclose the lines you want executed when your "if" statement is true. That will help you avoid mistakes like this.
WTM
82
WTM  
Matthew Colter:
Then my solution will not work. Take your original code and add braces around all of the lines you want executed for each of your "if" statements. Without braces the "if" statements only affect the next line. That is why your original code keeps setting the "candle time" and "current time" to the same value, both places where you are setting the candle time get executed every time your method runs. I think you might have been attempting to only change the candle time after a notification was sent.

As a general rule, always use braces to enclose the lines you want executed when your "if" statement is true. That will help you avoid mistakes like this.
void OnInit()
  {
   datetime candletime=0;
   datetime currenttime=0;
   currenttime=Time[0];
   double K_line=iStochastic(NULL,0,20,3,5,2,0,MODE_MAIN,1);
   double D_line=iStochastic(NULL,0,20,3,5,2,0,MODE_SIGNAL,1);
   double pK_line=iStochastic(NULL,0,20,3,5,2,0,MODE_MAIN,2);
   double pD_line=iStochastic(NULL,0,20,3,5,2,0,MODE_MAIN,2);

     {   
      if(pD_line>80 && D_line<80)
     
         if(currenttime!=candletime)
            (SendNotification((string) Period() + Symbol() + "Sell"))
     ;}
     {
      if(pD_line<20 && D_line>20)
        
         if(currenttime!=candletime)   
            (SendNotification((string) Period() + Symbol() + "Buy"))
     ;} 
  }

This code sends me notifications, but too late. It's sending them a bar too late. I want it to look back 2 closed bars only, with the 0th look-back bar being the current open bar. Also, the buy signals aren't working, just sell signals. Seems like something is blocking it? Maybe bracket problem again?

Matthew Colter
1165
Matthew Colter  
WTM:

This code sends me notifications, but too late. It's sending them a bar too late. I want it to look back 2 closed bars only, with the 0th look-back bar being the current open bar. Also, the buy signals aren't working, just sell signals. Seems like something is blocking it? Maybe bracket problem again?

Dyslexia is a difficult thing to deal with, yes. Your "if" statements look like superheroes, I'll never understand why they wear underpants on the outside. I mean, underpants are supposed to be on the inside to brace your junk right? Also, you've effectively destroyed your clock, so checking that the current time is different from the candle time will always be true. You've also given some of your semicolons downs syndrome, they're way downs away from where they should be
.
I'm not sure why they're dangling there like dingleberries
,
but it makes helping you harder for anyone who might be inclined to do so
.
//€
{
! can only conclude that you're bored And(trolololololololol) + nj // ¿por que?
.
}

Has fun with that more of
;
because








.


And you're welcome.
Goodnight.
Matthew Colter
1165
Matthew Colter  
if(true)
{
   Alert("aha!");
   Alert("ooh, two statements in one block!");
}
Print(Time[0]);
Print(TimeCurrent());
ExpertRemove();
The mobile version of this site doesn't offer any formatting buttons, so the example isn't going to turn pink.
123
To add comments, please log in or register