No announcement yet.

Partner 728x90


NT as a Black Box Development System

  • Filter
  • Time
  • Show
Clear All
new posts

    NT as a Black Box Development System

    I am investigating whether NT can be used to develop black box trading systems. I like NT's charting, back-testing, data-vendor interface, and data storage features.

    However, I am not sure how well it can adapt to more complex trading systems which perform statistical analysis on multiple kinds of data aggregated together.

    I have searched through the archives, but am not sure how the NT framework can be used to integrate more complex strategies. I have seen references to the DLL interface and COM interface and .NET/C# but am not sure how they work.

    On a very general level, what I need is a mechanism where NT can communicate with code developed outside the NT Indicator/Strategy setup.

    Is there any documentation/examples available which guides how NT can be instrumented to allow more complex usage of C#/.NET? Does NT7 have some extra features which facilitate this?

    I do want to be able to develop the complex code outside the NinjaScript editor using Visual Studio (Express perhaps). I want to be able to call functions and libraries in my own code, and make it work with NT as a front-end for charting, back-testing, data-management and eventually order management.

    Indepdendent of NT, I have coded up a basic interface to the IB C++ API which can get market data and also have linked in a library of technical indicators. However my current set-up does not have any charting capability and the data management capabilities are primitive. However, I do not want to reinvent the wheel!


    1. You can create DLL's externally and then reference those for access in the NinjaScript editor (right click in the code > References > Add)

    2. Those can be written in any language - unfortunately we don't have examples here as it's outside of what we support.

    3. You can't compile NS scripts in VS, but debug their values through breakpoints -
    BertrandNinjaTrader Customer Service


      Originally posted by NinjaTrader_Bertrand View Post

      3. You can't compile NS scripts in VS, but debug their values through breakpoints -
      I tested that last night and it seemed to work fine.

      Originally posted by NinjaTrader_Bertrand
      1. You can create DLL's externally and then reference those for access in the NinjaScript editor (right click in the code > References > Add)

      2. Those can be written in any language - unfortunately we don't have examples here as it's outside of what we support.
      I am a bit confused about #2 above. The documentation talks about converting Indicators/Strategies written using the NT template which is based on C# into dlls for export.

      I am not sure what you mean by "can be written in any language" above since the documentation only talks about indicators written using the NT template.

      (a) Do you imply that you create an NT indicator/strategy based on NT template, which will LOAD another dll created out of whatever language you chose? What are the compiler options to be used in Visual Studio to create a dll which will work in the NT environment?

      (b) When it comes to code execution, I presume that NT always has control. i.e. NT will call the overloaded OnBarUpdate, OnMarketData methods in the new class, but there is no way for the code to interrupt NT (say to place an order or to update a chart). Any action initiated by the new code has to be contained in those derived methods and is executed synchronously when NT makes the OnBarUpdate or OnMarketData call.

      (c) The reason I ask the above question, is that I expect my processing engine to work in a separate thread/process since I do not want it to block NT's execution in case it takes a lot of time during computation.

      (d) Related to (c), does NT start separate threads for different charts and relies on the OS to time-share? So an infinite loop in one indicator will not put the entire system on hold (though it might hog most of the compute resources).

      (e) Related to (d): On multi-core machines does NT distribute different charts to different cores just in case one chart has an indicator which hogs CPU (an infinite loop).

      (f) In terms of programing limitations, what should and should not be done in the Initialize() method of the custom strat/indicator?

      Working examples which show how the entire setup works (compiler options etc.) will of course be very useful, even if they are not supported (i.e. no follow-up questions are entertained).



        If you mean exporting it as a DLL you can do so by going to File>Utilties>Export NinjaScript and selecting "compiled assembly" instead of "source codes". Is that what you were referring to?
        Josh P.NinjaTrader Customer Service


          Now that I have more clarity of thought let me describe it in a different way.


          As a programmer trying to develop complex strategies, I would like to be able to modularize my code into different components which represent different aspects of the strategy. For that I need to be able to break down the project into different sub-projects for all the standard reasons (testing, version control, etc.). Essentially I want to refer to create and use custom code libraries.

          The Question. How do I do that? I do not care about security/protection right now. What are the
          (a) Visual Studio Compiler/Code generation options to use to make sure it works with NT. I am an experienced developer but not a .NET/C# guru. Perhaps the answer is obvious but what is it??

          (b) Continuing on (a), is it possible to use other languages and compile the code into an NT compatible format. I do not want to use a garbage collecting/memory managing language for some of my work so if I could write some portions in in C/C++ it will be great.

          (c) I presume once the dlls are available the documented ways of referencing them will work.

          An example where you create a custom library, compile it in VS project, and then use it in a NinjaScript file would be great. I could just reuse the VS project settings and get going.


          There are some generic computation I would like to share across strategies. These may not be instrument specific.

          I think it will be as simple as creating a singleton type, creating an instance of it and then making the exported methods accessible to other clients.

          For example, a class which provides a method which returns a number calculated based on the current value of the major market indices.

          What I think I need to do is to create an indicator which subscribes to the relevant market data (say SPY, IWM, DIA etc.) using the multi-instrument methodology and performs the relevant calculations which are then available to other clients.

          Internally, will NT share the same instance of the indicator for all the charts, or will it create a new instance of the class for every chart which references it. This is important since some of these global methods may do a significant amount of computation and as such are not meant to be duplicated. The computation is truly designed to be shared. Think in terms of a server providing information to a client.


            Attached is a sample Visual Studio Solution which can be used to create custom indicators and/or strategies, compiled into dll which can be dropped into bin/Custom folder
            Attached Files


              concering (B):

              You can use a static class as a container for your globally acessible parameters. In every place of your code where you access that class you will retrieve the same value.

              You need however a server-indicator-or-strategy-or-marketanalyzercolumn that must be always running and fill/update the static class.
              This is all C#, so you better study well msdn and the C# docu.
              You can interface with C++ also, but my advice is to convert to C# as much as you can (I did so with my code).

              I just read now that you are interested in a background calculation engine. I have done this for 2 of my projects, one is a clustering algorithm and the other is download of COTReport web pages.
              Both these tasks are run as a Thread that is put into the application ThreadPool for execution.
              This is all advanced C# , look for the MSDN docu on this, and I am glad it exists in C# as I find it so much easier to implement threads than in C++ (and i developped multithreading programms wiih my students in C,C++ in the 90s on a dual pentium pro). OK, I am not making my living from coding, but I advice you to just forget the old stuff and start fresh with C#)
              Last edited by zweistein; 12-03-2009, 03:30 AM.


                Thank you for the project. I will try it out.


                Thanks for the tips and how they work.

                I guess I still have some reservation about letting go of memory control having spent a decade optimizing complex programs solving NP complete problems.

                I am planning to stick to C# in the short term. However, there is another part of me which wants to keep the code as general and portable as possible so that I can reuse whatever I have developed here in not MS environments.


                  Questions about Multi-Instrument, Event handling

                  1. Asynchronous Event Handling
                  One of the features I am looking for in the black box is asynchronous event handling capability where my external code can ask Ninja Trader to respond to an event while not waiting for the OnBarUpdate/OnMarketData to occur.

                  I noticed that Folls has created a project which has an event handler to respond to an input generated from a custom form.

                  I wanted to understand how the event handlers are processed by Ninja Trader.

                  (1) I presume Ninja Trader will trigger the execution of code registered as the event handler once the event is triggered (with the delay associated with OS/MS implementation of even handlers). It will not wait for any of the standard calls (OnBarUpdate, OnMarketData etc.) to activate before checking for the event and executing the callback function.

                  (2) I presume the callback can be used to call all other NT functions, including functions related to order handling.

                  Any other information which is of relevance?

                  2. Multi-Instrument/Multi-TimeFrame Indicator

                  I noticed that I could not use the default Add Function to add a new data input apart from the default when creating an indicator. The IndicatorBase.Add function creates a new line object; there does not seem to be a function similar to StrategyBase.Add visible in the default configuration for an indicator.

                  So to create a multi-timeframe or multi-instrument indicator, I will have to create a dummy strategy which does not have any order handling feature but can be used to calculate certain values?

                  (a) If I need to draw something on the chart based on this strategy, what method do I use?

                  The StrategyBase.Add has 3 variants, two for adding data-series (periodType, period) and (Instrument, periodType, period) and the third one which accepts an Indicator Type. However, none of these will work if I want to plot the values of a DataSeries calculated in the strategy.

                  3. Exporting Indicator Values and Calculated Variables

                  I will appreciate if you could point me to documentation/examples on how to create variables which can be accessed by other indicators/strategies (export/public).

                  4. NT Run Time Handling of different Indicator/Strategy Objects

                  I am trying to develop a better understanding of how NT puts everything together.

                  From what I understand so far:

                  For EVERY Chart currently Open:
                  NT will create a NEW Instance of the Indicator Class Object or Strategy Class Object which is active for the Chart.
                  For EVERY Indicator/Strategy which refers to another Indicator:
                  NT will create another NEW Instance of the Indicator Class Object which will provide values to the calling indicator Class Object.

                  (1) I presume NT will ensure that the methods OnBarUpdate, OnMarketData are called in the proper recursive manner.
                  i.e. If IndictorA, calls IndicatorB, then prior to calling the OnBarUpdate method of IndicatorA, it will call the onBarUpdate method of IndicatorB to ensure that the values supplied by IndicatorB to IndicatorA are fresh.

                  (2) There is no sharing of Indicator class objects between charts. i.e. if Chart1 plots IndicatorA on 1 min bars for InstrumentX, and Chart2 also plots the same (IndicatorA on 1 min bars for InstrumentX), then two separate objects of type IndicatorA will be created internally and there will be no sharing of computation/data-structures

                  (3) If the user desires the reuse of computation across different instances of objects of the indicator class, they will have to explicitly manage it.



                    1. Unfortunately this is outside of our supported scope.

                    2. Correct, MultiSeries indicators are not supported with 6.5, we add this for 7 -

                    To plot from NS strategy, please see this -

                    3. Plots would be exposed per se, other variables and series can be exposed with this approach -

                    4. It depends if you're in realtime or backtesting mode, you can work with Update() in backtesting to update embedded values -

                    Correct, no cross communication of indicators or strategies.

                    The .NET framework would manage duplicate class calls in the internal cache, so if you call the same indicator on the same chart again using same paramters, it would be cached.
                    BertrandNinjaTrader Customer Service


                      Hi Bertrand:
                      Thanks for the message. It answers a lot of questions.

                      (0) I would like to get access to NT7 because I do need multi-time frame and multi-indicator. I am not placing real trades right now so can live with a Beta; I am more interested in exploring the capabilities and tool-set than anything else.

                      (1) Regarding the asynchronous calls: I am not interested in opening forms etc. What I want is to trigger the call back from my library code without having to WAIT for an even triggered by the NT executable.

                      Are there any restriction imposed by NT when it comes to handling asynchronous EventDriven callbacks. For example, a restriction could be that order handling HAS to be restricted to NT initiated methods (OnBarUpdate, OnMarketData etc.) or that any chart update or a plot has to be called within these functions.

                      (2) Plot Function: I also wanted to know what specific events trigger NT to call the Plot function in a custom indicator/strategy.



                        A comment to your topic 1.(1)
                        An event will not be delayed until one of the NT standard calls is executed (OnBarUpdate). It can be verified by trigger a custom event on a chart with no data feed connected (no OnBarUpdate is activated). Custom events would be executed in such a situation.



                          Hi Ralph:

                          Thanks for your message.

                          I am concerned that the behavior might be different with a live data-feed.

                          In general, a "DOs and DONTs with C#/.NET with NT" document will be a great help. In can understand why NT does not want to support extensive C# coding outside their framework. However, they can certainly assist power users in expanding the scope of NT by providing guidelines for best practices to use when using the expanded feature set of C#/.NET.


                            Originally posted by aviat72 View Post
                            I am concerned that the behavior might be different with a live data-feed.
                            Yep, who knows. At least it works with my application, although it is not tested comprehensively because I don't know how to cover every condition possible. There is one hint in the NT documentation, that such kind of operation is supported: TriggerCustomEvent


                              Originally posted by roonius View Post
                              Attached is a sample Visual Studio Solution which can be used to create custom indicators and/or strategies, compiled into dll which can be dropped into bin/Custom folder
                              I've compiled this DLL and dropped it into the bin/Custom folder, but the indicator is not listed. Is there anything that has to be done to make this visible or am I misinterpreting your post and you must still use Ninja Script in order to make the indicator visible.


                              Latest Posts


                              Topics Statistics Last Post
                              Started by gbajaj, Today, 03:35 PM
                              2 responses
                              Last Post gbajaj
                              by gbajaj
                              Started by space_trader, Today, 03:19 PM
                              2 responses
                              Last Post space_trader  
                              Started by max1ci6, Today, 02:02 AM
                              4 responses
                              Last Post joromero  
                              Started by Johnny, Today, 02:38 PM
                              1 response
                              Last Post NinjaTrader_Tyler  
                              Started by M_ichel, Today, 02:26 PM
                              1 response
                              Last Post NinjaTrader_Tyler