![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
|
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 |
|
|
|
|
|
#2 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,860
Thanks: 163
Thanked 580 times in 571 posts
|
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?
Matthew
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
|
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? |
|
|
|
|
|
#4 | |
|
Senior Member
|
Quote:
|
|
|
|
|
|
The following user says thank you to koganam for this post: |
|
|
|
#5 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,860
Thanks: 163
Thanked 580 times in 571 posts
|
Is this a multi series script? Or do you only run on one data series?
Matthew
NinjaTrader Customer Service |
|
|
|
|
The following user says thank you to NinjaTrader_Matthew for this post: |
|
|
|
#6 | |
|
Member
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
|
Quote:
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. |
|
|
|
|
|
|
#7 |
|
Member
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
|
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++; |
|
|
|
|
|
#8 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,860
Thanks: 163
Thanked 580 times in 571 posts
|
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.
Matthew
NinjaTrader Customer Service |
|
|
|
|
The following user says thank you to NinjaTrader_Matthew for this post: |
|
|
|
#9 | |
|
Member
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
|
Quote:
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]);
}
|
|
|
|
|
|
The following user says thank you to Spiderbird for this post: |
|
|
|
#10 |
|
NinjaTrader Customer Service
Join Date: Apr 2010
Location: Denver, CO, USA
Posts: 4,860
Thanks: 163
Thanked 580 times in 571 posts
|
Glad you got it working and I appreciate you following up with your solution.
Matthew
NinjaTrader Customer Service |
|
|
|
|
|
#11 |
|
Member
Join Date: Apr 2012
Posts: 67
Thanks: 27
Thanked 8 times in 7 posts
|
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;
}
|
|
|
|
|
The following user says thank you to Spiderbird for this post: |
|
|
|
#12 |
|
NinjaTrader Customer Service
Join Date: Aug 2011
Location: Denver, CO, USA
Posts: 2,895
Thanks: 241
Thanked 375 times in 365 posts
|
Spider,
Please feel free to contact us should you require additional assistance.
Adam P.
NinjaTrader Customer Service |
|
|
|
![]() |
| Tags |
| calculateonbarclose, firsttickofbar, increment, timestamp, variable |
| Thread Tools | |
| Display Modes | |
|
|
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 |