Restrict opening new position within 60min of previous close

 

Good day;

I'm trying to prevent my EA from opening a new position within 60 from having closed the previous position.


//declared global variable

datetime LastTradeCloseTime = 0;

//then under
void OnTick(){

//I've included it in my buy / sell conditions as follows:
if(!isPosOpen && FutureCloudGreen && PriceaboveCloud && PriceaboveTenkan && TenkanaboveKijun && 
      ChikouabovePrice && ChikouaboveCloud && WeekdaysCheck(TimeTradeServer()) && (TimeCurrent() - LastTradeCloseTime) > 60*60){

The EA is still opening positions immediately after closing the previous one.

Could you please point me in the right direction so that I can prevent those trades from happening?

 
Stephan Kelling:

Good day;

I'm trying to prevent my EA from opening a new position within 60 from having closed the previous position.


The EA is still opening positions immediately after closing the previous one.

Could you please point me in the right direction so that I can prevent those trades from happening?

Use the debugger, put a break point in before this IF statement and check the values.... especially that of LastTradeCloseTime

Better to use 3600 rather than 60*60 less work,  better still not to use hard coded values.

 
Thank you your input. I have followed  your recommendations and made changes as follows in the code...
//global variable added...

    input int SecondsDelay = 3600;
   datetime LastTradeCloseTime = DEAL_ENTRY_OUT;
  
//trade condition  adjusted...

//Buy Condition
   if(!isPosOpen && FutureCloudGreen && PriceaboveCloud && PriceaboveTenkan && TenkanaboveKijun && 
      ChikouabovePrice && ChikouaboveCloud && WeekdaysCheck(TimeTradeServer())){
      if(Magic && (TimeCurrent() - LastTradeCloseTime) > SecondsDelay){...etc

My EA is still opening positions (on occasion) as soon as the previous position is closed AND without complying to the conditions set?? It is weird, it runs smoothly but then on some occasions, it just goes ahead and opens 4 positions (not according to the conditions) immediately after the trailing SL closes a position (and this does not happen at  every instance when a position is closed either) and these random positions are immediately stopped out at the same SL price. 

Any advice is appreciated, with thanks.

 

My EA is still opening positions (on occasion) as soon as the previous position is closed AND without complying to the conditions set?? It is weird, it runs smoothly but then on some occasions, it just goes ahead and opens 4 positions (not according to the conditions) immediately after the trailing SL closes a position (and this does not happen at  every instance when a position is closed either) and these random positions are immediately stopped out at the same SL price. 

Any advice is appreciated, with thanks.

 
LastTradeCloseTime variable has to be updated to the time of the last deal, it can't just be left as global variable, it doesn't know when or how to update
 

Hi

I don’t know where you update this LastTradeClosTime variable but apparently, it’s not updated properly.  Or you can have another function which causes problems somewhere else in the code. You can try to not be using global variables here to avoid those problems – but let EA always find last closed trade and if it’s found within last 60 minutes blocked new trade, something like that:

bool isclosed60 = false;
      HistorySelect(TimeCurrent()-3600, TimeCurrent()); //find only trades within last 60 min
      for (int i = HistoryDealsTotal()-1; i >= 0; i--)
      {
         ulong ticket=HistoryDealGetTicket(i);
         if(HistoryDealGetString(ticket, DEAL_SYMBOL)==Symbol()){
            if(HistoryDealGetInteger(ticket, DEAL_MAGIC)==MagicNumber){
               if(HistoryDealGetInteger(ticket,DEAL_ENTRY)==DEAL_ENTRY_OUT){
                  isclosed60=true;
                  break;
                   }
                   }
         }   
      }

Best Regards