Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

NT Newbie Question. How to get the "Period" of primary BarsObject in Multi-TimeFrame strat

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    NT Newbie Question. How to get the "Period" of primary BarsObject in Multi-TimeFrame strat

    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

    #2
    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
    RayNinjaTrader Customer Service

    Comment


      #3
      imported post

      Thank You! Got it.

      Jim

      Comment


        #4
        imported post

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

        Thank you.

        Comment


          #5
          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.
          RayNinjaTrader Customer Service

          Comment


            #6
            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?

            Comment


              #7
              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.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                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.

                Comment


                  #9
                  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_JoshP; 10-03-2007, 12:34 AM.
                  Josh P.NinjaTrader Customer Service

                  Comment


                    #10
                    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):

                    ** 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, 12:50 AM. Reason: typo

                    Comment


                      #11
                      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_JoshP; 10-03-2007, 01:25 AM.
                      Josh P.NinjaTrader Customer Service

                      Comment


                        #12
                        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.

                        Comment


                          #13
                          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
                          Josh P.NinjaTrader Customer Service

                          Comment


                            #14
                            Wow, thanks! I'll definitely check them out. I was banging my head against this issue all day!

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by algospoke, Today, 06:40 PM
                            0 responses
                            10 views
                            0 likes
                            Last Post algospoke  
                            Started by maybeimnotrader, Today, 05:46 PM
                            0 responses
                            7 views
                            0 likes
                            Last Post maybeimnotrader  
                            Started by quantismo, Today, 05:13 PM
                            0 responses
                            7 views
                            0 likes
                            Last Post quantismo  
                            Started by AttiM, 02-14-2024, 05:20 PM
                            8 responses
                            168 views
                            0 likes
                            Last Post jeronymite  
                            Started by cre8able, Today, 04:22 PM
                            0 responses
                            10 views
                            0 likes
                            Last Post cre8able  
                            Working...
                            X