MarketData
Definition
MarketData can be used to access snapshot market data and for subscribing to market data events.
Notes:
- Remember to unsubscribe if you are no longer using the subscription.
- You should only unsubscribe to a market data event if you are actually subscribed.
Properties
| Property | Description |
|---|---|
| Ask | A MarketDataEventArgs representing the ask price |
| Bid | A MarketDataEventArgs representing the bid price |
| DailyHigh | A MarketDataEventArgs representing the daily high |
| DailyLow | A MarketDataEventArgs representing the daily low |
| DailyVolume | A MarketDataEventArgs representing the daily volume |
| Instrument | An Instrument representing the instrument |
| Last | A MarketDataEventArgs representing the last price |
| LastClose | A MarketDataEventArgs representing the last close |
| Opening | A MarketDataEventArgs representing the opening price |
| OpenInterest | A MarketDataEventArgs representing the open interest |
| Settlement | A MarketDataEventArgs representing the settlement price |
| Update | Event handler for subscribing/unsubscribing to market depth events |
Attempting to unsubscribe to this event before there is a subscription will generate errors.
Syntax
MarketData
Examples
1 // Example of subscribing/unsubscribing to market data from an Add On. The concept can be carried over 2 // to any NinjaScript object you may be working on. 3 public class MyAddOnTab : NTTabPage 4 { 5 private Instrument instrument; 6 7 public MyAddOnTab() 8 { 9 instrument = Instrument.GetInstrument("AAPL"); 10 if (instrument == null) 11 return; 12 13 // Subscribe to market data. Snapshot data is provided right on subscription 14 // Note: "instrument" is a placeholder in this example, you will need to replace 15 // with a valid Instrument object through various methods or properties available depending 16 // on the NinjaScript type you are working with (e.g., Bars.Instrument or Instrument.GetInstrument()) 17 if (!instrument.Dispatcher.HasShutdownStarted) 18 instrument.Dispatcher.InvokeAsync(() => instrument.MarketData.Update += OnMarketData); 19 20 // Printing snapshot market data for the last price and time 21 NinjaTrader.Code.Output.Process(instrument.MarketData.Last.Price.ToString() + " " + instrument.MarketData.Last.Time.ToString(), 22 PrintTo.OutputTab1); 23 } 24 25 // This method is fired on market data events 26 private void OnMarketData(object sender, MarketDataEventArgs e) 27 { 28 // Do something with market data events 29 } 30 31 // Called by TabControl when tab is being removed or window is closed 32 public override void Cleanup() 33 { 34 // Make sure to unsubscribe to the market data subscription 35 if (instrument != null) 36 instrument.MarketData.Update -= OnMarketData; 37 } 38 39 // Other required NTTabPage members left out for demonstration purposes. Be sure to add them in your own code. 40 }

