use time (Pip-Target not included):
datetime barExit = Time[0] + _Period*60*10 - 1; bool IsOpened = true; ... if (TimeCurrent() > Time[0] && IsOpend) { /* close*/...}
At the moment you take the trade you should capture the time of that bar with Time[0] as gooly shows.
Then it's merely a matter of adding how many seconds you want the order to remain open.
if you were on 1 minute charts then you would want to close after 10 minutes or 600 seconds.
Let's say the time of the current candle was had the datetime of 5,000,000 seconds. The number of seconds from the first second in 1970.
If you are trading on 5 minute time frame then obviously you would take 10 bars which would be 50 minutes or 3000 seconds
Or as gooly shows above .. _Period * 60 * 10. = 5 *60*10 =3000.
Every subsequent candle will have a start time 300 seconds later..
- 5,000,000,
- 5,000,300,
- 5,000,600,
- 5,000,900,
- 5,001,200,
- 5,001,500,
- 5,001,800,
- 5,002,100,
- 5,002,400,
- 5,002,700,
So the tenth candle would be started at 5,002,700 and would finish at 5,002,999
and then candle 11 would start at 3,003,000 You would want your trade to close if the time is AFTER 5,002,999
In other words 5 ,000,000 + 5*60*10 =5,003,000. minus 1 second is 5,000,2,999.
You would save this datetime in some variable like barExitTime.
It would also be a good idea to save the ticket number in an int variable like openTicket.
Then you merely put some logic in to see first of all if the order is still open and if it is...
check to see if it's past the time to close it. You can select the order by it's ticket.
if(!OrderSelect(openTicket, SELECT_BY_TICKET ) return; //if we don't select our ticket jump out. if(OrderCloseTime()) return; //if the order is open, it's OrderCloseTime will be 0 which equals false. //if the order is closed it's OrderCloseTime will be something other than 0 which means true. //if it is true then it is closed and it will jump out... (return) // if it has made it this far then we actually look at the time datetime and decide whether it's time to close. if(TimeCurrent()>barExitTime) OrderClose(openTicket,etc.......); of course you could put it all in one if condition like this.... if(OrderSelect(openTicket, SELECT_BY_TICKET) && (!OrderCloseTime() && TimeCurrent()>barExitTime ) OrderClose(openTicket,etc.......);
This is only one way you could do it and this code might have errors in it because I just wrote it off of the top of my head. (Which can be dangerous)...
1. Keep in mind that if you take this trade while on a 1 hour chart .. it is going to set the time out 10 hours..
2. If you then change time frames you will lose your "saved" exit time unless you save it in a global variable.
3. If you do save it, keep in mind it is not going to close in 10..... 15 minute candles.
4. You could also save the exit time in the comment of the trade so that you could examine it no matter how many times you change timeframes
making a global variable unnecessary.
Pippip...Jimdandy
Minor comment - as an alternative to _Period*60 you can use PeriodSeconds
Thanks a lot, but it doesnt work (at least in my code..) . I tried it before your help in a similar way, but in the backtest it isnt noted.
I tried it with (Time[0] > barExitTime ) or with ( TimeCurrent and different ways of barExitTime ( PeriodSeconds and _Period*60) but it isnt observed
//Close order OrderSelect(SellTicket,SELECT_BY_TICKET) && (TimeCurrent()>barExitTime); { double CloseLots = OrderLots(); double ClosePrice = Ask; bool Closed = OrderClose(SellTicket,CloseLots,ClosePrice,UseSlippage,Red); }or this way
//Close order { OrderSelect(SellTicket,SELECT_BY_TICKET); if(OrderCloseTime() == 0 && SellTicket > 0 && TimeCurrent()>barExitTime); {...}
I think i do something else until i have reached more knowledge.. The SL or TakeProfit is used but not the TimeSL..
Thanks a lot, but it doesnt work (at least in my code..) . I tried it before your help in a similar way, but in the backtest it isnt noted.
I tried it with (Time[0] > barExitTime ) or with ( TimeCurrent and different ways of barExitTime ( PeriodSeconds and _Period*60) but it isnt observed
I think i do something else until i have reached more knowledge.. The SL or TakeProfit is used but not the TimeSL..
Fx90 read in you reference about OrderSelect - you'll find an example (use it!!):
if ( OrderSelect(..) .. ) { ... }
This if has a very important meaning here! Start thinking why!
Fx90 read in you reference about OrderSelect - you'll find an example (use it!!):
This if has a very important meaning here! Start thinking why!
Thanks a lot, but i start now to teach myself with tutorials. i have a to small knowledge at the moment for some operators , functions,etc.. I try it again in a few days..
Hello,
i try it again and it doesnt work again.. it would be nice if someone can help me, this Time SL makes me sick..
//TimeSl_1 OrderSelect(OrdersTotal()-1,SELECT_BY_POS); { if( TimeCurrent() - OrderOpenTime() >= TimeToStr(_Period*60*Zeitstop_1,TIME_DATE+TIME_MINUTES+TIME_SECONDS) ) OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrNONE); }
The Time function has to be right because it works when i check it on a chart with a Comment. He shows the right OrderTicket too then.
But when i try to use it in a Expert Advisor it doesnt works. There must be something wrong with The OrderOpenTime and The OrderSelect function. The OpenOrderTime is zero if the order isnt selected.
Usually i use BuyTicket but I dont get the order.. i tried it in different ways (so the TimeSl_1 above could be wrong)
BuyTicket = OrderSend(...)
Is there Someone who can post here or send me a simple "TimeSL" Code which works right?
thx a lot
Haven't got a at least a warning:
if( TimeCurrent() - OrderOpenTime() >= TimeToStr(_Period*60*Zeitstop_1,TIME_DATE+TIME_MINUTES+TIME_SECONDS) )
You compare datetime to a string???
- Gooly's approach is that it it would close after 10 bar periods. That isn't the same as after 10 bars. If there are missing bars, no ticks ("Free-of-Holes" Charts - MQL4 Articles) or over the weekend it would close at the first tick of the next bar. If the order opened mid bar, the close time is mid bar of the 11th bar.
- If you want the counts of bars: iBarShift - MQL4 Documentation
![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
hello,
i want to Close the trades after special numbers of bars if a Target isnt reached; for example: after 10 bars CloseOrder if Target of 10 Pips isnt achieved.
I have problems to code it because i dont know a function which counts the bar of the Trade opening.
Has anybody an idea?
thx