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

Initialize() strangeness…

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

    Initialize() strangeness…

    I have code inside my custom strategy Initialize() method that checks the Instrument Name, Bar Type and Bar Value of the chart the strategy is being added to. When the strategy is first selected by the Strategies selection dialog, a call to my Strategy’s Initialize method happens. This first call shows “” for the Instrument Name, “Minute” for the Bar Type and “1” for the Bar Value. These are NOT the settings of my chart, but this is understandable since the strategy has not been “added” to my chart yet – not until I double click the strategy to add it, and then hit OK to dismiss the selection dialog.

    However, once the strategy is clearly added to my chart, the next call to my Initialize() method still has erroneous data. The Instrument name comes back correctly, but the Bar Type and the Bar Value remain set to their default value (“Minute” and “1”). It is not until I click the Strategy button again and exit out that the values are then correct.

    I am doing all of this in the Initialize() method. This behavior can be duplicated with this code:

    Code:
    [FONT=Calibri][SIZE=3]Print(Instrument.FullName.ToString());[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]Print(BarsPeriod.Id.ToString());[/SIZE][/FONT]
    [FONT=Calibri][SIZE=3]Print(BarsPeriod.Value.ToString());[/SIZE][/FONT]
    Again, it is not until going back into the strategy tab for the second time that the correct values are printed.

    I need to initialize variables based on how the chart is set up. Right now the only way I can do that is to remember to add the strategy to the chart then go back into the strategy selection dialog and exit out again. I’m assuming this is a bug, but maybe I am missing something?

    Thanks,
    -Scott

    #2
    Just do it in the OnBarUpdate() method. The Initialize() method is generally not the place to do much logic. In the OnBarUpdate() method just do it within an if statement.

    Code:
    if (CurrentBar == 0)
         // Do whatever you wanted to set;
    Josh P.NinjaTrader Customer Service

    Comment


      #3
      Josh - Thanks for the quick reply...

      With what I am trying to accomplish, I am pretty sure that won't work. I am working on saving/loading my strategy parameters to/from disk. I know this is outside what NinjaTrader support will provide, and that's fine, but I am curious as to the behavior I am seeing.

      After the strategy is added, the name of the Instrument is available in Initialize(), but the timeframe is not. Is this by design? If not, will it be fixed soon?

      I can most likely use the Instrument name alone for what I am trying to do, but it would be nice to further qualify the file name with the timeframe to keep the functionality scalable.

      Thanks,

      -Scott

      Comment


        #4
        Writing out to a file when CurrentBar = 0 should be the same as writing it out in the Initialize() method. You are effectively writing to your file before any calculations are done in the OnBarUpdate() method.

        I am unsure of the behavior with the Initialize() method so can't comment on it.
        Josh P.NinjaTrader Customer Service

        Comment


          #5
          Writing the file is not the issue. It's reading the right file when the strategy is loaded. For example, if I have an ER chart loaded using 5 minute bars, when I load my strategy, I want to read the ER-5Min file to set the user parms. This way the user can also edit them before hitting ok and starting the strategy.

          Because Initialize() has no idea what the Instrument name is or what type of period is being used, this is impossible. I could do this in OnBarUpdate() but then there is no way for the user to make changes before starting the strategy.

          It seems to me the Initialize() function of a strategy should be able to know what the chart settings are that it is being added to. As it stands now, the information being returned by Instrument.FullName, BarPeriods.Id nd BarPeriods.Value within Initialize() is bogus data - always defaulting to "", "minute" and "1" respectively no matter how the chart is setup.

          I'm not trying to be picky. This appears to be a bug. I just want to know if there is a chance this might be fixed by the next release. If not, I can create a stand-alone application that will handle all of the parameters. This would allow me to remove all parameters from the strategy completely - using the new app to "control" the strategy settings.

          Thoughts?


          Thanks,

          -Scott

          Comment


            #6
            I understand your concern and will discuss it with development.
            Josh P.NinjaTrader Customer Service

            Comment


              #7
              This is a limitation of the NT framework, you should access the properties in question in the OnBarUpdate() method.

              Comment


                #8
                Dierk,

                If this is a limitation of the NT framework, how do I accomplish what I am trying to do??? Please re-read this thread to understand what I am working on. I'm sorry, but your post makes no sense to me.

                -Scott

                Comment


                  #9
                  Edit my post below: You have to apply your logic first thing in OnBarUpdate().

                  Comment


                    #10
                    Can you even do it in OnBarUpdate? If you want to add something to the chart based on the parameters you read, or add another series based on those params, it's too late, no?

                    Comment


                      #11
                      Why would that be?

                      Comment


                        #12
                        What he's trying to do is read the parameter values so he doesn't have to enter them on the UI. Let's say you are using a Bollinger band and the period is one of the parameters. If you read the period to use from the file in OnBarUpdate, it's too late to add the indicator to the chart, no?

                        Comment


                          #13
                          Thanks for clarification.

                          Comment

                          Latest Posts

                          Collapse

                          Topics Statistics Last Post
                          Started by jxs_xrj, 01-12-2020, 09:49 AM
                          5 responses
                          3,290 views
                          1 like
                          Last Post jgualdronc  
                          Started by Touch-Ups, Today, 10:36 AM
                          0 responses
                          7 views
                          0 likes
                          Last Post Touch-Ups  
                          Started by geddyisodin, 04-25-2024, 05:20 AM
                          8 responses
                          61 views
                          0 likes
                          Last Post NinjaTrader_Gaby  
                          Started by Option Whisperer, Today, 09:55 AM
                          0 responses
                          8 views
                          0 likes
                          Last Post Option Whisperer  
                          Started by halgo_boulder, 04-20-2024, 08:44 AM
                          2 responses
                          24 views
                          0 likes
                          Last Post halgo_boulder  
                          Working...
                          X