Time elapsed since trade opened alert or EA

 

Hi

I use a breakeven EA to put trades to breakeven after a positive movement of X pips, also have a rule to put the stop loss or take profit (depending on whether the trade was in profit or loss at the time) to the entry price if 24 hours has elapsed since the trade opened if target or stop has not been hit. Currently i eyeball the open time and try to remember to make the shift when 24 hours has passed. There must be a better way!

In it's simplest form it would be an alert to say XYZ trade has been open for 24 hours and i manually make the change (when awake), or it could be part of the breakeven EA and do the job itself

I have had a good look around but have not found any existing indicator or EA to use as a guide for writing something up. Can anyone point me in the right direction to an EA or indicator or the basic code structure to help get me started?

Thanks

 
potsiea:

Hi

. . . . or the basic code structure to help get me started?

Thanks

OrederSelect() the orders one by one using a loop, for each, compare the OrderOpenTime() to TimeCurrent(), if the Order is older than 24 * PERIOD_D1 * 60 sound an Alert/Print()
 
RaptorUK:
OrederSelect() the orders one by one using a loop, for each, compare the OrderOpenTime() to TimeCurrent(), if the Order is older than 24 * PERIOD_D1 * 60 sound an Alert/Print()


Thank you for your assistance
 
potsiea:

Thank you for your assistance

RaptorUK:
OrederSelect() the orders one by one using a loop, for each, compare the OrderOpenTime() to TimeCurrent(), if the Order is older than 24 * PERIOD_D1 * 60 sound an Alert/Print()


Back again

I used your response and coded up what i thought would work, but it is not quite working. My experience with MQL4 is limited, so i am hoping you can tell me where i went wrong as i cannot work it out. I have this attached to a H1 chart, with a trade that has been open for approximately 53 hours, but the alert goes off when i compile the code, even though the threshold is 72 hours. I also only want one alert, not continuous. That one alert maybe after the passing of the threshold if MT4 is closed at the time, but all i have been able to achieve is one alert per hour

//+------------------------------------------------------------------+
//|                                                     24hrs V2.mq4 |
//|                        Copyright 2014, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright 2014, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

extern int HourstoStoporLimit = 72;  // Shift trade to BE or Limit after this number of hours
static datetime TimeStamp;
int AlertCount  = 0;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
     
   for (int i = OrdersTotal(); i >=0; i--)
     {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    if(TimeStamp != Time[0] && TimeCurrent()>= OrderOpenTime()+ (HourstoStoporLimit * PERIOD_H1 * 60))
    {
    Alert(Symbol()," Open for " + HourstoStoporLimit +" hours");
    AlertCount = 1;
    TimeStamp = Time[0];
    }
    }
//----
   return(0);
  }
//+------------------------------------------------------------------+

One other question, if i went one step further and got the EA to close the trade if current price hit the entryprice (from above or below) after the passing of the threshold time, would i be better getting the EA to adjust the stop or limit (which could pose an issue if current price was less than 20 pips from the entryprice), or just get it to close the trade next time price equalled the entryprice?

Many thanks

 
potsiea:


Back again

I used your response and coded up what i thought would work, but it is not quite working. My experience with MQL4 is limited, so i am hoping you can tell me where i went wrong as i cannot work it out. I have this attached to a H1 chart, with a trade that has been open for approximately 53 hours, but the alert goes off when i compile the code, even though the threshold is 72 hours. I also only want one alert, not continuous. That one alert maybe after the passing of the threshold if MT4 is closed at the time, but all i have been able to achieve is one alert per hour

One other question, if i went one step further and got the EA to close the trade if current price hit the entryprice (from above or below) after the passing of the threshold time, would i be better getting the EA to adjust the stop or limit (which could pose an issue if current price was less than 20 pips from the entryprice), or just get it to close the trade next time price equalled the entryprice?

Many thanks

The first obvious issue is your loop . . . if OrdersTotal() returns 3 meaning you have 3 open orders they will be in positions 0, 1 and 2 so your loop must start from OrdersTotal() - 1, this is probably causing the Alert to trigger.

Regarding closing the orders . . . code both options and test them, find which is better.
 
potsiea:


Back again

I used your response and coded up what i thought would work, but it is not quite working. My experience with MQL4 is limited, so i am hoping you can tell me where i went wrong as i cannot work it out. I have this attached to a H1 chart, with a trade that has been open for approximately 53 hours, but the alert goes off when i compile the code, even though the threshold is 72 hours. I also only want one alert, not continuous. That one alert maybe after the passing of the threshold if MT4 is closed at the time, but all i have been able to achieve is one alert per hour

One other question, if i went one step further and got the EA to close the trade if current price hit the entryprice (from above or below) after the passing of the threshold time, would i be better getting the EA to adjust the stop or limit (which could pose an issue if current price was less than 20 pips from the entryprice), or just get it to close the trade next time price equalled the entryprice?

Many thanks


     
  for(int i= OrdersTotal()-1; i>=0 ; i--)    // for (int i = OrdersTotal(); i >=0; i--)
     {
    OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    if(TimeStamp != Time[0] && TimeCurrent()>= OrderOpenTime()+ (HourstoStoporLimit * PERIOD_H1 * 60))

your loop is wrong

you're not checking the magicnumber of the trade or if OrderSymbol is Symbol()

it will mix up with other trades you have also open .....

 
deVries:

your loop is wrong

you're not checking the magicnumber of the trade or if OrderSymbol is Symbol()

it will mix up with other trades you have also open .....


Thank you to DeVries and RaptorUK for their assistance. I got it to work as desired.

Reason: