MarketData

View as Markdown

Definition

MarketData can be used to access snapshot market data and for subscribing to market data events.

Notes:

  1. Remember to unsubscribe if you are no longer using the subscription.
  2. You should only unsubscribe to a market data event if you are actually subscribed.

Properties

PropertyDescription
AskA MarketDataEventArgs representing the ask price
BidA MarketDataEventArgs representing the bid price
DailyHighA MarketDataEventArgs representing the daily high
DailyLowA MarketDataEventArgs representing the daily low
DailyVolumeA MarketDataEventArgs representing the daily volume
InstrumentAn Instrument representing the instrument
LastA MarketDataEventArgs representing the last price
LastCloseA MarketDataEventArgs representing the last close
OpeningA MarketDataEventArgs representing the opening price
OpenInterestA MarketDataEventArgs representing the open interest
SettlementA MarketDataEventArgs representing the settlement price
UpdateEvent 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.
3public 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}