Not allowing trade

 

Hi,

I need my EA stops to open trades if the last trade closed time is not > 10 min

I use 1 min chart graph.

So for example:

If the last long trade has been closed @ 10:30 so EA must not open long order before at least 10:40... but it could open short trade.


Please help

Cheers

 

Use OrderCloseTime() to find the time last trade closed, then don't allow another trade until OrderCloseTime() +600

600 being the number of seconds in 10 minutes

 
Thank you... what is the code to call the mode history?
 
FrenchyTrader:
Thank you... what is the code to call the mode history?
OrderSelect() from the history pool ( MODE_HISTORY ), find the most recently closed order, then check it's close time.
 

Hi,


This code doesn't print while I have closed 2 short trades with the EA???


//______________________________________


       // Get the last order handled by the EA the last time it was used. The value
       // defaults to zero if the global variable does not yet exist.   

       string strGlobalVariableName = WindowExpertName() + "_" + Symbol() + "_LastOrderCount";
       int LastHistoryCount = GlobalVariableGet(strGlobalVariableName);

       // Scan the order list. Doesn't matter whether this is done from zero upwards,
       // or from OrdersHistoryTotal() downwards. 
       
       for(int e = LastHistoryCount ; e < OrdersHistoryTotal(); e++)
        {
          OrderSelect(e, SELECT_BY_POS, MODE_HISTORY);
         
               if ( OrderSymbol() == Symbol() &&  OrderType () == OP_SELL && OrderMagicNumber() == magicalNumber) {
                      
                   datetime Sell_OrderClosePrice = OrderCloseTime(); Print ( "Sell_OrderClosePrice ", OrderCloseTime() );
                   //   Alert("OrderPrice");
                      
                        }
               }
       // Store the last-order count in a global variable which persists across multiple uses of the EA
       GlobalVariableSet(strGlobalVariableName, OrdersHistoryTotal());
 
FrenchyTrader:

Hi,


This code doesn't print while I have closed 2 short trades with the EA???


You aren't finding the most recently closed Order ? why aren't you checking all the closed Orders by starting your loop at 0 ? Sell_OrderClosePrice = OrderCloseTime() ?? Price == Time ?
 

Nothing is working:

Why is it so complicated to get the last close time trade???


datetime Sell_OrderCloseTime;
datetime BUY_OrderCloseTime;

for(int ii=OrdersHistoryTotal()-1;ii>=0;ii--)
 {
   //OrderSelect(ii, SELECT_BY_POS,MODE_HISTORY);
   
   OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==magicalNumber )
    {
       
       if(OrderType()==OP_BUY) {BUY_OrderCloseTime=OrderCloseTime(); } Print ( "BUY_OrderCloseTime ", OrderCloseTime() );
       if(OrderType()==OP_SELL) {Sell_OrderCloseTime=OrderCloseTime(); } Print ( "Sell_OrderCloseTime ", OrderCloseTime() );
      
      // break; 
    }
 } 
 
FrenchyTrader:

Nothing is working:

Why is it so complicated to get the last close time trade???



It's not.

Why are you using a loop, but checking the same trade every time?

You are using diffent values for ii, but selecting OrdersHistoryTotal()-1

 

That's because I'm noob.

Why Print is not working?

 
FrenchyTrader:

That's because I'm noob.

Why Print is not working?

Change

 OrderSelect(OrdersHistoryTotal()-1, SELECT_BY_POS,MODE_HISTORY);

to

 OrderSelect(ii, SELECT_BY_POS,MODE_HISTORY);

This should help towards writing your code to find the latest closed trade

datetime lastclosedtime =0;
int lastclosedticket;

for(int ii=OrdersHistoryTotal()-1;ii>=0;ii--)
   {
   OrderSelect(ii, SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol() && OrderMagicNumber()==magicalNumber )
      {
      if(OrderCloseTime() > lastclosedtime)
        {
        lastclosedtime = OrderCloseTime();
        lastclosedticket = OrderTicket();
        }
      }
   }
   
   OrderSelect(lastclosedticket,SELECT_BY_TICKET);
      if(OrderType()==OP_BUY)  Print ( "BUY_OrderCloseTime ", lastclosedtime );
      if(OrderType()==OP_SELL) Print ( "Sell_OrderCloseTime ", lastclosedtime );
       
 

Thank you so much.

I have tested and closed some orders so for "Lastclosedtime" I get in memory the last buy or sell order closed time.

So if I check

Currenttime() - Lastclosedtime > 600

to allow long trade and the last order is short... it won't let me enter long before 10 min while maybe last long order was closed 3 hours ago?!


So do I need to create 2 variables:

  datetime Long_Lastclosedtime = 0;

  datetime Short_Lastclosedtime = 0;
Reason: