I've seen many posts and inquiries about using python when developing indicators and strategies in NinjaTrader. I also came to the point when I needed to use python for my own project in NT8. Unfortunately, I couldn't find any available solution. Therefore, I managed to create my own solution that I'm sharing it with you.
I've created NT AddOn called NTPythonIntegrator which enables us to use python in NinjaScript. Solution is based on Pythonnet (https://pythonnet.github.io/pythonnet/dotnet.html).
Full source code for this AddOn are in the attachment (you'll need Visual Studio to open it and build it).
Disclaimer: Souce code is given without any warranties and with any support. You can freely use it as-is, for your own projects. It is not allowed to use given source code for commercial products.
Steps to install and configure python for NT8:
Prerequisite: you need to have installed python version 3.7 or higher (this is required for pythonnet to work correctly)
1. Open NTPythonIntegratorAddOn solution in Visual Studio
2. Open Project Properties -> Build Events: Here I've added post-build commands to copy required dlls to the NinjaTrader custom folder. You need to change the destination folder to point to the NinjaTrader\bin\Custom folder on your computer
3. Build solution. Make sure that NinjaTrader application is not running when building VS solution since it will copy some files to NinjaTrader\bin\custom folder.
4. If build was successful and all required files were successfully copied, open NinjaTrader app. NinjaTrader will recoginize that there is a new AddOn available and will ask you to confirm it.
5. You can start the AddOn from the Control Center window: New->FalcoTrader AddOns -> PythonIntegrator and the following window will open:
6. In this window you need to select PythonDLL file for your python installation. Also, if you are using virtual environments in python you can select virutal environment to use
7. Pressing Initialize button, will initialize Python Engine and will enable you to use and run python code within NinjaTrader. Shutdown button, shuts down PythonEngine.
8. Open NinjaScript editor (any script) and open "References" dialog. Add new reference: you need to add Python.Runtime.dll which is located in NinjaTrader8\bin\Custom folder. Add reference and close the "References" dialog.
So now you are all set to use python in NinjaScript but there are some things that you need to be aware of and follow:
1. PythonEngine needs to be unloaded (shut down) when you are compiling NinjaScript. If PythonEngine is initialize and you press Compile button NinjaTrader app will crash. So proper (although sometimes tedious) development cycle is: make changes to NinjaScript -> Compile -> Initialize Python Engine -> run script to test it -> Shutdown Python Engine-> make changes to NinjaScript -> Compile NinjaScript -> Initialize PythonEngine -> run script ... - you get an idea
2. In NinjaScript you need to add using Python.Runtime; declaration in order to use Pythonnet in the script
3. In your indicators or strategies I suggest to always perform tests if PythonEngine is initialized before using and pythonnet code. Otherwise you can get into all kind of troubles.
Example code would be:
try
{
if(PythonEngine.IsInitialized)
{
using(Py.GIL())
{
// run some code which includes python
}
} else
{
// run some code when PythonEngine is not initalized
// i.e set indicator value to 0
}
} catch (Exception e) {
// catch exception
Print($"Python initialization Exception, {e.Message}, Stack Trace: {e.StackTrace}");
}
_pythonModule = Py.Import("Module");
I hope you'll find this post and code useful.
If you find any errors in the source code and/or you make improvements please post your source code version here for everybody.
Thank you and happy trading.
FalcoTrader
NTPythonIntegratorAddOn.zip


Comment