Close order automatically after a given period and/or SL - page 3

 

This is very wrong . . .

OrderSelect(BuyTicket || SellTicket, SELECT_BY_TICKET);

(BuyTicket || SellTicket) will result in true or false . . . and in this case true or false will be regarded as a 1 or 0 . . . not your SellTicket or Buyticket number . . .

Stoploss = 40 . . . . when you use a stoploss in OrderSend it is a price . . . not a number of pips, slippage is a number of pips . . . read the documentation: OrderSend

stoploss - Stop loss level.
takeprofit - Take profit level.
 

okay I know what you mean, so i need a command to select the order while using OrderSelect...or is OrderSelect in this case generally wrong?

 

Read the docs . . . OrderSelect is needed to select the order before you can use the likes of OrderLots, OrderTicket, OrderOpenPrice, etc, etc "Note: The order must be previously selected by the OrderSelect() function."

If you don't need to use an information pertaining to an existing order you don't need OrderSelect. if you do . . . well then you do.

 
RaptorUK:

Read the docs . . . OrderSelect is needed to select the order before you can use the likes of OrderLots, OrderTicket, OrderOpenPrice, etc, etc "Note: The order must be previously selected by the OrderSelect() function."

If you don't need to use an information pertaining to an existing order you don't need OrderSelect. if you do . . . well then you do.


Currently I think that I would need this information, because if there is a position at the market I need to close itand to replace it for another. I thought OrderSelect would help me this way. But if Orderselect is just for open Orders and not for the active position at market it is not useful. Am I right?

But if OrderSelect doesn't help how can I close the active positions?

regards

Marc

 
If you don't use pending orders and you have open orders then they will be either a Buy or a Sell . . . a market order.
 
//+------------------------------------------------------------------+
//| Der Stundentrader.mq4 |
//| Der Marc |
//| Es gibt gar keine Internetseite |
//+------------------------------------------------------------------+
#property copyright "Der Marc"
#property link "Es gibt gar keine Internetseite"

//Wichtige Variablen
extern double Minlot=0.01;
extern int Digits2Round=2;
extern int PercentOfFreeDepo=1;
extern int Slippage=5;
extern int MagicNumber =1;
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;
extern int StopLoss=40;

//Globale Variablen
int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;

int openbuy = 0;
int opensell = 0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
UsePoint = PipPoint(Symbol());
UseSlippage = GetSlippage(Symbol(), Slippage);
}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double FreeDepo=NormalizeDouble(AccountBalance()-AccountMargin(),Digits2Round);
double Risk=NormalizeDouble((FreeDepo*PercentOfFreeDepo/100),Digits2Round);
double Lot=NormalizeDouble(Risk/(StopLoss/0.0001)*0.1,Digits2Round);
//===================== Lets determine lot size and risk ===================================
if ( Lot<Minlot )
{
Lot=Minlot;
}
Comment( "\n","Acceptable risk is ",PercentOfFreeDepo, "% = ",Risk," of the free money ",FreeDepo," in lots = ",Lot);
for(int i = OrdersTotal() - 1; i >= 0; i--)
    {
    if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
       {
       if((OrderOpenTime()+3600) < TimeCurrent())
          { 
           if (OrderType() == OP_BUY)          
              {
               bool Closed = OrderClose(OrderTicket() ,OrderLots(), Ask, UseSlippage, Red);           
               openbuy = 0;
              }
           if (OrderType() == OP_SELL)          
              {
               Closed = OrderClose(OrderTicket() ,OrderLots(), Bid, UseSlippage, Red);  
               opensell = 0;         
              }              
          }
       }
    }


//BuyOrder 
if ((TradeHour3==Hour())||(TradeHour4==Hour())||(TradeHour7==Hour())||(TradeHour10==Hour())||(TradeHour17==Hour())||(TradeHour18==Hour())||(TradeHour20== Hour()) && openbuy == 0) //Signal Buy
{
  openbuy=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage ,Ask - StopLoss * Point,0,"time trader buy order ",MagicNumber,0,Blue);
}
//SellOrder
if ((TradeHour12==Hour())||(TradeHour23==Hour())&& opensell == 0) //Signal Sell
{
  opensell=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,Bid + StopLoss * Point,0,"time trader sell order ",MagicNumber,0,Green);
} 

//----
return(0);
}
//+------------------------------------------------------------------+
//Pip Point Function
double PipPoint (string Currency)
{
int CalcDigits = MarketInfo(Currency, MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
return (CalcPoint);
}

//Get Slippage Function
int GetSlippage(string Currency, int SlippagePips)
{
int CalcDigits = MarketInfo(Currency, MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 4) double CalcSlippage = SlippagePips;
else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;
return (CalcSlippage);
}

This seems to be mostly correct, but now I need to know why the prog only executes the sell order @ 12 & 23...and not the buy orders...?
 

nirvanamac:

This seems to be mostly correct, but now I need to know why the prog only executes the sell order @ 12 & 23...and not the buy orders...?

It probably does . . and it probably generates an error that you aren't trapping . . .

Try . . .

//BuyOrder 
if ((TradeHour3==Hour())||(TradeHour4==Hour())||(TradeHour7==Hour())||(TradeHour10==Hour())||(TradeHour17==Hour())||(TradeHour18==Hour())||(TradeHour20== Hour()) && openbuy == 0) //Signal Buy
{
  openbuy=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage ,Ask - StopLoss * Point,0,"time trader buy order ",MagicNumber,0,Blue); 
  if (openbuy < 0) Print("OrderSend OP_BUY failed, error: ", GetLastError() );
}
//SellOrder
if ((TradeHour12==Hour())||(TradeHour23==Hour())&& opensell == 0) //Signal Sell
{
  opensell=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,Bid + StopLoss * Point,0,"time trader sell order ",MagicNumber,0,Green);
  if (opensell < 0) Print("OrderSend OP_SELL failed, error: ", GetLastError() );

} 

Your OrderClose is wrong . . . you open a Buy at Ask, to close a Buy you Sell . . . you Sell at Bid, you open a Sell at Bid to close a Sell you Buy . . . you Buy at Ask.

 
RaptorUK:
Your OrderClose is wrong . . . you open a Buy at Ask, to close a Buy you Sell . . . you Sell at Bid, you open a Sell at Bid to close a Sell you Buy . . . you Buy at Ask.
Or, since you already selected the order just use
Closed = OrderClose(OrderTicket() ,OrderLots(), OrderClosePrice(), UseSlippage, Red);  
And you don't have to differentiate the order type.
 

Thank you a lot for helping me...I modified the code with your hints. It looks like this way:

//+------------------------------------------------------------------+
//| Der Stundentrader.mq4 |
//| Der Marc |
//| Es gibt gar keine Internetseite |
//+------------------------------------------------------------------+
#property copyright "Der Marc"
#property link "Es gibt gar keine Internetseite"

//Wichtige Variablen
extern double Minlot=0.01;
extern int Digits2Round=2;
extern int PercentOfFreeDepo=1;
extern int Slippage=5;
extern int MagicNumber =1;
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;
extern int StopLoss=400;

//Globale Variablen
int BuyTicket;
int SellTicket;
double UsePoint;
int UseSlippage;

int openbuy = 0;
int opensell = 0;
//+------------------------------------------------------------------+
//| expert initialization function |
//+------------------------------------------------------------------+
int init()
{
UsePoint = PipPoint(Symbol());
UseSlippage = GetSlippage(Symbol(), Slippage);
}

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+
int start()
{
double FreeDepo=NormalizeDouble(AccountBalance()-AccountMargin(),Digits2Round);
double Risk=NormalizeDouble((FreeDepo*PercentOfFreeDepo/100),Digits2Round);
double Lot=NormalizeDouble(Risk/(StopLoss/0.0001)*0.1,Digits2Round);
//===================== Lets determine lot size and risk ===================================
if ( Lot<Minlot )
{
Lot=Minlot;
}
Comment( "\n","Acceptable risk is ",PercentOfFreeDepo, "% = ",Risk," of the free money ",FreeDepo," in lots = ",Lot);
for(int i = OrdersTotal() - 1; i >= 0; i--)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if((OrderOpenTime()+3600) < TimeCurrent())
{
if (OrderType() == OP_BUY || OP_SELL)
{
bool Closed = OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(), UseSlippage, Red);
openbuy = 0;
opensell = 0;
}
if (OrderType() == OP_SELL)
{
Closed = OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(), UseSlippage, Red);
opensell = 0;
openbuy = 0;
}
}
}
}
//BuyOrder
if ((TradeHour3==Hour())||(TradeHour4==Hour())||(TradeHour7==Hour())||(TradeHour10==Hour())||(TradeHour17==Hour())||(TradeHour18==Hour())||(TradeHour20== Hour()) && openbuy == 0) //Signal Buy
{
openbuy=OrderSend(Symbol(),OP_BUY,Lot,Ask,Slippage,Ask - StopLoss * Point,0,"time trader buy order ",MagicNumber,0,Blue);
if (openbuy < 0) Print("OrderSend OP_BUY failed, error: ", GetLastError() );
}
//SellOrder
if ((TradeHour12==Hour())||(TradeHour23==Hour())&& opensell == 0) //Signal Sell
{
opensell=OrderSend(Symbol(),OP_SELL,Lot,Bid,Slippage,Bid + StopLoss * Point,0,"time trader sell order ",MagicNumber,0,Green);
if (opensell < 0) Print("OrderSend OP_SELL failed, error: ", GetLastError() );
}

//----
return(0);
}
//+------------------------------------------------------------------+
//Pip Point Function
double PipPoint (string Currency)
{
int CalcDigits = MarketInfo(Currency, MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 3) double CalcPoint = 0.01;
else if(CalcDigits == 4 || CalcDigits == 5) CalcPoint = 0.0001;
return (CalcPoint);
}

//Get Slippage Function
int GetSlippage(string Currency, int SlippagePips)
{
int CalcDigits = MarketInfo(Currency, MODE_DIGITS);
if(CalcDigits == 2 || CalcDigits == 4) double CalcSlippage = SlippagePips;
else if(CalcDigits == 3 || CalcDigits == 5) CalcSlippage = SlippagePips * 10;
return (CalcSlippage);
}

While running the backtest there is one Error Message ERR_INVALID_TICKET (4108).

 

Not all orders were executed

Could it be that the Error belongs to the fact that the SL was triggered before?