0
votes

I would like to write out a boolean that returns false if the last order (close) has been performed less than 24 hours earlier.

I tried with this code, but it always returns false:

bool OnePerDay()
{
     if (  OrderSelect( 1, SELECT_BY_POS, MODE_HISTORY ) )
     {     

           if (  OrderOpenTime() <  24*60*60 ) return( true );
     }
     return( false );
}

My goal is to perform at least one trade per day (or other time interval), so it should open a position and close it, but not perform other orders in less than 24 hours after.

3
The code is syntactically legal and is just obeying your commands, so it has to return the false. Btw, do you have any reasons, why you have not +1 voted on any single answer that was provided to you in the past?user3666197
I know it is legal since compilation does not return errors. It does not have to return false at every order, but only when the order is performed less than 24 hours. I'm not shure that in this way he keep the correct order i.e. the last order performed by the testerperjliv
I voted +1 in most of the answer (more or less all of yours), but under 15 reputation this votes are not displayed. With this question (my 4th) I reached 15 reputation, and now i can +1 in all answers.perjliv

3 Answers

1
votes

OrderOpenTime() is a datetime variable, which means it is integer larger then zero and smaller then 2^31-1 representing number of seconds since 1 Jan 1970. E.g., it is 9:20:07 am UTC 14 Oct 2017, which is 1507972807 in datetime format. if you want to check that the trade is open today or earlier, you need to know start of today's date, e.g. iTime(_Symbol(),PERIOD_D1,0) or TimeCurrent()/(24*60*60)*(24*60*60) which is 1507939200. If you need to check that trade lasts less then 24 hours - either TimeCurrent()-OrderOpenTime() to compare with 24*60*60 or get expected time when it should return true and compare with that date. Additionally you need to think of cases when order is open on Friday and now it is Monday (so more then 24 hours but is it ok for your task?)

altogether:

const int DAY_SECONDS = 24*60*60;
bool isTradeOpenToday(int ticket){//true means trade lasts less then 24h
   datetime now = TimeCurrent();
   datetime now_minus_24h = now - DAY_SECONDS;
   if(OrderSelect(ticket,SELECT_BY_TICKET)){
      return now_minus_24h - OrderOpenTime() < 0;
   }
}
1
votes

How to scan the db.POOL?

#define aTimeLAST iTime( _Symbol, PERIOD_H1, 23 )  // 24 H1 Bars back at Market
     // aTimeLAST  Time() - 24*60*60               // 24 hours since this Bar

void OnTick(){

     if   WasNoTradeWithinLAST( aTimeLAST ) {
          ...
     }
}

bool WasNoTradeWithinLAST( const datetime aClosestAllowedTIME ) {

     for (  int anOrdinalNUM  = 0;
                anOrdinalNUM <  OrdersTotal();
                anOrdinalNUM++
                ) {
            if ( !OrderSelect( anOrdinalNUM; SELECT_BY_POS; MODE_HISTORY ){
                  PrintFormat( "OrderSelect( %d ) failed. Will continue.", anOrdinalNUM );
            }
            else {

                  if (  OrderCloseTime() >= aClosestAllowedTIME
                     || OrderOpenTime()  >= aClosestAllowedTIME
                        )
                        return( false ); // WELL, XTO WAS WITHIN LAST 24HRS
            }
     }
     return( true );  // NONE XTO HAS BEEN PERFORMED DURING LAST 24HRS
 }
0
votes

Found a way to write out the bool function, please read comment for the definitions.

The solution is in the correct enumeration of the closed order in the for cycle for the order select, so this boolean return true only when the last close order is performed in more than 24 hours before.

int orhito () { return OrdersHistoryTotal(); } // number of closed  orders
int orto () {   return OrdersTotal();        } // number of pending orders

//+------------------------------------------------------------------+
bool onePerTime() { /* boolean that should return true
                               if the last close order
                               was performed more than 24 hours before */

     if (  orhito() == 0 ) return ( true );
     else{
           for ( int i = orhito(); i >= 0 ; i-- ) { 
                 if (  OrderSelect( i, SELECT_BY_POS, MODE_HISTORY ) ) { 
                       if (  TimeCurrent() - OrderCloseTime()  < 24*60*60 )
                             return( false );
                 } 
           }
           return( true );
     }
}