Help needed with my EA

 

Hello,

 Does anyone know how to close an order after "X" bars have passed. The catch is, I only need it to work on the Strategy Tester (because I need it for my grad paper and I don't have enough time to run it on live) and not on live data. In live data I found that I can use time as recommended by many using the following type of code: 

 

int Duration = 5 * 3600; // 5 hours

int start(){
   int i;
   for (i=OrdersTotal() - 1; i>=0; i--){
      OrderSelect(SELECT_BY_POS, MODE_TRADES);
      if (TimeCurrent() - OrderOpenTime() > Duration){
         if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrNONE))
                  Print("OrderClose error ",GetLastError());
               return;
          }
      }
   }

 

 

But on Strategy Tester, the function TimeCurrent() won't work because it has the server time value of the moment when I hit the Start button on the ST (At least that's what I think it does and I checked on the Log and the time won't change). I also tried using Time[0] but with no results (maybe I used it wrong)

 

Could anyone give me some advice?

 

Thanks and best regards, 

 
use iBarShift
 

Put this simple 1 line code into an EA and use ST visual mode. You will see that TimeCurrent() does work in ST

   Comment(TimeToStr(TimeCurrent(),TIME_DATE|TIME_MINUTES|TIME_SECONDS));
 
ArriVM: But on Strategy Tester, the function TimeCurrent() won't work because it has the server time value of the moment when I hit the Start button on the ST
It has the time of the first tick of the bar it is simulating.
 
qjol:
use iBarShift

I tried using iBarShift with the following code:

#define CurrentBar 0;

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

int Duration = CurrentBar - BarShift + 1;

 But the result of Duration I get in the Log is always 0. Maybe I'm using the function wrong (it is actually my first EA). Could you give me some advice?

 

GumRai:

Put this simple 1 line code into an EA and use ST visual mode. You will see that TimeCurrent() does work in ST

 

Indeed it does, thank you so much. 

 

WHRoeder:
ArriVM: But on Strategy Tester, the function TimeCurrent() won't work because it has the server time value of the moment when I hit the Start button on the ST
It has the time of the first tick of the bar it is simulating.

 I tried to understand your answer and printed in the Log the OOT and the TimeCurrent() but they have the same value. I believe it's because I'm printing them inside the wrong cycle, but it's only a wild guess.

 

Thank you all for your answers. 

 

I'll post the closing block code to see if you can help me.

for(cnt=OrdersTotal() - 1;cnt>=0;cnt--)
  {
     if(OrderSelect(cnt,SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
     {    
         //--- closing positions
         
         if(OrderType()==OP_BUY || OrderType()==OP_SELL)
           {
                               
           int BarShift = iBarShift(NULL,0,OrderOpenTime(),false) ;  
           
            if(BarShift>3) {
                         
               if(!OrderClose(OrderTicket(),OrderLots(),Bid,3,clrNONE))
                  Print("OrderClose error ",GetLastError());
               return;
               }
            }

            //Trailing Stop            
            //Buy Orders-------------------------------------
         if (OrderType()==OP_BUY)
            {
              SL = Low[1]-Ask*Point;
              if( !OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0) )
                  Alert("OrderModify failed: ", GetLastError());            
            }
         else{
            //Sell Orders---------------------------
            if (OrderType()==OP_SELL)  
               SL = High[1]+Bid*Point;
               if( !OrderModify(OrderTicket(),OrderOpenPrice(),SL,OrderTakeProfit(),0) )
                  Alert("OrderModify failed: ", GetLastError()); 
          
            }   
          }
       } 
    } 

 

The results I'm getting are the following:

 

Results 

 

As you can see, it does not close any order even when more than 3 hours have passed (on an H1 timeframe).

 

Thanks in advance, best regards. 

 
ArriVM: I tried using iBarShift with the following code:
int Duration = CurrentBar - BarShift + 1;

 But the result of Duration I get in the Log is always 0. Maybe I'm using the function wrong (it is actually my first EA). Could you give me some advice?

Bars in the past have increasing shifts. Your duration is going negative.
 
ArriVM:

As you can see, it does not close any order even when more than 3 hours have passed (on an H1 timeframe).

What do your print statements tell you?

Do you use MagicNumber in your OrderSend() ?

If the order type is OP_SELL, you are trying to close at Bid instead of Ask, use OrderClosePrice  

 
WHRoeder:
Bars in the past have increasing shifts. Your duration is going negative.


Okay, I tried reversing the order of the equation, even removing the CurrentBar variable and I still get 0 from the iBarShift function:

  for(cnt=OrdersTotal() - 1;cnt>=0;cnt--)
  {
     if(OrderSelect(cnt,SELECT_BY_POS) && OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
     {    
         //--- closing positions
         
         if(OrderType()==OP_BUY || OrderType()==OP_SELL)
           {
                               
           int BarShift = iBarShift(NULL,0,OrderOpenTime(),false) ;  
           Print("BarShift = ",BarShift);
           
            if(BarShift>3) {
                         
               if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),3,clrNONE))
                  Print("OrderClose error ",GetLastError());
               return;
               }
           }

         }   
       } 

 

Result 

 

 

GumRai:

What do your print statements tell you?

Do you use MagicNumber in your OrderSend() ?

If the order type is OP_SELL, you are trying to close at Bid instead of Ask, use OrderClosePrice  

Thanks for the OrderClosePrice(), when I compiled the closing code in one if statement for OP_BUY and OP_SELL I  missed that. Above are the print statements I get in one operation. See that the order is opened at 2014.03.17 18:00 and hits SL 8 hours later when it should have closed around 22:00.  I do use Magic Number on the OrderSend():

  

//--- check for long position (BUY) possibility
         if(NEVOStoch_Main<30.0 && NEVOStoch_Signal<30.0 && MathAbs(Diff)<2.0)
           {
           TP = Bid + TakeProfit*Point;
           
            ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,TP,"NEVOStochBUY",MagicNumber,0,Green);
            if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                  Print("BUY order opened : ",OrderOpenPrice(), ", open time: ", OrderOpenTime() );
              }
            else
               Print("Error opening BUY order : ",GetLastError());
            return;
           }
           
         //--- check for short position (SELL) possibility
         if(NEVOStoch_Main>70.0 && NEVOStoch_Signal>70.0 && MathAbs(Diff)<2.0)
           {
           TP = Ask - TakeProfit*Point;
           
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,TP,"NEVOStochSELL",MagicNumber,0,Red);
            if(ticket>0)
              {
               if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES))
                  Print("SELL order opened : ",OrderOpenPrice(), ", open time: ", OrderOpenTime() );
              }
            else
               Print("Error opening SELL order : ",GetLastError());
           } 

 

 Thank you both for your cooperation.

 

ArriVM

..................... See that the order is opened at 2014.03.17 18:00 and hits SL 8 hours later when it should have closed around 22:00.  


The order was opened at 18:00 and iBarshift was checked also at 18:00, so of course it will be 0.

There are no more prints for iBarShift after that which means that the block of code is only executed once, immediately after sending the order. 

This suggests that there is something in your code that prevents the block of code executed. Maybe you have something like

if(OrdersTotal==0)
  {
  OrderSend()
  
  //
  //
  //
  //
  Code for checking iBarshift
  }

 Which would mean that all the time an order is open, the code will not be executed

 
GumRai:


The order was opened at 18:00 and iBarshift was checked also at 18:00, so of course it will be 0.

There are no more prints for iBarShift after that which means that the block of code is only executed once, immediately after sending the order. 

This suggests that there is something in your code that prevents the block of code executed. Maybe you have something like

 Which would mean that all the time an order is open, the code will not be executed

 

 


I do have that kind of code but the iBarshift was not on that block, so I really don't know what's wrong. However, I switched the closing block and placed it ahead of the buy/sell conditions (which has the code you pointed out so it only makes one order at a time) and now there are orders closing and the graph changed completely:

 

Before: 

 

After:

 

 

Do you know what could have happened?. Thanks a lot for your support. 

Reason: