Close order After 1H

 

Hi everyone,

i trey this cod for close open order after 1H but cant close after 1H.

can you help me and repair  for this cod.

tanks for all.

//+------------------------------------------------------------------+
//|                                                                  |
//|                             "Ghobar"                             |
//|                                                                  |
//+------------------------------------------------------------------+

extern int Magic1=1;
extern int Magic2=2;
extern double lot=0.1;
extern int slippage=6;
extern int stoploss=44440;
extern int takeprofit=55550;
extern bool EnableAlert=true;
extern double  timeclose=1;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
 
if(Volume[0]<=1)
{
 if(Orders()==0)
 {
  if(CandleStatus()=="buy")
  {
   Pendbuy();
   closeOrder();
   }
  if(CandleStatus()=="sell")
  {
   Pendsell();
   closeOrder();

     }
 
  }


 }
   return(0);
  }
//+------------------------------------------------------------------+
int Orders()
{
int num=0;

 for(int i=OrdersTotal()-1;i>=0;i--)
 {
 OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
 
 if(OrderMagicNumber()==Magic1 || OrderMagicNumber()==Magic2)
  
  num++;
  
  }
return(num);
}
//--------------------------------------------------------------------
string CandleStatus()
{
if(Close[1]>=Open[1])

return("buy");

else

if(Close[1]<Open[1])

return("sell");
}
//---------------------------------------------------------------------
void Pendbuy()
{
int ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,Ask-stoploss*MathPow(10,-Digits)
,Ask+takeprofit*MathPow(10,-Digits),"Ghobar",Magic1,0,Blue);


 if(EnableAlert==true)
 {
  if(ticket>0)
  Alert("Buy Signal");
  
  }
}
//---------------------------------------------------------------------
void Pendsell()
{

int ticket=OrderSend(Symbol(),OP_SELL,lot,Bid,slippage,Bid+stoploss*MathPow(10,-Digits)
,Bid-takeprofit*MathPow(10,-Digits),"Ghobar",Magic2,0,Red);

 if(EnableAlert==true)
 {
  if(ticket>0)
  Alert("Sell Signal");
  
  }
}

//---------------------------------------------------------------------

void closeOrder()
  {
   RefreshRates();

      for(int i=OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
              if(OrderSymbol()==Symbol() )
              {
               if(TimeCurrent()>OrderOpenPrice()+1*(3600*timeclose))
              
                  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Yellow);
              }

           }
        }
  }
 
Naqibjan:

i trey this cod for close open order after 1H but cant close after 1H.

can you help me and repair  for this cod.

Two major problems:

(1) Move your closeOrder() out of all 'if's:

int start()
  {
 
if(Volume[0]<=1)
{
 if(Orders()==0)
 {
  if(CandleStatus()=="buy")
   Pendbuy();
  if(CandleStatus()=="sell")
   Pendsell();
  }
 }
   closeOrder();
   return(0);
  }

(2) Change the highlighted part from Price to Time:

void closeOrder()
  {
   RefreshRates();

      for(int i=OrdersTotal()-1; i>=0; i--)
        {
         if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
           {
              if(OrderSymbol()==Symbol() )
              {
               if(TimeCurrent()>OrderOpenTime()+1*(3600*timeclose))
              
                  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Yellow);
              }

           }
        }
  }

You should also check your magic number in this function too, since your EA uses it.

 

Tanks dear Seng Joo Thio:

This is working now.

Seng Joo Thio:

Two major problems:

(1) Move your closeOrder() out of all 'if's:

(2) Change the highlighted part from Price to Time:

You should also check your magic number in this function too, since your EA uses it.

 
  1. if(Volume[0]<=1)
    
    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart,) volume is unreliable (miss ticks,) Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
    I disagree with making a new bar function, because it can only be called once per tick. A variable can be tested multiple times.
              New candle - MQL4 programming forum

  2.       for(int i=OrdersTotal()-1; i>=0; i--)
            {
             if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))
               {
                  if(OrderSymbol()==Symbol() )
    Using OrdersTotal (or OrdersHistoryTotal) directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

  3.   if(TimeCurrent()>OrderOpenPrice()+1*(3600*timeclose))
    Time (1563580800) will always be greater then price (1.23456)

  4. int ticket=OrderSend(Symbol(),OP_BUY,lot,Ask,slippage,Ask-stoploss*MathPow(10,-Digits)
    ,Ask+takeprofit*MathPow(10,-Digits),"Ghobar",Magic1,0,Blue);
    
    MathPow(10,-Digits) equals _Point. Simplify.
  5. You buy at the Ask and sell at the Bid. So for buy orders you pay the spread on open. For sell orders you pay the spread on close.
    1. Your buy order's TP/SL (or Sell Stop's/Sell Limit's entry) are triggered when the Bid reaches it. Not the Ask. Your SL is shorter by the spread and your TP is longer.
    2. Your sell order's TP/SL (or Buy Stop's/Buy Limit's entry) will be triggered when the Ask reaches it. To trigger at a specific Bid price, add the average spread.
                MODE_SPREAD (Paul) - MQL4 programming forum - Page 3 #25
    3. The charts show Bid prices only. Turn on the Ask line to see how big the spread is (Tools → Options (Control-O) → charts → Show ask line.)
  6. OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,Yellow);
    Using OCP (instead of Bid/Ask) is fine (no need to check order type for close price.) But if you potentially close multiple orders, you must call RefreshRates after the server call and before the next OrderSelect.
  7. Check your return codes for errors, and report them including GLE/LE. Don't look at it unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
Reason: