OrderUpdate

View as Markdown

Definition

OrderUpdate can be used for subscribing to order update events.

Remember to unsubscribe if you are no longer using the subscription.

Syntax

OrderUpdate

Examples

1/* Example of subscribing/unsubscribing to order update events from an Add On. The concept can be carried over to any NinjaScript object you may be working on. */
2public class MyAddOnTab : NTTabPage
3{
4 private Account account;
5 private Order myEntryOrder;
6 private Order profitTarget;
7 private Order stopLoss;
8
9 public MyAddOnTab()
10 {
11 // Find our Sim101 account
12 lock (Account.All)
13 account = Account.All.FirstOrDefault(a => a.Name == "Sim101");
14
15 // Subscribe to order updates
16 if (account != null)
17 account.OrderUpdate += OnOrderUpdate;
18 }
19
20 // This method is fired as the status of an order changes
21 private void OnOrderUpdate(object sender, OrderEventArgs e)
22 {
23 // Submit stop/target bracket orders
24 if (myEntryOrder != null && myEntryOrder == e.Order)
25 {
26 if (e.OrderState == OrderState.Filled)
27 {
28 string oco = Guid.NewGuid().ToString("N");
29
30 profitTarget = account.CreateOrder(e.Order.Instrument, OrderAction.Sell, OrderType.Limit, OrderEntry.Manual, TimeInForce.Day,
31 e.Quantity, e.AverageFillPrice + 10 * e.Order.Instrument.MasterInstrument.TickSize, 0, oco, "Profit Target", Core.Globals.MaxDate, null);
32 stopLoss = account.CreateOrder(e.Order.Instrument, OrderAction.Sell, OrderType.StopMarket, OrderEntry.Manual, TimeInForce.Day,
33 e.Quantity, 0, e.AverageFillPrice - 10 * e.Order.Instrument.MasterInstrument.TickSize, oco, "Stop Loss", Core.Globals.MaxDate, null);
34 account.Submit(new[] { profitTarget, stopLoss });
35 }
36 }
37 }
38
39 // Called by TabControl when tab is being removed or window is closed
40 public override void Cleanup()
41 {
42 // Make sure to unsubscribe to the orders subscription
43 if (account != null)
44 account.OrderUpdate -= OnOrderUpdate;
45 }
46
47 // Other required NTTabPage members left out for demonstration purposes. Be sure to add them in your own code.