stevehopwoodforex.com
https://www.stevehopwoodforex.com/phpBB3/
Print view

Coder challenge:Managing New Bars in Multipair/multiTF EA
https://www.stevehopwoodforex.com/phpBB3/viewtopic.php?t=813
Page 1 of 1
Author:  dietcoke [ Thu Sep 13, 2012 7:40 am ]
Post subject:  Coder challenge:Managing New Bars in Multipair/multiTF EA

I've written a function to flag a new bar on any timeframe.

Code: Select all

bool IsNewBar(int period=0)
{
	//+------------------------------------------------------------------+
	//| Returns true if it's a new bar 												|
	//+------------------------------------------------------------------+
   static int dt[] = {0,0,0,0,0,0,0,0,0};

   datetime now;
	int index;
	string tf;
	int curPeriod, nextPeriod;
	now	= TimeCurrent();

   switch( period )
   {
      case PERIOD_M1:   index = 0; tf = TFToStr(period); break;
      case PERIOD_M5:   index = 1; tf = TFToStr(period); break;
      case PERIOD_M15:  index = 2; tf = TFToStr(period); break;
      case PERIOD_M30:  index = 3; tf = TFToStr(period); break;
      case PERIOD_H1:   index = 4; tf = TFToStr(period); break;
      case PERIOD_H4:   index = 5; tf = TFToStr(period); break;
      case PERIOD_D1:   index = 6; tf = TFToStr(period); break;
      case PERIOD_W1:   index = 7; tf = TFToStr(period); break;
      case PERIOD_MN1:  index = 8; tf = TFToStr(period); break;
   }

	// if there is a saved gv then sync it with the index array
	if(gvc(tf)) dt[index] = gvg(tf);

	//get the current period start time from the array
	curPeriod	= dt[index];
	//calculate the next period start time
	nextPeriod	= MathMax(iTime(NULL,period,0),dt[index] + (60*period));

	//log("IsNewBar: Index= ",index,"dt[index]",curPeriod,nextPeriod,now,"Period=",TFToStr(period),"now >= nextPeriod",BoolToStr(now >= nextPeriod));

   if (now >= nextPeriod)
   {
      dt[index] = nextPeriod;
      gvs(tf,nextPeriod);
      log("IsNewBar: NewBar has started ",TimeToStr(now,TIME_DATE|TIME_MINUTES|TIME_SECONDS));
      return(true);
   }
   return(false);
}
It works great except for one thing which is that it depends on the first tick of a new bar on the ACTIVE chart which means that if I want to use the new bar flag on another pair, that pair may have either already seen a few ticks on the new bar or, more problematically for me, not yet have a tick for the new bar.

This causes problems when, for instance, I want the value of an indicator shifted to the bar just closed.

Has anybody got any ideas on how this might be tweaked to be accurate for all pairs, or better still, been there and done that?
Author:  dietcoke [ Fri Sep 14, 2012 1:04 pm ]
Post subject:  Re: Coder challenge:Managing New Bars in Multipair/multiTF E

I guess the question I need answering is:

Given That I know the time at which a new bar should start on any timeframe. How can I know when the first tick of the new bar has been received on any given pair???

Could I use iBars. I heard it's not reliable.

Looks like i forgot to post some other helper functions and globals the main function uses.

Code: Select all

//Globals
			bool 		isnewM1bar				;
			bool		isnewM5bar				;
			bool 		isnewM30bar				;
			bool		isnewH4bar				;
			bool 		isnewM15bar				;
			bool		isnewD1bar				;
			bool		isnewH1bar				;
			bool		isnewW1bar				;
			bool		isnewMN1bar				;				;


string TFToStr(int period)
{
   string tf;
   switch(period)
   {
      case PERIOD_M1:	tf = "M1"; break;
      case PERIOD_M5:	tf = "M5"; break;
      case PERIOD_M15:	tf = "M15";break;
      case PERIOD_M30:	tf = "M30";break;
      case PERIOD_H1:	tf = "H1";	break;
      case PERIOD_H4:	tf = "H4"; break;
      case PERIOD_D1:	tf = "D1";	break;
      case PERIOD_W1:	tf = "W1";	break;
      case PERIOD_MN1:	tf = "MN";
   }
   return(tf);
}

double gvg(string name, string prefix = "")
{
   name = prefix + name;
   return (GlobalVariableGet(name));
}

void gvs(string name, double value)
{
   GlobalVariableSet(name, value);
}

bool gvc(string name)
{
   return (GlobalVariableCheck(name));
}


Author:  NeoTrader [ Fri Sep 14, 2012 1:48 pm ]
Post subject:  Re: Coder challenge:Managing New Bars in Multipair/multiTF E

hi dietcoke,

why you don't use it like a ordinary IsNewBar Function?

eg.

Code: Select all

bool IsNewBar( int tf = 0 ) {
	
	static int prevTime[] = {0,0,0,0,0,0,0,0};
	bool newBar = false;
	int index;
	
	switch( tf ) {
		case PERIOD_M1:	index = 0; break;
		case PERIOD_M5:	index = 1; break;
		case PERIOD_M15: index = 2; break;
		case PERIOD_M30: index = 3; break;
		case PERIOD_H1: index = 4; break;
		case PERIOD_H4:	index = 5; break;
		case PERIOD_D1:	index = 6; break;
		case PERIOD_W1:	 index = 7; break;
		case PERIOD_MN1: index = 8; break;
	}

	if ( iTime( NULL, tf, 0 ) != prevTime[index] ) {
		newBar = true;
		prevTime[index] = iTime( NULL, tf, 0 );
	}
	
	return( newBar );
}
I don't tested this...was just an idea...

related to your tick problem...have you tried an external tick simulator like Empty4-ticker? This is a freeware tool that is linked also here on the forum...or search via google.
I use it usually on weekends for testing...but theoretically it should also work in normal trading hours...
also just an idea..

happy trading,

NeoTrader

-
Author:  dietcoke [ Mon Sep 24, 2012 5:40 pm ]
Post subject:  Re: Coder challenge:Managing New Bars in Multipair/multiTF E

NeoTrader wrote:hi dietcoke,

why you don't use it like a ordinary IsNewBar Function?

eg.

Code: Select all

bool IsNewBar( int tf = 0 ) {
	
	static int prevTime[] = {0,0,0,0,0,0,0,0};
	bool newBar = false;
	int index;
	
	switch( tf ) {
		case PERIOD_M1:	index = 0; break;
		case PERIOD_M5:	index = 1; break;
		case PERIOD_M15: index = 2; break;
		case PERIOD_M30: index = 3; break;
		case PERIOD_H1: index = 4; break;
		case PERIOD_H4:	index = 5; break;
		case PERIOD_D1:	index = 6; break;
		case PERIOD_W1:	 index = 7; break;
		case PERIOD_MN1: index = 8; break;
	}

	if ( iTime( NULL, tf, 0 ) != prevTime[index] ) {
		newBar = true;
		prevTime[index] = iTime( NULL, tf, 0 );
	}
	
	return( newBar );
}
I don't tested this...was just an idea...

related to your tick problem...have you tried an external tick simulator like Empty4-ticker? This is a freeware tool that is linked also here on the forum...or search via google.
I use it usually on weekends for testing...but theoretically it should also work in normal trading hours...
also just an idea..

happy trading,

NeoTrader

-

Thanks for the response neo.

The problem is this, and I think it will have to be resolved in any multipair autotraders.

If I'm using a single isnewBar variable to determine the start of a new bar. The plan is to calculate a bunch of indicators at the start of the new bar for all the pairs in the multipair group.


Lets say I calculate an indicator based on a shift of 1, intending to perfom an action based on the closing indicator value of the last bar.

What happens if one or more pairs in the group have not yet received a tick for the new bar?????

I've seen some odd things with indicator value during testing of Forex Robot and I can only put it own to the fact that indictors which are being calculated at bar start are calculating with the wrong shift sometimes. I can only put this down to CT4 not recognising a new bar until there is a tick for that pair

If that postulation is correct, it means you need a new bar flag for each pair.
Author:  NeoTrader [ Mon Sep 24, 2012 7:51 pm ]
Post subject:  Re: Coder challenge:Managing New Bars in Multipair/multiTF E

hi dietcoke,
dietcoke wrote:The problem is this, and I think it will have to be resolved in any multipair autotraders.

If I'm using a single isnewBar variable to determine the start of a new bar. The plan is to calculate a bunch of indicators at the start of the new bar for all the pairs in the multipair group.
ok... this would mean that you need an 2 dimensional array for all your pairs with the symbols in the 1st dimension and the barstatus per TF as the 2nd dimension.
dietcoke wrote:Lets say I calculate an indicator based on a shift of 1, intending to perfom an action based on the closing indicator value of the last bar.

What happens if one or more pairs in the group have not yet received a tick for the new bar?????
I think iBarShift could do the trick
int iBarShift( string symbol, int timeframe, datetime time, bool exact=false)
Search for bar by open time. The function returns bar shift with the open time specified. If the bar having the specified open time is missing, the function will return -1 or the nearest bar shift depending on the exact.
This should allow you to get the correct shift across all pairs.

hope this helps...

happy trading,

NeoTrader

-
All times are UTC Page 1 of 1