Trade X Hours after last trade

 

hi !

 I try to search a solution for allowed or not allowed a trade ( Buy or sell ) based on the last order time closed, look like that : OrderCloseTime() + Hours for allow the news trade

Help me please :(. ( Sorry for my poor english :s ) 

I try that but it's not work :

extern int Hours=3;
//--------------------------------------
static  datetime    TimeAllowed;
int newCount = HistoryTotal();  static  int prevCount;
if (newCount != prevCount){                 prevCount = newCount;
    datetime LastClose;
    datetime LastOpen;
    for(int pos=newCount-1; pos >= 0; pos--) if (
        OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders 
    &&  OrderOpenTime()     > LastOpen
    &&  OrderCloseTime()    > LastClose                 
    &&  OrderMagicNumber()  == MagicNumber             
    &&  OrderSymbol()       == Symbol()                 // and my pair.
    &&  OrderType()         == OrderType()){    // ??
        LastClose = OrderCloseTime();
    }
    datetime LastCloseHours = LastClose + Hours * 3600;
    TimeAllowed = LastCloseHours;
//-----------------------------------------------
my ea code
//-----------------------------------------------
if (TimeCurrent() >= TimeAllowed )
{
//order send buy
//order send sell 
}
 
Gui205:

hi !

 I try to search a solution for allowed or not allowed a trade ( Buy or sell ) based on the last order time closed, look like that : OrderCloseTime() + Hours for allow the news trade

Help me please :(. ( Sorry for my poor english :s ) 

I try that but it's not work :

So why are you checking the OrderOpenTime() ? you didn't mention anything about that ?  and what are you doing with OrderType() ?  it will always == OrderType()  !
 

Yes i now it useless, but i tryed everything ... Without success :/ 

 I have strictly no idea who allowed a trade 3 hours after the last order close,  and not before. 

 If you can help me, your welcome :).  

 
Gui205: I have strictly no idea who allowed a trade 3 hours after the last order close,  and not before.
"Who allowed?" Your code did. Drop the orderType and openTime. Add Print statement before the test and find out why.
Print("Now="+TimeToStr(TimeCurrent(), TIME_DATE|TIME_MINUTES|TIME_SECONDS)
   +" ok="+TimeToStr(TimeAllowed, TIME_DATE|TIME_MINUTES|TIME_SECONDS)
   +" T"+(TimeCurrent() - TimeAllowed)
   );
if (TimeCurrent() >= TimeAllowed )
 
Gui205:

Yes i now it useless, but i tryed everything ... Without success :/ 

 I have strictly no idea who allowed a trade 3 hours after the last order close,  and not before. 

 If you can help me, your welcome :).  

You didn't try everything,  you didn't try the correct solution . . .  did you try reading the Documentation ?  did you try to understand the code you borrowed from somewhere else ?  why are you checking the OrderOpenTime()  ?  if you want help perhaps you could answer a few simple questions first ?
 

Yes i read the documentation, i check the OrderOpenTime() because on this forum a guy have a similar problem ( just one trade on 24h ) and he use if(TimeCurrent()> OrderOpenTime + 86000 && TimeCurrent() + OrderCloseTime()+2*3600) 

I try to wrote a similar code by taking his code.

if(TimeCurrent >= OrderCloseTime() + Hours*3600){

//Buy & Sell

}

I try that but not working ... Have you got a good documentation RaptorUK ? Or the correct solution ?

 

Thank for help guy  

 
Gui205: Or the correct solution ?
I already gave you the solution why didn't you try it?
 
bool AllowOpen(int _iHour){

   int liLastClose = 0;
   for (int i = 0; i < OrdersHistoryTotal(); i++){
      if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == false) continue;
      liLastClose = MathMax(liLastClose, OrderCloseTime());
   }

   if (liLastClose+_iHour*3600 > TimeCurrent()){
      return(false);
   }
   return(true);
}
untested, hth
 

@WHRoeder i tryed print, but he print nothing :/. 

 

Thank @Russell but MTEditor doesnt want compil your code, can you explain to me that you wanted to do please ? 

 

It compiles in my editor. You should be able to call the function like this.

int start(){

   //manage open positions here

   if (!AllowOpen(3)) return(0);

   // open your trade here

   return(0);
}
the function just finds the youngest closed record in your history. It could be written somewhat more efficiently, but first make it work, then make it faster.
 
bool AllowOpen(int _iHour)  <==== - function definition unexpected

'liLastClose' - variable not defined

'_iHour' - variable not defined

 

 


Reason: