How to keep time between trades? Please Help Can't Find Solution Anywhere

 

Hello, I got this code from this forum and I've which should let my orders only be executed once its been 2 Hours since my last trade closed. However, If I run it then my EA does not make it's first trade at all. I've forced it to buy using the OnInit Function and closing it when the Ontick Function runs but it still doesn't seem to work. Is there a better way to approach this problem? How would I make it so that it only buys after an hour has passed since my last trade closed?

Any help will be appreciated.


//Global Variables
static datetime lastClose; 
datetime lastClosePrev = lastClose;


void OnTick(){

if(Time[0]<lastClose + 2*3600){
 //if(Criteria...){SendOrder...}
}

}

//Function
double CheckDateTime (){

for(int pos=0; pos < OrdersHistoryTotal(); pos++)
  
   if (OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY == true)){  
       if (OrderCloseTime()    > lastClosePrev){         
         if  (OrderMagicNumber()  == magic       
    &&  OrderSymbol()       == Symbol()               
    &&  OrderType()         <= OP_SELL){
        lastClose = OrderCloseTime();
    }}}}
 

I am not sure I understand your question but when I look at your code there are a lot of issues. Your "if" criteria is where you are supposed to call the function CheckDateTime(). This is so that the "CheckDateTime()" function is executed within the OnTick() function



//Global Variables
static datetime lastClose; 
datetime lastClosePrev = lastClose;


void OnTick(){

if(Time[0]<lastClose + 2*3600)
{
 //if(Criteria...){SendOrder...}
}
 CALL YOUR FUNCTION HERE 
}

//Function
double CheckDateTime (){

for(int pos=0; pos < OrdersHistoryTotal(); pos++)
  
   if (OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY == true)){  
       if (OrderCloseTime()    > lastClosePrev){         
         if  (OrderMagicNumber()  == magic       
    &&  OrderSymbol()       == Symbol()               
    &&  OrderType()         <= OP_SELL){
        lastClose = OrderCloseTime();
    }}}}
 
Tawanda Tinarwo:

I am not sure I understand your question but when I look at your code there are a lot of issues. Your "if" criteria is where you are supposed to call the function CheckDateTime(). This is so that the "CheckDateTime()" function is executed within the OnTick() function



Hello, I've figured it out partially, the problem I'm having is that my code buys really quickly after closing a position, my code is below.

//Global Variables
int TicketVLCTY;

void Ontick(){

if(Buy criteria...){
TicketVLCTY = OrderSend(...)
}

if(OrderSelect(TicketVLCTY,SELECT_BY_TICKET,MODE_TRADES)==TRUE){
     if (Time[0] > (OrderOpenTime()+10000000)){
       if(Sell Criteria...){
         OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),10,clrAliceBlue);
}

}

}

The problem is that if I use the same Time[0]>OrderCloseTime()+10000 For the buy order, then it never makes it's first trade because it has no closed orders previously.

 

You need to learn how to code properly , the code that you attach have a lot issues , which i don't want to talk about it at the moment . 

I am assume you wish your EA  open another trade after X close time previous trade ( in this case x close time are 2 hours ) . If that the case here example code i have written ;

void OnTick()
  {

   datetime allowedOpenTradeTime = orderCloseTime()+ (60*60*2) ; // this mean 2 hours

   if(TimeCurrent()>allowedOpenTradeTime ) // this code check if current order has or havent pass 2 hours , if true then the rest of this code will be executed
      if(/*condition execute open trade */)
        {
         /*execute order e.g buy,sell,sellStop etc...*/
        }
        
  }//end onTick


//+------------------------------------------------------------------+
//| return close time order
//+------------------------------------------------------------------+
datetime orderCloseTime()
  {
   datetime oct=0; //orderCloseTime
   for(int i=0 ; i<=OrdersHistoryTotal() ; i++)
      if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
        {
         oct=OrderCloseTime() // return the close time order
        }
   return oct;
  }
//+------------------------------------------------------------------+

my approach quite simple . 1st make a function returning order close  time trade  , 2nd make a simple variable (allowedOpenTradeTime) to store previous  time close order and add with X time ( which in your case 2 hours ) . 3rd check current time using Timecurrent() .If the current time have pass  allowedOpenTradeTime , so the ea will open another trade and vice versa. 


regards

Cosmas

 
  1. Rahul Shaji Parmeshwar: it never makes it's first trade because it has no closed orders previously.

    #1 CheckDateTime gets the last close datetime. Just have it set the variable to zero before the loop

  2. Cosmas Moses Boniface: You need to learn how to code properly
       for(int i=0 ; i<=OrdersHistoryTotal() ; i++)
          if(OrderSelect(i,SELECT_BY_POS,MODE_HISTORY))
            {
             oct=OrderCloseTime() // return the close time order
            }

    You need to learn how to code properly

    1. Do not assume history has only closed orders.
                OrderType() == 6, 7 in the history pool? - MQL4 programming forum 2017.11.30

    2. Do not assume history is ordered by date, it's not.
                Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum 2012.04.21
                Taking the last profit and storing it in a variable | MQL4 - MQL4 programming forum #3 2020.06.08

    3. Total Profit is OrderProfit() + OrderSwap() + OrderCommission(). Some brokers don't use the Commission/Swap fields. Instead, they add balance entries. (Maybe related to Government required accounting/tax laws.)
                "balance" orders in account history - Day Trading Techniques - MQL4 programming forum 2017.11.01

      Broker History
      FXCM
      Commission - <TICKET>
      Rollover - <TICKET>

      >R/O - 1,000 EUR/USD @0.52

      #<ticket>  N/A
      OANDA
      Balance update
      Financing (Swap: One entry for all open orders.)

 
Cosmas Moses Boniface #:

You need to learn how to code properly , the code that you attach have a lot issues , which i don't want to talk about it at the moment . 

I am assume you wish your EA  open another trade after X close time previous trade ( in this case x close time are 2 hours ) . If that the case here example code i have written ;

my approach quite simple . 1st make a function returning order close  time trade  , 2nd make a simple variable (allowedOpenTradeTime) to store previous  time close order and add with X time ( which in your case 2 hours ) . 3rd check current time using Timecurrent() .If the current time have pass  allowedOpenTradeTime , so the ea will open another trade and vice versa. 


regards

Cosmas

how to get orderclose time

 
hiloz #: how to get orderclose time

Did you bother to read the thread before posting? Did you look at the code at #4.2?

Perhaps you should read the manual.
   How To Ask Questions The Smart Way. (2004)
      How To Interpret Answers.
         RTFM and STFW: How To Tell You've Seriously Screwed Up.