AI Shadow Probe (Free Machine Learning Strategy)

This free NinjaScript strategy will place simulated trades behind the scenes to feel out the current market conditions. When your defined tracking time starts each day, the Shadow Probe will enter and track hundreds of simulated positions with every stop/target combination in a range you define (e.g. if you set the range to 2-100, the Shadow Probe will run simulated trades with a stop/target of 2/2, 2/3, 2/4…… all the way to 100/99, 100/100. With this Shadow Probe data you will know the most profitable S/T combination for the day, the most profitable S/T combination under certain conditions (i.e. the S/T combo that performs the best when the ADX is at a certain level), the most profitable S/T  combo over the last x minutes, and an infinite number of other ways you can think of to utilize the probe data. It comes out of the box with generic entry criteria (see the “entry logic” region), so you’ll need to add whatever criteria you like to use for direction filtering. The “entry logic” region is where you’ll want to start, just replace the example logic there to start experimenting.

If you’re looking for an indicator version you can find that here. The indicator version has properties that are accessible via strategy builder or manually coded strategies.

This NinjaScript download is a single zip file that you import directly into NinjaTrader. It’s not assembled which means you have full access to view and change the source code either in NinjaScript Editor or your preferred way to edit .cs files.

Free vs. Premium Version:
This free version is fully functional and ready for you to use or build off of, but it’s missing a few advanced/powerful features that you will find in the premium version. The free version only places simulated trades serially (i.e. a new 5/6 S/T combination probe trade will only be entered after the last 5/6 S/T trade has been exited. This means that on a wide stop/target combination like 25/25 you might only get a data point every 10 minutes or more for that combo. The Premium version can track multiple probe positions for a single S/T combo simultaneously. You define a time offset variable that will determine the number of seconds between simultaneous probe entries. This means you can sample a new trade every second for a 25/25 S/T combination if you want – this higher sample rate gives you a more realistic view of the current market conditions. The premium version also comes with built in tools to track only the most recent (last x) probe trades instead of keeping a running total for the whole day. You’ll also find tools to clear the probe tracking history on certain events (e.g. every 30 minutes, when MACD hits a certain value, when SMAs crossover), and more. The tools are fully explained in comments in the code.

Details / Instructions:
This strategy must be ran on a 1 tick data series. If you need to access a different time series in the same strategy you can copy and paste this free code to add a secondary time series.

When you enable this strategy with the NinjaScript Output window open, you will immediately see a printout of every stop/target combination that will be tested along with its corresponding index. Technically this index refers to the index of the array that stores your Shadow Probe trades. This information will be handy if you dig deeper and augment the code. As the strategy runs the output window will update with every real-time entry, showing its stop, target, and index. By default, these entries are based on the best-performing Shadow Probe stop/target combination for the day, but you can add to or change the entry criteria in many ways and change the Print() commands to reflect what you want to see. At the end of the day the output window will show you the S/T combination that had the highest daily net profit, both for longs and shorts.

Example output window:

Observing the Print() statements in the NinjaScript code will help you understand how to access the different data available in the Shadow Probe array.

Ideas to augment the probing:

Try lowering the stopTargetMax variable so it’s not tracking S/T all the way to 50.

You could also change this dynamically based on the current volatility or magnitude of recent swings. Leave the stopTargetMax at 50. In the Analyzing Shadow Data region there’s a for loop that finds the max net profit. By default, that loop runs through the whole array to find the max net profit, but you could rewrite that so that it only looks at S/T combos in the array that are <= the size of recent swings.

Try tracking volatility and only run the shadow tracker when volatility is < x. See attached txt for volatility tracking code.

Entering per max recent net profit is just a basic example. You can analyze the array in many other ways other than looking at net profit. You could try entering based on:

A S/T that has a 100% win rate over the last n trades (some might say this is the gambler’s fallacy, but you could say that a S/T with a 100% win rate over the last x trades is more dialed into the current conditions).

Use the break even values of each S/T and enter with the S/T that has the greatest win % over the break even %

Max average profit.

And all of these things (I believe) should only be tracked over a finite moving window (like what profitHistLength does).

One thing you might consider is tracking the time between shadow trades. A high S/T like 50/50 might go an hour before it’s hit depending on your market, which makes it irrelevant when the time frame you’re interested in is in minutes/seconds.

This code will not work with releases prior to NinjaTrader 8. Click here if you don’t know how to install NT strategies. Use at your own risk, and backtest backtest backtest before you go live with any strategy.


    1. Hi Alberto, I wrote it to be back-testable with market replay. I don’t believe it will work with the strategy analyzer. Strategy analyzer gives very inaccurate results, so I’d definitely switch your backtesting method to market replay. Definitely slower, but SA is pretty much junk.

  1. Hello again.
    the strategy is excellent. It is possible to obtain more information about the basis of the entries


    1. Hi Alberto, if you take a look at the “entry logic” region of the code you’ll see two different entry systems (one is only available in the premium version) and a little commented explanation. The one that comes with the lite version enters with the stop/target combination that has the highest shadow profit for the current day (e.g. if the a stop value of 12 and target value of 4 in the long direction has outperformed all of the other possible S/T combinations long or short that the shadow probe has tested, then it will enter a real order that’s 12/4 and long. It will continue to do this until a new S/T combination takes the profit lead for the day. It’s reset at the end of each trading day. The other enters with the stop/target combination that has the highest shadow profit over the last x number of shadow trades. This shorter lookback period is a more adaptive view of the market since market behavior is hardly ever consistent throughout the whole day.

    2. Forgot to mention that the entry logic is meant to be changed or have your own flavor added. I wrote the simplest entry criteria to be the default, but there are many directions you could go instead. By doing simple math on the data that’s stored in the shadow probe arrays you could for example:
      enter on highest win % over last x trades
      enter when there have been no losses over the last x trades
      enter on highest average profit

      Or you can simply add to the existing logic a built in indicator like a moving average or MACD to further filter your entries.

  2. I came across your page, and I’m trying to understand how this shadow logic strategy works. In reading, it appears that I have to initiate the ninjascript editor while the strategy is active within a chart? Does the chart have to be a 1 tick chart? That seems difficult to trade? Is that why you recommend using the code to add another time series? How long does it need to run before it will initiate a trade? It looks like within the code, the shodow probe runs by default for 15 minutes and then initiates live trading? Have you ever done a video to show how to utilize or set this up? Sorry for all the questions, but just trying to understand this very interesting concept. thank you for sharing; looks brilliant.

    1. Hi Gerry. If you load the zip file into NinjaTrader using tools>import in the control center the strategy will be available in the ninjascript editor. From there if click the compile button the strategy will be available in any strategy list (from your chart or control center strategies tab). I believe out of the box it’s set up to track for 45 minutes before trading. See the startTrackingTime and startTradingTime variables. It does need to run on a 1 tick series. You only need to add a second series in the code if you need to do something programmatically on a different time frame. I would recommend running the strategy from the strategies tab on your control center, that way it will be independent of your chart if you need it for visual purposes.

Leave a Reply

Your email address will not be published. Required fields are marked *