Only one buy and one sell order per day

 

Hi,


I need some coding help. I want to set one buy order and one sell order at a specific time at a specific day of the week. Now I want the code for only trade if no buy or sell order is open.

Who can help?

Thanks and Regards,

Jimbofx7

Files:
a1.01.mq4  4 kb
 

what is your problem?

 
qjol:

what is your problem?


With the Code attached there were open several buy and sell positions. But I only want to open one buy position and only one sell position.
 
//+------------------------------------------------------------------+
//|                                                        A1.mq4 |
//|                      Copyright © 2010, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

//---- input parameters
extern int TradeDay=1;
extern int TradeHour=22;
extern int TradeMinute=10;
extern double Lots= 1.0;
extern int Stoploss=100;
extern int Takeprofit=100;
extern int MagicNumberBuy=1001;
extern int MagicNumberSell=1002;
int OpenBuy = 0;
int OpenSell = 0;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
   
   int vTradeDay = DayOfWeek();
   
   int vDayOfWeek = DayOfWeek();
   //Comment ("DayOfWeek=", vDayOfWeek);
   
   int vDay=int Day();
   //Comment ("Day=", vDay);
   
   int vDayOfYear=DayOfYear();
   //Comment ("DayOfYear=", vDayOfYear);
   
   int vHour=Hour();
   //Comment ("Hour=", vHour);
   
   int vMinute=Minute();
   //Comment ("Minute=", vMinute);
   
   int vMonth=Month();
   //Comment ("Month=", vMonth);
   
   int vSeconds=Seconds();
   //Comment ("Seconds=", vSeconds);
   
   datetime vTimeCurrent=TimeCurrent();
   //Comment ("TimeCurrent=", vTimeCurrent);
   
   //int vTimeDay=TimeDay(D'2010.11.20');
   //Comment ("TimeDay=", vTimeDay);
   
   int vTotalTrades = OrdersTotal();
   
    
   Comment ("DayOfWeek=", vDayOfWeek," / ","Day=", vDay," / ","Month=", vMonth," / ","DayOfYear=", vDayOfYear," / ","Hour=", vHour," / ","Minute=", vMinute," / ","Seconds=", vSeconds," / ","TimeCurrent=", vTimeCurrent," / ","TotalTrades=",vTotalTrades/*"TimeDay=", vTimeDay*/);

   if (vHour==TradeHour && vMinute==TradeMinute && vTradeDay==TradeDay)  // genau Zeit muss eingetreten sein
      {
      if (OpenBuy == 0)
         {
         if (OrderSend(Symbol(),OP_BUY,Lots,Ask,5,Ask-Stoploss*Point,Ask+Takeprofit*Point,0,MagicNumberBuy,Green)>-1)
            {
            OpenBuy = 1;
            }
         }
      Sleep(1000);
      RefreshRates();
      if (OpenSell == 0)
         {
         if (OrderSend(Symbol(),OP_SELL,Lots,Bid,5,Bid+Stoploss*Point,Bid-Takeprofit*Point,0,MagicNumberSell,0,Green)>-1)
            {
            OpenSell = 1;
            }
         }
      }
   
//----
   return(0);
  }
//+------------------------------------------------------------------+

There are many ways to do it, this is one of them //i didn't checked it

 
jimbofx7:

With the Code attached there were open several buy and sell positions. But I only want to open one buy position and only one sell position.
int countBuy=0,
    countSell=0;
for(int pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
    OrderSelect(pos, SELECT_BY_POS)            // Only my orders w/
&&  OrderMagicNumber() == Magic.Number         // my magic number
&&  OrderSymbol()      == Symbol() ){          // and period and symbol
         if (OrderType() == OP_BUY)  countBuy++;
    else if (OrderType() == OP_SELL) countSell++;
}
...
//    if (OpenBuy == 0)
      if (OpenBuy == 0 && countBuy == 0)

 
qjol:

There are many ways to do it, this is one of them //i didn't checked it


Thanks for the code but it only opened two positions (1 buy and 1 sell). It has to open a buy and a sell order every week on the same day and time.


  if (vHour==TradeHour && vMinute==TradeMinute && vTradeDay==TradeDay)  // specific time has to be occured
      {
 
WHRoeder:


On which position in the code I have to include your code part?


Regards,

 

u r inconstant

jimbofx7:

Thanks for the code but it only opened two positions (1 buy and 1 sell). its opening ?

It has to open a buy and a sell order every week on the same day and time. its not ?

 
qjol:

u r inconstant


The backtest opened only this two positions. :-( (Backtest from 10.01.2010 to 30.11.2010)



1 2010.11.02 21:57 buy 1 1.00 1.6039 1.5939 1.6139 0.00 10000.00
2 2010.11.02 21:57 sell 2 1.00 1.6036 1.6136 1.5936 0.00 10000.00
3 2010.11.03 11:01 s/l 2 1.00 1.6136 1.6136 1.5936 -1001.40 8998.60
4 2010.11.03 11:12 t/p 1 1.00 1.6139 1.5939 1.6139 1000.50 9999.10

 

You're right you need to add the code that WHRoeder gave

 
int vTradeDay = DayOfWeek();
int vDayOfWeek = DayOfWeek();

datetime    now = TimeCurrent();        // or Time[0]
int         DOW = TimeDayOfWeek(now),   /* https://forum.mql4.com/33851
        // reports DayOfWeek() always returns 5 in the tester. No refresh?*/