bool variable for new bar

 

Hi, I need to set up a bool variable that is true when a new bar is started, for instance when the 10.00 AM bar is closed and the 11.00 AM is started. What do you think about this code?

bool isNewBar()

{
static datetime lastbar=0;
datetime curbar = Time[0];
if(lastbar!=curbar)
{
lastbar=curbar;
return (true);
}

else
{
return(false);
}

}

 

AJ

static datetime lastbar;

init()
{

lastbar=0;

}


bool isNewBar() 
{ 
datetime curbar = Time[0]; 

if(lastbar!=curbar) 
   { 
    lastbar=curbar; 
    return (true); 
   } 

else 
  { 
    return(false); 
  } 

}

But...whats wrong with <the first tick of the new bar>?

if(Volume[0]=1) // is first tick of new so go for it
{

   // Do new bar stuff here

}

FWIW

-BB-

 

> But...whats wrong with <the first tick of the new bar>?

After much testing across many servers & brokers, you should not rely on Volume[n] for anything

Any particular tick can get lost by server or network issues, also if your EA is 'busy' processing some prior tick when first tick comes in, then your in trouble

And finally, its not impossible that (theoretically of course) an unscrupulous broker might suppress first tick if you have open positions

static datetime LastTradeBarTime;




int init()
  {
//----


  LastTradeBarTime = Time[1]; // initialise the variable




//----
   return(0);
  }


start()
{

  // Do <any tick> code here such as trailing stops etc





  if (LastTradeBarTime == Time[0]) return(0); // Not <first detected tick> on this bar so quit

  else LastTradeBarTime = Time[0]; // and continue

  // Do <once per bar> trading stuff here




   return (0);
  }

FWIW

-BB-

 
BarrowBoy:
static datetime lastbar;

init()
{

lastbar=0;

}


bool isNewBar() 
{ 
datetime curbar = Time[0]; 

if(lastbar!=curbar) 
   { 
    lastbar=curbar; 
    return (true); 
   } 

else 
  { 
    return(false); 
  } 

}

Hi BB,

I'm wondering about making this into a ''canned' included file' and using it in 'modular programming' and calling it as a SubRoutine whenever it is needed?

I don't have enough experience yet to know if it is feasible and how to do it and my plates already full with more elementary stuff, but I'm thinking that this is likely possible and would be very helpful and make it very quick and easy to use whenever it is needed?

Thanks (< 8)

 

FourX

From the 'New' button on the editor, create a new Include file (which will be created in the experts\include folder)

Paste this in

bool isNewBar() 
{ 
datetime curbar = Time[0]; 

if(lastbar!=curbar) 
   { 
    lastbar=curbar; 
    return (true); 
   } 

else 
  { 
    return(false); 
  } 

}

and save it - you can then use the isNewBar in any EA that has a reference to your include file at the top, e.g.

#include <FourXFunctions.mqh> 

Do not compile includes, unlike libraries ;)

Good Luck

-BB-

 
BarrowBoy:

FourX

From the 'New' button on the editor, create a new Include file (which will be created in the experts\include folder)

Paste this in

and save it - you can then use the isNewBar in any EA that has a reference to your include file at the top, e.g.

Do not compile includes, unlike libraries ;)

Good Luck

-BB-

While 'trying' to splice the initial code into my EA I and set a new personal 'best?' record:

152 Errors and 46 warnings!!! LoL

I'll try the 'Included' file approach.

I can't imagine it being worse than it is now.

 
BarrowBoy wrote >>

FourX

From the 'New' button on the editor, create a new Include file (which will be created in the experts\include folder)

Paste this in

and save it - you can then use the isNewBar in any EA that has a reference to your include file at the top, e.g.

Do not compile includes, unlike libraries ;)

Good Luck

-BB-



Going back to the original code, won't you run into a problem by initializing LastBar in init() if the user presses F7 and changes parameters? e.g. if EA runs on a one hour chart and trade is entered at 12:00, the user sees the trade at 12:30 and decides he/she wants to change one of the parameters. When parameters are changed and re-submitted, init() runs again and the EA gives another "IsNewBar" signal even though it's not 13:00 yet, possible causing a 2nd unwanted trade to be executed. You would have to do something in de-init(), or initialize global variable LastBar at decalaration instead of init() to prevent that from happening.
 
FourX:

While 'trying' to splice the initial code into my EA I and set a new personal 'best?' record:

152 Errors and 46 warnings!!! LoL

I'll try the 'Included' file approach.

I can't imagine it being worse than it is now.

I got the included file made, but when I compiled the EA it kept telling me that 'lastbar' was not defined.

I went through all of the possible permutations and combinations that I knew until I got it so that the 'isNewBar' was undefined etc (and would be removed from the executable).

So I stumbled my way through that until I got everything to compile properly.

It's still doing trade activities at any time however.

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//|                                                                  |
//| Copyright © 2010 BarrowBoy @ MQL4.Com            for FourX       |
//|   ALL RIGHT RESERVED !       http://4-X.CA       NewBar.mq4      |
//|                                              2010-05-17 @ 10:01  |
//|                                                                  |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#property copyright "Copyright © 2010 Doug R Henderson  ALL RIGHT RESERVED !"
#property link      "http://4-X.CA"

//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//|                                            defines               |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// #define MacrosHello   "Hello, world!"
// #define MacrosYear    2005


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//|                                           DLL imports            |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

#import "user32.dll"
   int      SendMessageA(int hWnd,int Msg,int wParam,int lParam);

// #import "my_expert.dll"
//   int      ExpertRecalculate(int wParam,int lParam);
// #import


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//| EX4 imports                                                      |
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

 #import "stdlib.ex4"
   string ErrorDescription(int error_code);
// #import


//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

// From the 'New' button on the editor, create a new Include file 
//       (which will be created in the experts\include folder)
// Then paste this in:

// void lastbar();
int lastbar;

bool isNewBar() 

{ 
datetime curbar = Time[0]; 
if(lastbar!=curbar) 
   { 
    lastbar=curbar; 
    return (true); 
   } 

else 
  { 
    return(false); 
  } 

}


/*
and save it - you can then use the isNewBar in any EA that has a reference to your include file at the top, e.g.

#include <FourXFunctions.mqh> 

Do not compile includes, unlike libraries ;)

Good Luck

- BB -
*/
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

isNewBar();  // Holds transactions until the start of a new bar.
             // From the included file: NewBar.mqh

Including ignoring the money management and placing an 'infinite' # of trades ASAP, or it would if it had infinite funds. So it ends up being 'stopped out' almost immediately.

So I tried it on some different version that are working: sorta. They are either closing out the trades IMMEDIATELY after it places them, or at the next hour (1 hour charts) even if it is not supposed to.

So obviously I've got other problems.

I'll keep poking n prodding away at it and let you know how it's going, or !not as the case may be.

Thanks for your help BB, VERY much appreciated! (< 8)

 
BarrowBoy:

AJ

But...whats wrong with <the first tick of the new bar>?

FWIW

-BB-

Here is the 'NewBar' mqh file to add to the code library.

.................. I already added it.

Files:
newbar.mqh  3 kb
 
BarrowBoy:

FWIW = ????

It is keeping placing new orders to the chart interval, but not the SL.

It seems to me that it was like this without the 'isNewBar' though I'm not sure.

This may have more to do with the structure of the EA than the included file though.

I have LOTS of other versions that don't have 'isNewBar' in them and even some that work! LoL (< 8)

I'll research it a little bit and narrow it down.

Reason: