Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

initialize custom indicator in strategy

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

    initialize custom indicator in strategy

    this code worked in NT65 Strategy:
    Code:
           #region Variables
            private MyCustomIndicator myInd = null;
            private double nPrice = 0;
            ...
           #endregion
    
            protected override void Initialize()
            {
                myInd = MyCustomIndicator(parm1, parm2);
                Add(myInd);
                ...
            }
    
            protected override void OnBarUpdate()
            {
                if (myInd.MyPublicBooleanProperty)
                {
                    nPrice = myInd.MyPublicPriceProperty;
                    ...
                 }
                 ...
            }
    in NT7b4 the indicator compiles (and performs as expected). the strategy compiles with no errors.

    when i go to add the strategy to a chart, the strategy does not show up in the list of available strategies ... and there is an entry in the Log tab that says:
    "Unable to create instance of 'NinjaTrader.Strategy.myStrategy'. Most likely there is no default constructor defined, or the default constructor doesn't work properly. Exception has been thrown by the target of an invocation."

    any help appreciated.

    cheers,
    -e

    #2
    actually, i wonder if my problem is related to a public bool in the indicator (as opposed to BoolSeries)

    indicator:
    Code:
            [Browsable(false)]
            [XmlIgnore()]
            public bool MyPublicBooleanProperty
            {
                get { return myPublicBooleanProperty; }
                set { myPublicBooleanProperty= value; }
            }
    two questions:
    1. is there a reason why public bool's would not be accessible inside of a strat?
    2. is it better to use a BoolSeries and then evaluate it in the strat via myInd.MyPublicBoolSeries[0]?

    thx...
    -e

    Comment


      #3
      Please try without making an instance of an indicator like that and calling the indicator directly.
      Josh P.NinjaTrader Customer Service

      Comment


        #4
        are you saying to do this:

        Code:
               #region Variables
                private double nPrice = 0;
                ...
               #endregion
        
                protected override void Initialize()
                {
                    Add(MyCustomIndicator(parm1, parm2););
                    ...
                }
        
                protected override void OnBarUpdate()
                {
                    if (MyCustomIndicator.MyPublicBooleanProperty)
                    {
                        nPrice = MyCustomIndicator.MyPublicPriceProperty;
                        ...
                     }
                     ...
                }

        Comment


          #5
          Yes, except when you call your indicator you will need to use MyCustomIndicator(param1, param2).MyPublicBooleanProperty.
          Josh P.NinjaTrader Customer Service

          Comment


            #6
            ah, yes -- that makes sense. thanks for the clarification, Josh.

            just so i understand the issue, though -- were there changes made that would prohibit instantiating an indicator within a strategy? or are there performance issues that would make this less desirable?

            my goal was to reuse the code from the indicator (which draws out signals and graphically marks up the chart, etc.) and make the signal entry info Public so i could use it to place orders within the strategy.

            thanks for the info,
            -e

            Comment


              #7
              e-man,

              Creating instances of the indicator like you did earlier was never a supported method which is why we suggest it this way. It may or may not have a negative impact in this case and just wanted to eliminate one extra variable from the true issue.

              What you are doing can be done through exposed properties and should not be a problem.
              Josh P.NinjaTrader Customer Service

              Comment


                #8
                problem appears to be in my variable section ... the following code compiles with no errors in ninja editor; however, there is (apparently) something not quite right, since it is prohibiting my Strategy from appearing in the selection list ... would appreciate any help/insights:

                Code:
                #region Variables
                private static string sToday = DateTime.Now.ToShortDateString();
                private DateTime dtSessionBegin = DateTime.Parse(sToday + " 080300");
                private DateTime dtSessionEnd = DateTime.Parse(sToday + " 151459");
                ...
                #endregion
                cheers,
                -e

                Comment


                  #9
                  Unfortunately this now clearly is beyond what we could provide support for, since is standard C# KnowHow. May be the community wanted to contribute.

                  Comment


                    #10
                    understand ... but in the interest of beta-testing, i thought you might want to be aware of the fact that the ninja editor will compile the code, yet it's clearly a c# bug (my bug) since it causes the strategy to be "invisible".

                    if you add that code to any of the Sample* strategies and compile, you can make that strategy disappear from the selection-list.

                    that said, if anyone in the community can help out with the proper syntax, i'd certainly appreciate it. my goal is to get trading-time as input for the strategy (eg. only take trades during first 30m after open would require the parameter to be 083000 and 090000). these are the local variable defaults that match up with the user-input public properties. hope that makes sense for what i'm trying to achieve.

                    cheers,
                    -e
                    Last edited by e-man; 11-20-2009, 01:57 PM.

                    Comment


                      #11
                      This has nothing to do with NT7 beta testing: you can and always could code bugs into your strategies/indicators with would cause NT to mal-function (including total crash). Due to the nature of C# coding, there is nothing which would prevent you doing this.

                      >> if you add that code to any of the Sample* strategies and compile, you should make that strategy disappear.
                      Apparently there is a serious misunderstanding on your end on how C# code compiles and runs: you very well can code serious bugs (which you likely did), they would compile fine by the .NET C# standard compiler (this is NOT an NT feature) and only would show up at runtime, which is when NT tries to notify you.

                      Comment


                        #12
                        point taken. sorry for wasting your time on a false-bug report. that certainly wasn't my intention.

                        after some tweaking, this code seems to do what i need (posted here in case anyone else may have need):
                        Code:
                        private static DateTime dtToday = DateTime.Now;
                        private DateTime dtSessionBegin = new DateTime(dtToday.Year, dtToday.Month, dtToday.Day, 08, 30, 00);
                        private DateTime dtSessionEnd = new DateTime(dtToday.Year, dtToday.Month, dtToday.Day, 15, 14, 59);
                        have a great weekend.

                        cheers,
                        -e

                        Comment


                          #13
                          Glad it's working for you now.

                          Comment


                            #14
                            It will also work, if you move the code into the Initialize() method like so:

                            Code:
                            // vars section
                            private DateTime dtSessionBegin;
                            private DateTime dtSessionEnd;
                            
                            // inside Initialize()
                            string sToday = DateTime.Now.ToShortDateString();
                            dtSessionBegin = DateTime.Parse(DateTime.Now.ToShortDateString() + " 080300");
                            dtSessionEnd = DateTime.Parse(DateTime.Now.ToShortDateString() + " 151459");

                            Comment

                            Latest Posts

                            Collapse

                            Topics Statistics Last Post
                            Started by Geovanny Suaza, 02-11-2026, 06:32 PM
                            0 responses
                            603 views
                            0 likes
                            Last Post Geovanny Suaza  
                            Started by Geovanny Suaza, 02-11-2026, 05:51 PM
                            0 responses
                            349 views
                            1 like
                            Last Post Geovanny Suaza  
                            Started by Mindset, 02-09-2026, 11:44 AM
                            0 responses
                            104 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
                            560 views
                            1 like
                            Last Post RFrosty
                            by RFrosty
                             
                            Working...
                            X