X

## Attention!

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

 NinjaTrader Support Forum Help With Strategy

 General Programming General NinjaScript programming questions.

 06-27-2011, 12:01 PM #1 JohnnyChimpo Junior Member   Join Date: May 2011 Posts: 21 Thanks: 9 Thanked 0 times in 0 posts Help With Strategy Hello, Here is the problem I'm trying to solve: I have 2 periods in my strategy: daily and minute. Every day (in the MINUTE period), right before market close (ie 2:15pm) I'd like to compute the next value of the DAILY MACD chain, ie. compute MACD based off all prior daily values and then the values at 2:15pm instead of waiting for the session close at 2:30pm. That way I could exit a position before waiting for the next morning. I cannot use CalculateOnBarClose=false as other parts of my strategy rely on keeping that true. I am very able with C# and have worked some with Bars/IBar etc, so I just need to know the easiest way to do this. Thanks, help is much appreciated. EDIT: Here is what I've tried: Code: ``` Bars test =BarsArray[dailyBars]; int index=0; DateTime lastTime = test.Get(0).Time; for (int i=0; i lastTime && test.Get(i).Time
 06-27-2011, 12:56 PM #2 NinjaTrader_Josh NinjaTrader Product Manager     Join Date: May 2007 Location: Denver, CO Posts: 17,458 Thanks: 1 Thanked 106 times in 70 posts JohnnyChimpo, The easiest way is to use CalculateOnBarClose = false. You can use this despite your requirement to use true in some parts of your code. What you can do is set it to false and then add some if-statements around the areas of code that you want it to be running in true mode. Code: ```if (FirstTickOfBar) { if (Close[1] > Open[1]) // Do something; }``` The opening event of a bar is the exact same event as the closing event of a bar. This means on the very first tick of a bar, our prior bar just closed so if we used [1] index instead of [0] we will get the values of the bar that just closed. This is the exact same equivalent of CalculateOnBarClose = true. Josh NinjaTrader Customer Service Use Kinetick, NinjaTrader’s preferred market data service - Learn More Free online training events - View Schedule
 The following user says thank you to NinjaTrader_Josh for this post:
 06-27-2011, 01:11 PM #3 JohnnyChimpo Junior Member   Join Date: May 2011 Posts: 21 Thanks: 9 Thanked 0 times in 0 posts Thanks Josh, I'll implement that but one more question, how can I backtest CalculateOnBarClose=false, for example if my primary series period is set to 1 minute, and i set calculateonbarclose=false, when backtesting and I'm in my minute bars period ie: protected override void OnBarUpdate() { if (BarsInProgress == minuteBars) { // if I CHECK the MACD[dailyBars][0] will I get the up-to-minute result? or will it still be yesterdays? } }
 06-27-2011, 01:13 PM #4 NinjaTrader_Josh NinjaTrader Product Manager     Join Date: May 2007 Location: Denver, CO Posts: 17,458 Thanks: 1 Thanked 106 times in 70 posts Unfortunately you cannot backtest CalculateOnBarClose = false. Josh NinjaTrader Customer Service Use Kinetick, NinjaTrader’s preferred market data service - Learn More Free online training events - View Schedule
 06-27-2011, 01:16 PM #5 JohnnyChimpo Junior Member   Join Date: May 2011 Posts: 21 Thanks: 9 Thanked 0 times in 0 posts Ugh, no work around? That's why I was attempting to create my own set of Bars. I really need the ability to back test and optimize strategies on large amounts of historical data. Is there anything else you can think of?
 06-27-2011, 01:36 PM #6 NinjaTrader_Josh NinjaTrader Product Manager     Join Date: May 2007 Location: Denver, CO Posts: 17,458 Thanks: 1 Thanked 106 times in 70 posts Probably easiest would be to add a secondary Bar series of a lower time frame and trade close to the close of the day? Maybe just have it trade at 2:14:59 or something like that. Josh NinjaTrader Customer Service Use Kinetick, NinjaTrader’s preferred market data service - Learn More Free online training events - View Schedule
06-27-2011, 01:40 PM   #7
JohnnyChimpo
Junior Member

Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts

Quote:
 Originally Posted by NinjaTrader_Josh Probably easiest would be to add a secondary Bar series of a lower time frame and trade close to the close of the day? Maybe just have it trade at 2:14:59 or something like that.
I thought about that, but wouldn't that mean it would be using incorrect values for prior days? Like it would be using the prior days values at 2:14:59 instead of the actual close for those days? I want to use the actual closes for past days and the 2:14:59 value for the current day.

 06-27-2011, 01:59 PM #8 NinjaTrader_Josh NinjaTrader Product Manager     Join Date: May 2007 Location: Denver, CO Posts: 17,458 Thanks: 1 Thanked 106 times in 70 posts For prior days you can just use PriorDayOHLC() and it would always be accurate values. Josh NinjaTrader Customer Service Use Kinetick, NinjaTrader’s preferred market data service - Learn More Free online training events - View Schedule
 The following user says thank you to NinjaTrader_Josh for this post:
 06-27-2011, 02:33 PM #9 JohnnyChimpo Junior Member   Join Date: May 2011 Posts: 21 Thanks: 9 Thanked 0 times in 0 posts Josh, I think I still have a problem though, so lets say I have 2 MACD indicators now: MACD(DailyPeriod) and MACD(slightyLessThanDailyPeriod) The MACD(slightlyLessThanDailyPeriod) will still be using the wrong close for all prior days right? So the PriorDayOHLC() thing doesn't really help me in an indicator? Unless I'm understanding wrong. Thanks again.
 06-27-2011, 02:40 PM #10 NinjaTrader_Josh NinjaTrader Product Manager     Join Date: May 2007 Location: Denver, CO Posts: 17,458 Thanks: 1 Thanked 106 times in 70 posts You can't call MACD at all. That would require you to run it on a DataSeries which you just don't have one with the values you are looking for. Sure you can add a daily bars object to your script and run MACD on that Bars object, but that doesn't solve your issue with the current day. This means you just need to mimic the logic and just run it through values you do have which can be acquired through PriorDayOHLC() and your lower time frame series for the current day. Josh NinjaTrader Customer Service Use Kinetick, NinjaTrader’s preferred market data service - Learn More Free online training events - View Schedule
06-27-2011, 03:10 PM   #11
JohnnyChimpo
Junior Member

Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts

Quote:
 This means you just need to mimic the logic
Is it possible to look at the MACD indicator source code somewhere?

 06-27-2011, 03:28 PM #12 NinjaTrader_Josh NinjaTrader Product Manager     Join Date: May 2007 Location: Denver, CO Posts: 17,458 Thanks: 1 Thanked 106 times in 70 posts You should be able to open up the code for any of the indicators by going Edit NinjaScript > Indicators and then selecting the one you want to look at. Josh NinjaTrader Customer Service Use Kinetick, NinjaTrader’s preferred market data service - Learn More Free online training events - View Schedule
06-28-2011, 07:19 PM   #13
JohnnyChimpo
Junior Member

Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts

Quote:
 Originally Posted by NinjaTrader_Josh You should be able to open up the code for any of the indicators by going Edit NinjaScript > Indicators and then selecting the one you want to look at.
I looked at this and it seems super complex, especially when dealing with multiple periods on say the Stochastics indicator. Is there any unofficial work around for simulating CalculateOnBarClose=false ? I'm desperate at this point.

 06-29-2011, 03:26 AM #14 NinjaTrader_Bertrand NinjaTrader Customer Service     Join Date: Sep 2008 Location: Germany Posts: 22,411 Thanks: 252 Thanked 976 times in 959 posts You would need to recode the logic needed based on a much lower granular series - for example a 100 Period SMA on 1 min chart approximates a 20 Period one on a 5 min chart. Bertrand NinjaTrader Customer Service Use Kinetick, NinjaTrader’s preferred market data service - Learn More Free online training events - View Schedule
06-29-2011, 09:51 AM   #15
JohnnyChimpo
Junior Member

Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts

Quote:
 Originally Posted by NinjaTrader_Bertrand You would need to recode the logic needed based on a much lower granular series - for example a 100 Period SMA on 1 min chart approximates a 20 Period one on a 5 min chart.
Hi Bertrand,

Thanks for the reply. I've been spinning my wheels on this for the past couple days. With that solution I think we'd still have this problem:

Say we have a daily period and a minute period (the lower granular series).

Say we set the interval of the minute period to 385 minutes (5 minutes before close, just under 6.5 hours which is the daily period in my template). If I compute the MACD of the minute period, MACD will use past values from the lower granular series (5 minutes prior to close) and won't use the actual close/high/low/volume values from previous days.

In a nutshell, here's the problem:

I want to simulate CalculateOnBarClose=false so that, 5 minutes before market close I want to compute the next value of my daily MACD so I can exit if necessary before the next day gaps down.

^^ There has to be some way to solve this!

Pretty straight forward problem but I cannot figure out any way to replicate the daily bars and insert a new one etc. (you can add/remove to the end of the bars array but not insert a bar into the correct position)

I love your software but this is a major limitation to backtesting and it would take forever to gather enough data live to see if this is working correctly or not.

Thanks again, appreciate the brainstorming.
Last edited by JohnnyChimpo; 06-29-2011 at 09:56 AM.

 Thread Tools Display Modes Linear Mode

 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 Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home News and Announcements     News and Announcements Application Technical Support     ATM Strategies (Discretionary Trading)     Automated Trading     Charting     Connecting     Installation and Licensing     Market Analyzer     Miscellaneous Support         Historical Version 7 Beta Threads             Critical Information - Please review             Version 7 Beta General Questions & Bug Reports         Historical NinjaTrader 6.5 Archive             Historical NinjaTrader 6.5 Beta Threads     Strategy Analyzer     SuperDOM and other Order Entry Windows NinjaScript Development Support     General Programming     Indicator Development     Strategy Development NinjaScript Educational Resources     Reference Samples     Tips NinjaScript File Sharing     NinjaScript File Sharing Discussion     Version 7 Indicators     Version 7 Strategies     Version 7 Miscellaneous Suggestions and Feedback     Suggestions And Feedback

All times are GMT -6. The time now is 02:14 PM.

 -- Fixed Width ---- Fluid Width NinjaTrader Support Forum - Archive - Top