Algorithmic Trading Ninjascript

$50.00

This Algorithmic Trading NinjaScript will place simulated trades behind the scenes to feel out the current market conditions. The “Shadow Probe” will enter and track hundreds of simulated positions with a range of stop/target combinations in order to find the most successful stop/target combination at any given time.

Overview

This download contains the .cs code for the Algorithmic Trading NinjaScript strategy. We call this strategy "Shadow Probe" and here's what it does: The Shadow Probe 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, even thousands, 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, and so on, testing every combination therein. You can have the shadow trades for each stop/target combination run consecutively or overlap, giving you a more granular sampling of market conditions. With this data you will know the most successful and profitable stop and target combination at any given  moment. Since you have full access to the NinjaScript, you can  also add your own filtering criteria to find most profitable stop and target combination under specific conditions (i.e. the stop/target combo that performs the best when the ADX is at a certain level, or when one moving average is above another), the most profitable stop/target combo over the last x minutes, or 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 in the code). This region is where you can set the conditions that need to be met to trigger a buy or sell order with the stop/target combination with the highest likelihood of success according to the Shadow Probe. The “entry logic” region is where you’ll want to start, just replace the example logic there to start experimenting. You'll find comments throughout the code to guide you.

If you’re looking for an indicator version you can check out our AI Trading Indicator. The indicator version has properties that are accessible via strategy builder or manually coded strategies.

This Algorithmic Trading NinjaScript add-on is 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. You can download the Free Automated Trading NinjaScript if you'd like to take a peek at the code before purchasing. The main example what's missing in the free version is that it only places simulated trades consecutively/serially (i.e. a new 5/6 stop/target combination probe trade will only be entered after the last 5/6 stop/target 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 combination. The Premium version can track multiple probe positions for a single stop/target 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 stop/target 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 Secondary Series NinjaScript 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 number. This index number refers to the index of the array within the code that stores your Shadow Probe trades. This information will be handy as you dig deeper and augment the code. You will see comments in the .cs file that will guide you in using this information. 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 stop/target combination that had the highest daily net profit, both for longs and shorts.

Example output window:

Automated Trading NinjaScript 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 Shadow Probe

There are infinite ways to implement the data returned by our Algorithmic Trading NinjaScript. Try lowering the stopTargetMax variable so it’s not tracking stop/target 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 stop/target 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 volatility tracking code:

 

#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 stop/target 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 stop/target with a 100% win rate over the last x trades is more dialed into the current conditions).

Use the break even values of each stop/target and enter with the stop/target 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 stop/target 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.

You can think of the Shadow Probe as a framework, engine or platform you can tailor and plug your own modules into. The more you dig in and customize the code, the more it will become your own proprietary "black box" strategy.

Notes

This Algorithmic Trading NinjaScript add-on 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.

This code will not work with releases prior to NinjaTrader 8.

Got something to discuss?


Guest
Marty
10 months 7 days ago

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

Support
Support
10 months 7 days ago

Hi Marty, when you make a purchase you’ll receive credentials you can use at the ‘login’ link up top. You can login 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.

Guest
Andrea Thein
10 months 7 days ago

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."

Support
Support
10 months 7 days ago

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.

Guest
Andrea Thein
10 months 7 days ago

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

Support
Support
10 months 7 days ago

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.

Guest
GERRY
10 months 7 days ago

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.

Support
Support
10 months 7 days ago

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.

Guest
GERRY
10 months 7 days ago

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.

Support
Support
10 months 7 days ago

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.

Customer
tyertfgyhfg fghdfghfgy
10 days 23 hours ago

Hello, are you still updating the code and how can i see backtest on it?
Im really interested in have a contact with you.