Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

unsolicited initialization

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

    unsolicited initialization

    Hi,

    I'm developing a multi-instrument strategy. To backtest the strategy, I first picked an ETF - SHY from the instrument list on the left of Strategy Analyzer and selected Backtest... After setting the properties and started running the strategy, I found in the log that the strategy would occasionally and randomly got re-initialized against FDAX - an instrument that was neither in my strategy nor selected, when running the backtest.

    Any explanation? Thanks.

    -------------- log ----------------
    Initialize(SHY)
    Initialize() add VTI to BarsArray[1].
    Initialize() add XLB to BarsArray[2].
    Initialize() add XLI to BarsArray[3].
    Initialize() add XLK to BarsArray[4].
    Initialize() add XLV to BarsArray[5].
    Initialize() add XLE to BarsArray[6].
    Initialize() add XLF to BarsArray[7].
    Initialize() add XLP to BarsArray[8].
    Initialize() add XLU to BarsArray[9].
    Initialize() add XLY to BarsArray[10].
    Initialize() add GLD to BarsArray[11].
    ....
    Initialize(FDAX 09-13)
    Initialize() add VTI to BarsArray[1].
    Initialize() add XLB to BarsArray[2].
    Initialize() add XLI to BarsArray[3].
    Initialize() add XLK to BarsArray[4].
    Initialize() add XLV to BarsArray[5].
    Initialize() add XLE to BarsArray[6].
    Initialize() add XLF to BarsArray[7].
    Initialize() add XLP to BarsArray[8].
    Initialize() add XLU to BarsArray[9].
    Initialize() add XLY to BarsArray[10].
    Initialize() add GLD to BarsArray[11].
    Initialize() add SHY to BarsArray[12].
    ....
    ------------ end of log --------------

    #2
    cartoosh, that could be actually the case, since Initialize() is called across all your indicator or strategy scripts.

    Comment


      #3
      Thanks for reply, but not sure if I understand what you're saying. I have only one strategy that prints those traces. I selected SHY as the primary to backtest that strategy which was initialized at first and had run to a point. I didn't have that strategy used anywhere else. Where did Initialize(FDAX 09-13) come from?

      Comment


        #4
        Initialize() gets called across all scripts, even if you only backtested one script - so I guess there's another script in your collection using the FDAX internally?

        Comment


          #5
          I have two strategies and I went to add the strategy name to Initialize() of those two scripts. I backtested MyRS1 a few times and found that sometimes MyPriceSMA got initialized against FDAX and other times not initialized (at least not shown in the output window).

          Still in the trace below, MyRS1 got initialized twice against SHY and FDAX. I am not sure how FDAX got picked up. So, are you saying that all the compiled strategies always get initialized against a random instrument in addition to the one instrument I selected to backtest?

          ------------ log ------------
          myRS1 Initialize(SHY) begins
          myRS1 Initialize(SHY) add VTI to BarsArray[1].
          myRS1 Initialize(SHY) add XLB to BarsArray[2].
          myRS1 Initialize(SHY) add XLI to BarsArray[3].
          myRS1 Initialize(SHY) add XLK to BarsArray[4].
          myRS1 Initialize(SHY) add XLV to BarsArray[5].
          myRS1 Initialize(SHY) add XLE to BarsArray[6].
          myRS1 Initialize(SHY) add XLF to BarsArray[7].
          myRS1 Initialize(SHY) add XLP to BarsArray[8].
          myRS1 Initialize(SHY) add XLU to BarsArray[9].
          myRS1 Initialize(SHY) add XLY to BarsArray[10].
          myRS1 Initialize(SHY) add GLD to BarsArray[11].
          myRS1 Initialize(SHY) ends
          ...
          myRS1 Initialize(FDAX 09-13) begins
          myRS1 Initialize(FDAX 09-13) add VTI to BarsArray[1].
          myRS1 Initialize(FDAX 09-13) add XLB to BarsArray[2].
          myRS1 Initialize(FDAX 09-13) add XLI to BarsArray[3].
          myRS1 Initialize(FDAX 09-13) add XLK to BarsArray[4].
          myRS1 Initialize(FDAX 09-13) add XLV to BarsArray[5].
          myRS1 Initialize(FDAX 09-13) add XLE to BarsArray[6].
          myRS1 Initialize(FDAX 09-13) add XLF to BarsArray[7].
          myRS1 Initialize(FDAX 09-13) add XLP to BarsArray[8].
          myRS1 Initialize(FDAX 09-13) add XLU to BarsArray[9].
          myRS1 Initialize(FDAX 09-13) add XLY to BarsArray[10].
          myRS1 Initialize(FDAX 09-13) add GLD to BarsArray[11].
          myRS1 Initialize(FDAX 09-13) add SHY to BarsArray[12].
          myRS1 Initialize(FDAX 09-13) ends
          -------- end of log -------------

          Comment


            #6
            The Initialize method must be called to generate the list of strategies in the Strategy Analyzer. It will use one of the instruments in your default instrument list to initialize.

            You may want to use OnStartUp instead which would be called as soon as that instance of the strategy was started and only for that strategy:

            MatthewNinjaTrader Product Management

            Comment


              #7
              I tested Initialize(0 and OnStartUp() and found something interesting. Setting EntriesPerDirection works only in OnStartUp() but not Initialize(). Any explanation here?

              Comment


                #8
                cartoosh, we cannot reproduce this here - setting this property should be done in Initialize() as you then would see the effect immediately as a new strategy instance is loaded for example, where if you set it in OnStartUp() you would then see on loading the default of 1 in the UI and value changing to your code defined # after the first enablement, as OnStartUp() is then called.

                Comment


                  #9
                  What I want to accomplish is to set some of the variables (e.g. EntriesPerDirection and BarsRequired) based on the UI inputs. I can't get that to work, unless I reset them in OnStartUp(). The weird behavior is that Initialize() is not only called the first time the strategy is loaded, but also when Run Backtest is hit. Add() in Initialized() is executed, but other variable assignments don't appear to get done properly.

                  Here is my test strategy:
                  #region Using declarations
                  using System;
                  using System.ComponentModel;
                  using System.Diagnostics;
                  using System.Drawing;
                  using System.Drawing.Drawing2D;
                  using System.Xml.Serialization;
                  using NinjaTrader.Cbi;
                  using NinjaTrader.Data;
                  using NinjaTrader.Indicator;
                  using NinjaTrader.Gui.Chart;
                  using NinjaTrader.Strategy;
                  #endregion

                  // This namespace holds all strategies and is required. Do not change it.
                  namespace NinjaTrader.Strategy
                  {
                  /// <summary>
                  /// Enter the description of your strategy here
                  /// </summary>
                  [Description("Enter the description of your strategy here")]
                  public class MySimple : Strategy
                  {
                  #region Variables
                  // Wizard generated variables
                  private int number = 1; // Default setting for MyInput0
                  private string stock = "";
                  private string stock2 = "";
                  // User defined variables (add any user defined variables below)
                  #endregion

                  /// <summary>
                  /// This method is used to configure the strategy and is called once before any strategy method is called.
                  /// </summary>
                  protected override void Initialize()
                  {
                  CalculateOnBarClose = true;
                  EntriesPerDirection = number * 10; // set to the max number of active positions
                  EntryHandling = EntryHandling.UniqueEntries;
                  BarsRequired = number * 10;
                  stock2 = stock;
                  Print("MySimple.Initialize(" + Instrument.FullName + ") EntriesPerDirection=" + EntriesPerDirection + ", EntryHandling=" + EntryHandling + ", BarsRequired=" + BarsRequired + ", number=" + number + ", stock=" + stock + ", stock2=" + stock2);
                  if (stock != "")
                  Add(stock, PeriodType.Day, 1);
                  }

                  protected override void OnStartUp()
                  {
                  //EntriesPerDirection = number * 10; // set to the max number of active positions
                  //BarsRequired = number * 10;
                  //stock2 = stock;
                  }

                  /// <summary>
                  /// Called on each bar update event (incoming tick)
                  /// </summary>
                  protected override void OnBarUpdate()
                  {
                  Print("MySimple.OnBarUpdate(" + Instrument.FullName + ", " + BarsInProgress + ", " + CurrentBar + ") EntriesPerDirection=" + EntriesPerDirection + ", EntryHandling=" + EntryHandling + ", BarsRequired=" + BarsRequired + ", number=" + number + ", stock=" + stock + ", stock2=" + stock2);
                  }

                  #region Properties
                  [Description("")]
                  [GridCategory("Parameters")]
                  public int Number
                  {
                  get { return number; }
                  set { number = Math.Max(1, value); }
                  }
                  [Description("")]
                  [GridCategory("Parameters")]
                  public string Stock
                  {
                  get { return stock; }
                  set { stock = value; }
                  }
                  [Description("")]
                  [GridCategory("Parameters")]
                  public string Stock2
                  {
                  get { return stock2; }
                  set { stock2 = value; }
                  }
                  #endregion
                  }
                  }

                  Comment


                    #10
                    Hello cartoosh,

                    I will run a few tests and let you know what I find, but please keep in mind that Dynamically adding and instrument like using a string "stock" is not supported. We would only recommend hardcoding this.
                    JCNinjaTrader Customer Service

                    Comment


                      #11
                      Hello cartoosh,

                      Thanks for your patience.

                      So this would be just like the dynamically adding instruments via the Initialize() method it would not be supported. Not just because Initialize() is being called more than once which would be expected but because the variables are being set to their default states when this is happening which is throwing off your calculated values inside of Initialize().

                      Dynamically changing the variables you would want to do this inside of OnStartUp() since this is going to be called right before OnBarUpdate() logic is going to be processed and after the variables are going to be set to the user defined values.

                      Happy to be of further assistance.
                      Last edited by NinjaTrader_JC; 09-19-2013, 11:50 AM.
                      JCNinjaTrader Customer Service

                      Comment


                        #12
                        Hi JC,

                        I agree with you on the basic theory. In reality, though, some strategy variables can be set that way, while others just can't. E.g. Setting BarsRequired in OnStartUp() does not work. See the trace below. The inconsistency causes quite some confusion. I have to test each variable to see what works and what doesn't. Anyway, thank you so much for following up.

                        ------------------
                        MySimple.Initialize(SPY) EntriesPerDirection=20, EntryHandling=UniqueEntries, BarsRequired=20, number=2, stock=, stock2=
                        MySimple.OnStartUp(SPY) EntriesPerDirection=10, EntryHandling=UniqueEntries, BarsRequired=40, number=2, stock=, stock2=
                        MySimple.OnBarUpdate(SPY, 0, 10) EntriesPerDirection=10, EntryHandling=UniqueEntries, BarsRequired=40, number=2, stock=, stock2=
                        MySimple.OnBarUpdate(SPY, 0, 11) EntriesPerDirection=10, EntryHandling=UniqueEntries, BarsRequired=40, number=2, stock=, stock2=
                        MySimple.OnBarUpdate(SPY, 0, 12) EntriesPerDirection=10, EntryHandling=UniqueEntries, BarsRequired=40, number=2, stock=, stock2=
                        MySimple.OnBarUpdate(SPY, 0, 13) EntriesPerDirection=10, EntryHandling=UniqueEntries, BarsRequired=40, number=2, stock=, stock2=
                        MySimple.OnBarUpdate(SPY, 0, 14) EntriesPerDirection=10, EntryHandling=UniqueEntries, BarsRequired=40, number=2, stock=, stock2=
                        MySimple.OnBarUpdate(SPY, 0, 15) EntriesPerDirection=10, EntryHandling=UniqueEntries, BarsRequired=40, number=2, stock=, stock2=
                        MySimple.OnBarUpdate(SPY, 0, 16) EntriesPerDirection=10, EntryHandling=UniqueEntries, BarsRequired=40, number=2, stock=, stock2=
                        ---------------------------

                        Comment


                          #13
                          Hello cartoosh,

                          Correct, I will edit my previous post to make that more clear.

                          Just to make sure you are aware that the "Min. Bars required" inside of the Strategy Properties will set the BarsRequired. I know it will not dynamically set this by a multiplier but it is an easy way to set BarsRequired without having to add another variable.
                          JCNinjaTrader Customer Service

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                          0 responses
                          566 views
                          0 likes
                          Last Post Geovanny Suaza  
                          Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                          0 responses
                          330 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by Mindset, 02-09-2026, 11:44 AM
                          0 responses
                          101 views
                          0 likes
                          Last Post Mindset
                          by Mindset
                           
                          Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                          0 responses
                          547 views
                          1 like
                          Last Post Geovanny Suaza  
                          Started by RFrosty, 01-28-2026, 06:49 PM
                          0 responses
                          548 views
                          1 like
                          Last Post RFrosty
                          by RFrosty
                           
                          Working...
                          X