AI Shadow Probe (Machine Learning Strategy)

Description:
This premium 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, all simultaneously. 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. 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 premium version is fully functional and ready to trade. Don’t trade with it as-is though! The premium version has some advanced/powerful features that you won’t find in the free version (go ahead and give it a test run). 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.

Please write me at info@free-ninjatrader-algo-code.com if you need help working with the code or if you have any suggestions about changing or adding to it.

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 example:

#region Volatility
  // Collecting high/low from last x bars	
  volMaxList.Clear();
  volMinList.Clear();
  for(int i = 0; i < volPeriod; i++)
  {
    volMaxList.Add(High[i]);
  }
  for(int i = 0; i < volPeriod; i++)
  {
    volMinList.Add(Low[i]);
  }	
  // Finding max and min				
  volRangeMax = volMaxList.Max();
  volRangeMin = volMinList.Min();
  volatility = ((volRangeMax-volRangeMin) *4);			              
  // Set volatility levels
  if (Position.MarketPosition == MarketPosition.Flat)
  {	
    volatilityLevel = "low";          
    if (volatility > 6)
    {				
      volatilityLevel = "medium";
    }
    if(volatility > 10)
    {					
      volatilityLevel = "high";						
    }
  }		  
#endregion

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.

Notes:
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.

10 comments

  1. Hi, I’m interested in your AI Shadow Probe (Premium Machine Learning Strategy). If I purchase the strategy, will I also be able to receive any future updates or enhancements you make to this strategy?
    Thanks, Marty

    1. Hi Marty, when you make a purchase you’ll receive credentials you can use at the ‘log in’ link up top. You can log in any time to access the latest version of any code you’ve purchased. If there’s an update to any of your items we’ll send an email to notify you.

  2. Hello – I purchased this today. You reference Volatility tracking code in the above description. Where can I find a link to this?

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

    1. Hi Andrea, Thanks for purchasing. I’ve corrected the typo and entered the example volatility code in the post above. Paste that region into your strategy and you can use the volatilityLevel variable in your entry logic to filter out different volatility levels.

  3. Any suggestions on how to improve results? This is with a shadow learning time from 8am-12pm on the ES.

    “End of day
    The most profitable Shadow Probe Stop/Target combination for LONG trades for the whole day was: Stop: 16 / Target: 16
    The current day’s P/L for LONG probes with this S/T combination was: -4331.28
    The most profitable Shadow Probe Stop/Target combination for SHORT trades for the whole day was: Stop: 16 / Target: 14
    The current day’s P/L for SHORT probes with this S/T combination was: -5431.52

    1. Hi Andrea, running the shadow probe by itself isn’t always enough to produce a positive P/L. The shadow probe is meant to augment your existing trading style/entry logic. Say, as a simple example, your method is to enter long when the 20 SMA crosses above the 50 SMA. How do you determine the stop/target value? Most traders feel this out or use a set number for the instrument they’re trading. The probe is designed to determine the best stop/target for you based on current market conditions, taking the guesswork out. In other words, this strategy can help you find the best stop/target values, but the entry logic is all up to you.

  4. Will the probe constantly update in the paid edition to reflect current market conditions/sentiment? I’ve noticed with the free version that once it sets a bias early in the session, it only trades in the direction of the bias settled on? Does the paid version do the same thing? Or is this just the entry condition out of the box? It’s very rare that the market just moves in one direction from the open. Out of the box on the ES, with no programming changes, it actually is profitable to some extent over the past 4 days on the live market, but I can see where filtering would make a huge difference. Do you have any examples of the strategy modified that we can look at for any of the hypothetical suggestions you reference in the user notes? I’m just learning about NT code and it helps to see examples.

    1. Hi Gerry, First I’ll say all the code on this site is meant for advanced users who are familiar with C# and NinjaScript, but spending some time learning how all the free code here works is a good way to get familiar with some more advanced NT concepts. You won’t be able to do much with the shadow probe unless you can code your own logic to utilize it. I’ll paste from a previous comment that might help explain:

      Running the shadow probe by itself isn’t always enough to produce a positive P/L. The shadow probe is meant to augment your existing trading style/entry logic. Say, as a simple example, your method is to enter long when the 20 SMA crosses above the 50 SMA. How do you determine the stop/target value? Most traders “feel” this out or use a set number for the instrument they’re trading. The probe is designed to determine the best stop/target for you based on current market conditions, taking the guesswork out. In other words, this strategy can help you find the best stop/target values, but the entry logic is all up to you.

      The premium version does include logic to only consider the last x number of shadow trades instead of the whole session’s worth so it can dynamically adapt to changing market conditions. There’s also a variable called “clearOnTimer” in the premium version that will clear the P/L data for a stop/target combination if it’s been x seconds since the last time it was cleared. You could also modify this to clear at set hours of the day. This will allow the probe to relearn from scratch for a particular S/T combination.

  5. Yes, I understand the strategy is not intended to run out of the box, and I understand that it is about finding the optimal targets. Seeing coded examples helps me to understand the logic and decipher how to utilize the code. I’m far from advanced, but I see the power in this AI assisted approach to setting stop target and profit targets dynamically as the conditions change. It’s good to know the paid version can adapt by altering the parameters. If the probe is reset in one of the ways you are describing, will it then shadow scan all of the possible combinations for 45 minutes before trading again? That’s where I’m not following the logic. I purposely turned the strategy on today after 8:00 am; actually at 9 am, and it immediately took a trade. Was it utilizing what it learned the day before, or does it literally reset and relearn the optimal parameters each day or each time it resets? Thanks for taking the time to answer these questions, and hopefully, I can move forward without having to bother you. I will purchase the paid version tonight as it seems much more plausible to use this and learn from this version vs. the free version for obvious reasons.

    1. Good question/point about the relearning period after a clear. Right now it does not relearn after a clear, but I may actually remove the clearing timer because it’s a legacy feature that got replaced by a better system to track conditions dynamically. In the premium version under the “Analyzing Shadow Data” region there is an option to find the most profitable stop/target combo over the last x number of shadow trades instead of the whole day. This acts as a moving window, e.g. if you set the lookback period to 75 (with a variable named profitHistLength in the dashboard region) the most profitable S/T combo will be calculated using only the last 75 shadow trades for each S/T combo. I believe this is much better solution to what the clearing timer was trying to achieve.

Leave a Reply

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