why don't the orders close in MT4 Build 646?

 

They say the best way to learn is by making mistakes - so I must be learning a lot right now.

 

I know the coding may not be neat but it is a start for me.  I am trying to learn two things.  

 

1) How to only place one order per bar.

2) How to close an order after a set number of bars have passed. 

 

My problem is that no order of any kind get closed. 

 

I am running IC Markets  MT version 4 build 646.

 

(On a back test environment version 4 build 451 the orders do close - which is odd)

 

Any thoughts on why the orders now won't close? 

  

 

 

//+------------------------------------------------------------------+
// needs work
//+------------------------------------------------------------------+
extern int MagicNumber=15;
extern int Slippage=3;
extern double StopLoss=2000;
int ThisBarTrade = 0; 
 


//+------------------------------------------------------------------+
//    Open Expert Start Function
//+------------------------------------------------------------------+
int start()
{
double MyPoint=Point;
double TheStopLoss=0;

                         
        //+------------------------------------------------------------------+
        //    Order Open Section
        //+------------------------------------------------------------------+ 

                
                                 
        { 
        if (Bars != ThisBarTrade &&   DayOfWeek()==5)   // If the ThisBarTrade variable does not equal Bars, we can continue
          {
          int result=0;
                {
                 if(iRSI(NULL,PERIOD_H4,2,PRICE_CLOSE,0) > 80)
                                {
                                ThisBarTrade = Bars ;
                                result=OrderSend(Symbol(),OP_BUY,0.3,Ask,Slippage,0,0,"RSI(2) 1 HOUR FRIDAY",MagicNumber,0,Blue);
                                }
                }
                {
          	if(iRSI(NULL,PERIOD_H4,2,PRICE_CLOSE,0) < 20)
                		{
                        	ThisBarTrade = Bars ;
                                result=OrderSend(Symbol(),OP_SELL,0.3,Bid,Slippage,0,0,"RSI(2) 1 HOUR FRIDAY",MagicNumber,0,Red);
                                }
                }
           }


        //+------------------------------------------------------------------+
        //    Close Order Open Section
        //+------------------------------------------------------------------+

        }




        


        
                

        //+------------------------------------------------------------------+
        //    Order_BUY_Close Section
        //+------------------------------------------------------------------+
                
   for(int iPosBUY=OrdersTotal() -1; iPosBUY >=0; iPosBUY--) if(
   OrderSelect(iPosBUY,SELECT_BY_POS)
   && OrderType()==OP_BUY
   && OrderSymbol()  == Symbol()
   && OrderMagicNumber() == 15
   ){
   int BUYduration = iBarShift(0,NULL, OrderOpenTime());
   
   if (BUYduration < 8) continue;
         else
                                {
                                OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                                }       
                        



        //+------------------------------------------------------------------+
        //    Close Order_BUY_Close Section
        //+------------------------------------------------------------------+

        }
        
        //+------------------------------------------------------------------+
        //    Order_SELL_Close Section
        //+------------------------------------------------------------------+
                
   for(int iPosSELL=OrdersTotal() -1; iPosSELL >=0; iPosSELL--) if(
   OrderSelect(iPosSELL,SELECT_BY_POS)
   && OrderType()==OP_SELL
   && OrderSymbol()  == Symbol()
   && OrderMagicNumber() == 15
   ){
   int SELLduration = iBarShift(0,NULL, OrderOpenTime());
   
   if (SELLduration < 8) continue;
         else
                                {
                                OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,Red);
                                }
                                



        //+------------------------------------------------------------------+
        //    Close Order_SELL_Close Section
        //+------------------------------------------------------------------+

        }



//+------------------------------------------------------------------+
//    Close Expert Start Function
//+------------------------------------------------------------------+

return(0);
}


 
int BUYduration = iBarShift(0,NULL, OrderOpenTime());

 iBarShift returns number of bars for the specified period. If you write NULL, it will use current chart period. If you are using for example H1, you will get number of one hour bars that elapsed since order was opened. If you want to use  iBarShift and count minute bars, put PERIOD_M1 as second parameter:

int BUYduration = iBarShift(0,PERIOD_M1, OrderOpenTime());

 

More things to check: 

  • you have input variable MagicNumber, but in if conditions you use constant "15". If you change magic number from input parameters form, your code will not work any more
  • compiler warns that you are not checking output of OrderClose(). It is a good practice to check if there were errors while executing order handling functions.

 
drazen64:

 iBarShift returns number of bars for the specified period. If you write NULL, it will use current chart period. If you are using for example H1, you will get number of one hour bars that elapsed since order was opened. If you want to use  iBarShift and count minute bars, put PERIOD_M1 as second parameter:

 

More things to check: 

  • you have input variable MagicNumber, but in if conditions you use constant "15". If you change magic number from input parameters form, your code will not work any more
  • compiler warns that you are not checking output of OrderClose(). It is a good practice to check if there were errors while executing order handling functions.

 

 

 

 

 

 

 

Thanks for the response.  I added the Period_H4 but it still does not close.  It must be a difference between builds 451 and 646 as it works fine in build 451 on backtest.
 

I just checked iBarShift documantation, it says that first argument should be NULL if you want current symbol. You have 0. It should look like this: 

int BUYduration = iBarShift(NULL,PERIOD_M1, OrderOpenTime());

 

 

If above does not help, here are some additional steps to find an error:

  • Check is MagicNumber changed or is it 15 on order?
  • 8 bars on H4 is 32 hours. How old are your orders?
  • Print iBarShift result.
  • Check result of OrderClose()
  • Check result of OrderSelect()
  • Check logs to see if there is any error information

 
drazen64:

I just checked iBarShift documantation, it says that first argument should be NULL if you want current symbol. You have 0. It should look like this: 

 

 

If above does not help, here are some additional steps to find an error:

  • Check is MagicNumber changed or is it 15 on order?
  • 8 bars on H4 is 32 hours. How old are your orders?
  • Print iBarShift result.
  • Check result of OrderClose()
  • Check result of OrderSelect()
  • Check logs to see if there is any error information


Thanks again for taking the time.  It is the weekend so no markets to test but I ran it in strategy tester on the IC Markets build and it works fine.  Printed out iBarShift and can see it counting up and then closing the orders....  Very odd.  So it works in strategy tester but doesn't work on their live market data.

Any thoughts?  I guess I can also ask their help desk?

 
n4btb: Any thoughts?  I guess I can also ask their help desk? 
  1. If you check your return codes, the message (or its absense) tells you where the problem is.
  2. The service desk isn't going to help you debug your code anymore than we can.
  3. if (Bars != ThisBarTrade && ...
    
    Search "new bar" Bars is unreliable (max bars on chart), volume is unreliable (can miss ticks) Always use Time[0].
 

This short script will print magic numbers of you open orders:

int start()
  {
    for(int i=0; i<OrdersTotal(); i++) 
    {
      if((OrderSelect(i,SELECT_BY_POS)==true))
      {
         Print( "Order : ", OrderTicket(), ", ",  OrderSymbol(), ", MagicNum= ", OrderMagicNumber());
      }
    }
   return(0);
  }

 

 Run this and check magic numbers.

 
drazen64:

I just checked iBarShift documantation, it says that first argument should be NULL if you want current symbol. You have 0. It should look like this: 

 

 

If above does not help, here are some additional steps to find an error:

  • Check is MagicNumber changed or is it 15 on order?
  • 8 bars on H4 is 32 hours. How old are your orders?
  • Print iBarShift result.
  • Check result of OrderClose()
  • Check result of OrderSelect()
  • Check logs to see if there is any error information


Drazen64.....  you were right!  Thank you.  In the live environment iBraShift needed NULL and PeriodH4.  That was a great help.  No idea why it worked in test but not on live data but that I guess is why we run things on demo accounts as well as back testing!  You have saved me a lot of time and it is a good lesson to learn.  I guess it comes down to FTFM as Raptor would probably say.
Reason: