Close trades after 30 mins

 

I need the code to close all active trades if they have been active for longer than 30 mins.

can anyone help?

thank you.

 
onemore:

I need the code to close all active trades if they have been active for longer than 30 mins.

can anyone help?

thank you.

Hi,


Good day,


Are you looking for a script to close trades on time??


Best wishes,

 
scarface:

Hi,


Good day,


Are you looking for a script to close trades on time??


Best wishes,


no, i'm looking for code that, within my ea, closes all open trades after 30 mins.

smth like: if(TimeCurrent()-OrderOpenTime()>=30) then {CloseTrades();}

the constant 30 does not work, so i wonder what constant i should use for accurately describing 30mins

 
onemore:


no, i'm looking for code that, within my ea, closes all open trades after 30 mins.

smth like: if(TimeCurrent()-OrderOpenTime()>=30) then {CloseTrades();}

the constant 30 does not work, so i wonder what constant i should use for accurately describing 30mins

Hi,


O.k, look here:

// place these variables on top of your EA

int wenum=0,cnt=0,mode=0;
datetime StartTime;



void CloseOrdbyTime()
{
     int total=OrdersTotal();
    
     for (cnt=0;cnt<total;cnt++)
     {
        OrderSelect(cnt, SELECT_BY_POS);  
        mode=OrderType();
        if ( mode <= OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
        {
          datetime OpenTime=StrToTime(TimeToStr(OrderOpenTime(),TIME_DATE));
          datetime CurnTime=StrToTime(TimeToStr(CurTime(),TIME_DATE));
          int weekday=TimeDayOfWeek(OrderOpenTime());
          if (weekday <=5 && DayOfWeek()==1 && CurnTime > prevwe && CurnTime > OpenTime) {wenum =wenum + 1; prevwe = CurnTime;}      
          
          int CalcBars=TradePeriod+2*wenum;
        
            if ((CurTime()-OpenTime)>=CalcBars*60)
            {
              if (mode==OP_BUYLIMIT)
              OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Green);
              if (mode==OP_BUYSTOP)
              OrderClose(OrderTicket(),OrderLots(),Bid, Slippage, Blue);
              if (mode==OP_SELLLIMIT)
              OrderClose(OrderTicket(), OrderLots(), Ask, Slippage,Pink);
              if (mode==OP_SELLSTOP)
              OrderClose(OrderTicket(),OrderLots(), Ask, Slippage,Red);
              if (mode==OP_BUY )
     OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,Yellow);
     if (mode==OP_SELL)
     OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,White);
            }
        }
    }
// place this one below in Start() function
if( TradePeriod > 0 )CloseOrdbyTime(); }


By the way, this code for the end of the day close. You can change it to anytime you want.

I hope this is useful.

Best of luck.

Scarface
 
scarface:

void CloseOrdbyTime()
[...]

Of the top of my head there are two problems with this function as it stands:


  • The code is looping up through the the order list while simultaneously doing things which modify the order list. There are countless existing discussions on this forum about the need to loop down through a list if you're altering the contents of the list. For example, see cloudbreaker's post in https://www.mql5.com/en/forum/120128
  • The code tries to remove pending orders using OrderClose(). I'm not aware of any broker who allows this. As far as I know, pending orders have to be removed using OrderDelete(), not OrderClose().


smth like: if(TimeCurrent()-OrderOpenTime()>=30) then {CloseTrades();}

the constant 30 does not work, so i wonder what constant i should use for accurately describing 30mins

This approach should work fine, except that the difference between TimeCurrent() and OrderOpenTime() is in seconds, not minutes. The check should be >=1800, not >=30.

 
jjc:

Of the top of my head there are two problems with this function as it stands:


  • The code is looping up through the the order list while simultaneously doing things which modify the order list. There are countless existing discussions on this forum about the need to loop down through a list if you're altering the contents of the list. For example, see cloudbreaker's post in https://www.mql5.com/en/forum/120128
  • The code tries to remove pending orders using OrderClose(). I'm not aware of any broker who allows this. As far as I know, pending orders have to be removed using OrderDelete(), not OrderClose().


This approach should work fine, except that the difference between TimeCurrent() and OrderOpenTime() is in seconds, not minutes. The check should be >=1800, not >=30.


ok thanks a lot, to both. so the constants are seconds. thats good to know. i think i tried that before but didnt work, but i;ll give it another shot now.

that nice coding scarface if you do not mind i will make use of that function when i will neeed it, is that ok?

Reason: