Problem with timing

 

Hi Friends,

I am facing a problem, where which I don't have an idea why its happening.

Using the code given below I am trying to run the function at specific time gap only. If not, it should return "Out of Time"

While compiling this code the V_LINEs are drawn correctly at the current day's xyz time.

But while starting for next day without compiling it again takes yesterday's xyz time and returns "Out of Time".

Any help appreciated.

datetime open_time = 0, close_time = 0;
int init()
  {
   Print("Script name is ",Symbol());
   
   if(Symbol() == "FTSE_100")
   {
   open_time = D'04:15'; // these times are take from M15 chart and I am sure there's no problem with the time mentioned here
   close_time = D'15:45';
   }
   else if(Symbol() == "Nasdaq_100")
   {
   open_time = D'00:15';
   close_time = D'23:30';
   }
   else if(Symbol() == "CAC_40")
   {
   open_time = D'03:15';
   close_time = D'15:30';
   }
   else
   {
   open_time = D'00:15';
   close_time = D'23:30';
   }
   bool ot = ObjectCreate("Open_Time", OBJ_VLINE, 0, open_time, 0);
   bool ct = ObjectCreate("Close_Time", OBJ_VLINE, 0, close_time, 0);
   return(0);   
  }
  
  int start()
  {
 
      if(TimeCurrent() >= open_time && TimeCurrent() <= close_time)
      {
      // Blah blah blah
      }
      else
      {
      Print("Out of Time : ",TimeCurrent());
      }
      return(0);
  }

11:03:23 Robot_code_final Nasdaq_100,M15: Out of Time : 1332207043

 
datetime open_time = 0, close_time = 0;
int init()
  {
   Print("Script name is ",Symbol());
   
   if(Symbol() == "FTSE_100")
   {
   open_time = D'04:15'; // these times are take from M15 chart and I am sure there's no problem with the time mentioned here
   close_time = D'15:45';
   }
   else if(Symbol() == "Nasdaq_100")
   {
   open_time = D'00:15';
   close_time = D'23:30';
   }
   else if(Symbol() == "CAC_40")
   {
   open_time = D'03:15';
   close_time = D'15:30';
   }
   else
   {
   open_time = D'00:15';
   close_time = D'23:30';
   }
   bool ot = ObjectCreate("Open_Time", OBJ_VLINE, 0, open_time, 0);
   bool ct = ObjectCreate("Close_Time", OBJ_VLINE, 0, close_time, 0);
   return(0);   
  }
  
  int start()
  {

      if(TimeCurrent() >= open_time && TimeCurrent() <= close_time)
      {
      // Blah blah blah
      }
      else
      {
       if(TimeCurrent() > close_time){open_time=open_time+(24*60*60);close_time=close_time+(24*60*60);}
      Print("Market closed : ",TimeCurrent());
      }
      return(0);
  }
 
krishna_gopal_2:

Hi Friends,

I am facing a problem, where which I don't have an idea why its happening.

Using the code given below I am trying to run the function at specific time gap only. If not, it should return "Out of Time"

While compiling this code the V_LINEs are drawn correctly at the current day's xyz time.

But while starting for next day without compiling it again takes yesterday's xyz time and returns "Out of Time".

Any help appreciated.

11:03:23 Robot_code_final Nasdaq_100,M15: Out of Time : 1332207043

init() is run once . . . so the open and close times are not updated for the next day . . .
 
RaptorUK:
init() is run once . . . so the open and close times are not updated for the next day . . .


Thanks friend. I shifted the code to start()

Lets see tomorrow.

Moreover, can you explain what a spread is actually.

Why it been set at such higher value? (10 pips for NSE_NIFTY)

Is there any way to reduce this pip?

Because my EA couldn't place orders at some points because of minimum 10 pips between ASK & Open price of the order.

If its reduced to 2 pips my EA will work fine, and I can test it easily.

Please refer me a broker that provides world indexes to trade (NASDAQ, FTSE, CAC, NSE_NIFTY etc)

 
krishna_gopal_2:


Thanks friend. I shifted the code to start()

Lets see tomorrow.

Moreover, can you explain what a spread is actually.

Spread is the difference between the Bid price and the Ask price . . . . you buy at Ask and sell at Bid . . . . the spread is set by your Broker. I don't know of any Brokers that offer the Nifty, sorry.
 
  1. krishna_gopal_2:

    Using the code given below I am trying to run the function at specific time gap only. If not, it should return "Out of Time"

    While compiling this code the V_LINEs are drawn correctly at the current day's xyz time.

    But while starting for next day without compiling it again takes yesterday's xyz time and returns "Out of Time".

       open_time = D'00:15';

    Because D'...' with no date is the day of compilation.

    if you want today 00:15 use

    opentime = StrToTime("00:15");
    // or
    #define HR0015 900 // (00*60+15)*60
    datetime now = Time[0],
             bod = now - now % 86400,
             h0015 = bod + HR0015;

  2.  if(TimeCurrent() > close_time){open_time=open_time+(24*60*60);close_time=close_time+(24*60*60);}
    This will not work, not taking account weekends or holidays.
    if (h0015 > now){
        datetime yesterday = iTime(NULL, PERIOD_D1, 1); // Find yesterday accounting for weekends/holidays.
        h0015 = yesterday + HR0015;
    }

  3. You must either delete your previous lines or move them or use a different name.
    void VLine(string name, datetime T0, color clr){    //  #define WINDOW_MAIN 0
        if (!Show.Objects)  return;
        if      (ObjectMove( name, 0, T0, 0 )){}
        else if (!ObjectCreate( name, OBJ_VLINE, WINDOW_MAIN, T0, 0 ))
            Alert("ObjectCreate(",name,",VLINE) failed: ", GetLastError() );
        if (!ObjectSet(name, OBJPROP_COLOR, clr )) // Allow color change
            Alert("ObjectSet(", name, ",Color) [2] failed: ", GetLastError() );
        if (!ObjectSetText(name, TimeToStr(T0, TIME_MINUTES), 10))
            Alert("ObjectSetText(",name,") [4] failed: ", GetLastError());
    }
    

 
WHRoeder:
  1. krishna_gopal_2:

    Using the code given below I am trying to run the function at specific time gap only. If not, it should return "Out of Time"

    While compiling this code the V_LINEs are drawn correctly at the current day's xyz time.

    But while starting for next day without compiling it again takes yesterday's xyz time and returns "Out of Time".

    Because D'...' with no date is the day of compilation.

    if you want today 00:15 use


  2. This will not work, not taking account weekends or holidays.
  3. You must either delete your previous lines or move them or use a different name.


William, You are really great. Your help is really very worthy.

 
RaptorUK:
Spread is the difference between the Bid price and the Ask price . . . . you buy at Ask and sell at Bid . . . . the spread is set by your Broker. I don't know of any Brokers that offer the Nifty, sorry.


Dear Simon,

Thank you for your reply.

How about brokers providing NASDAQ, FTSE, CAC futures to trade via mt4? With lowest spread.

One more question. Spread is the difference between ASK and BID. Thats ok.

But why there is a difference and why the difference vary from broker to broker?

 
krishna_gopal_2:


But why there is a difference and why the difference vary from broker to broker?

It's how the Broker makes money . . . other Brokers offer lower Spreads but also charge commission. There are websites out there that review Brokers, DYOR . . . don't rely on what other people tell you though, it is your money at risk not theirs.
 
WHRoeder:
  1. krishna_gopal_2:

    Using the code given below I am trying to run the function at specific time gap only. If not, it should return "Out of Time"

    While compiling this code the V_LINEs are drawn correctly at the current day's xyz time.

    But while starting for next day without compiling it again takes yesterday's xyz time and returns "Out of Time".

    Because D'...' with no date is the day of compilation.

    if you want today 00:15 use


  2. This will not work, not taking account weekends or holidays.
  3. You must either delete your previous lines or move them or use a different name.


I like your solutions William but I don't see this is wrong

It is about this William

if(TimeCurrent() > close_time){open_time=open_time+(24*60*60);close_time=close_time+(24*60*60);}

I am working with the idea that TimeCurrent() is just a datetime. After the market is closed the new open time is 24*60*60 seconds more then last openingtime

if DAX is closed no trading so no new tick coming that is for weekend and for market is closed. I don't see why this won't work. It can bring the day after a new tick coming first from friday to saturday but the mondays it needs only a few ticks to get to the right openingtime (and closingtime) we don't miss much

So if we count to the last known openingtime +87840 seconds and it becomes saturday opening then market closed so with next tick we count directly again +87840

until we have new marketclosetime > TimeCurrent()

And what problem is it if we compile after

open_time = D'00:15';

We get ready for the time the market is open again......

Only Seasontimechange when markets or broker change time it might we have to change the parameter for time we open and close

 
deVries:


I like your solutions William but I don't see this is wrong

You have a good point.

I corrected the code as,

 if(Symbol()=="NSE_Nifty")
   {
   open_time = StrToTime("00:00");
   close_time = StrToTime("05:50");
   }
   else if(Symbol() == "FTSE_100")
   {
   open_time = StrToTime("4:15");
   close_time = StrToTime("15:45");
   }

It works. Thank you all.

Reason: