NinjaTrader Support Forum  

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 03-15-2007, 05:48 AM   #1
jbeninga
 
Join Date: Mar 2007
Location: , ,
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

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
jbeninga is offline  
Reply With Quote
Old 03-15-2007, 08:02 AM   #2
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Post imported post

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
NinjaTrader_Ray is offline  
Reply With Quote
Old 03-15-2007, 08:16 AM   #3
jbeninga
 
Join Date: Mar 2007
Location: , ,
Posts: 17
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

Thank You! Got it.

Jim
jbeninga is offline  
Reply With Quote
Old 03-16-2007, 02:07 PM   #4
SuzyG
 
Join Date: Nov 2006
Location: , ,
Posts: 66
Thanks: 0
Thanked 0 times in 0 posts
Post imported post

Can you please describe the usage of PeriodType and PeriodType.Volume, PeriodType.Minute,etc.

Thank you.
SuzyG is offline  
Reply With Quote
Old 03-17-2007, 05:41 AM   #5
NinjaTrader_Ray
Administrator
 
NinjaTrader_Ray's Avatar
 
Join Date: Nov 2004
Location: Denver, CO, USA
Posts: 11,163
Thanks: 6
Thanked 45 times in 32 posts
Post imported post

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.
NinjaTrader_Ray is offline  
Reply With Quote
Old 10-02-2007, 11:31 PM   #6
qbit9
Junior Member
 
Join Date: Oct 2007
Posts: 9
Thanks: 0
Thanked 0 times in 0 posts
Default BarsArray and Bars not accessible in Strategy.Initialize()?

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
Code:
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?
qbit9 is offline  
Reply With Quote
Old 10-02-2007, 11:44 PM   #7
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

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.
NinjaTrader_Josh is offline  
Reply With Quote
Old 10-03-2007, 12:11 AM   #8
qbit9
Junior Member
 
Join Date: Oct 2007
Posts: 9
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by Josh View Post
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:

Code:
#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.
qbit9 is offline  
Reply With Quote
Old 10-03-2007, 12:22 AM   #9
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

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 like
Code:
if (SMA(20)[0] > 100 && SMA(BarsArray[1], 20)[0] > 200)
     // Do something
For more information on multi-time frame & instruments check out this page: http://www.ninjatrader-support.com/H...struments.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.
Last edited by NinjaTrader_Josh; 10-03-2007 at 12:34 AM.
NinjaTrader_Josh is offline  
Reply With Quote
Old 10-03-2007, 12:48 AM   #10
qbit9
Junior Member
 
Join Date: Oct 2007
Posts: 9
Thanks: 0
Thanked 0 times in 0 posts
Default

Quote:
Originally Posted by Josh View Post
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):

Quote:
** 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:

Code:
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?
Last edited by qbit9; 10-03-2007 at 12:50 AM. Reason: typo
qbit9 is offline  
Reply With Quote
Old 10-03-2007, 01:22 AM   #11
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

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
Code:
if (BarsInProgress == 1)
     Print(myIndicator()[0].ToString());
or
Code:
Print(myIndicator(BarsArray[1])[0].ToString());
Both snippets should print the values tied to your secondary bars object.
Last edited by NinjaTrader_Josh; 10-03-2007 at 01:25 AM.
NinjaTrader_Josh is offline  
Reply With Quote
Old 10-03-2007, 01:42 AM   #12
qbit9
Junior Member
 
Join Date: Oct 2007
Posts: 9
Thanks: 0
Thanked 0 times in 0 posts
Default

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.
qbit9 is offline  
Reply With Quote
Old 10-03-2007, 10:31 AM   #13
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

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.
Attached Files
File Type: zip MultiTimeFrameDataSeriesTest.zip (3.8 KB, 41 views)
NinjaTrader_Josh is offline  
Reply With Quote
Old 10-03-2007, 11:36 AM   #14
qbit9
Junior Member
 
Join Date: Oct 2007
Posts: 9
Thanks: 0
Thanked 0 times in 0 posts
Default

Wow, thanks! I'll definitely check them out. I was banging my head against this issue all day!
qbit9 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 10:52 PM.