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

OnStateChange error when connectiing

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

    #16
    OK, thanks.

    I look forward to the updated example.

    Comment


      #17
      I have edited the updated thread safe sample into my previous code. This said, this sample is only to resolve a threading difference between versions of NinjaTrader, rather than your question. Please test this sample on your end. If it does not resolve your query, since it has been some time since this ticket was opened, please include screenshots that demonstrate your query in a reply. This will give us more context to provide the best assistance possible.

      To send a screenshot with Windows 7 or newer I would recommend using Window's Snipping Tool.
      Click here for instructions
      Alternatively to send a screenshot press Alt + PRINT SCREEN to take a screenshot of the selected window. Then go to Start--> Accessories--> Paint, and press CTRL + V to paste the image. Lastly, save as a jpeg file and send the file as an attachment.
      Click here for detailed instruction
      Jessica P.NinjaTrader Customer Service

      Comment


        #18
        Jessica,

        Thanks for the code sample. However, the duplication errors remain on re-connection after I incorporated your changes.

        I have attached screenshots. As you can see, we have duplication of my custom buy and sell menu items (I have another indicator 'Move to Bookmark' which also gets duplicated).
        Attached Files

        Comment


          #19
          I am glad the updated code sample resulted in code on your end with no threading messages.

          With regard to duplicate menu items, I recommend in your indicator on your end adding a

          Print(State)

          Method to your OnStateChange, outside of any if blocks. I then recommend running the Visual Studio debugger, documented here.



          If you put a breakpoint on the Print line I suggested adding, and make it the first line in OnStateChange, this should give you all the information you will need to determine exactly how NinjaScript states are being loaded. NinjaScript's states are documented here :



          If you discover that states are not operating in accord with that documentation please let us know so we can repair NinjaTrader for yourself and other customers.
          Jessica P.NinjaTrader Customer Service

          Comment


            #20
            Well, after incorporating the changes in your latest example I now find that merely editing the parameters of any indicator, even a built-in NT one causes duplication of the menu items associated with my indicator.

            Perhaps I don't understand 'state'. My impression was that it only refers to the state of the current object/indicator, not the state of the whole set of indicators.

            EDITED: The menu items also no longer get cleaned up after removing the indicator.
            Last edited by trader_rick; 06-28-2017, 11:08 AM.

            Comment


              #21
              Hello trader_rick,

              Thank you for your note.

              State represents the current progression of the object as it advances from setup, processing data, to termination. These states can be used for setting up or declaring various resources and properties. The current object/indicator configurations you referred to are represented in the enum Configure with State.Configure.

              A NinjaScript object goes through a series of state changes during its lifecycle. To better visualize how each instance goes through its States, please consider the logic below:

              1. In order to display the indicator name in the list of "Available" indicators, the NinjaTrader core must find the Name of each installed indicator defined in their SetDefaults. This occurs simultaneously for every indicator installed on the system in order to build the full list of available indicators.

              2. The selected indicator is then cloned and SetDefaults is called again in order to display the default properties to the "Properties" grid. This only occurs for the individual indicator.

              3. After the user has set their desired property settings and press OK or Apply, the indicator is once again cloned and runs through its full state management. This only occurs for the indicator configured to execute on the chart.

              Since AddOns run in the background and are not dependent on UI elements, they will run through their SetDefaults/Terminated states after each NinjaScript compile and startup/shutdown of NinjaTrader. The configured instance will also be cloned back to UI instances during various user actions (e.g, re-opening an indicator dialog to reconfigure settings, or user copying & pasting the indicator to a new panel or chart). Therefore you should not assume that objects (such as ChartControl) will not be accessible in the UI instances. In some extreme scenarios, you may need to execute custom logic before or after an object is cloned. Overriding the default behavior can be done via the virtual Clone() method.

              I've provided a link to our NinjaScript Lifecycle page on our Help Guide which is a great educational resource to help with understanding the State changes behind NinjaScript object instantiation:http://ninjatrader.com/support/helpG...fecycle_of.htm

              In regards to your comment about Visual Studio 2008 not working for you in relation to NinjaTrader 8, may I ask you to provide some additional information on what exactly you are experiencing?

              Per our Visual Studio Debugging page located in our Help Guide for NinjaTrader 8, we recommend using Visual Studio 2015 as other versions of Visual Studio may have potential limitations we are not aware of. I would not be able to accurately say if Visual Studio 2008 is capable of correctly debugging the NT8 custom project as I have no way to test 2008. I would suggest updating to a newer Visual Studio if at all possible.

              For more on Visual Studio Debugging with NinjaTrader 8, please see the following link to our Help Guide: http://ninjatrader.com/support/helpG..._debugging.htm

              Please let us know if you have any additional questions.

              Comment


                #22
                Chris,

                Thank you for the reply. I am a little better informed now on 'state'.

                I will deal with one problem at a time in order to keep things simple.

                Nowhere in the documentation you have given me does it describe making a connection causes a state change. Yet when I connect to a live datafeed, I get the following state changes (and error message) in my indicator;

                Terminated
                Indicator 'aaMoveToTime': Error on calling 'OnStateChange' method: The calling thread cannot access this object because a different thread owns it.
                SetDefaults
                Configure
                DataLoaded
                Historical
                Transition
                Realtime

                So it looks like making a new connection attempts to kill the indicator before instituting a new copy. Is that indeed the case? (Duplication of my menu item thus occurs because NT cannot clean up 'cleanly'.)

                thanks

                P.S. Re: VS2008 - I will try using a later version of VS.

                Comment


                  #23
                  Hello trader_rick,

                  Thank you for the reply.

                  It is correct that an indicator is reinstantiated upon making a new connection if it reloads historical data. Reloading historical data does occur in most cases.

                  Please let us know if we may provide further assistance.

                  Comment


                    #24
                    Thank you Chris, that was helpful.

                    I made some changes and the duplication problem is now resolved. Thanks once again.

                    Comment

                    Latest Posts

                    Collapse

                    Topics Statistics Last Post
                    Started by StrongLikeBull, Yesterday, 04:05 PM
                    1 response
                    12 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by Mestor, 03-10-2023, 01:50 AM
                    14 responses
                    375 views
                    0 likes
                    Last Post z.franck  
                    Started by molecool, 10-09-2017, 10:48 AM
                    5 responses
                    1,621 views
                    0 likes
                    Last Post trader-ap  
                    Started by The_Sec, Yesterday, 03:53 PM
                    1 response
                    12 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Started by mmenigma, Yesterday, 03:25 PM
                    1 response
                    11 views
                    0 likes
                    Last Post NinjaTrader_Gaby  
                    Working...
                    X