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 05-07-2008, 01:53 AM   #1
michaelbb
Member
 
Join Date: May 2008
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default BarsSinceEntry() real-time vs. Historical

I get different result from BarSinceEntry() in real-time vs. historical data. In real-time I get the entrybar with BarSinceEntry() == 1 / on historical data with BarSinceEntry() == 0 ( CalculateOnBarClose is true).

My goal is to set profit target to BE +/- 1 if entry bar is not moving (close equal or below/above close of signal bar).

My test code for illustration (OnBarUpdate Method):

...
int barSinceEntryLimit = 0;
if (!Historical)
barSinceEntryLimit = 1;
if (isLongTrade && BarsSinceEntry() == barSinceEntryLimit && ( Close[0] <= Close[1] ))
beExitLong = true;
if (isShortTrade && BarsSinceEntry() == barSinceEntryLimit && ( Close[0] >= Close[1] ))
beExitShort = true;
...

Can someone please explain why BarsSinceEntry() works different on historical / real-time data?
michaelbb is offline  
Reply With Quote
Old 05-07-2008, 02:45 AM   #2
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

It depends on when your entry order was actually submitted/filled. Please see this article about the discrepancies between real-time and historical: http://www.ninjatrader-support.com/H...sBacktest.html
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-07-2008, 04:09 AM   #3
michaelbb
Member
 
Join Date: May 2008
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

Dont understand your answer. Link to does not help at all.

Did some debugging to understand what is going on.

From my point of view BarsSinceEntry() is not working correct.

Example:

Signal bar is the bar ending 10:34 (EntryLong is called on barclose of this bar).

In backtest order if filled at 10:34:00 / in market replay order is filled at 10:34:00 / in real time order is filled at 10:34:01.

So entry bar is 10:38 bar in my understanding?

On bar close of 10:38 bar next OnBarUpdate is executed. At this time in backtest BarsSinceEntry() is 0 / in market replay and in real-time BarsSinceEntry() is 1 (real-time not tested / debugged but it would explain error I got yesterday) .

In my opinion BarsSinceEntry() should be 0 on 10:38 bar. But I understand that at processing of OnBarUpdate the 10:42 bar has already started (OnBarUpdate is fired on first tick of 10:42 bar?). So its ok as well if you think that it should be 1. But then it should also be 1 in backtest!!
michaelbb is offline  
Reply With Quote
Old 05-07-2008, 07:18 AM   #4
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

BarsSinceEntry historical and realtime is the same value: the numbers of bars between when an execution occurred and "CurrentBar". Please make sure you have CalculateOnBarClose=True on your real time tests.

Sample: Is the execution sits on the 10:34 bar of your 1 minute series (you could check on chart) and OnBarUpdate is processing the 10:37 bar, then BarsSinceEntry will be 3 real time and historical.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 05-07-2008, 07:59 AM   #5
michaelbb
Member
 
Join Date: May 2008
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

As I told you from my tests it is not the same!

Do you expect the same value or did you test it?

In your example at processing the 10:37 bar BarsSinceEntry() will return 3 on historical data and 4 on market replay or real-time. I assume that "processing the 10:37 bar" means the bar with Time[0] is 10:37 (CalculateOnBarClose=True).

That is result of my tests/debugging.
michaelbb is offline  
Reply With Quote
Old 05-07-2008, 09:01 AM   #6
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

Please double check that the execution on chart in all cases is on the 10:34 bar.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 05-07-2008, 09:34 AM   #7
michaelbb
Member
 
Join Date: May 2008
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

As posted above (was an 4 min Chart):
- EntryLong is called in OnBarUpdate (CalculateOnBarClose=True) of bar with Time[0] == 10:34 for backtest / market replay / real-time
- Execution is at 10:34:00 / 10:34:00 / 10:34:01 (this is bar with Time[0] == 10:38)
- at OnBarUpdate Event of this bar (10:38) return value of BarsSinceEntry() Method is 0/1/1

Backtest and market replay return values of BarSinceEntry() from debugger.
michaelbb is offline  
Reply With Quote
Old 05-07-2008, 11:33 AM   #8
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

To double check. You observe this behavior:
- backtest: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 0
- market replay: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 1
- real time: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 1

Please verify and confirm. Thanks
NinjaTrader_Dierk is offline  
Reply With Quote
Old 05-07-2008, 12:09 PM   #9
michaelbb
Member
 
Join Date: May 2008
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Dierk View Post
To double check. You observe this behavior:
- backtest: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 0
- market replay: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 1
- real time: on chart you see the execution at the 10:34 bar, BarsSinceEntry on OnBarUpdate for the 10:38 bar yields 1

Please verify and confirm. Thanks
Execution is on 10:38 bar for backtest, market replay and real-time - rest is correct
michaelbb is offline  
Reply With Quote
Old 05-07-2008, 12:47 PM   #10
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

Thanks for clarification and for reporting this. This is a bug which will be fixed with next update: real time and replay will yield 0 like back test in scenarios below.
NinjaTrader_Dierk is offline  
Reply With Quote
Old 07-14-2008, 02:33 AM   #11
chehming
Junior Member
 
Join Date: Jan 2008
Posts: 3
Thanks: 0
Thanked 0 times in 0 posts
Default BrasSinceEntry() real-time vs. Historical

I still find that BarsSinceEntry() function returns differenet results between historical data(backtesting) and real-time data. Possibly, it perform abnormally during the real-time situation.

For Example :
I try to implement "Time-based Exit" using BarsSinceEntry() function. But, the problem is during the real-time testing, it always fails to RESET and calcuate Bars since last entry. Actually, it aggregate the number of bars since very First entry.




Setup : 1. MACD crossover for Long/Short trigger point.
2. Fixed Time Exit. Set 5minutes(==300 seconds)

Please check the attched pictures and below OutPut window. You will find that "Time-based Exit" function perform correctly only one time.



OutPut Window
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
03:54:05.9132500ES 09-08 Globex - Short( 1 ) 1.202 Position Waiting
Current Minute :52
~~~~~TimeOut~~~~~ 300
~~~~~TimeOut~~~~~ 301
~~~~~TimeOut~~~~~ 302
~~~~~TimeOut~~~~~ 303
~~~~~TimeOut~~~~~ 304
~~~~~TimeOut~~~~~ 305
~~~~~TimeOut~~~~~ 306
~~~~~TimeOut~~~~~ 307
~~~~~TimeOut~~~~~ 308
~~~~~TimeOut~~~~~ 309
~~~~~TimeOut~~~~~ 310
~~~~~TimeOut~~~~~ 311
~~~~~TimeOut~~~~~ 312
~~~~~TimeOut~~~~~ 313
03:54:06.5538750ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
Current Minute :54
03:55:53.3351250ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
Current Minute :56
03:57:52.8507500ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
Current Minute :58
03:59:52.9913750ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
Current Minute :0
04:01:52.7257500ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
Current Minute :2
04:04:09.1007500ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
Current Minute :4
04:05:54.4913750ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
Current Minute :6
04:05:54.4913750ES 09-08 Globex - CrossAbove Checking
04:05:55.6945000ES 09-08 Globex - ENTRY Long_Cross
~~~~~TimeOut~~~~~ 453
04:08:21.3351250ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
Current Minute :8
04:09:52.5070000ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
Current Minute :10
04:11:56.0695000ES 09-08 Globex - Flat( 0 ) 0 Position Waiting
Current Minute :12
2008/7/14 上午 04:11:56ES 09-08 Globex - CrossBelow Checking
04:11:56.0695000ES 09-08 Globex - ENTRY Short_Cross
~~~~~TimeOut~~~~~ 489
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Attached Images
File Type: jpg Time_out.jpg (82.5 KB, 15 views)
chehming is offline  
Reply With Quote
Old 07-15-2008, 12:28 AM   #12
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Hi chehming,

I will investigate this for you. Can you clarify that this is what you are using for your exit?
Code:
if (BarsSinceEntry() > 5)
     ExitLong();
Or some variant of it? Thanks.
NinjaTrader_Josh is offline  
Reply With Quote
Old 07-15-2008, 12:48 AM   #13
chehming
Junior Member
 
Join Date: Jan 2008
Posts: 3
Thanks: 0
Thanked 0 times in 0 posts
Default BarsSinceEntry() real-time vs. Historical

Yes, it is similar. 300 seconds == 5 mins. Please test the code in real-
time situation.

THANKS.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

private int timeout = 300; // Default setting for TimeOut

protected override void Initialize()
{
CalculateOnBarClose = true;
Add(PeriodType.Second,1);
}



protected override void OnBarUpdate()
{


if (BarsInProgress == 1)

{

if (BarsSinceEntry(1,"CrossEntry",0) >= timeout && Positions
[1].MarketPosition ==
MarketPosition.Long )

{ timeoutOrder=ExitLongLimit(1,true,1,Closes[1]
[0],"TimeOut_Long","CrossEntry");
Print("~~~~~TimeOut~~~~~ " + BarsSinceEntry(1,"CrossEntry",0));
}

else if (BarsSinceEntry(1,"CrossEntry",0) >= timeout && Positions[1].MarketPosition

== MarketPosition.Short )
{ timeoutOrder=ExitShortLimit(1,true,1,Closes[1]

[0],"TimeOut_Short","CrossEntry");
Print("~~~~~TimeOut~~~~~ " + BarsSinceEntry(1,"CrossEntry",0));
}

}
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~
chehming is offline  
Reply With Quote
Old 07-15-2008, 01:49 AM   #14
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
Default

Please see my attached strategy. I am unable to produce problems with the exits in real-time on my end.

My output logs show it count from 0 up to 6 to exit (which is correct since CalculateOnBarClose is set to true).

Woops. Didn't see your earlier post. Will check that implementation too now.
Attached Files
File Type: zip BarsSinceEntryTest.zip (1.0 KB, 11 views)
NinjaTrader_Josh is offline  
Reply With Quote
Old 07-15-2008, 01:50 AM   #15
NinjaTrader_Dierk
Administrator
 
NinjaTrader_Dierk's Avatar
 
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
Default

To make sure we all are on the same page: please double check your are on latest 6.5.1000.4. Thanks
NinjaTrader_Dierk is offline  
Reply With Quote
Reply

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
Tick Filter Failed (Real Time & Historical) wayneFH Strategy Development 8 11-02-2011 09:22 AM
How real real-time DOM and TS ? maxima General Programming 5 05-01-2008 02:54 PM
Strategy Fails to Transition from Historical to Real Time wayneFH Strategy Development 5 04-07-2008 12:45 PM
Mixing real-time and historical data daven Connecting 1 12-07-2007 01:09 PM
Backfilling Forex Real-Time Charts with Historical Data JohnL Connecting 4 05-24-2007 11:42 AM


All times are GMT -6. The time now is 04:18 AM.