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

How to find the PeriodType during Initialize?

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

    How to find the PeriodType during Initialize?

    Hi,

    I would like to add another symbol during the initialize period.

    However reading the manuals the use of BarsPeriod.Id and BarsPeriod.Value is not valid in the Initialize call.




    How to rewrite the it to the correct way to find out the time frame and the chart is in ?

    Code:
    protected override void Initialize()
            {
                .....
                Add("AAPL", BarsPeriod.Id, BarsPeriod.Value);
            }
    The idea is that my second symbol is always producing the same timeframe bars as my main chart.

    Wessel
    Attached Files

    #2
    Wessel, unfortunately you can't dynamically add as there's no bars object yet in the Initialize(), you could create a string input and just set this input as needed to mirror the timeframe chosen.
    BertrandNinjaTrader Customer Service

    Comment


      #3
      I haven't used this in a while, but try it out:

      In Variables:

      PHP Code:
      private string hedgeInstrument = @""// Default setting for HedgeInstrument
      private int barValue
      In Initialize:

      PHP Code:
      barValue Math.Max(1base.BarsPeriod.Value);
      Add(hedgeInstrument,BarsPeriod.IdbarValue); 
      That should add the second instrument (defined in the hedgeInstrument variable) as BarsArray[1], with the same bars period as the base instrument.

      Also search for some of kdordens posts, and the Spread indicators in the NT7 downloads secition to see how others have added a second instrument with the same periodicity as the base instrument.

      YMMV etc etc

      Comment


        #4
        BarsPeriods works in Initialize()

        Wessel, try using BarsPeriods (Note the 's' on the end). The following code works for me, with NT 7.0.1000.2 :

        Code:
        protected override void Initialize() {
               Print("Running on " + BarsPeriods[0].Value.ToString() + " " + BarsPeriods[0].Id.ToString() + " bars.");
        }
        The NT manual says that BarsPeriod should not be accessed within Initialize(), but it says no such thing about BarsPeriods (note the 's').

        Could NT support please confirm whether it is okay to use BarsPeriods within Initialize() ?

        Another possibility that occurs to me is to call Add() from within OnStartUp() where presumably there is no restriction on using BarsPeriod.

        Comment


          #5
          splat, since it is not advised to use BarsPeriod in Initialize(), it probably holds true to not use BarsPeriods in Init(). I will have someone confirm/deny this tomorrow.
          AustinNinjaTrader Customer Service

          Comment


            #6
            Hi Austin,

            While you are checking that, please check whether it would be okay to access these items from OnStartUp().

            Comment


              #7
              Austin would be correct here, the recommendation would include BarsPeriods as well...a series needs to be Add()ed in the Initialize, not in OnStartUp().
              BertrandNinjaTrader Customer Service

              Comment


                #8
                Hi Bertrand,

                So, if I want to add a secondary data series, such that it has four times the period of the primary data series, how would you suggest I do it (without hard-coding the period) ?

                Also, I would like to know why BarsPeriods should not be used in Initialize. The code I posted uses BarsPeriods in Initialize, and it works for me (if the value did not exist, it would not work). I tested it with different timeframes from weeks to ticks, and it reliably printed out the time unit and quantity.

                Comment


                  #9
                  splat, I'm glad to hear it works for you however as there are limitations in this concept we unfortunately do not support the dynamic addition / configuration of series. You should be able to create inputs for your multiplier as well so you can set the value as needed from the GUI as Initialize() then would be called.
                  BertrandNinjaTrader Customer Service

                  Comment


                    #10
                    Hi Splat,

                    you seem to be reinventing the wheel here. Check out my indicator which has all the features you're looking to implement. http://www.ninjatrader.com/support/f...ad.php?t=38011.

                    Anyway, BarsPeriods is the way to go, where BarsPeriods[0] represents settings of default/primary instrument on a chart and is always populated/initialized and safe to use. If you need to check secondary instrument you can use BarsPeriods[1] and so on...

                    Comment


                      #11
                      Hi UltimateIndicator,

                      Your indicator looks interesting. But you are jumping the gun when you say I am re-inventing the wheel.

                      Thanks for confirming my thoughts about BarsPeriods. When you pointed out that BarsPeriods[0] is always populated (as makes sense), it dawned on me that if I add a secondary series, BarsPeriods[1] will only be populated after the addition (at earliest), and maybe *that* is the reason for the warnings from NT support. That is fine with me, because I only need to get the specification of the primary series.

                      Comment

                      Latest Posts

                      Collapse

                      Topics Statistics Last Post
                      Started by Waxavi, 04-19-2024, 02:10 AM
                      3 responses
                      41 views
                      0 likes
                      Last Post gaz0001
                      by gaz0001
                       
                      Started by Max238, Today, 01:28 AM
                      2 responses
                      26 views
                      0 likes
                      Last Post NinjaTrader_ChristopherJ  
                      Started by Shansen, 08-30-2019, 10:18 PM
                      25 responses
                      949 views
                      0 likes
                      Last Post NinjaTrader_BrandonH  
                      Started by JonesJoker, 04-22-2024, 12:23 PM
                      8 responses
                      42 views
                      0 likes
                      Last Post JonesJoker  
                      Started by timko, Today, 06:45 AM
                      0 responses
                      6 views
                      0 likes
                      Last Post timko
                      by timko
                       
                      Working...
                      X