Using NTDirect.dll and ExternalDataFeed NinjaTrader 7

NinjaTrader has an Exernal Data Feed connection type. To connect to it, one would use the NTDirect.dll. Below is an example c# console application that writes a simulated data feed to a symbol “TEST”.

First, a few words on the DLL

  • It is a standard, unmanaged dll, not a dotnet assembly or anything like that. To use it from a .net application, you use the directive [DllImport(“NTDirect.dll”)]. You don’t reference it like you would an assembly. The NTDirect.dll resides in your system32 folder so its on the search path. See more on DllImport on Microsoft if you’re interested.
  • It exposes a lot of methods some for trading, some for retrieving information from Ninja and some for setting values for a symbol. The program below only makes use of the last type of methods.
  • There are 3 methods I found that allow to update a custom feed – Ask, Bid and Last. The program below makes use of the Last method.

Outline of what we will do and how to set things up

First we set Ninja to use the “External Data Feed” connection. In the instrument manager, we define a new symbol “TEST”. This is the name of the symbol we will be feeding. Next, we add TEST to the default symbol list and open a chart with TEST.

Finally when we run our program, it will start feeding Ninja. If all went well, you should then see chart bars start to move up and down.

See this video for a quick step by step

The code

The application is simple. Its a c# console application that loops infinitely and on each iteration will:

  1. Sleep for 2 seconds
  2. Calculate a new random price for TEST
  3. Update Ninja

Most of the code below is really the program wrapping stuff and the generation of the random prices.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Runtime.InteropServices;
using System.Threading;

namespace NT7ExternalDataFeed2
{

    class Program
    {
        [DllImport("NTDirect.dll")]
        public static extern int Last(string instrument, double price, int size);

        [STAThread]
        static void Main(string[] args)
        {
            // used to generate randome movement in price
            Random rDelta = new Random();

            // prices start at 100 and will randomly move and down within the while loop below
            double lastPrice = 100.00;

            // direction determines the movement, 1 is up, -1 is down. start with up
            int direction = 1;

            bool go = true;
            while (go == true)
            {
                Thread.Sleep(2000);

                // generate a random LastPrice;
                direction = direction == 1 ? -1 : 1; // if last move was up, this move will be down and vise versa
                int delta = rDelta.Next(5);
                lastPrice += delta * direction;
                System.Console.WriteLine("Direction " + direction + "Delta " + delta + "Last Price " + lastPrice);

                // call Ninja and set the last price with a volume of 100
                if (Last("TEST", lastPrice, 100) == -1)
                {
                    System.Console.WriteLine("There was an error calling ninja - terminating execution");
                    break;
                }
            }
        }
    }
}

Download

Link to visual studio project

Some final comments

By default, NinjaTrader does not store the data feed for historical or replay purposes. So once you close the application, the data is lost.

In order to have Ninja store the data feed, under Tools->Options, navigate to the Data tab and check “Save chart data as historical” and “Record for market replay”.