Created
July 4, 2023 08:31
-
-
Save samuelcaldas/a73b7d66224e9f9b8eb46e7d3e4f64d7 to your computer and use it in GitHub Desktop.
An exemple for testing ninjatrader addons
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using NinjaTrader.Cbi; | |
using NinjaTrader.UnitTest; | |
using System; | |
using System.Linq; | |
namespace NinjaTrader.NinjaScript.AddOns | |
{ | |
public class AddonTests : TestCase | |
{ | |
private const string InstrumentName = "MES 09-23"; | |
private const string AccountName = "Sim101"; | |
private Instrument instrument; | |
private Account selectedAccount; | |
public AddonTests(string name) : base(name) | |
{ | |
instrument = Instrument.GetInstrument(InstrumentName); | |
selectedAccount = Account.All.FirstOrDefault(a => a.Name == AccountName); | |
} | |
public void TestInstrument() | |
{ | |
AssertEqual(InstrumentName, instrument.FullName, "Instrument name is not correct"); | |
} | |
public void TestAccount() | |
{ | |
AssertEqual(AccountName, selectedAccount.Name, "Account name is not correct"); | |
} | |
} | |
public class AddonTestsAddon : NinjaTrader.NinjaScript.AddOnBase | |
{ | |
private ConnectionManager connectionManager; | |
protected override void OnStateChange() | |
{ | |
if (State == State.SetDefaults) | |
{ | |
Name = "AddonTest"; | |
Description = "An add-on that tests the Ninjatrader add-ons"; | |
} | |
else if (State == State.Configure) | |
{ | |
connectionManager = new ConnectionManager(); | |
Connection.ConnectionStatusUpdate += OnConnectionStatusUpdate; | |
} | |
else if (State == State.Terminated) | |
{ | |
Connection.ConnectionStatusUpdate -= OnConnectionStatusUpdate; | |
connectionManager.Disconnect(); | |
} | |
} | |
protected void OnConnectionStatusUpdate(object sender, ConnectionStatusEventArgs connectionStatusUpdate) | |
{ | |
if (connectionStatusUpdate.Status == ConnectionStatus.Connected) | |
{ | |
TestSuite suite = new TestSuite(); | |
// Execução dos testes | |
suite.Add(new AddonTests("TestInstrument")); | |
suite.Add(new AddonTests("TestAccount")); | |
TestResult result = suite.Run(); | |
// Exibição dos resultados dos testes | |
result.PrintSummary(); | |
} | |
else if (connectionStatusUpdate.Status == ConnectionStatus.ConnectionLost) | |
{ | |
NinjaTrader.NinjaScript.NinjaScript.Log("Connection lost at: " + DateTime.Now, LogLevel.Information); | |
} | |
} | |
} | |
public class ConnectionManager | |
{ | |
private const string DefaultConnectionName = "Simulated Data Feed"; | |
private Connection connection; | |
public Connection Connection | |
{ | |
get { return connection; } | |
private set { connection = value; } | |
} | |
public ConnectionManager(string connectionName = DefaultConnectionName) | |
{ | |
Connect(connectionName); | |
} | |
private void Connect(string connectionName) | |
{ | |
ConnectOptions connectOptions = null; | |
lock (Core.Globals.ConnectOptions) | |
connectOptions = Core.Globals.ConnectOptions.FirstOrDefault(o => o.Name == connectionName); | |
if (connectOptions != null) | |
{ | |
Connection connection = null; | |
lock (Connection.Connections) | |
connection = Connection.Connections.FirstOrDefault(c => c.Options.Name == connectionName); | |
if (connection != null && connection.Status == ConnectionStatus.Connected) | |
{ | |
Connection = connection; | |
} | |
else | |
{ | |
Connection = Connection.Connect(connectOptions); | |
} | |
} | |
else | |
{ | |
NinjaScript.Log("Could not connect. No connect options found.", LogLevel.Error); | |
} | |
} | |
public void Disconnect() | |
{ | |
if (Connection != null && Connection.Status == ConnectionStatus.Connected) | |
{ | |
Connection.Disconnect(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment