Encog NinjaTrader Getting Started
Encog is an advanced Machine Learning Framework that supports Neural Networks and other Machine Learning algorithms. Ninja Trader is an advanced market analysis and execution program that can be used to analyze market data and even perform automated trading. This introductory article will show you how to install the Encog Framework Indicator for Ninja Trader 7. This indicator allows communication between Ninja Trader and an Encog Application. The Encog Application can be either the Encog Workbench or an application that you create. The Encog Framework Indicator allows you to use Encog to create indicators for Trading Platforms, such as Ninja Trader.
This article will focus on how to get the indicator installed and export data from Ninja Trader to the Encog Workbench. You can also create Java or .Net applications that communicate with the Encog Framework Indicator. Even though Ninja Trader requires that indicators be written in C#, you can write your Encog based indicator in either Java or C#. The Encog Framework Indicator for Ninja Trader is written in C#, but uses sockets to communicate back to an Encog Application.
Contents |
Required Software
The Encog Framework Indicator was added to Encog 3.2. Encog 3.2 is currently under development, and has not yet been officially released. However, most of the code necessary to use Ninja Trader is already available. If you do not mind keeping up with the releases of the development version of Encog 3.2 you may enjoy trying out some of this "prerelease" software. Otherwise, you can wait for the official release of Encog 3.2.
You will need to download the following components to use this article.
- Encog Framework Indicator - Download Encog Nina Indicator
- Encog Workbench - Download Encog Workbench 3.2
We will begin with installing the indicator.
Installing the Encog Ninjatrader Framework Indicator
First, make sure that you do not have a version of the Encog Indicator already installed. If the indicator is already installed, you should delete it first. Choose Tools->Edit Ninjascript->Indicator.... You will see something similar to the following.
If you see an indicator named EncogFrameworkIndicator choose Delete. If you do not see the Encog Indicator, there is nothing to delete. Either way, you can close the above window.
You are now ready to import the Encog indicator. To do this choose File->Utilities->Import Ninjascript.... This might bring up the following warning.
This is good advice. Ninjascript is regular C# and can do nearly anything. It would be easy to write a virus in Ninjascript. Choose Yes if you would like to install the Encog Indicator. This will take you to the following file selection dialog.
Navigate to the ZIP file that you downloaded previously. Your filename may be slightly different than the above, depending on the current version of the indicator. If the import is successful, you will see the following alert.
The Encog indicator is now installed and ready. The next step is to launch the Encog workbench to configure it to allow the Encog Indicator to connect to it.
Preparing the Encog Workbench
If you have not already done so, install the Encog Workbench. You can find more information in the article Installing Encog Workbench. Once the Encog Workbench loads, choose to create a new project. This will create a project with an empty directory. You are now ready to configure the workbench to accept connections from indicators. This is done in the Encog Workbench configuration dialog. Choose Edit->Config.... This will show you the following dialog.
Choose the Indicator tab. Fill in the values as you see above. The port 5128 is the default Encog Indicator port. If this port is already in use, you will need to choose a different one. If you try to use 5128 and it is already in use, you will get a bind error. Clicking the checkbox tells Encog to attempt a connection. Once you choose OK and close the dialog box the workbench will attempt to bring up a socket based server, that will wait for connections. Additionally, every time you launch the Encog Workbench this server will start. You do not need to enable it every time you launch the workbench. The following message will be displayed if Encog has started the server.
The server is now up and indicators are free to connect. If you would ever like to see what clients have connected to your workbench, choose View->Connections.... This will display the following tab.
The above tab shows a connection. At this point you will not have a connection, and your tab will be empty.
You are now ready to download data from Ninjatrader. Choose Tool->Generate Training Data.... This will open the following dialog.
Scroll to Import from Indicator. Choosing this option will open the following dialog.
Here you can choose what data items you want. By default we are using HIGH, LOW, OPEN and CLOSE. The number between the brackets specifies how far in to the past we want to go for each bar. You can also make use of 3rd party indicators here. For example, to use MACD, specify the following.
For more information on using 3rd party indicators, see the article on the Encog Framework Indicator.
MACD(12,26,9).Avg[1]
The Encog Indicator will make use of C# Reflection to locate and use the 3rd party indicator.
Once you start the indicator, you will see a tab showing the progress.
You are now ready to switch back to Ninjatrader and make use of the Encog Indicator.
Using the Encog Indicator
At this point you should have the Encog Framework Indicator installed and the Encog Workbench waiting to download data. It is now time to make use of the indicator. Indicators are used primarily in two places in Ninjatrader.
- Indicators are used on charts
- Indicators are used as the basis for strategies
For this example, we will place the indicator on a chart. The indicator will download all information sent to this chart. This is a popular means of exporting data from Ninjatrader. There are several third party indicators that are designed to download financial data and other indicators to files and databases. The Encog indicator will send this data over a socket to an Encog Application. For this example, the Encog Application is the Encog Workbench.
To begin choose File->New->Chart.... This will open the following dialog.
It is necessary to tell Ninjatrader what you would like to chart. For this example, I choose to chart Apple Computer, which has the symbol AAPL. Use the input box near the type and type the symbol. To add it to the chart choose New. Now that you have one security added to the chart, choose OK. This will show you a chart of Apple, you can see this chart here.
We will now add an indicator to the chart. To add an indicator, use one of the tooltip icons. The indicator icon is the third from the right. If looks like a small chart with a red and green line. This will open the indicator dialog shown here.
Scroll down and find EncogFrameworkIndicator. If you are using a later version, the name may be slightly different. Once you select the EncogFrameworkIndicator you will be able to edit its options, as seen above. Scroll to the top to see Encog's options. You can leave the host set to localhost and the port set to 5128, if you have not changed these options. It is possible to have the workbench running on a different machine. If this is the case, you will need to enter the address of that remote machine. You can now click New to add the Encog indicator. You can choose OK to add the indicator. At this point, the indicator will attempt to connect to the workbench. You should see something similar to the following.
As you can see, the output from the Encog indicator is displayed in a separate pane. If you would have performed to overlay you could have chosen a different target pane in the previous step. The download indicator does not display any output, other than a message saying not to expect output. You can see that message above. If any error occurred, you should see that message displayed.
You should now see that the workbench has downloaded some data. The workbench should now look like the following.
You can choose to stop the download, and the downloaded CSV File will be available. The contents of my download are shown here.
"INSTRUMENT","WHEN","HIGH","LOW","OPEN","CLOSE","VOL" "aapl",20110502150000,350.47,345.5,349.74,346.28,15811178 "aapl",20110503150000,349.89,345.62,347.99,348.2,11198607 "aapl",20110504150000,351.83,346.88,348.26,349.57,13901747 "aapl",20110505150000,350.95,346.05,348.4,346.75,12009336 "aapl",20110506150000,350,346.21,349.69,346.66,10008755 "aapl",20110509150000,349.2,346.53,347.86,347.6,7312593 ... "aapl",20120315150000,600.01,578.55,599.61,585.56,41416824 "aapl",20120316150000,589.2,578,584.72,585.57,29481697 "aapl",20120319150000,601.77,589.05,598.37,601.1,32186970 "aapl",20120320150000,606.9,582,599.51,605.96,29166719
Additional Information
Some additional links, that might be helpful. More will be added soon.
- Encog Homepage
- Neural Network Forums
- Encog and Ninjatrader
- NinjaTrader
- [http://www.heatonresearch.com/content/non-mathematical-introduction-using-neural-networks A Non-Mathematical Introduction to Using Neural Networks
- Neural Networks and Prediction Video














