help for this code

 
Hello

I love to open trade in 00:00 time every odd days  but When it comes to holiday , order start in first week day that odd or non odd !!?  How can I fix it ?

#define  DAY 2*86400 // day is 86400 seconds times to 2 and start from odd day
#define  HOUR 3600 // hour is 3600 seconds
#define  MINUTE 60 // minute is 60 seconds

input double Lots    = 10;
input int    TP      = 500;
input int    SL      = 500;
input string comment = "MyOrder";
input int    magic   = 1234;

input int StartHour   = 00;
input int StartMinute = 00;
datetime  NextTrade   = StartHour*HOUR + StartMinute*MINUTE;

//+--------------------------------------------------------------------------------------+
//| OnInit()                                                                             |
//+--------------------------------------------------------------------------------------+
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

//+--------------------------------------------------------------------------------------+
//| OnDeinit()                                                                           |
//+--------------------------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
  }

//+--------------------------------------------------------------------------------------+
//| OnTick()                                                                             |
//+--------------------------------------------------------------------------------------+
void OnTick()
  {
   datetime now = TimeCurrent();
   if(now >= NextTrade)
     {
      if(OrderSend(Symbol(),OP_BUY,Lots,Ask,1,Bid-(SL*Point),Ask+(TP*Point),comment,magic,0,0) < 0)
        {
         printf("OrderSend() failed. Error code: %i", GetLastError());
        }
      else
        {
         datetime midnight = now - (now % DAY);
         NextTrade = midnight + DAY + StartHour*HOUR + StartMinute*MINUTE;
         printf("Next Trade scheduled for %s", TimeToString(NextTrade));
        }
     }
    }

 
I used this code for odd days trading and i think work !

if(now >= NextTrade && (Day()==1 || Day()==3 || Day()==5 || Day()==7 || Day()==9 || Day()==11 || Day()==13 || Day()==15 || Day()==17 || Day()==19 || Day()==21 || Day()==23 || Day()==25 || Day()==27 || Day()==29 || Day()==31 ) )
    
But i have problem with first code that can start from special day and work  Every other day !!

Thanks

 
  1. Your code
    if(now >= NextTrade && (Day()==1 || Day()==3 || Day()==5 || Day()==7 || Day()==9 || Day()==11 || Day()==13 || Day()==15 || Day()==17 || Day()==19 || Day()==21 || Day()==23 || Day()==25 || Day()==27 || Day()==29 || Day()==31 ) )
    Simplified
    if(now >= NextTrade && Day() % 2 == 1 )
  2. trader92: start from special day and work  Every other day !!
    #define SPECIAL_DAY 1
    int fromSD = MathAbs(Day() - SPECIAL_DAY);
    if(now >= NextTrade && fromSD % 2 == 0) // Special day +- 2n
 
What do you mean by every other day?

Do you mean every other trading day?

If you are unable to describe exactly what you want then you will not be able to write the code.

You could check for a new bar on D1, so that you know that a new trading day has started. Check if a trade was opened during the previous D1. If a trade was opened, don't open one today.
 
whroeder1:
  1. Your code
    if(now >= NextTrade && (Day()==1 || Day()==3 || Day()==5 || Day()==7 || Day()==9 || Day()==11 || Day()==13 || Day()==15 || Day()==17 || Day()==19 || Day()==21 || Day()==23 || Day()==25 || Day()==27 || Day()==29 || Day()==31 ) )
    Simplified
    if(now >= NextTrade && Day() % 2 == 1 )
  2. #define SPECIAL_DAY 1
    int fromSD = MathAbs(Day() - SPECIAL_DAY);
    if(now >= NextTrade && fromSD % 2 == 0) // Special day +- 2n
Thanks so much , for excellent help
 
Keith Watford:
What do you mean by every other day?

Do you mean every other trading day?

If you are unable to describe exactly what you want then you will not be able to write the code.

You could check for a new bar on D1, so that you know that a new trading day has started. Check if a trade was opened during the previous D1. If a trade was opened, don't open one today.

Thanks so much for your answer


with whroeder1  code my problem was solved ,

When i use code of first post ,
#define  DAY 2*86400 // day is 86400 seconds times to 2 and start from odd day
My order open in for example 1-2-2017 and then open in 3-2-2017 but because 5-2-2017 is sunday and the market is closed and it should go to 7-2-2017 for order but that code send order in first day of market was open!!

but with  code code works properly ! and open order in 7-2-2017 and not open in 6-2-2017 !

and i mean this , for example :  1-2-2017 open order at special time and next day don't open then 3-2-2017 open and when at
second day market was closed count that closed day and Not rejected that!
because my code rejected that closed day.

 
Excuse me but i test it again and when i used code for many month it can't work properly !

Please see :

It should open trade in 2-2-2017 !!
 

Your original code, and the simplified version provided by @whroeder1 , treats each month individually

Try using DayOfYear() rather than Day()

It will still fall out of sync over New Year, depending on whether it is a leap year or not. 

 
And if you want to stay in sync over the New Year
#define HR2400 86400
#define SECONDS uint
SECONDS    time(datetime when=0){
      return SECONDS(when == 0 ? TimeCurrent() : when) % HR2400;               }
datetime   date(datetime when=0){
      return datetime( (when == 0 ? TimeCurrent() : when) - time(when) );      }
////////////////////////////////////////////////////////////////////////////////
:
int today    = date() / HR2400;
int fromSD = today - SPECIAL_DAY;
if(now >= NextTrade && fromSD % 2 == 0) // Special day +- 2n
Reason: