![]() |
|
|||||||
| Strategy Development Support for the development of custom automated trading strategies using NinjaScript. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Member
Join Date: May 2008
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
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? |
|
|
|
|
|
#2 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#3 |
|
Member
Join Date: May 2008
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
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!! |
|
|
|
|
|
#4 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
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.
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#5 |
|
Member
Join Date: May 2008
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
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. |
|
|
|
|
|
#6 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
Please double check that the execution on chart in all cases is on the 10:34 bar.
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#7 |
|
Member
Join Date: May 2008
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
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. |
|
|
|
|
|
#8 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
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
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#9 | |
|
Member
Join Date: May 2008
Posts: 42
Thanks: 0
Thanked 0 times in 0 posts
|
Quote:
|
|
|
|
|
|
|
#10 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
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.
Dierk
NinjaTrader Customer Service |
|
|
|
|
|
#11 |
|
Junior Member
Join Date: Jan 2008
Posts: 3
Thanks: 0
Thanked 0 times in 0 posts
|
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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
|
|
|
|
#12 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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();
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#13 |
|
Junior Member
Join Date: Jan 2008
Posts: 3
Thanks: 0
Thanked 0 times in 0 posts
|
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)); } } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~ |
|
|
|
|
|
#14 |
|
NinjaTrader Product Manager
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 107 times in 70 posts
|
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.
Josh
NinjaTrader Customer Service |
|
|
|
|
|
#15 |
|
Administrator
Join Date: Mar 2005
Location: Bamberg, Germany
Posts: 9,994
Thanks: 0
Thanked 6 times in 6 posts
|
To make sure we all are on the same page: please double check your are on latest 6.5.1000.4. Thanks
Dierk
NinjaTrader Customer Service |
|
|
|
![]() |
| Thread Tools | |
| Display Modes | |
|
|
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 |