Skip to main content

pjsIndies – advanced Ninjatrader strategy usage via pjsAPI and sample strategy

Some of you will know or have noticed the following optional settings in many of the pjsXXX indies.


What are they? What do they do? Well, this is my attempt to explain all that to you.

Firstly, a little background, and, please forgive me if this gets a little boring or technical. It really is considered advanced NinjaTrader coding, and I do not expect everyone to follow along with this, but I think it is worth explaining why I took this route with my own code.

NinjaTrader strategies are typically built by loading an instance of the indicator into your code and attempting to read data they share (plots), via the plot series. For me, that is not scalable and certainly not efficient. Here’s why. You quite possibly have an instance of the indicator already on your chart. That indicator uses quite a lot of resources already. For every ‘series’ that indicator has, you have multiple data points for every single bar. All the values in that indicator are also being calculated, Ninjatrader itself has to maintain the status of this indie, series, etc. Now, consider that if you use ‘BUILDER’, you probably end up with several new instances of this indie running in your strategy all repeating the same calculations and taking the same resources, simply to get the values that are already on your chart. Make sense? No, it did not to me, either, especially if you are looking to share point-in-time info, rather than the entire historical dataset. Why do I need another instance of the indie and a series for 1 value?

So, my preference was something different. Over the years I developed this API, which efficiently shares data between indicators and strategy code without having to spawn new instances of it all over the place. it is blazingly efficient. Many of my indies already use it, because it is what I use today. In addition, what you see is what you get. If that indie is on the chart, you can see the data and that is what is being used. I have developed several useful tools around it, and it’s possible I might also share some of those in future. This article however is all about how you might use this in your own code.

Each indicator that participates has an indicator ID, to identify it. This is an integer number between 1 and 40

Each indicator that participates has an instrument ID, to identify the instrument it is being used on. This is an integer number between 1 and 40

All ID pairs MUST be unique.

That’s it. If the options are enabled in the indicator settings, the indicator will feed data to the API that you can read in your own code. Typically, the least information is a BIAS output that will normally be 0 for none, 1 for long, -1 for short. Other values may also be available including levels and zones.

I have included a sample strategy for NT8 below. Here is a chart with the strategy running in playback

Ninjatrader advanced strategy

This uses the licensed pjsHeikenAshi indicator and assumes you have the instrument ID set to 3, and the indicator ID set to 14. If added to the chart, it will overlay the current BIAS and trend strength as shown in the image. It will also print a list of supply and demand zones to the NinjaTrader debug Output window (assuming you enable zones in the HA indie settings). This code is a simple example of how you might use the API and indicator in your own code. At it’s simplest, this may be -1 for a red bar and 1 for a green bar, but as you reconfigure the options in the indicator, this can become more of a 1 for uptrend, -1 for downtrend, etc.

Please note that NO instance of the indicator is loaded inside of the strategy as would traditionally be done. It relies on the instance on your chart (any chart!). This makes it very efficient, but the downside to some (which I do not care about), you cannot backtest this via the strategy analyzer – You must use replay if you do backtesting. This code only works in ‘live’ environments. Also, be aware – If you recompile your code in NT, and this includes importing new code, your add-ons may not be ‘synced’ with your ‘other’ code. Either refresh all your charts/strategies to get them on the same page, or restart NT after a re-compile.

Here is a link to the code (zipped) . This is standard NT strategy code and goes into the strategies folder. Do not try and ‘import’ this code!

Please note – Due to the more advanced nature of this topic, my support is limited. I want to share this functionality with users that might use it, but the indies, as always, are provided ‘as is’. Fully licensed users can seek some advice via the relevant discord room (‘the-labs’ is probably best for api techie stuff!) if they wish. Strategy development is a complex task and a complete project own it’s own.

Some of my much older code (many years) may use an older version of the API, which this example may not apply to.

All the code and information here is supplied ‘as is’. You may freely use the API with any licensed indie that supports it. In the future, I may cover adding the API to your own indies. Support for this is already in the api you have.

Any general Q’s please feel free to comment below. Any specific coding advice etc., probably best to use the-labs as mentioned above.

Have fun

Paul.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.