NinjaTrader 8
Guide on how to integrate licensing library into NinjaScripts
Adding licensing to your NinjaScript
After you have created your NinjaScript, you will need to integrate Licensing Library to validate users' licenses and protect your product from unauthorized distribution.
Save PoshTrader.Licensing.dll into %UserProfile%\Documents\NinjaTrader 8\bin\Custom\
Restart the NinjaTrader platform so it can load the external library
Add the library as a reference (right-click in NinjaScript Editor > References…)
Import namespace of the library by adding
using PoshTrader.Licensing;
Add Item Attributes to the main class
Validate library origin to prevent tampering
Create
ItemLicense
on the startupCheck license status during runtime
using System;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using PoshTrader.Licensing;
namespace NinjaTrader.NinjaScript.Indicators
{
[Item(ID, "Sample Indicator", Version = "1.0.0", Author = "John Doe")]
public class SampleIndicator : Indicator
{
private ItemLicense _license;
protected override void OnStateChange()
{
if (State == State.Configure)
{
try
{
var assembly = Assembly.GetAssembly(typeof(ItemLicense));
var certificate = X509Certificate.CreateFromSignedFile(assembly.Location);
if (certificate.GetCertHashString() != "07FF85A977EF497368AA1ADEB64D43B2B55BD4D8")
{
throw new Exception("Could not validate 'PoshTrader.Licensing' assembly origin.");
}
}
catch (CryptographicException)
{
throw new Exception("Could not load 'PoshTrader.Licensing' assembly certificate. Platform restart might be required.");
}
}
else if (State == State.DataLoaded)
{
_license = new Licensing(this);
if (!_license.IsValid())
{
throw new Exception("Product license validation failed.");
}
}
}
protected override void OnBarUpdate()
{
if (!_license.IsValid())
return;
// Put your OnBarUpdate logic here
}
}
}
Creating a distribution file
To create the distribution file, please follow the steps below for creating an archive file containing your NinjaScript.
From the Control Center window select the menu Tools > Export > NinjaScript Add-On… to open the “Export NinjaScript” dialog window
Select the option “Export as compiled assembly”
Select “Protect compiled assembly” (For information on protection read here)
Press
Add
buttonSelect all of the files required, including
References – PoshTrader.Licensing
and pressOK
A list of all files that will be exported will be shown
Press the
Export
button to export the selected filesA file dialog will open where you can choose the location to create your distribution file.
Add
PoshTrader.Licensing.dll
to the exported archive file.
Last updated
Was this helpful?