Download MetaTrader 5
To add comments, please log in or register
You sell neither signals nor apps? No problem, everyone can make profit!
Mimi
499
Mimi 2015.07.10 07:22 

In this EA, It opens order on each hourly bar & closes with it. It closes in 60 mins.

if ( T11 ==0 && T1 == 0 && BuyTicket == 0 )      
   { 
      if ((OrderSelect(BuyTicket,SELECT_BY_TICKET)) == false)    
       {                 
           double OpenPrice = Ask;
           StopLoss = low - (SecondSL * UsePoint);
           TakeProfit = NormalizeDouble(( MathRound((MathAbs(OpenPrice - StopLoss))* CalcPoint1))*2,6);
           double BuyStopLoss = StopLoss;
           double BuyTakeProfit = OpenPrice + (TakeProfit * UsePoint);
 
// Open buy order
BuyTicket = OrderSend(Symbol(),OP_BUY,LotSize,OpenPrice,UseSlippage,BuyStopLoss,BuyTakeProfit,"Buy Order",MagicNumber,0,Green);
SellTicket = 0;

       
//-----close trade after 60 mins //----
      
     if(OrderSelect(BuyTicket, SELECT_BY_TICKET)==true)
     {  
       datetime order_open_time = OrderOpenTime();
       datetime k = order_open_time + 3600;
          
       if(TimeCurrent()>=k) 
       {  
            double CloseLots = LotSize;
            double ClosePrice = Bid;
            bool Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);        
         
        }
      }      
  }        
 }

In above code, it never close the trade after 60 mins, cause Timecurrent() is never getting above k or equal it.  How to solve this?

Any help will be appreciated.  Thank you

whroeder1
13635
whroeder1 2015.07.10 11:46  

Of course not. You just opened it, so they're just about equal.

On the next tick BuyTicket is not zero, and the first IF says to do nothing.

Mimi
499
Mimi 2015.07.11 01:32  
WHRoeder:

Of course not. You just opened it, so they're just about equal.

On the next tick BuyTicket is not zero, and the first IF says to do nothing.

Thank you for your reply. Then how can I close trade after 60 minutes. I tried doing it using following codes found from other thread. But that doesn't worked too.

  int maxDuration = 60 * 60; 
      if(OrderSelect(BuyTicket, SELECT_BY_TICKET)==true)
     {  
       datetime order_open_time = OrderOpenTime();
       int duration = (int)TimeCurrent() - (int)order_open_time;
             
       if(duration >= maxDuration) 
       {  
            double CloseLots = LotSize;
            double ClosePrice = Bid;
            bool Closed = OrderClose(BuyTicket,CloseLots,ClosePrice,UseSlippage,Red);        
         
       }
      }

  Lastly I don't understand about orderselect. If I want to open buy order or close buy order, then what should be the orderselect "BuyTicker or Sellticket" and what will be it "true" or "false" ?

if ( T11 ==0 && T1 == 0 && BuyTicket == 0 )      

 In above line if i changed it to SellTicket == 0 then will it be all right? 

Mimi
499
Mimi 2015.07.14 05:19  
int duration = (int)TimeCurrent() - (int)order_open_time;

This particular code shows data as 1436767200. That's why its always greater than 3600.

Please help. I need to solve the issue.  

whroeder1
13635
whroeder1 2015.07.14 16:40  
  1. Simplify
    int duration = int(TimeCurrent() - order_open_time);
  2. 1436767200 = Mon, 13 Jul 2015 06:00:00 GMT Therefor the order had not yet opened (OOT == 0.) Selecting by ticket means you must test for open and/or for close.
  3. EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a orderSelect loop to recovery, or persistent storage (GV/file) of ticket numbers required.
Mimi
499
Mimi 2015.07.15 03:26  
WHRoeder:
  1. Simplify
  2. 1436767200 = Mon, 13 Jul 2015 06:00:00 GMT Therefor the order had not yet opened (OOT == 0.) Selecting by ticket means you must test for open and/or for close.
  3. EA's must be coded to recover. If the power fails, OS crashes, terminal or chart is accidentally closed, on the next tick, any static/global ticket variables will have been lost. You will have an open order but don't know it, so the EA will never try to close it, trail SL, etc. How are you going to recover? Use a orderSelect loop to recovery, or persistent storage (GV/file) of ticket numbers required.

Thank you for the code. It worked...I understood my mistake.

//------WHRoeder CODE for Order Close -------
int maxDuration = 60 * 60; // 60 minutes
for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) 
if (OrderSelect(pos, SELECT_BY_POS)  &&  OrderMagicNumber() == MagicNumber &&  OrderSymbol() == Symbol() )
{               
    int duration = int(TimeCurrent() - OrderOpenTime());    
    if (duration >= maxDuration)
     bool closed = OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),UseSlippage,Blue);
}
/
To add comments, please log in or register