Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Specify second BarsArray instrument at run time

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

    Specify second BarsArray instrument at run time

    Hey,

    Is it possible to set specify a second instrument in the following manner under Initialize():

    Add(myInstrument, myPeriodType, myPeriod);

    Where the three parameters would be set in the Strategy Analyzer at run time ("FGBL ##-##", PeriodType.Minute, 1) so that you can easily choose a second instrument for a strategy.

    If I can't do this then I would need to rewrite my code any time I wanted to change the second BarsArray instrument correct?

    Thanks,
    darmbk.

    #2
    Hello darmbk,

    Thank you for your inquiry.

    While unsupported, it is possible to do this. Please note, however, that attempting to optimize these values through the Strategy Analyzer would not work as the Initialize() method is only run once.

    What you could do is create three variables. Example:
    Code:
    private int period = 1;
    private string instrument = "AAPL";
    private PeriodType thePeriod = PeriodType.Minute;
    And then create three properties that will allow you to change these variables. Example:
    Code:
    #region Properties
    [Description("")]
    [GridCategory("Parameters")]
    public int Period
    {
         get { return period; }
         set { period = Math.Max(1, value); }
    }
    [Description("")]
    [GridCategory("Parameters")]
    public string Instrument
    {
         get { return instrument; }
         set { instrument = value; }
    }
    [Description("")]
    [GridCategory("Parameters")]
    public PeriodType ThePeriod
    {
         get { return thePeriod; }
         set { thePeriod = value; }
    }
    #endregion
    You will then be able to change these parameters through the Strategy Analyzer.
    Zachary G.NinjaTrader Customer Service

    Comment


      #3
      Hey ZacharyG,

      For these purposes, is it possible to reference the 'Data series' parameters that would be set manually in the Strategy Analyzer gui? Specifically, Type and Value will be the same for the second instrument so it would be efficient to simply refer to their values rather than redefine them with separate variables 'period' and 'thePeriod' as you have suggested.

      Thanks,
      darmbk.

      Comment


        #4
        Hello darmbk,

        You would be able to access the bar type and value of the primary Data Series by using BarsPeriod.Id and BarsPeriod.Value, respectively.

        Please take a look at this link in the NinjaTrader help guide for further information about BarsPeriod: http://ninjatrader.com/support/helpG...barsperiod.htm
        Zachary G.NinjaTrader Customer Service

        Comment


          #5
          Hey ZacharyG,

          The page you refer to notes that these properties "should NOT be accessed within the Initialize() method". Does this simply mean both methods you have suggested are unsupported and if this is the case, is one of these methods 'less correct' than the other? If there's no difference then it would seem that using the BarsPeriod method is neater for sure.

          Cheers,
          darmbk.

          Comment


            #6
            Hello darmbk,

            The help guide says this because there may be times that the primary Bars object has not yet loaded and will return an error. This is why we recommend against this. It's not guaranteed to work 100% of the time.

            Manually specifying the period and value as I've outlined in my initial reply would be more reliable:

            Code:
            private PeriodType thePeriod = PeriodType.Minute;
            private int period = 1;
            
            #region Properties
            [Description("")]
            [GridCategory("Parameters")]
            public PeriodType ThePeriod
            {
                 get { return thePeriod; }
                 set { thePeriod = value; }
            }
            
            [Description("")]
            [GridCategory("Parameters")]
            public int Period
            {
                 get { return period; }
                 set { period = Math.Max(1, value); }
            }
            #endregion
            Zachary G.NinjaTrader Customer Service

            Comment

            Latest Posts

            Collapse

            Topics Statistics Last Post
            Started by argusthome, 03-08-2026, 10:06 AM
            0 responses
            111 views
            0 likes
            Last Post argusthome  
            Started by NabilKhattabi, 03-06-2026, 11:18 AM
            0 responses
            60 views
            0 likes
            Last Post NabilKhattabi  
            Started by Deep42, 03-06-2026, 12:28 AM
            0 responses
            38 views
            0 likes
            Last Post Deep42
            by Deep42
             
            Started by TheRealMorford, 03-05-2026, 06:15 PM
            0 responses
            43 views
            0 likes
            Last Post TheRealMorford  
            Started by Mindset, 02-28-2026, 06:16 AM
            0 responses
            79 views
            0 likes
            Last Post Mindset
            by Mindset
             
            Working...
            X