How to compare OrderOpenTime() and present time?

 

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++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol()==Symbol())
{
if (OrderType() == OP_BUY) {

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

 
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++) {

OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
if ( OrderSymbol()==Symbol())
{
if (OrderType() == OP_BUY) {

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.

 

Thanks,

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


jean

 

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.

 
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 ?


Thread start date - 2009.03.08
 

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.

 
i found this post useful X years after....
 
mheathcote89 #: i found this post useful X years after....

You shouldn't.

  1. If you read the documentation, you would know datetimes are in seconds.
  2. omovaze #: where: 60 = mins, timeFrame = SignalTimeFrame and increment = number of bars on that timeFrame from OrderOpenTime().

    This assumes every bar every exists — they don't. What if there are no ticks during a specific candle period? 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)

    Use iBarShift.

Reason: