NinjaTrader Support Forum  

Go Back   NinjaTrader Support Forum > NinjaScript Development Support > Strategy Development

Strategy Development Support for the development of custom automated trading strategies using NinjaScript.

Reply
 
Thread Tools Display Modes
Old 07-17-2012, 11:01 AM   #1
Spiderbird
Member
 
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
Question FirstTickofBar Variables - Incrementing Twice?

Hi all,

I have a variable I'm attempting to increment from 0 when a certain condition is achieved. It seems to work, but since it's in the "if (FirstTickOfBar)" area, it increments it twice. I discovered this in the Log file after going through a market replay session.

Here's what the code looks like. FYI, COBC is false.

if (FirstTickOfBar)
{

if (volume > volMA_14 * 2)
{
volumeSpikes++;
Print("volumeSpikes is now " + volumeSpikes + ".");
}
}

When this condition is met, volumeSpikes shows up in two log entries.
It first says "volumeSpikes is now 1" on the first line, with a timestamp of 9:57 AM.
The second says "volumeSpikes is now 2" on the second line, with a timestamp of 9:57 AM.

Any ideas on this one? Thanks in advance for any advice and your time!

Yours,
Spider
Spiderbird is offline  
Reply With Quote
Old 07-17-2012, 11:15 AM   #2
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,860
Thanks: 163
Thanked 580 times in 571 posts
Default

Spider,

Do you see this on historical data, or real-time data?

I have ran this historically and only see one instance true at this time on the output window.

What log are you referring to? Are only logging to the output window, or else where?

Is it possible you have two instances of the same script running, resulting in two conditions at the same time?
NinjaTrader_Matthew is offline  
Reply With Quote
Old 07-17-2012, 02:50 PM   #3
Spiderbird
Member
 
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
Default

Hi Matthew,

I'm running my strategy on recorded data taken during a trading session (I had my computer on, recording the day session with the Market Recorder). I had captured a full day of data tracking SPY.

When I typed 'Log', I meant the output window. My mistake there. In fact, I modified the Print scripts to give more information and show what I mean. Here's the new text:

The number for Bars.BarsSinceSession is 13.
volumeSpikes is now 1 at the following time (7/17/2012 3:47:02 PM).
---------------------------------------------------------------------------------------
For this volume spike, volume is 38000 and the 14 MA Volume * 2 is (31205).


The number for Bars.BarsSinceSession is 13.
volumeSpikes is now 2 at the following time (7/17/2012 3:47:02 PM).
---------------------------------------------------------------------------------------
For this volume spike, volume is 38000 and the 14 MA Volume * 2 is (31205).

And I checked. I'm not running two scripts at the same time. I did check to see if that volumeSpikes variable was increased in another part of the script, but it isn't.

I also looked further down the output window to find the following:

The number for Bars.BarsSinceSession is 14.
volumeSpikes is now 3 at the following time (7/17/2012 3:47:03 PM).
---------------------------------------------------------------------------------------
For this volume spike, volume is 49600 and the 14 MA Volume * 2 is (40271).


The number for Bars.BarsSinceSession is 14.
volumeSpikes is now 4 at the following time (7/17/2012 3:47:03 PM).
---------------------------------------------------------------------------------------
For this volume spike, volume is 49600 and the 14 MA Volume * 2 is (40271).

So it looks like it's definitely doubling, but why? Any ideas?
Spiderbird is offline  
Reply With Quote
Old 07-17-2012, 03:13 PM   #4
koganam
Senior Member
 
Join Date: Feb 2008
Location: Durham, North Carolina, USA
Posts: 3,356
Thanks: 24
Thanked 1,304 times in 1,067 posts
Send a message via Skype™ to koganam
Default

Quote:
Originally Posted by Spiderbird View Post
Hi Matthew,

I'm running my strategy on recorded data taken during a trading session (I had my computer on, recording the day session with the Market Recorder). I had captured a full day of data tracking SPY.

When I typed 'Log', I meant the output window. My mistake there. In fact, I modified the Print scripts to give more information and show what I mean. Here's the new text:

The number for Bars.BarsSinceSession is 13.
volumeSpikes is now 1 at the following time (7/17/2012 3:47:02 PM).
---------------------------------------------------------------------------------------
For this volume spike, volume is 38000 and the 14 MA Volume * 2 is (31205).


The number for Bars.BarsSinceSession is 13.
volumeSpikes is now 2 at the following time (7/17/2012 3:47:02 PM).
---------------------------------------------------------------------------------------
For this volume spike, volume is 38000 and the 14 MA Volume * 2 is (31205).

And I checked. I'm not running two scripts at the same time. I did check to see if that volumeSpikes variable was increased in another part of the script, but it isn't.

I also looked further down the output window to find the following:

The number for Bars.BarsSinceSession is 14.
volumeSpikes is now 3 at the following time (7/17/2012 3:47:03 PM).
---------------------------------------------------------------------------------------
For this volume spike, volume is 49600 and the 14 MA Volume * 2 is (40271).


The number for Bars.BarsSinceSession is 14.
volumeSpikes is now 4 at the following time (7/17/2012 3:47:03 PM).
---------------------------------------------------------------------------------------
For this volume spike, volume is 49600 and the 14 MA Volume * 2 is (40271).

So it looks like it's definitely doubling, but why? Any ideas?
Is your indicator exposing a double or int as a public variable?
koganam is offline  
Reply With Quote
The following user says thank you to koganam for this post:
Old 07-17-2012, 03:55 PM   #5
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,860
Thanks: 163
Thanked 580 times in 571 posts
Default

Is this a multi series script? Or do you only run on one data series?
NinjaTrader_Matthew is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Matthew for this post:
Old 07-17-2012, 05:03 PM   #6
Spiderbird
Member
 
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
Default

Quote:
Originally Posted by koganam View Post
Is your indicator exposing a double or int as a public variable?
Hi Koganam,

Your post prompted me to try the following variable declaration variations:

double volumeSpikes;
double volumeMFI;

public double volumeSpikes;
public double volumeMFI;

public int volumeSpikes;
public int volumeMFI;

int volumeSpikes;
int volumeMFI;

I compiled the code and re-applied the strategy to the replay chart, watching the output window. Same dynamic happens (volumeSpikes is incremented twice)

I read elsewhere in the forums that it could be an inherent problem with FirstTickOfBar, but I would have to look up that thread I found (I'm holding a newborn and typing with one hand.)

@Matthew - Good thought. I'm running the strategy on only one data series.
Spiderbird is offline  
Reply With Quote
Old 07-17-2012, 05:11 PM   #7
Spiderbird
Member
 
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
Default

Here's another thought... FirstTickOfBar is within a couple of other "if" sections. Here's the complete rundown:

protected override void OnBarUpdate()
{

(...)
if (ToTime(Time[0]) >= 83500 && ToTime(Time[0]) <= 144500)

(...)
// Note: else if below is part of a market position check (flat, short, long)
else if (Position.MarketPosition == MarketPosition.Short)
{

(...)
if (FirstTickOfBar)
{

(...)
if (volume > volMA_14 * 2)
{
volumeSpikes++;
Spiderbird is offline  
Reply With Quote
Old 07-18-2012, 07:16 AM   #8
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,860
Thanks: 163
Thanked 580 times in 571 posts
Default

Is this the only area you're using volumeSpikes++ - you don't check this if the position is flat or long either?

Can you try printing market position as well to ensure that you're only incrementing this variable when the strategy is short?

Ultimately, I'd suggest taking it out of any block other than 'FirstTickOfBar' to ensure it only gets called once as expect and then build it back from there.
NinjaTrader_Matthew is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Matthew for this post:
Old 07-20-2012, 08:18 AM   #9
Spiderbird
Member
 
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
Default Thanks!

Quote:
Originally Posted by NinjaTrader_Matthew View Post
Is this the only area you're using volumeSpikes++ - you don't check this if the position is flat or long either?

Can you try printing market position as well to ensure that you're only incrementing this variable when the strategy is short?

Ultimately, I'd suggest taking it out of any block other than 'FirstTickOfBar' to ensure it only gets called once as expect and then build it back from there.
Hi Matthew,

The code actually checks to see if the position is long, short or flat before checking volume, so that's in there. I did as you suggested and printed out my market position for visual reference and to verify where I was in the market.

Plus, all aspects of volumeSpikes++ are in those FirstTickOfBar areas, so that's also nixed.

The code below ended up being my final solution that works. I put a few more volume checks in there, but it does the trick by comparing a saved Time[0] variable and rechecking it in the "if" loop illustrated below.

Thanks to everyone for their help!

Yours,
Robert

Code:
else if (Position.MarketPosition == MarketPosition.Long)
				{
					if (FirstTickOfBar)
					{
						highPriceLine = High[HighestBar(High, barLookBack_inTrade)];
						lowPriceLine = Low[LowestBar(Low, barLookBack_inTrade)];
						
						DrawHorizontalLine("highPriceLine", false, highPriceLine, Color.DarkSeaGreen, DashStyle.Solid, 4);
						DrawHorizontalLine("lowPriceLine", false, lowPriceLine, Color.DarkRed, DashStyle.Solid, 4);
						
						/*  Calibration:  Check bar for Volume / MFI and set appropriate variables
							---------------------------------------------------------------------- */
						
						if (volumeTime != ToTime(Time[0]))
						{	volumeCheck = true;  }
						
						if (volumeCheck == true)
						{	
							if (mfi_5 > 95 && volume > volMA_14 * 3)	
							{
								volumeMFI++;
								BackColorSeries[1] = Color.CadetBlue;
								Print("MFI Hit");
							}
							
							else if (volume > volMA_14 * 3)
							{  
								volumeSpike3x++;
								BackColorSeries[1] = Color.CornflowerBlue;
								Print("3x Hit");
							}
							
							else if (volume > volMA_14 * 2)
							{  
								volumeSpike2x++;
								BackColorSeries[1] = Color.AliceBlue;
								Print("2x Hit");
							}
							
							volumeCheck = false;
							volumeTime = ToTime(Time[0]);
						}
Spiderbird is offline  
Reply With Quote
The following user says thank you to Spiderbird for this post:
Old 07-20-2012, 08:23 AM   #10
NinjaTrader_Matthew
NinjaTrader Customer Service
 
NinjaTrader_Matthew's Avatar
 
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,860
Thanks: 163
Thanked 580 times in 571 posts
Default

Glad you got it working and I appreciate you following up with your solution.
NinjaTrader_Matthew is offline  
Reply With Quote
Old 08-20-2012, 01:57 PM   #11
Spiderbird
Member
 
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
Default

Hi Matthew,

I still ran into multiple order problems, BUT, I did figure out that I had a Add(PeriodType.Minute) in my initialize section that caused the double run discussed above.

The offending code was this:
Code:
protected override void Initialize()
{
	CalculateOnBarClose = false;
	Unmanaged = true;
	TraceOrders = true;
	ExitOnClose = true;
			
	BarsRequired = 5;
	Add(PeriodType.Minute, 1); // This line was the culprit
			
	EntriesPerDirection = 2; 
	EntryHandling = EntryHandling.AllEntries;
}
I'll test tomorrow to see if that is also causing multiple orders to be submitted.
Spiderbird is offline  
Reply With Quote
The following user says thank you to Spiderbird for this post:
Old 08-20-2012, 02:03 PM   #12
NinjaTrader_AdamP
NinjaTrader Customer Service
 
NinjaTrader_AdamP's Avatar
 
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
Default

Spider,

Please feel free to contact us should you require additional assistance.
NinjaTrader_AdamP is offline  
Reply With Quote
Reply

Tags
calculateonbarclose, firsttickofbar, increment, timestamp, variable

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Price of FirstTickOfBar sgordet Strategy Development 1 01-04-2011 03:35 AM
Need some help with FirstTickOfBar grd974 Indicator Development 3 02-22-2010 07:17 AM
FirstTickOfBar palinuro Miscellaneous Support 16 12-23-2009 07:22 AM
FirstTickOfBar PrTester Version 7 Beta General Questions & Bug Reports 8 12-16-2009 02:08 PM
FirstTickOfBar DaveS General Programming 4 07-03-2009 10:58 AM


All times are GMT -6. The time now is 08:40 PM.