digitalpips

Metatrader Finding the last trade bar

There are times when your strategy would like to see if a trade was taken within some period. This could be in order to limit your trades, or because you signal generation might not be as fine tuned, and you only want to take the initial trade, or to split up your trades into smaller traders every few minutes.

Metatrader provides a method to search the orders that have already closed.

OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)

We can then use iBarShift to figure out what bar index the order was closed or opened.

Putting this together we can create a simple loop that will check all our trades for our current expert and symbol, and test.

Here, any trades that have been closed less then 3 bars earlier, wills top the trading of the expert adviser.

 for(i=OrdersHistoryTotal()-1;i>=0;i--)
       {
        if(  OrderSelect(i, SELECT_BY_POS,MODE_HISTORY) ){  
         if(OrderSymbol()==Symbol() && OrderMagicNumber()==g_expertId)
          {
          
            if( iBarShift( Symbol(),NULL,OrderCloseTime() ) < 3 ){
             
               g_activateTrades = false;    
               break;

            }
            
          }
         }
       }

Leave a Reply