Close order automatically after a given period and/or SL

 

Hi together,

I'd like to know if it's possible to close an order automatically after a period and/or SL is reached...

For example:

I've opened a trade at 1p.m. and want to let this order closed when the quote reached it's StoppLoss or a period of 2 hours elapsed.

Here's the actual code:

// Buy criteria
if ((TradeHour3==Hour())||(TradeHour4==Hour())||(TradeHour7==Hour())||(TradeHour10==Hour())||(TradeHour17==Hour())||(TradeHour18==Hour())||(TradeHour20==Hour())) //Signal Buy
{
int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,slb,tpb,"time trader buy order ",BuyMagicNumber,0,Blue);
if(openbuy<1){int buyfail=1;}
}

}// halt1

if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
// Sell criteria
if ((TradeHour12==Hour())||(TradeHour23==Hour())) //Signal Sell
{
int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sls,tps,"time trader sell order ",SellMagicNumber,0,Green);
if(opensell<1){int sellfail=1;}
}

}// halt2

Thank you in advance

Marc

 

Please use this to post code . . . it makes it easier to read.

 
Loop through the open orders checking when they were opened using OrderOpenTime ( don't forget to use OrderSelect first ), then check that time vs TimeCurrent to see if 2hrs have elapsed . . if they have close the order.
 
nirvanamac:

Hi together,

I'd like to know if it's possible to close an order automatically after a period and/or SL is reached...

For example:

I've opened a trade at 1p.m. and want to let this order closed when the quote reached it's StoppLoss or a period of 2 hours elapsed.

Here's the actual code:

// Buy criteria
if ((TradeHour3==Hour())||(TradeHour4==Hour())||(TradeHour7==Hour())||(TradeHour10==Hour())||(TradeHour17==Hour())||(TradeHour18==Hour())||(TradeHour20==Hour())) //Signal Buy
{
int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,slb,tpb,"time trader buy order ",BuyMagicNumber,0,Blue);
if(openbuy<1){int buyfail=1;}
}

}// halt1

if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
// Sell criteria
if ((TradeHour12==Hour())||(TradeHour23==Hour())) //Signal Sell
{
int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sls,tps,"time trader sell order ",SellMagicNumber,0,Green);
if(opensell<1){int sellfail=1;}
}

}// halt2

Thank you in advance

Marc


// Buy criteria
if ((TradeHour3==Hour())||(TradeHour4==Hour())||(TradeHour7==Hour())||(TradeHour10==Hour())||(TradeHour17==Hour())||(TradeHour18==Hour())||(TradeHour20==Hour())) //Signal Buy
{
int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,slb,tpb,"time trader buy order ",BuyMagicNumber,0,Blue);
if(openbuy<1){int buyfail=1;}
}

}// halt1

if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
// Sell criteria
if ((TradeHour12==Hour())||(TradeHour23==Hour())) //Signal Sell
{
int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sls,tps,"time trader sell order ",SellMagicNumber,0,Green);
if(opensell<1){int sellfail=1;}
}

}// halt2
 
nirvanamac:
I'd like to know if it's possible to close an order automatically after a period and/or SL is reached...
  1. If the SL is reached, it closes itself.
  2. Otherwise, you just have to code it. Where is your OrderClose() and your test for time?
 
WHRoeder:
  1. If the SL is reached, it closes itself.
  2. Otherwise, you just have to code it. Where is your OrderClose() and your test for time?


HI WHRoeder,

I think you mean the hole code or?

//---- input parameters
extern double    Lots=0.01;
extern int       TakeProfit=200;
extern int       StopLoss=200;
extern int       Slip=50;
extern int BuyMagicNumber =1;
extern int SellMagicNumber =2;
extern bool AllowBuy=true;
extern bool AllowSell=true;
extern int  TradeHour3=3;
extern int  TradeHour4=4;
extern int  TradeHour7=7;
extern int  TradeHour10=10;
extern int  TradeHour17=17;
extern int  TradeHour18=18;
extern int  TradeHour20=20;
extern int  TradeHour12=12;
extern int  TradeHour23=23;
 
//+------------------------------------------------------------------+
//| expert starts                                  |
//+------------------------------------------------------------------+
int start()
  {
//----
int StopMultd,Sleeper=1;
 
 
 
int digits=MarketInfo("EURUSD",MODE_DIGITS);
if(digits==5){StopMultd=10;} else{StopMultd=1;}
double TP=NormalizeDouble(TakeProfit*StopMultd,Digits);
double SL=NormalizeDouble(StopLoss*StopMultd,Digits);
double Slippage=NormalizeDouble(Slip*StopMultd,Digits);
 
// Calculate stop loss
double slb=NormalizeDouble(Ask-SL*Point,Digits);
double sls=NormalizeDouble(Bid+SL*Point,Digits);
 
// Calculate take profit
double tpb=NormalizeDouble(Ask+TP*Point,Digits);
double tps=NormalizeDouble(Bid-TP*Point,Digits);
 
//-------------------------------------------------------------------+
//Check open orders
//-------------------------------------------------------------------+
if(OrdersTotal()>0){
  for(int i=1; i<=OrdersTotal(); i++)          // Cycle searching in orders
     {
      if (OrderSelect(i-1,SELECT_BY_POS)==true) // If the next is available
        {
          if(OrderMagicNumber()==BuyMagicNumber) {int halt1=1;}
          if(OrderMagicNumber()==SellMagicNumber) {int halt2=1;}
        }
     }
}
//-------------------------------------------------------------------+
 
 
if((halt1!=1)&&(AllowBuy==true)){// halt1
 
// Buy criteria
if ((TradeHour3==Hour())||(TradeHour4==Hour())||(TradeHour7==Hour())||(TradeHour10==Hour())||(TradeHour17==Hour())||(TradeHour18==Hour())||(TradeHour20==Hour())) //Signal Buy
 {
   int openbuy=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,slb,tpb,"time trader buy order ",BuyMagicNumber,0,Blue);
   if(openbuy<1){int buyfail=1;}
 }
 
}// halt1
 
if((halt2!=1)&&(AllowSell==true)){// halt2
RefreshRates();
 // Sell criteria
 if ((TradeHour12==Hour())||(TradeHour23==Hour())) //Signal Sell
 {
   int opensell=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,sls,tps,"time trader sell order ",SellMagicNumber,0,Green);
   if(opensell<1){int sellfail=1;}
 }
 
}// halt2
 
 
//-------------------------------------------------------------------+
// Error processing
//-------------------------------------------------------------------+
if(buyfail==1||sellfail==1){
int Error=GetLastError();
  if(Error==130){Alert("Wrong stops. Retrying."); RefreshRates();}
  if(Error==133){Alert("Trading prohibited.");}
  if(Error==2){Alert("Common error.");}
  if(Error==146){Alert("Trading subsystem is busy. Retrying."); Sleep(500); RefreshRates();}
 
}
 
// if(openbuy==true||opensell==true)Sleep(1*60*1000*Sleeper);
//-------------------------------------------------------------------
   return(0);
  }
//+-----------------------------------
 

actually there is just a stopploss. I only want to close the order 2 hours after a new bar was opened or the last second of 1:59:59...My problem is to fill the code into my present one...

 
RaptorUK:
Loop through the open orders checking when they were opened using OrderOpenTime ( don't forget to use OrderSelect first ), then check that time vs TimeCurrent to see if 2hrs have elapsed . . if they have close the order.
Just code that . . . it really isn't hard.
 
RaptorUK:
Just code that . . . it really isn't hard.

I hope you can give me hints if I fail ;-)
 
AFAIK everyone here wants to help but not to be a slave for anyone
 
qjol:
AFAIK everyone here wants to help but not to be a slave for anyone


Yes of course I understand...I don't want to have slaves for my EA's....the only thing I need to know is how to understand in which way a program works...

If you have a simple EA like SMA Cross...this is not a great problem...but the more you want to achieve the more harder it gets...(at least for me)