NinjaTrader Support Forum  
X

Attention!

This website will be down for maintenance from Friday May 24th at 6PM MDT until Sunday May 26th at 12PM MDT. We apologize for the inconvenience. If you need assistance during this time, please email sales@ninjatrader.com


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-21-2009, 12:23 PM   #1
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default Calculate On Bar Close - Unexpected Results

NT Support:

I am currently running a test using the sample SMA Strategy with CalculateOnBarClose set to False.

It seems to work as expected, but I noticed something was not consisent with the expected behavior.

A CrossBelow was fire, and the system went short. This was expected. Then after a few bars, the current bar with the issue spiked up thus causing a CrossAbove and getting Long, but then after the bar closed, you can see that it did not really CrossAbove (the sma) and it left the trade Long when it should clearly be straight. Only after a stop loss did it get out of the trade.

So again, position was Short.

Some bars go by and the SMA is slowly creeping up with the fast catching up to the slow.

A during one of the later bars, a spike up happened, firing a CrossAbove IntraBar and the Closed Current Position, and put the system Long 1.

(all fine up until this point).

Now intrabar we also didn't hold that spike and the downtrend continued, the SMA is clearly inversed to the downside and we did not get put back short again, instead it left us long even though it clearly crossed back below intrabar (fast cut down through the slow).

Could you elaborate in regards to how this might occur and propose a solution to preventing it other than COBC = True?


Thank you in advance,
r2kTrader is offline  
Reply With Quote
Old 05-21-2009, 01:18 PM   #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

The condition was evaluated to true and so it placed a trade, just because the condition is no longer true does not mean it will close that trade. Works as expected. There is no logic saying if the condition is all of a sudden not true to place a closing trade.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-21-2009, 02:44 PM   #3
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
The condition was evaluated to true and so it placed a trade, just because the condition is no longer true does not mean it will close that trade. Works as expected. There is no logic saying if the condition is all of a sudden not true to place a closing trade.
Josh,

I understand. But I do not believe your answer applies to my question per se. If we fired a crossAbove intrabar during an up spike, then it should put us long, got that. Likewise, when the spike dies and then reverses, the SMA is now inverted with fast back below the slow (short), but it DIDN'T FIRE a trade reflecting that. I checked the logs and orders.

Something doesn't make sense here. I believe there is a logical explanation, but the one you are giving me I don't believe applies here :-(
r2kTrader is offline  
Reply With Quote
Old 05-21-2009, 03:08 PM   #4
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

r2kTrader,

CrossAbove/Below conditions are determined based on the lookback period set and the latest tick information. It does not compare latest tick versus previous tick. You are likely looking at the previous bar's close versus the current tick. If it crosses above then it is true, if it falls back down on the same bar, the cross below is still NOT true because there was no cross below based on the previous bar. As such, no short trade will be placed.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-21-2009, 05:49 PM   #5
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Josh,

This was helpful. So I want to be sure I get this correct.

Note: Calc On Bar Close = False

Based on the fact the COBC = false, why would it be looking at the prior bar? My lookback is the default, which is 1 bar I assume. I am not saying if price Crosses above, I am saying if SMA value crosses, so if it crossed above, didn't it cross below when it resumed going lower?

I kind of understand what you are saying, but I don't see how we can crossabove, and then not cross back below. Your logic makes sense to me with COBC = true, because we crossed above, but didn't close above, etc.

Bear in mind, as I look at the chart, the final plot showed that the fast did not "close" above the slow, rather the price action visited above on that spike and retraced.

Thanks


Quote:
Originally Posted by NinjaTrader_Josh View Post
r2kTrader,

CrossAbove/Below conditions are determined based on the lookback period set and the latest tick information. It does not compare latest tick versus previous tick. You are likely looking at the previous bar's close versus the current tick. If it crosses above then it is true, if it falls back down on the same bar, the cross below is still NOT true because there was no cross below based on the previous bar. As such, no short trade will be placed.
r2kTrader is offline  
Reply With Quote
Old 05-22-2009, 07:09 AM   #6
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

r2kTrader,

CrossAbove/Below does not look at the last tick versus the prior tick. It looks at last tick versus prior bar. This is the way it has always worked. You are checking for a condition to cross above condition to be true on a bar. If it moved above on that bar it is true. If it now moves back down it is now back to false. Becoming false again does not trigger a cross below logic. This is what it is designed to do and it behaves as expected. If you want some more logic in there you need to self program it.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-22-2009, 05:45 PM   #7
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
r2kTrader,

CrossAbove/Below does not look at the last tick versus the prior tick. It looks at last tick versus prior bar. This is the way it has always worked. You are checking for a condition to cross above condition to be true on a bar. If it moved above on that bar it is true. If it now moves back down it is now back to false. Becoming false again does not trigger a cross below logic. This is what it is designed to do and it behaves as expected. If you want some more logic in there you need to self program it.
So CrossAbove works as follows?

SMA fast = 400
SMA slow = 390

Current bar has 3 minutes left on 5m bar. Currently Fast is above Slow and we are long 1 car.

during the bar, we get a pullback to say 380, this would fire a CrossBelow?

But if it reverts back to say 405, it won't fire a CrossAbove?

hmm, yeah, that makes perfect sense to the layman, lol.

I hear what you are saying. If I am understanding this correctly, as per the example, we started out with a CrossAbove under our best, hence crossAbove was true and put us in trade. But when we intrabar crossedBelow firing a false, it doesn't "undo" the CrossAbove, that value is still essentially true ? I hope you can see how this might be a bit confusing. I am sure with COBC=on, it's a little more black and white.

At least it is behaving as expected, thanks for the time to explain this to me and I hope it helps someone else.
r2kTrader is offline  
Reply With Quote
Old 05-23-2009, 02:43 PM   #8
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,421
Thanks: 252
Thanked 982 times in 964 posts
Default

r2kTrader, you're correct in understanding CrossAbove and CrossBelow are not connected in their outcomes, hence this can lead to whipsaws if you run this to update on every tick or CalculateOnBarClose = false. This is expected and you would need to add custom logic helping preventing your condition becoming true / false intrabar too many times.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 05-26-2009, 07:58 AM   #9
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default Calculate On Bar Close - Strategy with Indicator

Support,

Which takes precedence as I just noticed/realized that the SMA has a General Property for COBC.

If Strategy has COBC = false, but SMA Indicator has General Property set to COBC=True, will the SMA be aligned and subjected to its own property, or does the Strategy Property take precedence?

Thank you,


r2kTrader
r2kTrader is offline  
Reply With Quote
Old 05-26-2009, 08:01 AM   #10
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

r2kTrader,

Your indicators should not have any COBC line. There is a bug with it so please do not use any COBC lines except in the overarching script.

If IndicA calls IndicB, only IndicA should have a COBC line if at all.
If Strategy calls IndicA, only Strategy should have a COBC line if at all.
NinjaTrader_Josh is offline  
Reply With Quote
Old 05-26-2009, 12:12 PM   #11
r2kTrader
Senior Member
 
Join Date: Sep 2008
Posts: 483
Thanks: 0
Thanked 2 times in 2 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
r2kTrader,

Your indicators should not have any COBC line. There is a bug with it so please do not use any COBC lines except in the overarching script.

If IndicA calls IndicB, only IndicA should have a COBC line if at all.
If Strategy calls IndicA, only Strategy should have a COBC line if at all.
This explains the anomoly I was seeing.
r2kTrader is offline  
Reply With Quote
Old 01-22-2010, 08:24 PM   #12
Stevo
Junior Member
 
Join Date: Jan 2010
Posts: 19
Thanks: 0
Thanked 0 times in 0 posts
Default

Hi,
To my relief I just stumbled upon this thread and this seems to explain my NT 6.5 strategy's eratic behavior.

Does anyone know of a work around for this without resorting to the (un-backtestable) OnMarketData() method?

My strategy needs COBC = false
but the custom indicator that drives most of it needs COBC = true

Thank you in advance,
Stevo
Stevo is offline  
Reply With Quote
Old 02-19-2011, 10:27 AM   #13
Trader.Jon
Senior Member
 
Join Date: Dec 2008
Posts: 338
Thanks: 0
Thanked 0 times in 0 posts
Default Is this the same

I have noticed this in an indicator ... should it be commented out when used in a strategy??

indicator.CalculateOnBarClose = CalculateOnBarClose;
Trader.Jon is offline  
Reply With Quote
Old 02-22-2011, 11:03 AM   #14
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,421
Thanks: 252
Thanked 982 times in 964 posts
Default

Jon, this should be commented out if you plan on calling the indicator programmatically.
NinjaTrader_Bertrand 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
"Calculate on Bar Close" John833 General Programming 8 07-21-2009 05:13 AM
Calculate on bar close unclelou Charting 1 03-25-2009 05:43 PM
Calculate on Bar Close or not? Hugo1 Strategy Development 3 03-09-2009 07:53 AM
Unexpected results from script suedeuno Indicator Development 3 02-17-2009 04:38 AM
Question about Calculate on Bar close slowhand Strategy Development 8 02-13-2009 11:24 AM


All times are GMT -6. The time now is 11:32 PM.