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

Custom Indicator not working properly from strategy

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

    Custom Indicator not working properly from strategy

    Hi NinjaTrader Forum & Support team.

    I'm a software developer but relatively new to NinjaTrader.

    I have a custom indicator which is working correctly on a chart,
    And that performed correctly from a strategy a couple of days ago
    (Put out results on strategy analyzer, entered trades correctly etc.)

    A day passed, and I went back to run the strategy in strategy analyzer after further development and the custom indicator included In it, and I run into the Object Reference not set to an instance of an object.(only internal logic of the strategy itself was added,nothing was changed in regards to instantiating indicators)

    Further investigation revealed that the OnStartUp method of my custom indicators
    Are no longer getting fired when called from the strategy, and therefore it's public Members are not getting instantiated, leading to Object Reference not set to an instance of an object when called upon from the containing strategy

    The strategy itself is a MTF strategy.

    I use visual studio for developing and debugging, and it's really easy to see that everytime I'm creating an instance custom indicator from a strategy, it's not hitting the OnStartUp() method even once. again, I'd like to remind that independently on a chart, all of my custom indicators work properly

    My custom Indicator doesn't plot anything, so I haven't added it using the Add() method (although i tried adding the custom indicators using the Add() methond as well, it didn't changed anything)
    To the strategy, again, reminding you that this approach worked fine just a day before..

    I also noticed that when I'm trying to create an instance of my custom indicator from the immediate window in visual studio with the scope of the strategy, it's output is
    the type <my custom indicator> exists both in '1'.dll and '2'.dll meaning that
    Ninja trader creates two dll containing the same type.

    it still give me back an instance of my custom indicator, but it's members which are not primitive types are not instantiated (meaning that OnStartUp of the indicator wasn't fired,
    where all of the system indicators are instantiated for my custom indicator)

    Trying to clean and rebuild didn't change this situation,
    Trying to delete one of the dll's in the ninjatrader bin folder didn't changed.

    upon creating the customer indicator using:

    MyCustomerIndicator _customIndicator = new MyCustomIndicator()

    I see that all public members of _customIndicator which are not primitive types
    (meaning indicators themselves, system indicators like the SMA or Stochastics)
    are instantiated and are not null
    but trying to do something like:

    _customIndicator.SMA[0] leads to index out of bounds exception.

    Upon further invastigating I've noticed that the Values collection of the custom Indicator are not getting passed from the strategy to the custom indicator, regardless of passing a
    BarsArray[x] or not. (it's an MTF strategy) meaning that doing
    var x = new CustomerIndicator()
    or
    var x = new CustomerIndicator(BarsArray[1])

    makes no difference.

    I've also tried to create a constructor to accept an IDataSeries object
    and to send explicitly a BarsArray[0] and to accept it into the Input property of the
    Custom indicator. again - doesn't matter.

    I'll be very happy to set up a remote session with you guys.
    Please help.

    Waiting for you guys to respond so i can send you code example, trace and log files to an email address..

    #2
    Hello ahadari28, and thank you for your question.

    With heavily state driven applications such as NinjaTrader, it is typically best to use the provided entry points into your program. NinjaTrader 7 provides the Initialize() routine, and NinjaTrader 8 provides the OnStateChange() routine with the State.Configure state. These are provided for exactly the sort of situation you are encountering - to ensure that nested indicators or strategies, or similar NinjaScript objects, are properly configured, and have enough already configured information and environment available to them.

    I would therefore recommend moving your indicator's initialization logic into one of these routines, Initialize() for NT7 or OnStateChange() for NT8.

    If you do so and you still see your indicator not being properly instantiated, or any other questions we may help with come up, please let us know so we can assist further.
    Jessica P.NinjaTrader Customer Service

    Comment


      #3
      Hey Jessica, and thanks for the reply first of all.

      Moving the instantiation of my custom indicators to the strategy's Initialize()
      doesn't change the situation, the OnStartUp() & OnBarUpdate() of the custom indicators are still not getting called,
      Even if I instantiate them from the strategy's Initialize().

      More over, those indicators are to perform with different time frames, meaning that I cant instantiate them in the Strategy.initialize() since BarsArray isn't guaranteed to be initialized itself.

      I was under the impression that an indicator inside of a strategy should get it's OnStartUp()
      Called before either OnBarUpdate() - of either the strategy or of its indicators...

      Comment


        #4
        Also, I'd like to point out the OnBarUpdate() & OnStartUp() of the indicators are NOT getting called at all,
        Leading to null reference exceptions...

        Comment


          #5
          Originally posted by ahadari28 View Post
          Also, I'd like to point out the OnBarUpdate() & OnStartUp() of the indicators are NOT getting called at all,
          Leading to null reference exceptions...
          As with all frameworks, things are not necessarily just plain vanilla c#. NinjaTrader objects are not simply c# objects: they are NinjaScript objects, and must be handled with the proper syntax. Specifically there are routines that NinjaTrader runs on on indicator before it is first used, so NinjaTrader arranges it so that Indicators are called without the new keyword.

          You are a developer, so I would say that if you examined the NinjaTrader-generated text in an indicator, you would be able to decipher what has been done. Whereas you could call the Indicator with new, then duplicate the NT setup, there is hardly any need. Just use the syntax that NT has documented. (ie., you instantiate Indicators without the new keyword).

          Comment


            #6
            Hey koganam, actually that's exactly what happened. :-)

            I already caught that after seeing that Ninja runs it's startup procedures based on the cache, inside the
            Auto generated methods. But I do appreciate your response.

            However I must say that after going thoroughly over the documentation, I couldn't find any reference over NOT using the new keyword to instantiate custom indicators, otherwise I wouldn't have done it in the first place.

            Thanks again for the response, and I'd love to know if there's a more exhaustive documentation source other than the help guide in NinjaTrader. Advanced stuff are covered very briefly.

            Comment


              #7
              Originally posted by ahadari28 View Post
              Hey koganam, actually that's exactly what happened. :-)

              I already caught that after seeing that Ninja runs it's startup procedures based on the cache, inside the
              Auto generated methods. But I do appreciate your response.

              However I must say that after going thoroughly over the documentation, I couldn't find any reference over NOT using the new keyword to instantiate custom indicators, otherwise I wouldn't have done it in the first place.
              Well, I may have misspoken somewhat. I have not seen it explicitly stated anywhere in the documentation, so it is more a deduction (and verification, by exactly what you are seeing), than an explicit statement, other, of course, than many of the examples extant.

              If you look at the tutorials in the NT Help, you will clearly see that Indicators are being accessed as if they were methods. However, Intellisense clearly shows that in fact, they are the indicators. That leads to the conclusion that they must be methods that return an indicator. The question then becomes: "Why such a convoluted process and where is the method being created?"

              Examining the "no-touch" code in the indicator shows where the method is created and made, and inserted as part of a partial class. Looking at how the cached indicators are created reveals why.
              Thanks again for the response, and I'd love to know if there's a more exhaustive documentation source other than the help guide in NinjaTrader. Advanced stuff are covered very briefly.
              Someone did write a little inexpensive book that revealed some of this stuff, but I forget the name of the book. You might try a Google search.

              Edit: I found it. ref: http://ninjalaunchpad.com/

              Note: I have no affiliation with them, and indeed I have not even read the book. I just have heard good things about it.
              Last edited by koganam; 10-28-2016, 02:35 PM.

              Comment


                #8
                Thank you koganam.

                ahadari28, I have mentioned your comments with respect to new MyIndicator(args) vs MyIndicator(args) not being well documented to some of my colleagues.
                Jessica P.NinjaTrader Customer Service

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by GussJ, 03-04-2020, 03:11 PM
                16 responses
                3,281 views
                0 likes
                Last Post Leafcutter  
                Started by WHICKED, Today, 12:45 PM
                2 responses
                19 views
                0 likes
                Last Post WHICKED
                by WHICKED
                 
                Started by Tim-c, Today, 02:10 PM
                1 response
                9 views
                0 likes
                Last Post NinjaTrader_ChelseaB  
                Started by Taddypole, Today, 02:47 PM
                0 responses
                5 views
                0 likes
                Last Post Taddypole  
                Started by chbruno, 04-24-2024, 04:10 PM
                4 responses
                53 views
                0 likes
                Last Post chbruno
                by chbruno
                 
                Working...
                X