Download MetaTrader 5
To add comments, please log in or register
Do you have questions to the application's developer - ask them in Comments section
Dominic Gilbert
1632
Dominic Gilbert 2014.09.24 12:49 
//+------------------------------------------------------------------+
//| Check History to see if trade hit stop                           |
//+------------------------------------------------------------------+
bool CheckHistory()
{
 for(int b=OrdersHistoryTotal()-1; b>=0; b--)
   {

    if( !OrderSelect(b,SELECT_BY_POS,MODE_HISTORY))continue;
     {
      if( OrderMagicNumber() == MagicNumber1  )
        if(OrderSymbol() == Symbol())
        {
        RefreshRates();
         
        if( OrderType() == OP_BUY && OrderStopLoss() >= OrderClosePrice())
            {
            //H1_Buy_Touch = "None";
            HistoryBarTime = OrderCloseTime();
            return(true);
            } 

        if( OrderType() == OP_SELL &&  OrderClosePrice() >= OrderStopLoss())
            {
            //H1_Sell_Touch = "None";
            HistoryBarTime = OrderCloseTime();
            return(true);
            } 
        }
    } 
  }

HistoryBarTime = 0; 
return(false); 
}
Would someone please be kind enough to tell me if I am doing this correctly? It works in ST. Just want to make sure that I am actually ONLY look at the last trade on the pair and seeing if it hit the stop. If the LAST trade on the pair did not hit the stop it should return false with the HistoryBarTime being equal to "0"...

Don't want this conflicting with other pairs and also looking at wrong trade...
Taskin Osman
803
Taskin Osman 2014.09.24 13:50  

Few hints;

 

  • you dont need to RefreshRates too often, better refresh it once right before first "for". 
  • You should not trust OrdersHistoryTotal()-1 to be last trade. I dont think its reliable fact, i would first create a for loop which finds latest trade. All depends what is for you latest trade. It can be OrderID, EntryTime, ExitTime, you have to judge it yourself what is for you "last". Then i would compare just this ticket.

bool CheckHistory()
{
 RefreshRates();
 int lastTrade=-1;
 // Find latest Trade, this assmes, last OrderID is last ticket 
 for(int i=0;i<OrdersHistoryTotal();i++)
      if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) 
          if (OrderTicket()>lastTrade) lastTrade=OrderTicket();      
 
 // Now lets load this last Trade and decide if it hit SL
 if (lastTrade>-1 && OrderSelect(lastTrade,SELECT_BY_TICKET,MODE_HISTORY))
   {
      if( OrderMagicNumber() == MagicNumber1  )
        if(OrderSymbol() == Symbol())
        {
                 
        if( OrderType() == OP_BUY && OrderClosePrice() <= OrderStopLoss()  )
            {
            //H1_Buy_Touch = "None";
            HistoryBarTime = OrderCloseTime();
            return(true);
            } 

        if( OrderType() == OP_SELL &&  OrderClosePrice() >= OrderStopLoss())
            {
            //H1_Sell_Touch = "None";
            HistoryBarTime = OrderCloseTime();
            return(true);
            } 
        }
   } 
HistoryBarTime = 0; 
return(false); 
}
Keith Watford
Moderator
8016
Keith Watford 2014.09.24 13:52  

You don't do any checks to see if it was actually the last trade.

What is the last trade? Last by open time, last by close time?

If your code does find the last trade and it hit TP, it will be ignored and your loop will continue until it finds a trade that matches the criteria .

If a single trade in History has the same magic number and Symbol and also OP_BUY && OrderStopLoss() >= OrderClosePrice() or OP_SELL &&  OrderClosePrice() >= OrderStopLoss() is true 

 the function will return true

whroeder1
13656
whroeder1 2014.09.24 14:33  
DomGilberto: Would someone please be kind enough to tell me if I am doing this correctly?
if( OrderType() == OP_BUY && OrderStopLoss() >= OrderClosePrice())
When a stop is triggered, it becomes a market order. There is no guarantee your condition will true. I'd use
double OCP = OrderClosePrice();
bool isCloseBySL = MathAbs( OCP - OrderStopLoss() ) < MathAbs( OCP - OrderTakeProfit() );
Dominic Gilbert
1632
Dominic Gilbert 2014.09.24 16:48  

Thanks for your inputs everyone. Just to be clear, what I mean by "Last Trade" is, I want to check the LAST closed trade on Symbol == OrderSymbol()  &&  OrderMagicNumber() == MagicNumber and find it if it hit it's stop. If it has not hit it's stop, then the Bool CheckHistory

returns(false);

When it gets called again, then check if it hit stop, if so return(true). I am only interested in the very last trade on this pair and it's outcome.

 

I'll have a play with the feedback you guys have given, thanks! 

Dominic Gilbert
1632
Dominic Gilbert 2014.09.25 15:02  
//+------------------------------------------------------------------+
//| Check History to see if trade hit stop                           |
//+------------------------------------------------------------------+
bool CheckHistory()
{
 RefreshRates();
 
 bool isCloseBySL;
 int lastTrade=-1;
 
 for(int i=0;i<OrdersHistoryTotal();i++) // < --- Is this bit definitely right? 
  if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) 
   if (OrderTicket()>lastTrade) lastTrade=OrderTicket();      
 
 
 if (lastTrade>-1 && OrderSelect(lastTrade,SELECT_BY_TICKET,MODE_HISTORY))
   {
    if( OrderMagicNumber() == MagicNumber1  )
      if(OrderSymbol() == Symbol())
      {
                 

      if( OrderType() == OP_BUY )
        {
         double OCP = OrderClosePrice();
             
         isCloseBySL = MathAbs( OCP - OrderStopLoss() ) < MathAbs( OCP - OrderTakeProfit() );
         if( OrderStopLoss() >= OrderClosePrice() && isCloseBySL )
           {
            HistoryBarTime = OrderCloseTime();
            return(true);
           }
         } 

       if( OrderType() == OP_SELL )
         {
          double OCP = OrderClosePrice();
            
          isCloseBySL = MathAbs( OrderStopLoss() - OCP ) < MathAbs( OrderTakeProfit() - OCP );
          if( OrderClosePrice() >= OrderStopLoss() && isCloseBySL)
            {
             HistoryBarTime = OrderCloseTime();
             return(true);
            }
         } 
      }
   } 
HistoryBarTime = 0; 
return(false); 
}
Thanks for your help everyone. I have a question regarding the above though; do I increment or decrement when select historical trades? (highlighted line above?)
Dominic Gilbert
1632
Dominic Gilbert 2014.09.25 17:10  

I completely forgot to ask - as this function is under "OnTick()" is there a crafty way so that rather than check this function on EVERY SINGLE TICK (which is a waste of resources), I want to run this function once (but I don't know how to determine "once" I cannot use OrderOpenThis == 0"), check the last trade, "did it hit stop?", if yes or no, then you do not need to run it again until next trade is OPENED.

I am finding this part to be slowing down my back-testing a little bit. (sorry if thats vague). 

whroeder1
13656
whroeder1 2014.09.25 18:26  
DomGilberto: is there a crafty way so that rather than check this function on EVERY SINGLE TICK (which is a waste of resources), I want to run this function once
OnTick(){
   static int OOC=0;
   int OOCprev = OOC; OOC = OrdersTotal();
   if(OOC < OOCprev){
      CheckHistory(); // An order closed
Dominic Gilbert
1632
Dominic Gilbert 2014.09.25 18:53  
Spot on thanks WHRoeder. 
Keith Watford
Moderator
8016
Keith Watford 2014.09.26 10:03  
WHRoeder:
DomGilberto: is there a crafty way so that rather than check this function on EVERY SINGLE TICK (which is a waste of resources), I want to run this function once
OnTick(){
   static int OOC=0;
   int OOCprev = OOC; OOC = OrdersTotal();
   if(OOC < OOCprev){
      CheckHistory(); // An order closed

What if one or more orders were opened and less or the same amount of orders closed in between executions of this code?

Would it be better to see if OrdersHistoryTotal had increased? 

whroeder1
13656
whroeder1 2014.09.26 13:20  
GumRai: Would it be better to see if OrdersHistoryTotal had increased?
  1. That would be better, I wasn't thinking of other charts. (One up me)
  2. Best would be checking if OrdersHistoryTotal has changed. If history isn't set to unlimited, it can also decrease. (One up you :)
/
To add comments, please log in or register