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
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.
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](https://c.mql5.com/36/28/new-candle.jpg)
- 2014.04.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.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?
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.
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.
-
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 }
-
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.
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.
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](https://c.mql5.com/36/28/trigger-an-alert-action-when-the.jpg)
- 2019.10.04
- www.mql5.com
![MQL5 - Language of trade strategies built-in the MetaTrader 5 client terminal](https://c.mql5.com/i/registerlandings/logo-2.png)
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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