/*Example of subscribing/unsubscribing to position update events from an Add On. The concept can be carried over to any NinjaScript object you may be working on.*/
public class MyAddOnTab : NTTabPage
{
private Account account;
public MyAddOnTab()
{
// Find our Sim101 account
lock (Account.All)
account = Account.All.FirstOrDefault(a => a.Name == "Sim101");
// Subscribe to position updates
if (account != null)
account.PositionUpdate += OnPositionUpdate;
}
// This method is fired as a position changes
private void OnPositionUpdate(object sender, PositionEventArgs e)
{
// Output the new position
NinjaTrader.Code.Output.Process(string.Format("Instrument: {0} MarketPosition: {1} AveragePrice: {2} Quantity: {3}",
e.Position.Instrument.FullName, e.MarketPosition, e.AveragePrice, e.Quantity), PrintTo.OutputTab1);
}
// Called by TabControl when tab is being removed or window is closed
public override void Cleanup()
{
// Make sure to unsubscribe to the positions subscription
if (account != null)
account.PositionUpdate -= OnPositionUpdate;
}
// Other required NTTabPage members left out for demonstration purposes. Be sure to add them in your own code.
}