NinjaTrader Support Forum  
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


Go Back   NinjaTrader Support Forum > NinjaScript Development Support > General Programming

General Programming General NinjaScript programming questions.

Reply
 
Thread Tools Display Modes
Old 06-27-2011, 12:01 PM   #1
JohnnyChimpo
Junior Member
 
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
Default 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<test.Count; i++)
								{
									if (test.Get(i).Time > lastTime && test.Get(i).Time<Time[0] && test.Get(i).Time.Day!=Time[0].Day)
									{
										lastTime = test.Get(i).Time;
										index=i;
									}
									
								}
							
							Bars evalBars = new Bars(test.Instrument,test.Period,test.From,Time[0],test.SplitAdjusted,test.DividendAdjusted);
							for (int i=0; i<index; i++)
								{
									evalBars.Add(test.Get(i).Open,test.Get(i).High,test.Get(i).Low,test.Get(i).Close,test.Get(i).Time,test.Get(i).Volume,1,false);
								}
								
											
							IBar sampleBar = test.Get(index);
											
							Print("LAST: OPEN: " + sampleBar.Open + " HIGH: " + sampleBar.High + " LOW: " + sampleBar.Low + " CLOSE: " + sampleBar.Close + " TIME: " +sampleBar.Time.ToString() + " VOL: "+sampleBar.Volume); 
							Print("NEW: OPEN: "+ CurrentDayOHL().CurrentOpen[0] + " HIGH: " + CurrentDayOHL().CurrentHigh[0] + " LOW: " + CurrentDayOHL().CurrentLow[0] + " CLOSE: " + Close[0] + " TIME: " +Time[0].ToString() + " VOL: "+volumeCount);
						    Print("EVAL COUNT: " + evalBars.Count);
						    Print("Daily COUNT: " + test.Count);
								
							evalBars.Add(CurrentDayOHL().CurrentOpen[0],CurrentDayOHL().CurrentHigh[0],CurrentDayOHL().CurrentLow[0],Close[0],Time[0],volumeCount,1,false);

Then use MACD(evalBars,etc,etc,etc) to compare...
Last edited by JohnnyChimpo; 06-27-2011 at 12:22 PM.
JohnnyChimpo is offline  
Reply With Quote
Old 06-27-2011, 12:56 PM   #2
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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.
NinjaTrader_Josh is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Josh for this post:
Old 06-27-2011, 01:11 PM   #3
JohnnyChimpo
Junior Member
 
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
Default

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?
}

}
JohnnyChimpo is offline  
Reply With Quote
Old 06-27-2011, 01:13 PM   #4
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

Unfortunately you cannot backtest CalculateOnBarClose = false.
NinjaTrader_Josh is offline  
Reply With Quote
Old 06-27-2011, 01:16 PM   #5
JohnnyChimpo
Junior Member
 
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
Default

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?
JohnnyChimpo is offline  
Reply With Quote
Old 06-27-2011, 01:36 PM   #6
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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.
NinjaTrader_Josh is offline  
Reply With Quote
Old 06-27-2011, 01:40 PM   #7
JohnnyChimpo
Junior Member
 
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
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.
JohnnyChimpo is offline  
Reply With Quote
Old 06-27-2011, 01:59 PM   #8
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

For prior days you can just use PriorDayOHLC() and it would always be accurate values.
NinjaTrader_Josh is offline  
Reply With Quote
The following user says thank you to NinjaTrader_Josh for this post:
Old 06-27-2011, 02:33 PM   #9
JohnnyChimpo
Junior Member
 
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
Default

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.
JohnnyChimpo is offline  
Reply With Quote
Old 06-27-2011, 02:40 PM   #10
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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.
NinjaTrader_Josh is offline  
Reply With Quote
Old 06-27-2011, 03:10 PM   #11
JohnnyChimpo
Junior Member
 
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
Default

Quote:
This means you just need to mimic the logic
Is it possible to look at the MACD indicator source code somewhere?
JohnnyChimpo is offline  
Reply With Quote
Old 06-27-2011, 03:28 PM   #12
NinjaTrader_Josh
NinjaTrader Product Manager
 
NinjaTrader_Josh's Avatar
 
Join Date: May 2007
Location: Denver, CO
Posts: 17,458
Thanks: 1
Thanked 106 times in 70 posts
Default

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.
NinjaTrader_Josh is offline  
Reply With Quote
Old 06-28-2011, 07:19 PM   #13
JohnnyChimpo
Junior Member
 
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Josh View Post
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.
JohnnyChimpo is offline  
Reply With Quote
Old 06-29-2011, 03:26 AM   #14
NinjaTrader_Bertrand
NinjaTrader Customer Service
 
NinjaTrader_Bertrand's Avatar
 
Join Date: Sep 2008
Location: Germany
Posts: 22,411
Thanks: 252
Thanked 976 times in 959 posts
Default

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.
NinjaTrader_Bertrand is offline  
Reply With Quote
Old 06-29-2011, 09:51 AM   #15
JohnnyChimpo
Junior Member
 
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by NinjaTrader_Bertrand View Post
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.
JohnnyChimpo 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


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