View Full Version : NT Newbie Question. How to get the "Period" of primary BarsObject in Multi-TimeFrame strat
jbeninga
03-15-2007, 06:48 AM
Hello, trying to port my 250KLOC EasyLanguage script to NT6 and have come up with a couple of questions.
1) Re: Multi-TimeFrame Strategy: How do I find the "Period" and "PeriodType" for the primary barseries? My strategy utilizes both volume and minute charts and then creates larger time/volume frame charts based upon what period the base chart is running in. Is there a property value somewhere which has this information?
2) Not really a question but a request..... What I would*really* like to do is have the ability toeither resize the volume charts and/or remove/add them at the beginning of the day based upon some average of daily volume and be able to backtest. Any chance this functionality could be supported in the future?
Thanks in advance,
Best Regards,
Jim
NinjaTrader_Ray
03-15-2007, 09:02 AM
Hi Jim,
1) You can access
Bars.Period.Id
Bars.Period.Value
Id returns a value of PeriodType. So, PeriodType.Volume, PeriodType.Minute etc...
Value returns an integer value for the interval.
Bars points to the current BarsInProgress (calling the OnBarUpdate) event. So, when BarsInProgess == 0, you will have the primary bars object. Alternatively, you can access
BarsArray[0].Period which will always point to the primary bars object.
2) I will add this as a feature request for future consideration.
Ray
jbeninga
03-15-2007, 09:16 AM
Thank You! Got it.
Jim
SuzyG
03-16-2007, 03:07 PM
Can you please describe the usage of PeriodType and PeriodType.Volume, PeriodType.Minute,etc.
Thank you.
NinjaTrader_Ray
03-17-2007, 06:41 AM
PeriodType is an enum representing the support perdiod/interval type of a bars object.
PeriodType.Minute
PeriodType.Seconds
PeriodType.Tick
etc...
Some people may have indicators that set a variable value based on the interval being used.
qbit9
10-03-2007, 12:31 AM
Hi I've been trying to get BarsArray[0].Period.Id and BarsArray[0].Period.Value and use them to initialize a second time period series.
Essentially I try to do the following
protected override void Initialize()
{
Add(BarsArray[0].Period.Id, BarsArray[0].Period.Value * 2);
} But this doesn't seem to work.
Is the BarsArray not initialized until after Strategy.Initialize() has returned?
NinjaTrader_Josh
10-03-2007, 12:44 AM
That is correct. The error message from the log when you do that code is as follows: Failed to call 'Initialize' for strategy 'MultiTimeFrame': 'BarsArray' property can not be accessed from within 'Initialize' method.
qbit9
10-03-2007, 01:11 AM
That is correct. The error message from the log when you do that code is as follows: Failed to call 'Initialize' for strategy 'MultiTimeFrame': 'BarsArray' property can not be accessed from within 'Initialize' method.
Icic. Thanks for your reply. Sorry, but total noob here.
I've also been trying to plot from two time frames. I notice that if I Add() the same indicator with different parameters, it generates two separate instances. I want to have one indicator plot the results from the primary time frame, and the second plot the results from a second time frame. But since I can't access the BarsArray object in Strategy.Initialize(), how can I achieve this? I've tried doing the following:
#region variables
...
private bool oneTimeFlag = false;
...
#endregion
...
protected override void Initialize()
{
Add(PeriodType.second, 480);
Add(MyIndicator(0));
Add(MyIndicator(1));
}
protected override void OnBarUpdate()
{
if(BarsInProgress == 1) {
if(!oneTimeFlag) {
Indicators[1].Input = BarsArray[1];
oneTimeFlag = true;
}
}
}This was a total long shot, but the interesting thing is that it kind of works. The two indicators seem to go ahead and plot using the separate time frame data. But if I scroll the plots off the chart and then scroll back the plots disappear, as if NT has forgotten they're there.
Is it possible to plot indicator values from different time frames, and if so, what is the correct way of doing so?
Thanks in advance.
NinjaTrader_Josh
10-03-2007, 01:22 AM
I don't think it is possible because of the way the chart would be. One time frame would have more points than the other time frame and plotting something like SMA wouldn't match up. For instance, trying to plot the SMA(5) of a 1-min bar wouldn't work on a 5-min chart simply because where would the extra 4 data points from the 1-min SMA(5) go.
If you want to access values of the different time period's SMA, this is possible. You can do something likeif (SMA(20)[0] > 100 && SMA(BarsArray[1], 20)[0] > 200)
// Do somethingFor more information on multi-time frame & instruments check out this page: http://www.ninjatrader-support.com/HelpGuideV6/MultiTimeFrameInstruments.html
The way you have done it seems to be a workaround that fits your bill though. I don't know of a better 'official' way to do it so continuing using your way should be fine.
Edit: Actually you might want to check the values on your indicator plots. It might not match up with the values you wanted.
qbit9
10-03-2007, 01:48 AM
The way you have done it seems to be a workaround that fits your bill though. I don't know of a better 'official' way to do it so continuing using your way should be fine.
Thanks Josh for all your help. Appreciate it.
Heh, but it doesn't really work. NT seems to loose track of the plots after a while. After some digging I found this in Help (here (http://www.ninjatrader.com/webnew/support.htm)):
** In a multi-time frame and instrument strategies, a DataSeries object will ONLY be synchronized to the primary data series. You would need to use your own collection or array to store data for supplementary series.
which might explain the Index out of range errors I'm getting in the Log, cuz I suspect that swapping the value of the Input parameter for the indicator is kind of like pulling the rug out from under it ...
So now I'm just wondering how this affects indicators accessed from within strategies. Say I have a 1 second primary time frame, and add a 5 minute secondary time frame. If I call a custom indicator that sets up its own DataSeries to store series data, when BarsInProgress == 1, does that indicator actually hold datapoints on a second resolution (in sync with the primary time frame)? And when I do something like:
if(BarsInProgress==1) {
MyIndicator theIndicator = MyIndicator(0);
Print(theIndicator.MyDataSeries[5].ToString());
}what value am I retrieving? The value of the indicator 5 seconds ago, or the value of the indicator 25 minutes ago?
And what if my primary time frame has a longer period than my secondary one (e.g. primary is 5 minute period, while secondary is 1 second)? If the indicator has a custom Dataseries that is sync'd to the primary period of 5 minutes, what happens to all the data points for the 1 second time frame?
NinjaTrader_Josh
10-03-2007, 02:22 AM
I believe you would get the value of 5 seconds ago since like the message said, it is only synced to the primary bars. I would think there are no data points for 1-sec when your primary period is 5mins. The indicator isn't run on a 1-sec time frame thus no data points. I have never tried using DataSeries in the manner you are trying for before.
To make things simple, if you don't use DataSeries and just use the indicator itself, you can access the desired time period by various means such as
if (BarsInProgress == 1)
Print(myIndicator()[0].ToString());orPrint(myIndicator(BarsArray[1])[0].ToString());Both snippets should print the values tied to your secondary bars object.
qbit9
10-03-2007, 02:42 AM
Hmmm. I guess I need to rethink my implementation. The reason I'm using DataSeries is because I need to do some intermediate calculations on series data. Essentially I'm trying to establish an envelope that is a moving average plus and minus the range. But I need to smooth that envelope exponentially and thus need to store the values in two DataSeries so that I can feed it to EMA. But if the data points are out of sync with the bar numbers, I'm probably getting the wrong results. Arrrgh. Bummer.
NinjaTrader_Josh
10-03-2007, 11:31 AM
Hi qbit9,
I ran several tests to see what happens with DataSeries during multi-time frames and the results are promising. You can use them and they will be linked to the proper time frame they were called from (they will be synced with whichever bar).
Here are my test files. I tested it with a DataSeries set to take on the values of SMA(14) and when I call it from within a strategy it prints out the proper SMA(14) value across both time frames.
qbit9
10-03-2007, 12:36 PM
Wow, thanks! I'll definitely check them out. I was banging my head against this issue all day!