# How to compare OrderOpenTime() and present time?

122

I want to set stoploss closer to open price when the position was opened for more than 120 min:

So, I have this:

for (int i = 0; i < OrdersTotal(); i++) {

if ( OrderSymbol()==Symbol())
{

if (present time - OrderOpenTime() >= 120 min) {

Here I don't know how to express: (present time - OrderOpenTime() >= 120 min)

The key question is what this OrderOpenTime() is returned, it's yy mn dd hh:min or it's hh:mm?

Could anybody help?

Many thanks,

Jean

94

MontS wrote >>

I want to set stoploss closer to open price when the position was opened for more than 120 min:

So, I have this:

for (int i = 0; i < OrdersTotal(); i++) {

if ( OrderSymbol()==Symbol())
{

if (present time - OrderOpenTime() >= 120 min) {

Here I don't know how to express: (present time - OrderOpenTime() >= 120 min)

The key question is what this OrderOpenTime() is returned, it's yy mn dd hh:min or it's hh:mm?

Could anybody help?

Many thanks,

Jean

use TimeCurrent() for present time...

check the documentation....its very clear.

https://docs.mql4.com/dateandtime/TimeCurrent

The result will be in seconds.

122

Thanks,

Can the output of OrderOpenTime() be time in minute or date + hour: minute format?

jean

37

The output of OrderOpenTime() is quite complex, as the program code would only see it as 'int' and you would need convert it to 'str' => TimeToStr() before you get the output in valued time. But then this would return the time in Year-month-day hour-min. So to answer your question on how you compare this in real time, you wouldn't / shouldn't bother about the time value, but the int value and the exact time frame that generated the entry signal or would generate an exit. Try this:

// variable declaration

int orderTime = OrderOpenTime();

int timePlus = orderTime + ((60*timeFrame)*increment);

int currentTime = iTime(symbol, timeFrame, shift);

//final comparisons

if(orderTime == currentTime ) {

OrderClose( int ticket, double lots, double price, int slippage, color Color=CLR_NONE)

}

where: 60 = mins, timeFrame = SignalTimeFrame and increment = number of bars on that timeFrame from OrderOpenTime().

Example if your signal timeFrame is 15mins and you would want to close the trade in 120mins, you would have 120/15 = 8, so you would want to use '8' and you 'increment' that is 8 15mins candle from OrderOpenTime();

This should work for the task at hand. Hope you understand it. Happy Trading.

Moderator
18036

omovaze:

This should work for the task at hand. Hope you understand it. Happy Trading.

It's taken you almost 5 years to offer this advice ? why ? Please do not dredge up ancient threads for no good reason, it just causes confusion for everyone else. Your solution also fails . . . what if there is no tick during the second when orderTime == currentTime ?

97

When people are searching for answers to these problems, who really cares if the thread is ancient? It's still relevant.

I'd rather find a thread that has a conclusion than the final word being "It took 5 years?"

Who cares, as long as there's an answer at the end of it so it doesn't waste everyone's time trawling through memoirs of self-importance and "chest beating".

Google found this "ancient thread" for me which is how I got to this point whilst searching for info on how to create a method to compare the OrderOpenTime with the number of bars as I don't want whiplashed orders (i.e. minimum timeframe before a position close is possible with SLs in place for reversal that sit outside of the minimum timeframe).

Maybe you should instead correct the code that doesn't work and post the solution instead of telling the person trying to at least offer a solution that their solution fails.