How long a trade has been open in days (notifier) - page 2

 
Conor Mcnamara #:
// 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

 
johnsbf #:

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:

// 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[];

datetime initializationTime;
bool alerted = false;

void OnInit() {

initializationTime = TimeCurrent();
}

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 ((long)time_of_position >= time_threshold && !alerted && TimeCurrent() >= initializationTime) {
                    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);
                        
                    alerted = true;
                }
            }
        }
    }

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  
  }

}


untested code though

 
  1. Conor Mcnamara #: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

  2. 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)

  3. Just remember the alert time and alert when necessary.

    #define DT_MAX D'3000.12.31'
    datetime nextAlert = DT_MAX; // global
    ⋮
            OrderSend(…); nextAlert= TimeCurrent() + …;
    ⋮
            for (int i = 0; i < OrdersTotal(); i++) {
                if (OrderSelect(i, SELECT_BY_POS)) {
                    datetime now=TimeCurrent();
                    if(nextAlert < now){ nextAlert=now +  …; Alert( … );
    
  4. johnsbf #:

    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.

    #define HR1000 (10*3600)
    nextAlert=tomorrow() + HR1000; Alert( … );
              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

 
Conor Mcnamara #: untested code though
    if (OrdersTotal() > 0) {
        for (int i = 0; i < OrdersTotal(); i++) {

Unnecessary if statement. What does the loop do if there are no orders?

 
William Roeder #:

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

 
William Roeder #:
  1. Code fails if there is no tick that second


  2. 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)

  3. Just remember the alert time and alert when necessary.

  4. 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?

 
johnsbf #:

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.