Trade X Hours after last trade - page 2

 
Gui205: @WHRoeder i tryed print, but he print nothing :/.
Then your code isn't executing. Add more elsewhere and track it down.
 
where did you place the code? Are you familiar with the concept of functions? Metatrader doesn't allow nesting of functions. They can only "exist" on the global scope. Your EA should look like this.
int start(){

   //manage open positions here

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

   // open your trade here

   return(0);
}

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);
}
 

It's work but now he trade on alls candle :/

 
8-) yes. So, that is not what you wanted, I guess. All you need to do is adjust this condition
liLastClose+_iHour*3600 > TimeCurrent()
 

I think the problem  is my code, I had to change him because my manage order was under my order send .... So i had to keep him before 

it's possible to send you my code by PM ? Just if I have not made a mistake?

 
I am not sure what you actually want to achieve. the AllowOpen function finds the last order close time. This time is the number of seconds from 1 jan 1970. With that in mind you can calculate when the next order should be allowed to open. Hence the comparison with TimeCurrent() ( the last known servertime ) So exactly 3 hours after the last close this function will return true and thus allows a new trade to be opened. If you want to open on the bar open you should compare to Time[0] or only call the function when a new bar starts.
 

I send you my code by PM for you see what i mean 

 
if(TradeAllowed==0){
 total=OrdersTotal();
  if(total<MaxOrders)
  {
if (!AllowOpen(3)) return(0);
// Open buy
if(OpenBuy==1){
if (LastBarTraded != Time[0]){ 

ok a few pointers on coding:

- it is a good practice to make functions from the different parts of the code, this way you have a better overview of the code, and function call is easily swapped with and another one.

- always use proper indenting, this helps you keep track of all the curly brackets and makes the code better readable.

- don't nest when it is not really needed ( take a look at the if function in the for-loop I gave you. it simply calls continue (next iterator) so the rest of the code in the for-loop isn't nested)

- don't abuse the if  ... else, in most cases you can set the default value and overwrite it on if condition (take a look at the else in your init() function , it is not needed )

- don't use variables on the global scope when they are only used in 1 function. If a value need to be kept in memory declare them static

- it is best to use curly brackets when your statement uses multiple lines, and leave them out when the whole statement is evaluated on a single line

if((OrderMagicNumber()==MagicNumber1)&&(OrderTakeProfit()==0)&&(OrderSymbol()==Symbol())){ OrderModify(OrderTicket(),0,OrderStopLoss(),tpb,0,CLR_NONE); }


now the problem

- you are using

total=OrdersTotal();

this won't work if you have multiple ea's running or maybe even multiple magicnumbers in one EA. so best make a counting function that always give you the correct data


if(OpenBuy==1){
   if (LastBarTraded != Time[0]){ 
      int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"Buy Order",MagicNumber1,0,Blue);
      LastBarTraded = Time[0];
   }
}

LastBarTraded is set eventhough the OrderSend command could fail. You should check if your command was successful. Better check upfront if there is a new bar

 

example of a counting function.


int      Expert.iOrderCount[6];
int      Expert.iLastTicket[6];
//other vars used in count

int start(){
   
   Expert.Count();

  //handle positions
}

void Expert.Count(){

   ArrayInitialize(Expert.iOrderCount, 0);
   Expert.iOrderCountTT = 0;
   Expert.dtLastTime = 0;
   
   for (int i = 0; i < OrdersTotal(); i++){
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == false) continue;
      if (OrderMagicNumber() != Expert.iMagicNumber) continue;
      
      Expert.iOrderCount[OrderType()]++;
      Expert.iLastTicket[OrderType()] = OrderTicket();  
      Expert.iOrderCountTT++;   
      Expert.dtLastTime = MathMax(Expert.dtLastTime, OrderOpenTime());
   }
   
   for(i = OrdersHistoryTotal(); i >=0; i--){
      if (OrderSelect(i, SELECT_BY_POS, MODE_HISTORY) == false) continue;
      if (OrderMagicNumber() != Expert.iMagicNumber) continue;
      if (OrderType() > 1) continue;
      Expert.dLastOrder = OrderProfit();
      Expert.iLastType = OrderType();
      break;
   } 
}
 

Thank for your anwser and your advice, it's verry nice, because i m not a expert :/. 

 But now, how to forbid the ea to take a news order 3 hours after the last close ?  

in buy condition, &&( TimeCurrent() >= OrderCloseTime()+3*360){  ?  

Reason: