{"id":8169,"date":"2025-09-04T23:20:35","date_gmt":"2025-09-05T04:20:35","guid":{"rendered":"https:\/\/librarytestdev.wpenginepowered.com\/?post_type=doc&#038;p=8169"},"modified":"2025-12-22T10:16:26","modified_gmt":"2025-12-22T16:16:26","slug":"using-python-with-tt-net-sdk-client-side","status":"publish","type":"doc","link":"https:\/\/library-staging.tradingtechnologies.com\/apis\/tt-net-sdk\/appendix-tt-net-sdk\/using-python-with-tt-net-sdk-client-side\/","title":{"rendered":"Using Python with TT .NET SDK Client Side"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">To support integrating Python with TT .NET SDK Client Side applications, one option is to use the&nbsp;<a href=\"https:\/\/ironpython.net\/\">IronPython<\/a>&nbsp;implementation of the Python programming language. IronPython supports the ability to import and utilize any .NET DLL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To begin using Python with TT .NET SDK Client Side, use the following procedure:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Navigate to\u00a0<a href=\"https:\/\/ironpython.net\/download\/\">https:\/\/ironpython.net\/download\/<\/a>\u00a0and download the latest version of IronPython.<\/li>\n\n\n\n<li>Download the TT .NET SDK Client Side DLL from NuGet via Visual Studio or download the DLL manually. For more information, refer to the section called\u00a0<a href=\"\/tt-net-sdk\/articles\/gs-reference-dll.html\">Referencing the DLL<\/a>.<\/li>\n\n\n\n<li>Import the TT .NET Client Side DLL into your Python code using the following import command:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import clr\nclr.AddReferenceToFileAndPath(\"tt-net-api.dll\")\nfrom tt_net_sdk import *\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The sample code snippet below shows how to simply call the TT .NET SDK Client Side functionality inside a Python script. The sample submits an order that follows the bid.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from System import Action\n\nimport sys\nimport clr\n\nclr.AddReferenceToFileAndPath(r\"tt-net-api.dll\")\nfrom tt_net_sdk import *\n\n\nm_api = None\nm_instrument = None\nm_ts = None\nm_isOrderBookDownloaded = 0\nm_orderKey = None\n\n\n\ndef m_ts_OrderBookDownload(sender, e):\n    print (\"OrderBookDownload complete...\")\n    global m_isOrderBookDownloaded\n    m_isOrderBookDownloaded = 1\n\n\ndef m_ts_OrderAdded(sender, e):\n    print (\"Order added: \" + e.ToString())\n\n\ndef m_ts_OrderDeleted(sender, e):\n    print (\"Order deleted: \" + e.ToString())\n    sys.exit()\n\n\ndef m_ts_OrderFilled(sender, e):\n    print (\"Order filled: \" + e.ToString())\n\n\ndef m_ts_OrderRejected(sender, e):\n    print (\"Order rejected: \" + e.ToString())\n    sys.exit()\n\n\ndef m_ts_OrderUpdated(sender, e):\n    print (\"Order updated: \" + e.ToString())\n\n\ndef m_ts_OrderStatusUnknown(sender, e):\n    print (\"Order status unknown: \" + e.ToString())\n    sys.exit()\n\n\ndef m_ts_OrderTimeout(sender, e):\n    print (\"Order timed out: \" + e.ToString())\n    sys.exit()\n\n\ndef m_priceSubscription_FieldsUpdated(sender, e):\n    global m_orderKey\n    global m_isOrderBookDownloaded\n    global m_ts\n    if e.Error is None:\n                if e.Fields.GetBestBidPriceField().Value != None and m_isOrderBookDownloaded == 1:\n                    if m_orderKey == None:\n                        op = OrderProfile(m_instrument)\n                        op.Account = m_api.DefaultAccount\n                        op.BuySell = BuySell.Buy\n                        op.OrderType = OrderType.Limit\n                        op.OrderQuantity = Quantity.FromDecimal(m_instrument, 10)\n                        op.LimitPrice = e.Fields.GetBestBidPriceField().Value\n\n                        if m_ts.SendOrder(op) == False:\n                            print (\"Send new order Failed!!\")\n                            sys.exit()\n                        else:\n                            m_orderKey = op.SiteOrderKey\n                            print (\"Sent new order key = \" + op.SiteOrderKey)\n                    elif m_ts.Orders.ContainsKey(m_orderKey) and m_ts.Orders&#91;m_orderKey].LimitPrice != e.Fields.GetBestBidPriceField().Value:\n                        op = m_ts.Orders&#91;m_orderKey].GetOrderProfile()\n                        op.LimitPrice = e.Fields.GetBestBidPriceField().Value\n                        op.Action = OrderAction.Change\n\n                        if m_ts.SendOrder(op) == False:\n                            print (\"Sent order update failed!!\")\n                        else:\n                            print (\"Send change order succeeded.\")\n\n\ndef m_api_TTAPIStatusUpdate(sender, e):\n    if e.IsReady :\n       print (\"Ready...\")\n       print (\"Look up instrument...\")\n       m_instrLookupRequest = InstrumentLookup(Dispatcher.Current, MarketId.CME, ProductType.Future,\"CL\",\"CL Dec19\")\n       e2 = m_instrLookupRequest.Get()\n\n    if e2 == ProductDataEvent.Found:\n        global m_instrument\n        m_instrument = m_instrLookupRequest.Instrument\n        print (\"Instrument found...\")\n\n        global m_ts\n        m_ts = TradeSubscription(Dispatcher.Current)\n        m_ts.OrderBookDownload += m_ts_OrderBookDownload\n        m_ts.OrderAdded += m_ts_OrderAdded\n        m_ts.OrderDeleted += m_ts_OrderDeleted\n        m_ts.OrderFilled += m_ts_OrderFilled\n        m_ts.OrderUpdated += m_ts_OrderUpdated\n        m_ts.OrderUpdated += m_ts_OrderStatusUnknown\n        m_ts.OrderUpdated += m_ts_OrderTimeout\n        m_ts.Start()\n\n        m_priceSubsciption = PriceSubscription(m_instrLookupRequest.Instrument, Dispatcher.Current)\n        m_priceSubsciption.Settings = PriceSubscriptionSettings(PriceSubscriptionType.MarketDepth)\n        m_priceSubsciption.FieldsUpdated += m_priceSubscription_FieldsUpdated\n        m_priceSubsciption.Start()\n    else:\n        print (\"Cannot find instrument: \" + e.ToString())\n\n\ndef ttNetApiInitHandler(api, ex):\n        if ex is None:\n            global m_api\n            m_api = api\n            api.TTAPIStatusUpdate += m_api_TTAPIStatusUpdate\n            api.Start()\n        else:\n            print (\"TT.NET SDK Initialization Failed: \" + ex.Message)\n\n\ndef Init():\n    opt = TTAPIOptions(ServiceEnvironment.UatCert,\"7601b8a0-dd23-2a8a-ee52-3712c53841d4:9ad4a302-5577-45c3-11db-6138b2757cca\",5000)\n    apiInitializeHandler = ApiInitializeHandler(ttNetApiInitHandler)\n    TTAPI.CreateTTAPI(Dispatcher.Current, opt, apiInitializeHandler)\n\n\nm_disp = Dispatcher.AttachWorkerDispatcher()\nm_disp.DispatchAction(Action(Init))\nm_disp.Run()\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For more information on the various commands and syntax, refer to the&nbsp;<a href=\"https:\/\/ironpython.net\/documentation\/\">IronPython Documentation<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong>&nbsp;TT does not provide support for specific Python implementations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p class=\"wp-block-paragraph\">To support integrating Python with TT .NET SDK Client Side applications, one option is to use the&nbsp;<a href=\"https:\/\/ironpython.net\/\">IronPython<\/a>&nbsp;implementation of the Python programming language. IronPython supports the ability to import and utilize any .NET DLL.<\/p>\n<p class=\"wp-block-paragraph\">To begin using Python with TT .NET SDK Client Side, use the following procedure:<\/p>\n<p class=\"wp-block-paragraph\">The sample code snippet below shows how to simply call the TT .NET SDK Client Side functionality inside a Python script. The sample submits an order that follows the bid.<\/p>\n<p class=\"wp-block-paragraph\">For more information on the various commands and syntax, refer to the&nbsp;<a href=\"https:\/\/ironpython.net\/documentation\/\">IronPython Documentation<\/a>.<\/p>\n<p class=\"wp-block-paragraph\"><strong>Note:<\/strong>&nbsp;TT does not provide support for specific Python implementations.<\/p>\n","protected":false},"author":2,"template":"wp-custom-template-single-doc-tt-net-sdk","meta":{"_acf_changed":true,"footnotes":""},"docs-category":[960],"class_list":["post-8169","doc","type-doc","status-publish","hentry","docs-category-appendix-tt-net-sdk"],"acf":[],"_links":{"self":[{"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/doc\/8169","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\/8169\/revisions"}],"wp:attachment":[{"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/media?parent=8169"}],"wp:term":[{"taxonomy":"docs-category","embeddable":true,"href":"https:\/\/library-staging.tradingtechnologies.com\/ja\/wp-json\/wp\/v2\/docs-category?post=8169"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}