Did you know that you can use WPF to add any type of functionality to your NinjaTrader strategy or indicator classes? Well, yes you can. In this article, we will be adding a WPF window form to a strategy and passing values to the form and receiving values back.
So the whole set up consists of a few elements.
AddOn code
XAML code for the form
Strategy code
To start off with lets briefly discuss the addon code where the main magic occurs in the LoadXaml() method which takes care of sourcing your xaml form layout file. In other words inside it, you have to point to where your XAML file is and you use the XAML file to design the elements of your future user interface ie. form.
The XAML form uses standard WPF x:Name = “UIElementName” to assign different elements. You need to know what the names are because in order to get access to them in your code-behind inside the AddOn we are going to use something called the LogicalTreeHelper to find the LogicalNode by name.
So for example, let’s say you have a textbox UI element inside your XAML called FileName. Inside your AddOn LoadXaml method you first need to:
var page = new Page(); create a Page class object
var fs = new FileStream(Path.Combine(NinjaTrader.Core.Globals.UserDataDir, @"bin\Custom\AddOns\YourXamlName.xaml"), FileMode.Open); Create a file stream object from your xaml file.
var fileName = LogicalTreeHelper.FindLogicalNode(page, "FileName") as TextBox; Find the text box UI element inside the XAML.
Overall, once you got the element you can do whatever you want to it. As an example provided, you can subscribe to its events, like we do with the button or pass on data into it as we do with the text box.
You might also notice, that we have a public Strategy object property that can be passed from the strategy class. This exactly what we need to connect the form to any data used inside the strategy class.
So now, it is becoming quite clear that the strategy initializes the addon and passes itself into the addon therefore if you have any public fields inside the strategy class they can be used to pass on data to and from your form.
I hope the article got you started off with WPF for NinjaTrader. Subscribe for updates.