Skip to main content

Ninja Trader Code Protection Guide

This guide explains how Ninja Trader vendors can protect their Ninja Scripts using Agile.NET Code Protection to prevent reverse engineering and unauthorized distribution of their intellectual property.

Overview

Ninja Trader has partnered with SecureTeam to offer comprehensive code protection solutions to Ninja Trader vendors at an affordable price. By default, Ninja Trader provides built-in code encryption, but vendors can choose to enhance this protection with additional security measures including code virtualization and symbol renaming using Agile.NET Core Code Protection v7.

Protection Requirements by Ninja Trader Version

Ninja Trader v8.1.2 and Above

For vendors targeting Ninja Trader v8.1.2 and above, we recommend using Agile.NET Core Code Protection for NT v7.0.* to protect their add-ons.

While Ninja Trader applies code encryption by default, we strongly recommend applying additional protection measures:

  • Code Virtualization - Protects critical algorithms and license validation logic
  • Symbol Renaming - Obfuscates class and method names to make reverse engineering more difficult

Ninja Trader Pre-v8.1.2

For vendors targeting pre-NT 8.1.2 versions, you must use Agile.NET v6.6.0.35 to protect your Ninja Scripts.

Limitations for Pre-v8.1.2

  • Cannot apply code virtualization
  • Cannot apply symbol renaming
  • Limited to default configuration provided by Ninja Trader installation

Implementation Guide

Symbol Renaming

To enable symbol renaming in Ninja Trader, you need to modify the Agile.NET configuration file:

  1. Navigate to your Ninja Trader installation folder
  2. Open the file: <Ninja Trader Application Folder>\bin\custom\backup\AgileDotNet.cls
  3. Edit the file using a text editor
  4. Change Obfuscation="false" to Obfuscation="true"
  5. Save the file

Once symbol renaming is enabled Agile.net will rename all private and internal symbols in your code.

Important Note: Agile.NET may exclude certain symbols from the obfuscation process to prevent breaking the code functionality. This includes:

  • Public APIs that Ninja Trader expects to remain unchanged
  • Entry points and method signatures required by the Ninja Trader framework
  • Properties and methods that are called via reflection
  • Symbols that would cause runtime errors if renamed

This automatic exclusion ensures your protected scripts continue to function properly within the Ninja Trader environment.

Code Virtualization

Code virtualization takes a fundamentally different approach to MSIL code protection compared to traditional encryption methods. Instead of processing the original MSIL code directly, code virtualization converts your MSIL code into virtual opcodes that are only understood by a unique virtual machine.

How Code Virtualization Works:

  1. Virtual Opcode Generation: Your selected methods are converted into virtual opcodes - a unique language that Agile.NET synthesizes specifically for each protected method
  2. Unique Virtual Machine: Each virtualized method produces its own unique virtual machine that can process its specific set of virtual opcodes
  3. One-Way Transformation: Unlike encryption where code must be decrypted back to MSIL before execution, virtual machines directly process the protected code in virtual machine language
  4. Enhanced Security: The strength of code virtualization comes from its one-way transformation property, making it ideal for protecting software against unauthorized distribution

Why Code Virtualization is Superior:

  • No Decryption Risk: Unlike encrypted code that must be decrypted before execution (creating vulnerability windows), virtualized code never exists as readable MSIL
  • Method-Specific Protection: Each method gets its own unique virtual machine and opcode set, making reverse engineering extremely difficult
  • Runtime Execution: The virtual machine processes the code directly without converting back to original MSIL

Enabling Code Virtualization:

To enable code virtualization in Ninja Trader, you need to modify the Agile.NET configuration file:

  1. Navigate to your Ninja Trader installation folder
  2. Open the file: <Ninja Trader Application Folder>\bin\custom\backup\AgileDotNet.cls
  3. Edit the file using a text editor
  4. Change PerformCodeVirtualization="false" to PerformCodeVirtualization="true"
  5. Save the file

Once code virtualization is enabled, you can apply it to specific methods or entire classes.

To add code virtualization to your protection configuration, use the System.Reflection.ObfuscationAttribute to decorate classes and methods that require additional protection:

For specific methods:

[ObfuscationAttribute(Feature = "codevirtualization", Exclude = false)]
private bool ValidateLicense()
{
// License validation logic
// This method will be converted to virtual opcodes and executed by a unique virtual machine
}

[ObfuscationAttribute(Feature = "codevirtualization", Exclude = false)]
private void OnBarUpdate()
{
// Important trading algorithm
// This method will be converted to virtual opcodes and executed by a unique virtual machine
}

For entire classes (all members):

[ObfuscationAttribute(Feature = "codevirtualization", ApplyToMembers = true, Exclude = false)]
public class MyProtectedIndicator : Indicator
{
// All methods in this class will be protected with code virtualization
// when PerformCodeVirtualization is enabled in AgileDotNet.cls
}

Complete Example

Here's a complete example showing how to protect a Ninja Trader indicator:

AgileDotNet.cls Configuration File

First, ensure your <Ninja Trader Application Folder>\bin\custom\backup\AgileDotNet.cls file is configured correctly:

<?xml version="1.0" encoding="utf-8"?>
<AgileDotNet Version = "7.0.0.29" >
<AssemblyList>
<Assembly Path="NinjaTrader.Custom.dll" Secure="true" Obfuscation="true" FlowObfuscation="false" MethodCallObfuscation="false" ILMerge="none"/>
</AssemblyList>
<Settings>
<General OutputDirectory="." SignatureFile="" PfxPassword="" FilePathMode="relativepath" GenerateDebugInfo="True" />
<Obfuscation ObfuscationMapFile="ObfuscationMap" RenamingScheme="printablechars" CrossAssemblyObfuscation="false" ExcludeXamlTypes="false" ControlFlowObfuscation="basic" PredefindSymbolNamesFilePath="">
<RenamingExclusions />
</Obfuscation>
<Secure SecureUserStrings = "false" EncryptManagedResources="false" RedistName="AgileDotNetRT" RedistName64="AgileDotNetRT64" DisableRuntimeEmbedding="False" AntiDebuggerDetection="False" SkipSmallMethods="True" />
<CodeVirtualization PerformCodeVirtualization="true"/>
<LicenseFeatures />
</Settings>
<Licensing>
<Licenses />
</Licensing>
</AgileDotNet>

Key settings in the configuration:

  • Obfuscation="true" - Enables symbol renaming
  • PerformCodeVirtualization="true" - Enables code virtualization
  • Secure="true" - Enables code encryption

Protected Ninja Script Example

using System;
using System.Reflection;
using NinjaTrader.NinjaScript;

public class ProtectedIndicator : Indicator
{
private bool isLicensed = false;

[ObfuscationAttribute(Feature = "codevirtualization", Exclude = false)]
protected override void OnStateChange()
{
if (State == State.SetDataLoaded)
{
ValidateLicense();
}
}

[ObfuscationAttribute(Feature = "codevirtualization", Exclude = false)]
private bool ValidateLicense()
{
// License validation logic
// This method is protected with code virtualization
try
{
// Your license validation code here
isLicensed = true;
return true;
}
catch
{
isLicensed = false;
return false;
}
}

[ObfuscationAttribute(Feature = "codevirtualization", Exclude = false)]
protected override void OnBarUpdate()
{
if (!isLicensed)
return;

// Your protected algorithm logic
// This method is protected with code virtualization
CalculateIndicator();
}

private void CalculateIndicator()
{
// Your indicator calculations
// This method inherits protection from the class-level attribute
}
}

Compatibility Information

Agile.NET v7.0.0.x Backward Compatibility

Question: Is Agile.NET v7.0.0.x backward compatible?

Answer: Yes, Agile.NET v7.0.0.x should be backward compatible down to NinjaTrader v8.1.2.

Pre-v7.0.0.x Compatibility

Question: Will NinjaScripts protected with Agile.NET v6.9.1.x run on NinjaTrader v8.1.6?

Answer: Yes, assemblies protected with Agile.NET versions prior to v7.0.0.x should run on v8.1.6 without issues.

Exporting Protected Scripts

Once you have applied the appropriate obfuscation attributes to your Ninja Scripts, you need to export them from Ninja Trader to create the protected assemblies.

Export Process

  1. Open Ninja Trader and navigate to your protected scripts
  2. Add your indicator or strategy to the NinjaScript Editor
  3. Select "Export" from the context menu
  4. Check 'Protect compiled assembly'
  5. Save the protected assembly to your desired location

The exported assembly will contain your protected code with:

  • Code encryption (applied by default)
  • Symbol renaming
  • Code virtualization (where ObfuscationAttribute(Feature = "codevirtualization") is specified)

For detailed export instructions and advanced configuration options, refer to the official Ninja Trader Documentation.


Power your business with Pulse Billing

Looking to monetize your NinjaTrader add-ons with licensing, trials, and subscriptions? Pulse Billing provides end-to-end vendor tooling for license validation, checkout, subscriptions, trials, coupons, and affiliate management. Continue with the NinjaTrader Vendor Licensing & Subscription Management Guide to implement license checks and subscription flows.