Putting a delay between closing and opening of trades

 
Hi
A question for MetaTrader Gurus. For a particular symbol, when a trade is closed, I would like to open the new trade with a 15 seconds interval. Any code snippets please?
I would like to express my gratitude in anticipation.
Thanks and best regards,
Long John Silver
 
LongJohnSilver:
Hi
A question for MetaTrader Gurus. For a particular symbol, when a trade is closed, I would like to open the new trade with a 15 seconds interval. Any code snippets please?
I would like to express my gratitude in anticipation.
Thanks and best regards,
Long John Silver
You can simply use Sleep(15000);  but this will not allow you to test in the Strategy Tester.  A better method would be to use a variable to save the time hen the order was closed and then on each tick check if 15 seconds have elapsed,  the issue with this is that if you have no tick for 30 seconds you will wait 30 seconds not 15,  but at least you would be able to perform a more valid test in the Strategy Tester.
 
RaptorUK:
You can simply use Sleep(15000);  but this will not allow you to test in the Strategy Tester.  A better method would be to use a variable to save the time hen the order was closed and then on each tick check if 15 seconds have elapsed,  the issue with this is that if you have no tick for 30 seconds you will wait 30 seconds not 15,  but at least you would be able to perform a more valid test in the Strategy Tester.


Thanks RaptorUK...I am grateful.
The problem with sleep is that I can use it after OrderClose which I use to exit at Take-Profit. However, I do not know how to use it if the trade exits at SL using Metatrader's intrinsic stop-loss exit..I prefer not to use OrderClose to exit at stop-loss because MetaTrader's stoploss feature is far quicker than OrderClose..I have seen that using OrderClose at SL could even cause me to lose 10 pips or more...Please tell me if I am not clear..
Best regards,
Silver
 
LongJohnSilver:

Thanks RaptorUK...I am grateful.
The problem with sleep is that I can use it after OrderClose which I use to exit at Take-Profit. However, I do not know how to use it if the trade exits at SL using Metatrader's intrinsic stop-loss exit..I prefer not to use OrderClose to exit at stop-loss because MetaTrader's stoploss feature is far quicker than OrderClose..I have seen that using OrderClose at SL could even cause me to lose 10 pips or more...Please tell me if I am not clear..
Best regards,
Silver

Post-Script: There seems to be a possibility by selecting the trade in history mode, however, that seems to be possible in 'select by position'. I am not clear how to imagine positions. Plus I am not sure whether a trade which just closed would be available to be selected from the history.
 
LongJohnSilver:

Post-Script: There seems to be a possibility by selecting the trade in history mode, however, that seems to be possible in 'select by position'. I am not clear how to imagine positions. Plus I am not sure whether a trade which just closed would be available to be selected from the history.

Yes you can do that,  use a for loop to loop through the closed orders to find the most recently closed order and check how long ago it was closed using OrderCloseTime()

The closed trades are in the MODE_HISTORY pool and run from positions  0   to   OrdersHistoryTotal() - 1 

 
RaptorUK:

Yes you can do that,  use a for loop to loop through the closed orders to find the most recently closed order and check how long ago it was closed using OrderCloseTime()

The closed trades are in the MODE_HISTORY pool and run from positions  0   to   OrdersHistoryTotal() - 1 


Thanks RaptorUK. That is a big help. I was looking for this OrderHistoryTotal...
One precision requested: I thought that the positions should always be traversed from top to bottom i.e. OrdersTotal()-1 to 0. In the case of history, we have to traverse from 0 to OrdersHostoryToal - 1?
Thanks again for your expertise...
Silver
 
LongJohnSilver:

Thanks RaptorUK. That is a big help. I was looking for this OrderHistoryTotal...
One precision requested: I thought that the positions should always be traversed from top to bottom i.e. OrdersTotal()-1 to 0. In the case of history, we have to traverse from 0 to OrdersHostoryToal - 1?

You can go either way,  counting up or counting down.  

It matters if you are counting through the open orders and are deleting/closing orders as the numbers of orders in the pool will change when you delete/close one.

 
Thanks RaptorUK..So nice of you...
 

Every time  OrdersHistoryTotal() is changed up you take 

static datetime timelastclosedtrade;

if (OrdersHistoryTotal() != numberofclosedtrades)
   {
    timelastclosedtrade = TimeCurrent();
    numberofclosedtrades = OrdersHistoryTotal();
   }
    

 

 You only have to check the last trade in History if 

timelastclosedtrade + 15 > TimeCurrent()

 when the EA does get to try open a new trade

 
deVries:

Every time  OrdersHistoryTotal() is changed up you take 

 

 You only have to check the last trade in History if 

 when the EA does get to try open a new trade

 Or if the last closed trade is manually placed or belongs to a different EA or maybe a different symbol/timeframe.  ;-)

 
RaptorUK:

 Or if the last closed trade is manually placed or belongs to a different EA or maybe a different symbol/timeframe.  ;-)



That's what I wrote also but you only have to do this if other conditions are true for the EA to open a new trade

if you have a huge ordershistoryfile to check everytime a trade has closed or only when it is needed makes a big difference 

So if ..... 

timelastclosedtrade + 15 < TimeCurrent()

 then there is no need for checking last closed trade