![]() |
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
|
|||||||
| General Programming General NinjaScript programming questions. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Junior Member
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#2 |
|
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;
}
Josh
NinjaTrader Customer Service |
|
|
|
|
The following user says thank you to NinjaTrader_Josh for this post: |
|
|
|
#3 |
|
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? } } |
|
|
|
|
|
#4 |
|
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 |
|
|
|
|
|
#5 |
|
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?
|
|
|
|
|
|
#6 |
|
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 |
|
|
|
|
|
#7 |
|
Junior Member
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#8 |
|
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 |
|
|
|
|
The following user says thank you to NinjaTrader_Josh for this post: |
|
|
|
#9 |
|
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. |
|
|
|
|
|
#10 |
|
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 |
|
|
|
|
|
#11 | |
|
Junior Member
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
|
Quote:
|
|
|
|
|
|
|
#12 |
|
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 |
|
|
|
|
|
#13 |
|
Junior Member
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
|
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.
|
|
|
|
|
|
#14 |
|
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 |
|
|
|
|
|
#15 | |
|
Junior Member
Join Date: May 2011
Posts: 21
Thanks: 9
Thanked 0 times in 0 posts
|
Quote:
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 | |
|
|