{"id":8061,"date":"2025-09-04T23:22:08","date_gmt":"2025-09-05T04:22:08","guid":{"rendered":"https:\/\/librarytestdev.wpenginepowered.com\/?post_type=doc&#038;p=8061"},"modified":"2025-09-04T23:23:53","modified_gmt":"2025-09-05T04:23:53","slug":"working-with-price-subscriptions-2","status":"publish","type":"doc","link":"https:\/\/library-staging.tradingtechnologies.com\/apis\/tt-net-sdk\/subscribing-for-market-data-tt-net-sdk\/working-with-price-subscriptions-2\/","title":{"rendered":"Working with price subscriptions"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\" id=\"process-for-creating-price-subscriptions\">Process for creating price subscriptions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To create a price subscription and start receiving updates, you:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create a\u00a0<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.PriceSubscription.html\">PriceSubscription<\/a>\u00a0object for a specific Instrument.<\/li>\n\n\n\n<li>Specify the price subscription type.<\/li>\n\n\n\n<li>Create an event handler method to be called when market data updates for the\u00a0<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.Instrument.html\">Instrument<\/a>\u00a0are available.<\/li>\n\n\n\n<li>Register the event handler with the\u00a0<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.PriceSubscription.html\">PriceSubscription<\/a>\u00a0instance.<\/li>\n\n\n\n<li>Call the Start method of the\u00a0<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.PriceSubscription.html\">PriceSubscription<\/a>\u00a0instance.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><em>Warning<\/em>&nbsp;: TT strongly recommends users maintain their price subscriptions. To prevent issues, users must avoid unsubscribing and immediately resubscribing to market updates.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"subscribing-for-market-data\"><a href=\"\/tt-net-sdk\/articles\/md-price-sub.html#subscribing-for-market-data\"><\/a>Subscribing for market data<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Typically, you start a price subscription for an&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.Instrument.html\">Instrument<\/a>&nbsp;when TT .NET SDK fires the Update event for the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.InstrumentLookup.html\">InstrumentLookup<\/a>&nbsp;object you created to find the Instrument.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following snippet modifies the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.InstrumentLookup.html#OnData\">OnData<\/a>&nbsp;event handler to start a price subscription for market depth data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void m_instrLookupRequest_OnData(object sender, InstrumentLookupEventArgs e)\n{\n    if (e.Event == ProductDataEvent.Found)\n    {\n        \/\/ Instrument was found\n        Instrument instrument = e.InstrumentLookup.Instrument;\n        Console.WriteLine(\"Found: {0}\", instrument);\n\n        \/\/ Subscribe for market Data\n        m_priceSubsciption = new PriceSubscription(instrument, tt_net_sdk.Dispatcher.Current);\n        m_priceSubsciption.Settings = new PriceSubscriptionSettings(PriceSubscriptionType.MarketDepth);\n        m_priceSubsciption.FieldsUpdated += m_priceSubscription_FieldsUpdated;\n        m_priceSubsciption.Start();\n    }\n    else if (e.Event == ProductDataEvent.NotAllowed)\n    {\n        Console.WriteLine(\"Not Allowed : Please check your Token access\");\n    }\n    else\n    {\n        \/\/ Instrument was not found and TT API has given up looking for it\n        Console.WriteLine(\"Cannot find instrument: {0}\", e.Message);\n    }\n}\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"creating-the-subscription-event-handler\"><a href=\"\/tt-net-sdk\/articles\/md-price-sub.html#creating-the-subscription-event-handler\"><\/a>Creating the subscription event handler<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">After the subscription starts, TT .NET SDK retrieves all of the market data for the Instrument and invokes the event handler method you registered with the subscription. TT .NET SDK fires the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.PriceSubscription.html#FieldsUpdated\">FieldsUpdated<\/a>&nbsp;event with the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.UpdateType.html\">FieldsUpdatedEventArgs.UpdateType<\/a>&nbsp;property set to&nbsp;<strong>UpdateType.Snapshot<\/strong>&nbsp;for the initial market data snapshot. You can use the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.PriceSubscriptionFields.html#GetFieldIds\">PriceSubscriptionFields.GetFieldIds<\/a>&nbsp;method to process all of market data. Subsequently, when any of the market data fields change, TT .NET SDK sets the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.UpdateType.html\">FieldsUpdatedEventArgs.UpdateType<\/a>&nbsp;property to&nbsp;<strong>UpdateType.Incremental<\/strong>&nbsp;to indicate that only some of the market data changed. In this case, you can use the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.PriceSubscriptionFields.html#GetChangedFieldIds\">PriceSubscriptionFields.GetChangedFieldIds<\/a>&nbsp;method to retrieve the ids of only the fields whose values changed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The following code snippet shows the structure of a sample price subscription event handler method.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void m_priceSubscription_FieldsUpdated(object sender, FieldsUpdatedEventArgs e)\n{\n    \/\/ Inside market fields\n    foreach (FieldId id in e.Fields.GetFieldIds())\n    {\n        Field f = e.Fields&#91;id];\n        \/\/ Put code to process the field here\n    }\n    Console.WriteLine(\"Ask Depth Snapshot\");\n    int askDepthLevels = e.Fields.GetLargestCurrentDepthLevel(FieldId.BestAskPrice);\n    for (int i = askDepthLevels - 1; i &gt;= 0; i--)\n    {\n        Quantity q = e.Fields.GetBestAskQuantityField(i).Value;\n        if (Quantity.IsEmpty(q))\n            continue;\n        Console.WriteLine(\" Level=\" + i + \" Qty=\" + q + \" Price=\" + e.Fields.GetBestAskPriceField(i).Value.ToString());\n        int askDetailedDepthLevels = e.Fields.GetLargestCurrentDetailedDepthLevel(FieldId.AskDetailedDepthQuantity, i);\n        for (int j = 0; j &lt; askDetailedDepthLevels; j++)\n        {\n            Quantity qu = e.Fields.GetAskDetailedDepthQuantityField(i, j).Value;\n            if (Quantity.IsEmpty(qu))\n                continue;\n            Console.WriteLine(\"  DETAILED DEPTH  Level=\" + j + \" Detailed Depth Qty=\" + qu + \" Price=\" + e.Fields.GetBestAskPriceField(i).Value.ToString());\n        }\n    }\n    Console.WriteLine(\"Bid Depth Snapshot\");\n    int bidDepthLevels = e.Fields.GetLargestCurrentDepthLevel(FieldId.BestBidPrice);\n    for (int i = 0; i &lt; bidDepthLevels; i++)\n    {\n        Quantity q = e.Fields.GetBestBidQuantityField(i).Value;\n        if (Quantity.IsEmpty(q))\n            continue;\n        Console.WriteLine(\" Level=\" + i + \" Qty=\" + q + \" Price=\" + e.Fields.GetBestBidPriceField(i).Value.ToString());\n        int bidDetailedDepthLevels = e.Fields.GetLargestCurrentDetailedDepthLevel(FieldId.BidDetailedDepthQuantity, i);\n        for (int j = 0; j &lt; bidDetailedDepthLevels; j++)\n        {\n            Quantity qu = e.Fields.GetBidDetailedDepthQuantityField(i, j).Value;\n            if (Quantity.IsEmpty(qu))\n                continue;\n            Console.WriteLine(\"  DETAILED DEPTH Level=\" + j + \" Detailed Depth Qty=\" + qu + \" Price=\" + e.Fields.GetBestBidPriceField(i).Value.ToString());\n        }\n    }\n}\n<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"extracting-market-data-values\">Extracting market data values<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"\/tt-net-sdk\/api\/tt_net_sdk.FieldId.html\">FieldId<\/a>&nbsp;is an enumerated type defining the set of valid market data fields, such as DirectBidPrice, DirectBidQuantity, etc. When used as an index to extract market data fields from the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.FieldsUpdatedEventArgs.html#Fields\">Fields<\/a>&nbsp;property of the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.FieldsUpdatedEventArgs.html\">FieldsUpdatedEventArgs<\/a>&nbsp;object, it returns a&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.Field.html\">Field<\/a>&nbsp;object.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To extract the actual value, you can call the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.Field.html#tt_net_sdk_Field_FormattedValue\">FormattedValue<\/a>&nbsp;property of the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.Field.html\">tt_net_sdk.Field<\/a>&nbsp;object, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Field f = e.Fields&#91;id];\nstring s = f.FormattedValue;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You can also use the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.Field.html#tt_net_sdk_Field_FormattedValue\">FormattedValue<\/a>&nbsp;property of the&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.Field.html\">tt_net_sdk.Field<\/a>&nbsp;object object to reference to the Object storing the value. You can then cast it to its real type. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>foreach (FieldId id in e.Fields.GetFieldIds())\n{\n  Field field = e.Fields&#91;id];\n  if (field is PriceField pf)\n  {\n      Price p = pf.Value;\n  }\n  else if (field is QuantityField qf)\n  {\n      Quantity q = qf.Value;\n  }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively, you can access specific&nbsp;<a href=\"\/tt-net-sdk\/api\/tt_net_sdk.Field.html\">tt_net_sdk.Field<\/a>&nbsp;values directly without needing to cast the return value, as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Price bid = e.Fields.GetDirectBidPriceField().Value;\nQuantity bidQty = e.Fields.GetBidMarketQuantityField().Value;<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Process for creating price subscriptions To create a price subscription and start receiving updates, you: Warn [&hellip;]<\/p>\n","protected":false},"author":2,"template":"wp-custom-template-single-doc-tt-net-sdk","meta":{"_acf_changed":true,"footnotes":""},"docs-category":[771],"class_list":["post-8061","doc","type-doc","status-publish","hentry","docs-category-subscribing-for-market-data-tt-net-sdk"],"acf":[],"_links":{"self":[{"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/doc\/8061","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/doc"}],"about":[{"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/types\/doc"}],"author":[{"embeddable":true,"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/users\/2"}],"version-history":[{"count":0,"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/doc\/8061\/revisions"}],"wp:attachment":[{"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/media?parent=8061"}],"wp:term":[{"taxonomy":"docs-category","embeddable":true,"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/docs-category?post=8061"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}