cannot detect new bar form. - page 2

 
Viffer:

Just some observations....

The datetime declaration needs to be STATIC or make sure it's on the on the global scope. You don't need D'1970.01.01 00:00:00'. zero is fine and the same thing as that date. infact, you don't even need =0 as this is the default value for any declared variable. Also, assuming time_period=0 (which it needs to be), Time[0] is the same as iTime(NULL,time_period,0) and faster to type!

Hope that's of use

V

Oh... and

 
johanmalan:


Thanks for SRC I was not aware will try next time
 
Viffer:

Just some observations....

The datetime declaration needs to be STATIC or make sure it's on the on the global scope. You don't need D'1970.01.01 00:00:00'. zero is fine and the same thing as that date. infact, you don't even need =0 as this is the default value for any declared variable. Also, assuming time_period=0 (which it needs to be), Time[0] is the same as iTime(NULL,time_period,0) and faster to type!

Hope that's of use

V

Oh... and


Hi

Why STATIC - it is working as I have it there - What problems can jump up that I am not aware of? I use iTime because I force differnet time frames and not just the current time frame of the current chart.

Thanks for the reply it always helps!!

 
johanmalan:


Hi

Why STATIC - it is working as I have it there - What problems can jump up that I am not aware of? I use iTime because I force differnet time frames and not just the current time frame of the current chart.

Thanks for the reply it always helps!!

If a variable is declared in start(), it will be reinitialised every tick so if
datetime current_bar_opentime=D'1970.01.01 00:00:00';

is inside start(), that means every tick current_bar_opentime will be set to ...well 0 and will therfore always be != iTime(NULL,time_period,0).

it's explained fully here ..https://book.mql4.com/variables/types

V

 
robofx.org:

This happens because when EA is started New_Time is 0 and Function_New_Bar() returns true. You can avoid this with the following code:

bool Function_New_Bar()
      {
      if(Volume[0]>1) return(false);
      static datetime New_Time = 0;
      bool New_Bar = false;
      if (New_Time!= Time[0])
         {
         New_Time = Time[0];
         New_Bar = true;
         }
      return(New_Bar);

       


My question might sound quite ridiculous, but I don't really know -even after analyzing mql4 documentation (https://docs.mql4.com/predefined/variables/volume) -what is that volume variable for. I know only, that if local history is empty, function returns 0. After writing script which writes down 50 last values of volume i got inputs which varies between 30 and 50. Does it denote any change of price in pips during single tick or it's any measure of volume of transactions made on forex market during every single tick - which btw would be sort of imponderabilia ? If it's so, then how is it evaluated- in brokers dealing rooms?
 
shirava2k12:

My question might sound quite ridiculous, but I don't really know -even after analyzing mql4 documentation (https://docs.mql4.com/predefined/variables/volume) -what is that volume variable for. 
It's the number of ticks that occurred during the bar in question . . .  a tick can be a change in Bid,  a change in Ask or, I believe, a change in other MarketInfo data.
 
  1. Viffer: Well, looks like you can't declare a STATIC with an expression. Never knew that.
    You certainly can but with a constant expression only. That is the value the EA is loaded with, but see #4 below.

  2. datetime current_bar_opentime=D'1970.01.01 00:00:00';
    Exactly the same as datetime v=0;

  3. robofx.org:

    This happens because when EA is started New_Time is 0 and Function_New_Bar() returns true. You can avoid this with the following code:

    bool Function_New_Bar()
          {
          if(Volume[0]>1) return(false);
    Volume is unreliable because you can miss ticks. Therefor this code will return false if you miss a tick at the start of a new bar and you ignore entire bars.
  4. This will also not work correctly if you change pair/time frame, etc. Deinit/init cycles do NOT reload static or common(global) variables.
    Instead of static variablesI use this pattern
    type Function(){
       static type v = const;
       :
    }
    int init(){
       OnInitFunction(); OnInitFunction2()...
    }
    :
    type functionV; OnInitFunction(){ functionV = const; }
    type Function(){
       :
    }

  5. I don't recommend using a function to detect a new bar because it can be called only once per start(). It fails if you call it a second time. (This is my only exception to factoring code to functions.)
    New bar or first bar
    datetime Time0;
    int init(){ Time0=0;}
    int start(){
       // Every bar code
       bool newBar = Time0 != Time[0]; Time0 = Time[0];
       if (!newBar) return;
       // New bar code
    New bar only
    datetime Time0;
    int init(){ Time0=Time[0];}
    int start(){
       // Every bar code
       if (Time0 == Time[0]) return; Time0 = Time[0];
       // New bar code
    New bar or near start of the first bar
    datetime Time0;
    int init(){ Time0=Time[0];
       if (TimeCurrent() - Time0 < Period()) // 1.7%
          Time0 = 0;
    }
    int start(){
       // Every bar code
       if (Time0 == Time[0]) return; Time0 = Time[0];
       // New bar code
    
 

Hi,


First off all thank you all for your posts and help in coding.

As in this topic in my opinion best works this:


bool New_Bar = false;
static datetime New_Time;

int init()
  {
//----
   New_Time = Time[0];
//----

   return(0);

int start()
  {
//----
   Fun_New_Bar();
if (New_Bar == false)
   return;
else Alert("new bar");
//----
   return(0);

  }

void Fun_New_Bar()
  {
   New_Bar = false;
   if (New_Time!= Time[0])
      {
       New_Time = Time[0];
       New_Bar = true;
      }
  }

And if you need different time frame use something like this:

iTime (Symbol(), PERIOD_M1 ,0))

instead Time[0].


This one is my first and shortest option.

 
dvarius:

Hi,


First off all thank you all for your posts and help in coding.

As in this topic in my opinion best works this:



And if you need different time frame use something like this:

instead Time[0].


This one is my first and shortest option.


Doesn't this do the same thing?

It is more concise and dispenses with the unnecessary variable New_Bar

datetime New_Time;

int init()
  {
//----
   New_Time = Time[0];
//----

   return(0);

int start()
  {
//----
  
if (Fun_New_Bar() == false)
   return(0);
else Alert("new bar");
//----
   return(0);

  }

bool Fun_New_Bar()
  {
   if (New_Time!= Time[0])
      {
      New_Time = Time[0];
      return(true);
      }
   else
      return(false);
  }
 
GumRai:


Doesn't this do the same thing?

It is more concise and dispenses with the unnecessary variable New_Bar

In my opinion it's bad practice to use a function for this . . . you can only use it once per tick within start() if you call it a second time on the same tick, the same tick that is the start of a new bar, it will return false . . .
Reason: