help for write my first EA in mql4 - page 2

 
Thanks so much  honest_knave  and whroeder1  but that is not clear for me how that code work !!

 
void OnTick()
  {
   datetime now = TimeCurrent();
   if(now >= NextTrade)
     {
      if(OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,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));
        }
     }
  }
or

datetime now = TimeCurrent();
   datetime midnight = now - (now % DAY);
   NextTrade = midnight + DAY + StartHour*HOUR + StartMinute*MINUTE;
   if(now >= NextTrade){
      if(OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0) < 0)
        :
first said get currenttime and set in in "now" but we don't need current time we need  StartHour  and StartMinute !!

and then
if(now >= NextTrade)
why?

and then we shouldn't do this ??
OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0)

then write :

if(OrderSend(Symbol(),OP_BUY,Lots,Ask,1,0,Ask+(TP*Point),comment,magic,0,0) < 0)
??

Thanks so much for your help and your time
 

Perhaps first we need to clearly define what you expect the code to do.

Although it might seem to be a simple EA, working with time isn't the easiest place to begin.

Most EAs are designed to work when a new tick comes in. But, you don't know when the next tick is going to arrive. It depends on the instrument, the time of day, the day of the week and so on.

If you are happy to accept "on the first tick that arrives after 10:14:00" then life becomes easier.

If you want "a trade to be placed at exactly 10:14:00" then it gets more complicated.

Which are you looking for? 

 
honest_knave:

Perhaps first we need to clearly define what you expect the code to do.

Although it might seem to be a simple EA, working with time isn't the easiest place to begin.

Most EAs are designed to work when a new tick comes in. But, you don't know when the next tick is going to arrive. It depends on the instrument, the time of day, the day of the week and so on.

If you are happy to accept "on the first tick that arrives after 10:14:00" then life becomes easier.

If you want "a trade to be placed at exactly 10:14:00" then it gets more complicated.

Which are you looking for? 

I think we have thick every minutes and it possible to use  10:14 but i know this is not stable that we have thick on every seconds like 10:14:00" !

Is your code work with first case? can you explain how it work ? it isn't clear for me !

how can i do it for second case ? is this true for second case?:

#define  DAY 86400 // day is 86400 seconds
#define  HOUR 3600 // hour is 3600 seconds
#define  MINUTE 60 // minute is 60 seconds
#define  SECOND 1

input double Lots    = 0.1;
input int    TP      = 200;
input int    SL      = 200;
input string comment = "MyOrder";
input int    magic   = 1234;

input int StartHour   = 14;
input int StartMinute = 15;
input int StartSecond = 16;

datetime  NextTrade   = StartHour*HOUR + StartMinute*MINUTE + StartSecond*SECOND;

//+--------------------------------------------------------------------------------------+
//| 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,0,Ask+(TP*Point),comment,magic,0,0) < 0)
        {
         printf("OrderSend() failed. Error code: %i", GetLastError());
        }
      else
        {
         datetime midnight = now - (now % HOUR);
         NextTrade = midnight + HOUR + StartHour*HOUR + StartMinute*MINUTE + StartSecond*SECOND;
         printf("Next Trade scheduled for %s", TimeToString(NextTrade));
        }
     }
    }


And can we use this code for Hundredth of a second(centisecond ) and millisecond:(is it possible to use it in real trading ? is there this Accuracy in trading? )


#define  DAY 86400 // day is 8'640'000 centiseconds
#define  HOUR 3600 // hour is 360'000 centiseconds
#define  MINUTE 60 // minute is 6'000 centiseconds
#define  SECOND 0.01 // second is 0.01 centiseconds
#define  CENTISECOND 1


input double Lots    = 0.1;
input int    TP      = 200;
input int    SL      = 200;
input string comment = "MyOrder";
input int    magic   = 1234;

input int StartHour   = 14;
input int StartMinute = 15;
input int StartSecond = 16;
input int StartCentiSecond = 885;

datetime  NextTrade   = StartHour*HOUR + StartMinute*MINUTE + StartSecond*SECOND + StartCentiSecond*CENTISECOND ;

//+--------------------------------------------------------------------------------------+
//| 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,0,Ask+(TP*Point),comment,magic,0,0) < 0)
        {
         printf("OrderSend() failed. Error code: %i", GetLastError());
        }
      else
        {
         datetime midnight = now - (now % HOUR);
         NextTrade = midnight + HOUR + StartHour*HOUR + StartMinute*MINUTE + StartSecond*SECOND + StartCentiSecond*CENTISECOND;
         printf("Next Trade scheduled for %s", TimeToString(NextTrade));
        }
     }
    }




Again ,Thanks so much
Reason: