Create a Custom Order Management Class in NinjaScript
Learn how to create a custom class in C# and NinjaScript that will make managing trade exits more streamlined. In this article we will discuss how to do this and why you want to.
Today’s post covers creating your own stop and profit orders in NinjaScript using a custom class. This makes it easier to implement multiple exit types into a strategy for research purposes. It also makes the code easier to reuse for future strategies. This will be one of a series of articles covering changes to the underlying code framework at HGT.
Disclaimer: the following post is an organized representation of my research and project notes. It doesn’t represent any type of advice, financial or otherwise. Its purpose is to be informative and educational. Backtest results are based on historical data, not real-time data. There is no guarantee that these hypothetical results will continue in the future. Day trading is extremely risky, and I do not suggest running any of these strategies live.
Code
There are three different order classes to create: a static stop loss/take profit (SL/TP), a trail by indicator (ATR), and a reset trail. Since our strategies are designed to keep track of our order objects, we need to be able to return multiple values back to the strategy. We will use tuples to accomplish this.
A tuple is a data structure that can hold a fixed number of items. Each item can be a different type. They are useful when you want to return multiple values without creating a separate class. This is particularly handy when developing trade systems as we often need to return multiple pieces of information from a method, such as an entry and exit order or an entry order and an SL value.
Trade Manager Constructor
This first section is the class constructor. When we instantiate the trade manager object in the strategy, we must pass in a reference to the strategy object. This will allow us to interact with the strategy methods and access any information present in the strategy. Because we are instantiating it with a reference to the strategy, we don’t need to worry about updating the object in the strategy on each bar update.
This is where we will start.