Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

Bars in Initialize

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

    Bars in Initialize

    In Ninja6.5, I checked if (this.Bars == null) to determine if the Initialize was being called from the "Indicators" dialog (in which case this.Bars was null) or from the initialization of the indicator before plotting.

    In Ninja7, an exception is thrown whe accessing any Bars related data.

    How should I determine the difference between being called from the dialog verses from the indicator initialization? I'd prefer to not have to use FindWindow() to look for the dialog because it uses a pretty generic forms class and the title itself "Indicators" is also pretty generic.

    I suspect you're going to ask me why I need this. I'm doing something which I know is not supported.

    I save our indicator settings in an XML file on the first bar OnBarUpdate(). And the next time the user creates a new chart, I load their previous settings from the XML file in OnInitialize() ... but this is a little tricky because we only want to load the settings when using the "Indicators" dialog, otherwise, the default settings will overwrite the indicator settings.

    We've found this more intuitive than using templates, plus it allows us to easily carry settings forward from 6.5 to 7.

    Thanks!
    Doug

    ---

    ZoneTrader(c), available from TraderUSA.net, is a complete indicator package with multitime frame Zones of support and resistance, ABC and Fibonacci pivots, projections, and retracements, real time news, and our proprietary balance lines, triggers, and trend points. Visit our website to learn more.

    #2
    Unfortunately the feature you had been using in NT6.5 never was supported nor am I aware of a replacement in NT7.

    Comment


      #3
      Here's my real hacky code that gets the job done. This is advanced programming, it is not guaranteed to work in the future. It relies on knowing details about Ninja's code looking at the stack. It would be much easier/better if Ninja could provided a setting for this, maybe we'll get it in NT7.1 or NT8, given that I'm posting a really ugly solution for this.

      Code:
        protected bool IsInitDialog() {
      #if NT7
          // Determine when we are invoked to add a new indicator,
          // verses loading an already existing indicator,
          // by looking at the stack frame.
          // This is very ugly, and quite a hack, but works.
          StackTrace stackTrace = new StackTrace();
          StackFrame[] stackFrames = stackTrace.GetFrames();  
          for (int i = 0; i < stackFrames.Length; i ++) {
            MethodBase method = stackFrames[i].GetMethod();
            if (method.Name == "OnTypesSelectedIndexChange") {
              return true;
            }
            else if (method.Name == "set_SelectedTypes") {
              return false;
            }
          }
          return false;
      #else
          return this.Bars == null;
      #endif
        }
      ZoneTrader(c), available from TraderUSA.net, is a complete indicator package with multitime frame Zones of support and resistance, ABC and Fibonacci pivots, projections, and retracements, real time news, and our proprietary balance lines, triggers, and trend points. Visit our website to learn more.

      Comment


        #4
        Originally posted by douggreen View Post
        Here's my real hacky code that gets the job done. This is advanced programming, it is not guaranteed to work in the future. It relies on knowing details about Ninja's code looking at the stack. It would be much easier/better if Ninja could provided a setting for this, maybe we'll get it in NT7.1 or NT8, given that I'm posting a really ugly solution for this.
        Hi douggreen,

        Thank you for this very elegant solution. I also could not understand why NT does not have any property or flag that says where Initialise is called from. Because of this my indicators were loading bunch of useless information and were adding needless bars of secondary instruments just by opening indicators wizard. Now I can arrange this properly.

        Thanks a lot !

        P.S. NT guys, why don't you make something that will give developers a hint on where Initialize was called from.

        Comment


          #5
          Originally posted by dimkdimk View Post
          NT guys, why don't you make something that will give developers a hint on where Initialize was called from.
          Yes, please... This seems like something that would only take a very few minutes of NinjaTrader developer time, and it would make things a lot easier for some of us if we could identify which Initialize call was which.

          Comment


            #6
            Why don't you just check if ChartControl == null? Should be null when called from the window and not null if called from the chart.

            Comment


              #7
              Originally posted by terenyi View Post
              Why don't you just check if ChartControl == null? Should be null when called from the window and not null if called from the chart.
              I guess that would work in some cases.

              However, I was rather hoping for a supported interface, where a single variable (e.g., with enum values) could be inspected that would identify for us which of the many times that Initialize is being called, so we don't have to keep initializing things over and over again that never get used, or worse have to be cleaned up, freed or closed before we can do the final Initialize that we want to do just before the indicator actually starts getting fed data at OnBarUpdate, OnMarketData, etc..

              In the past I've solved some of this with a "first time" flag in OnBarUpdate and done initialization in that method instead, but that's sometimes an undesirable workaround, and in my guesstimation would nearly always be inferior to what I've asked for.

              Comment


                #8
                I just enumerate all open forms and check the form class name to find the indicator dialog form.

                Works fine for 2 month already without problems.


                www.zweisteintrading.eu

                ninjatinymobiletrader

                Comment


                  #9
                  Originally posted by zweistein View Post
                  I just enumerate all open forms and check the form class name to find the indicator dialog form.

                  Works fine for 2 month already without problems.
                  Thanks for the idea.

                  Originally posted by KBJ View Post
                  However, I was rather hoping for a supported interface...
                  I've also used creative and unsupported interfaces in the past, I was just hoping for a simple solution (i.e., requiring very minimal NinjaTrader developer time) that would be supported (i.e., not break) in the future.

                  Comment


                    #10
                    terenyi has the simplest answer which works in NT6.5 and NT7, kinda like the this.Bars check I originally used, I just didn't think to check ChartControl, thanks!

                    Code:
                    protected bool IsSettingsDialog() {
                       return this.ChartControl == null ? true : false;
                    }
                    Last edited by douggreen; 01-11-2010, 05:30 PM.

                    Comment


                      #11
                      douggreen: Thanks. (However, as covered in other threads, ChartControl use is unsupported: see this and this.)

                      In fact, Wow!

                      These NinjaTrader support forums are incredible, and you guys really are determined to be quite helpful (and me too, at times).

                      However, I was trying to address a request to the NinjaTrader support and/or development team to ask for a supported interface, so I could tell the difference between the several different types of calls to Initialize which occur during the following situations:
                      1. Initial loading of a chart when a workspace is being loaded
                      2. The different times that the Initialize method is called during the selection and initial loading of an indicator onto a chart
                      3. Or when an indicator's parameter settings are changed.
                      4. When the NinjaScript for a chart is reloaded (F5/Refresh).
                      5. etc.

                      Now, please, please, please... Let the NinjaTrader support or developer personnel answer the question.

                      I'm in the middle of converting about 45 different indicators that I've written over the last 2.9 years to get them to work with 7.0, and right now I'm having to undo all the unsupported cleverness that I came up with during that time period.

                      Please believe me when I say that I can dream up many incredibly clever, but non-supported hacks of my own (and in fact I already did so and they came back to bite me), and that's why I'm now looking for supported interfaces that will prevent my having to re-do this complicated porting process all over again in another 6-months or a year when the next version of NinjaTrader is released (and yes, I do predict a much faster time to market for 7.5 or 8.0 or whatever is next).

                      Thanks again to all who've offered such very inventive solutions!!!

                      And now over to Josh, Ray, Dierk, etc. (please).

                      Comment


                        #12
                        Your request is noted and added to the list of future considerations.

                        Comment


                          #13
                          Can I add a vote of support to this please. It seems plenty of us have been hacking away trying to get our serious Initialize()s to not give us too much hassle. I too do lots of stuff in Initialize(), since how else are you going to have a strategy that knows a lot.

                          With respect, it doesn't seem a smooth design decision to have Initialize executed for all sorts of internal reasons - it would be great if we could at least distinguish so we could return without doing much.

                          I promise to accept the consequences in any dialog box ;-)

                          Comment


                            #14
                            Originally posted by douggreen View Post
                            terenyi has the simplest answer which works in NT6.5 and NT7, kinda like the this.Bars check I originally used, I just didn't think to check ChartControl, thanks!

                            Code:
                            protected bool IsSettingsDialog() {
                               return this.ChartControl == null ? true : false;
                            }
                            This works for me pretty well when running strategies in the strategy analyzer, however, it also cuts out the initialize step when enabling a strategy in the strategy tab. Any ideas how I can exclude that case?

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            605 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            351 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            105 views
                            0 likes
                            Last Post Mindset
                            by Mindset
                             
                            Started by Geovanny Suaza, 02-02-2026, 12:30 PM
                            0 responses
                            560 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by RFrosty, 01-28-2026, 06:49 PM
                            0 responses
                            561 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X