Trigger an Alert/Action when the candle closes

 
double currentPrice = Open[0];
   double lastPrice = Open[1];
  
   if ( (lastPrice > 52.75 ) )
  
 
      Alert("Hello");  


Hi. I am building my first EA.

I want to trigger an action in this case an alert when a candle closes.
Exactly: If the price was at e.g. 52.75 at any point during the most recently closed candle e.g. 1 hour timeframe. then trigger an alert.

Is my code correct?
Is this code reading whatever timeframe I have selected on the chart?


Thanks

 
Your code is not correct — it will Alert on every tick when the condition is true. Check for a new bar.
For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
          New candle - MQL4 programming forum
 
William Roeder:
Your code is not correct — it will Alert on every tick when the condition is true. Check for a new bar.


datetime time0; int init(){ time0 = Time[0];  // No mid bar allowed.
   :
}
int start(){
   bool isNewBar = time0 != Time[0]; time0 = Time[0];
  

double lastPrice = Open[1];
  
   if ( (lastPrice > 52.75 ) ) 
   

Alert("Hello");  
   


How do I use the check for new bar code in this context?

I want to detect if a price has been hit & then trigger an alert only when a new bar appears.

 
Elixium: How do I use the check for new bar code in this context?
if new bar, then check your price. What part of the code in "New candle" was unclear? Why did you copy only the first line out of five?
 
William Roeder:
if new bar, then check your price. What part of the code in " New candle" was unclear? Why did you copy only the first line out of five?

I copied the code from section 4. of your message here:

https://www.mql5.com/en/forum/150885#comment_3766805


Is this correct? Logically "If there is a new bar & lastprice within 1 bar back hit X price then execute alert"


void OnTick()
  {
//---
datetime time0; int init(){ time0 = Time[0];  // No mid bar allowed.
   :
}
int start(){
   bool isNewBar = time0 != Time[0]; time0 = Time[0];
  

double lastPrice = Open[1];
  
   if ( (isNewBar && lastPrice > 52.75 ) ) 
   

Alert("Hello");  
   
New candle
New candle
  • 2014.04.04
  • www.mql5.com
Anyone got a good piece of code for build 600+ that will determine if a tick that came in is the start of a new candle or not...
 
Elixium:


Hi. I am building my first EA.

I want to trigger an action in this case an alert when a candle closes.
Exactly: If the price was at e.g. 52.75 at any point during the most recently closed candle e.g. 1 hour timeframe. then trigger an alert.

Is my code correct?
Is this code reading whatever timeframe I have selected on the chart?


Thanks

What is 52.75 ? pip? Close? Open? Ask? Bid? Spread?

 
Ahmad Zuhairdi Noh:

What is 52.75 ? pip? Close? Open? Ask? Bid? Spread?

$52.75 on e.g. Apple stock. I picked a random number.

If the price of e.g. $52.75 occured within the current open candle then alert me when there is a new candle that this price has occured.

Wait for the open candle to close first before sending the alert.

 
Elixium:

$52.75 on e.g. Apple stock. I picked a random number.

Ahh, I see.. this is the code for MT4..

extern double targetPrice = 52.75; //put this line above OnInit so you can easily change it in the Input section
int barMark = 0; //put this line above OnInit to initialize the mark in a global scope

void OnTick(){
  if(Close[0] > targetPrice && barMark != Hour()){
    Alert("Price exceed level!");
    barMark = Hour(); //this mark the time to make sure the alert only notify once per hour. You can change it to Minute() or Day() etc.
  }
}

But, the code above is if you want the alert to be activated when the price is above the target. You can change ">" to "<" if you want it to be activated whenever it is below target.

There you go. Hope this help.

Goodluck.

 
Elixium: I copied the code from section 4. of your message here:
  1. Really? My link went to #3. Now compare the code:

    Your code
     Posted code from the link
    int start(){
       bool isNewBar = time0 != Time[0]; time0 = Time[0];
    
    
    
    
    
    
    
    void OnTick(){
       static datetime timeCur; datetime timePre = timeCur; timeCur=Time[0];
       bool isNewBar = timeCur != timePre;
       if(isNewBar){ 
         : // Once per bar
       }
       : // every tick
    }
    Multiple lines, No static datetime

  2. void OnTick()
      {
    //---
    datetime time0; int init(){ time0 = Time[0];  // No mid bar allowed.
       :
    }
    int start(){
       bool isNewBar = time0 != Time[0]; time0 = Time[0];
      
    You should stop using the old event handlers and IndicatorCounted and start using the new ones.
              Event Handling Functions - Functions - Language Basics - MQL4 Reference
              How to do your lookbacks correctly.

    You definitely can not use both.

 
Ahmad Zuhairdi Noh:

Ahh, I see.. this is the code for MT4..

But, the code above is if you want the alert to be activated when the price is above the target. You can change ">" to "<" if you want it to be activated whenever it is below target.

There you go. Hope this help.

Goodluck.

Thanks. I tried this and the alert executed immediately, It does limit the alert to once per hour but I need the alert to be delayed until a new bar appears, not to show up immediately.
 

Ok I've tried this, but now I have 3 errors that I don't know how to fix.

Update:

Ok I can see where the problem might be, I'll try again soon.


https://www.mql5.com/en/forum/323617#comment_13434476


void OnTick(){
   static datetime timeCur; datetime timePre = timeCur; timeCur=Time[0];
   bool isNewBar = timeCur != timePre;
   if(isNewBar){ // Error: ':'unexpected token
     : // Once per bar
   } // // Error: ':' unexpected token
   : // every tick
}

double lastPrice = Open[1];
  
if ( (isNewBar && lastPrice > 52.75 ) ) // Error "if expressions are not allowed on a global scope"
   

Alert("Hello"); 
Trigger an Alert/Action when the candle closes
Trigger an Alert/Action when the candle closes
  • 2019.10.04
  • www.mql5.com
Hi. I am building my first EA. I want to trigger an action in this case an alert when a candle closes. Exactly: If the price was at e.g. 52...
Reason: