You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
// Globals input int number_of_days = 3; // Number of days to check int seconds_in_a_day = 1440 * 60; // Seconds in a day int time_threshold = number_of_days * seconds_in_a_day; int alerted_positions[]; void OnTick() { if (OrdersTotal() > 0) { for (int i = 0; i < OrdersTotal(); i++) { if (OrderSelect(i, SELECT_BY_POS)) { int ticket = OrderTicket(); // Get the unique ticket ID datetime open_time = OrderOpenTime(); int open_bars = iBarShift(_Symbol, _Period, open_time); int time_of_position = open_bars * PeriodSeconds(); if (time_of_position >= time_threshold && !IsPositionAlerted(ticket)) { double currentProfit = OrderProfit(); string alert_message = "Position has been open for more than " + IntegerToString(time_of_position) + " seconds. Current profit: " + DoubleToString(currentProfit, 2) + " GBP"; Alert(alert_message); SendNotification(alert_message); ArrayResize(alerted_positions, ArraySize(alerted_positions) + 1); alerted_positions[ArraySize(alerted_positions) - 1] = ticket; } } } } } bool IsPositionAlerted(int ticket) { if (ArraySize(alerted_positions) == 0) { return false; // no positions alerted yet } else{ for (int i = 0; i < ArraySize(alerted_positions); i++) { if (alerted_positions[i] == ticket) { return true; } } } return false; }
Thanks, but still returns 2no. alerts per second. I guess it would be fine if it just checks once a day at a specified time
Thanks, but still returns 2no. alerts per second. I guess it would be fine if it just checks once a day at a specified time
Alerts in an EA is a tricky situation in OnTick. There's so much code which won't work, and then some solutions which will work. I haven't tried alerts in an EA yet, only in indicators, so I don't know the most polished solution.
but this logic should definitely work:
untested code though
if((long)time_of_position % 60 == 0){ alerted = false; //allow the alert to run again when a minute passes } if((long)time_of_position % seconds_in_a_day == 0){ alerted = false; //allow the alert to run again when a day passes }
Code fails if there is no tick that second
Forum on trading, automated trading systems and testing trading strategies
How long a trade has been open in days (notifier)
johnsbf, 2024.08.15 13:53
Maybe the sleep function?
Sleep - Common Functions - MQL4 Reference
or
Code fails if there is no tick that minute. There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
"Free-of-Holes" Charts - MQL4 Articles (2006)
No candle if open = close ? - MQL4 programming forum (2010)
Just remember the alert time and alert when necessary.
Thanks, but still returns 2no. alerts per second. I guess it would be fine if it just checks once a day at a specified time
Then set the time to what you want.
Find bar of the same time one day ago - MQL4 programming forum #1 & #6 (2017)See also Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01)
Dealing with Time (Part 2): The Functions - MQL5 Articles
Unnecessary if statement. What does the loop do if there are no orders?
Unnecessary if statement. What does the loop do if there are no orders?
the thought here was that it should only bother processing all the logic if orders (one or more) exist
Code fails if there is no tick that second
Code fails if there is no tick that minute. There can be minutes between ticks during the Asian session, think M1 chart. Larger charts, think weekend, market holiday (country and broker specific), requires knowledge of when your broker stops and starts (not necessary the same as the market.)
"Free-of-Holes" Charts - MQL4 Articles (2006)
No candle if open = close ? - MQL4 programming forum (2010)
Just remember the alert time and alert when necessary.
Then set the time to what you want.
Find bar of the same time one day ago - MQL4 programming forum #1 & #6 (2017)See also Dealing with Time (Part 1): The Basics - MQL5 Articles (2021.10.01)
Dealing with Time (Part 2): The Functions - MQL5 Articles
Thanks to you and Conor for help on this.
Sorry, I'm confused. Do I need to insert the code from items 2 & 3 and if so, where?
Thanks to you and Conor for help on this.
Sorry, I'm confused. Do I need to insert the code from items 2 & 3 and if so, where?
Thanks for your help. I've got it sorted.