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

Accessing NinjaTrader8 from external application

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

    Accessing NinjaTrader8 from external application

    Hello everyone,
    I am a newbie to Ninjatrader8, so kindly help me to get rid of an issue.

    Requirement -
    1. How to keep NT8 application in listening mode to respond to buy/sell signal sent by external application.
    2. How to send price and indicator data from ninjatrader application to external application

    Any help would be highly appreciated.

    Thanks.

    #2
    Hello AroraAbhishek,

    Thank you for your post.

    The NinjaTrader API could be used to have NinjaTrader interact with an external application.



    Support for the API is very limited.

    The Application Programming Interface (API) is available in the NTDirect.dll (unmanaged) and NinjaTrader.Client.dll (managed) files for you to use in an external application to interface with NinjaTrader. These two files provide the same functions, however, you may find the NinjaTrader.Client.dll is easier to reference in your custom application.

    Support for the API is limited from NinjaTrader Support. We are not able to assist with any code in an application external to NinjaTrader. We are able to detail the usage of any methods available from the API.

    Below I am providing a publicly available link to the help guide that lists the functions available to the API.
    Help Guide NT8 API functions - http://ninjatrader.com/support/helpG..._interface.htm

    Let us know if we may assist further.
    Brandon H.NinjaTrader Customer Service

    Comment


      #3
      Thankyou NinjaTrader_BrandonH for your quick reply.

      Please allow me enlighten the requirement a bit. I am stuck here since days.

      External app has to send requests to Ninjatrader using ZeroMQ. -> To achieve this I want to create C# dll and captures all the requests from External app via zeroMQ. And internally calls the NinjaTrader.Client.dll to perform the actions. So all the communication between External app and Ninja Trader application has to go via C# dll using ZeroMQ.

      So C# dll method has to asynchronously run infinite times to be able to keep its socket open for incoming messages and hence to capture the request. The same communication way has to be used to
      -> Set trail stops
      -> Price and Indicator data to be sent to external application.

      Please guide me to achieve this with no impact on other features of Ninja Trader app.

      Thanks in advance.

      Comment


        #4
        Hello AroraAbhishek,

        Thank you for your note.

        To accomplish your goals, you could use the NinjaTrader API to have NinjaTrader communicate with external applications. That said, this would go beyond our means of support we are able to provide.

        As stated in my previous post "Support for the API is limited from NinjaTrader Support. We are not able to assist with any code in an application external to NinjaTrader. We are able to detail the usage of any methods available from the API."

        See the help guide below that lists the functions available to the API.
        NT8 API functions - https://ninjatrader.com/support/helpGuides/nt8/?dll_interface.htm

        Also, see this forum post for unsupported examples and more information - https://ninjatrader.com/support/foru...art#post793626

        Let us know if we may assist further.
        Brandon H.NinjaTrader Customer Service

        Comment


          #5
          Thankyou NinjaTrader_BrandonH for your reply.

          I have found a little success in what i was looking for but now stuck in a new situation.
          My requirement is : I want to run a function asynchronously. So this function should start working as we start a chart and place an indicator on it and should stop when we remove the indicator. Function contains an infinite loop which will run continuously.

          Could you please guide me how to do that.

          Thanks in advance!

          Comment


            #6

            Greetings AroraAbhishek,


            Originally posted by AroraAbhishek View Post

            I want to run a function asynchronously. So this function should start working as we start a chart and place an indicator on it and should stop when we remove the indicator. Function contains an infinite loop which will run continuously.

            Could you please guide me how to do that.

            Thanks in advance!
            The TriggerCustomEvent via a Timer approach is commonly used and might be all you need to nail this need.

            If that does not meet the need then next I would consider 1) simple Parent Child Class constructs with child classes to host functions you want to run more indidependently or 2) at startup try instantiating static funtions or static classes via calls in the "else if (State == State.DataLoaded) { }" block of "protected override void OnStateChange()"

            1st Choice




            Originally posted by AroraAbhishek View Post

            ... it and should stop when we remove the indicator. Function contains an infinite loop which will run continuously.


            Thanks in advance!
            My unofficial understanding is that on removal of the indicator NT8 it will automatically manage termination of resources established by the indicator except for generally speaking some drawing objects you have introduced to the charts. You must managed disposal of those objects regularly and in the (State == State.Terminated) { }" block of "protected override void OnStateChange()"

            Good reading at these links and the NT-team has a reference samples if you need it.
            NT8 State Lifecycle - https://ninjatrader.com/support/help...fecycle_of.htm
            NT8 Code Design Best Practices - https://ninjatrader.com/support/help..._practices.htm
            Using Dispose() in NT8 https://ninjatrader.com/support/help...se_dispose.htm




            Originally posted by AroraAbhishek View Post

            I have found a little success in what i was looking for but now stuck in a new situation.

            "External app has to send requests to Ninjatrader using ZeroMQ. -> To achieve this I want to create C# dll and captures all the requests from External app via zeroMQ. And internally calls the NinjaTrader.Client.dll to perform the actions. So all the communication between External app and Ninja Trader application has to go via C# dll using ZeroMQ.

            So C# dll method has to asynchronously run infinite times to be able to keep its socket open for incoming messages and hence to capture the request"

            Thanks in advance!

            I would love to hear more about what you learned about building a good messaging integration with NT8.


            HedgePlay





            Comment


              #7
              Hello AroraAbhishek,

              Thank you for your note.

              hedgeplay is correct. You could use TriggerCustomEvent() along with a Timer to run a function asynchronously.

              See the example in the help guide documentation below for more information.
              TriggerCustomEvent - https://ninjatrader.com/support/help...ustomevent.htm

              hedgeplay is also correct about the removal of an indicator and managing the disposal of custom objects in State.Terminated. See the help guide documentation linked by hedgeplay for more information.

              Originally posted by hedgeplay View Post
              My unofficial understanding is that on removal of the indicator NT8 it will automatically manage termination of resources established by the indicator except for generally speaking some drawing objects you have introduced to the charts. You must managed disposal of those objects regularly and in the (State == State.Terminated) { }" block of "protected override void OnStateChange()"

              Good reading at these links and the NT-team has a reference samples if you need it.
              NT8 State Lifecycle - https://ninjatrader.com/support/help...fecycle_of.htm
              NT8 Code Design Best Practices - https://ninjatrader.com/support/help..._practices.htm
              Using Dispose() in NT8 https://ninjatrader.com/support/help...se_dispose.htm
              Let us know if we may assist further.
              Brandon H.NinjaTrader Customer Service

              Comment


                #8
                Greetings to you!

                Thank you very much NinjaTrader_BrandonH and hedgeplay for your replies. It really helped me to came out of that situation.

                I have added TriggerCustomEvent in my indicator code and thus the made changes to call my async function like given in the link you provided and is working now.

                As the function is an infinite loop, it starts when we add indicator in the chart. But the issue now occurring is, it does not stop when we remove the indicator. It stops only when we close the NT8 app.
                Due to this a little change in requirement : Now need to add a button to start and stop the function (infinite loop).

                Please guide me.

                Thanks in advance.

                Comment


                  #9
                  Originally posted by AroraAbhishek View Post
                  Greetings to you!


                  As the function is an infinite loop, it starts when we add indicator in the chart. But the issue now occurring is, it does not stop when we remove the indicator. It stops only when we close the NT8 app.
                  Due to this a little change in requirement : Now need to add a button to start and stop the function (infinite loop).

                  Please guide me.

                  Thanks in advance.

                  Maybe simpler to first try the default stop method in the examples on that same page. https://ninjatrader.com/support/help...ustomevent.htm

                  See the code in "else if (State == State.Terminated)"

                  HedgePlay

                  Code:
                  protected override void OnStateChange()
                  {
                  ...
                  
                  else if (State == State.Terminated)
                  {
                      // Stops the timer and removes the timer event handler
                      if (myTimer != null)
                      {
                          myTimer.Enabled = false;
                          myTimer.Elapsed -= TimerEventProcessor;
                          myTimer = null;
                      }
                  }
                  }


                  Comment


                    #10
                    Hello AroraAbhishek,

                    Thank you for your note.

                    It would be possible to add a button to a chart that calls TriggerCustomEvent when the button is clicked. Please see the attached example script demonstrating adding a button to the chart that uses TriggerCustomEvent.

                    Let us know if we may assist further.
                    Attached Files
                    Brandon H.NinjaTrader Customer Service

                    Comment


                      #11
                      Hi hedgeplay,

                      I have already tried the default stop method on State.Terminated but seems like the infinite loop does not end there.

                      Comment


                        #12

                        Is your processor pegged at this time? When the processor stays pegged I have seen the platform not respond to a strategy disable event from the Control Center for about 10 mins until the processor settled down.

                        If you processor is pegged then that might cause a cascade of unhappy things for you in the platform. So if pegged then I might try a combo of slowing down timing / reducing CPU time per cycle.

                        Personally, I prefer to piggy back off of a variety of existing events and reach for independence by using tick update events from a "data useful" second busy instrument before adding timers. When the indicator or strategy exits, the events stop coming and shut down happens quickly.

                        However, if the CPU and memory loading stays low I would not discourage use of the recommended timer solution for the platform.

                        Hopefully the NT team has answers for you as well.

                        HedgePlay
                        Last edited by hedgeplay; 05-12-2021, 10:06 AM.

                        Comment


                          #13
                          Hi Hedgeplay,

                          Greetings for the day!

                          Thanks for your reply. CPU health looks fine during that time.

                          I am trying to use one function mentioned here : https://ninjatrader.com/support/help..._interface.htm
                          int Connected(int showMessage) but it always returns -1. Can you guide me here.

                          Also, could you please guide me, how to get the following details from NT8 app via C#.
                          1. Price and indicator data . (I believe price can be fetched by using a method mentioned in above URL ) but how to get indicator data?
                          2. Performance matrix
                          3. Information on account being traded.

                          Thanks in advance.

                          Comment


                            #14
                            Hi AroraAbhishek,

                            From earlier threads I have seen I think the Brandon and the NT team will have stronger answers for the API topics.

                            I will just standby and observe on the API topics.

                            HedgePlay



                            Comment


                              #15
                              Hello AroraAbhishek,

                              Thank you for your note.

                              Please see the unsupported example created by my colleague Chelsea in the link below regarding using Connected() from the NT API Functions. Please note that the DLL interface is simply provided as-is and Chelsea's unsupported example is personal a project of his that may be helpful.



                              In regards to your other questions, the DLL interface will not give indicator values, you would need to calculate that on the appication side when you give it data. The DLL interface would not be suitable for questions 1, 2 and 3. You would need to get this information through the platform.

                              1. Price and indicator data. (I believe price can be fetched by using a method mentioned in above URL ) but how to get indicator data?

                              See this help guide documentation in regards to accessing price data - https://ninjatrader.com/support/help...riceseries.htm

                              You could get data about a specific indicator by accessing the system indicator methods for that indicator in a custom NinjaScript.
                              See this help guide documentation for a list of system indicator methods available to use - https://ninjatrader.com/support/help...indicators.htm

                              2. Performance matrix

                              You could use the Trade Performance window to review logged trades in the database. Please note that you would need to use unsupported MySQL DB access to read the database with NinjaScript. Otherwise, you could look at SystemPerformance in a strategy to review strategy performance. The SystemPerformance object holds all trades and trade performance data generated by a strategy.

                              See this help guide documentation about SystemPerformance - https://ninjatrader.com/support/help...temperformance

                              3. Information on account being traded.

                              You could reference the Account tab of the Control Center or use Account.Get in a custom NinjaScript to get information on the account being traded.
                              See this help guide documentation about Account.Get for more information - https://ninjatrader.com/support/helpGuides/nt8/get.htm

                              Let us know if we may assist further.
                              Brandon H.NinjaTrader Customer Service

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by maybeimnotrader, Yesterday, 05:46 PM
                              1 response
                              18 views
                              0 likes
                              Last Post NinjaTrader_ChelseaB  
                              Started by Perr0Grande, Yesterday, 08:16 PM
                              1 response
                              7 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by f.saeidi, Yesterday, 08:12 AM
                              3 responses
                              25 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by algospoke, Yesterday, 06:40 PM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Started by quantismo, Yesterday, 05:13 PM
                              1 response
                              14 views
                              0 likes
                              Last Post NinjaTrader_Gaby  
                              Working...
                              X