{"id":194,"date":"2021-03-17T05:31:08","date_gmt":"2021-03-17T05:31:08","guid":{"rendered":"https:\/\/algomojo.com\/blog\/?p=194"},"modified":"2025-02-27T07:43:59","modified_gmt":"2025-02-27T07:43:59","slug":"exiting-cover-order-and-bracket-order-for-a-particular-trading-instrument","status":"publish","type":"post","link":"https:\/\/algomojo.com\/blog\/exiting-cover-order-and-bracket-order-for-a-particular-trading-instrument\/","title":{"rendered":"Exiting Cover Order and Bracket Order for a Particular Trading Instrument"},"content":{"rendered":"\n<p>Bracket Order and Cover Order are multi-legged orders and canceling the bracket order and cover order for a particular trading instrument there is no direct mechanism available from the API provisions supplied by the brokers. Hence here is the Amibroker AFL code to do the exit by reading the orderbook and processing the pending cover order and bracket order from the orderbook.<\/p>\n\n\n\n<p><strong>Supported Brokers<\/strong> : Aliceblue, Tradejini, Zebu<\/p>\n\n\n\n<p><strong>Why Exiting Cover Order and Bracket Order automatically is required?<\/strong><\/p>\n\n\n\n<p>To Build a Intraday stop and reverse models using Bracket Order and Cover Order with protection.<\/p>\n\n\n\n<p>To implement the Exit Bracket Order I had designed a prototype model which can be later used in your AFL code to simply your coding requirements.<\/p>\n\n\n\n<p><strong>Parameter Controls<\/strong><\/p>\n\n\n\n<p>We can classify the modules into two types<\/p>\n\n\n\n<p>1)Main Module (Exit Bracket and Cover Orders.afl) &#8211; used to cancel\/exit the BO and CO orders for a particular trading instrument<\/p>\n\n\n\n<p>2)Header Module (ExitBOCOOrders.afl) &#8211; used to parse the orderbook and to cancel the BO and CO orders (this AFL code needs to be placed under the Amibroker\\Formulas\\Include folder)<\/p>\n\n\n\n<p><strong>Main Module (Exit Bracket and Cover Orders.afl)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-javascript\">#include &lt;ExitBOCOOrders.afl&gt;\n\n\nSymbol = ParamStr(&quot;Symbol&quot;,&quot;RELIANCE-EQ&quot;);\nPcode = ParamList(&quot;Pcode&quot;,&quot;BO|CO&quot;,1);\ntrigger = ParamTrigger(&quot;Trigger Button&quot;,&quot;Cancel Orders&quot;);\n\nif(trigger)\n{\nresp = GetOrderBook();\nCancelOrder(resp,Symbol,Pcode); \/\/output in string format\n}<\/code><\/pre>\n\n\n\n<p><strong>Header Module (ExitBOCOOrders.afl)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-javascript\">\/\/store the file under the name - ExitBOCOOrders.afl under the Amibroker\\Formulas\\Include folder\n\nuid = ParamStr(&quot;Client ID&quot;,&quot;TS2499&quot;);\nuser_apikey = ParamStr(&quot;user_apikey&quot;,&quot;86cbef19e7e61ccee91e497690d5814e&quot;); \/\/Enter your API key here\napi_secret = ParamStr(&quot;api_secret&quot;,&quot;8e8b207de43446c65f379bf2145b62fc&quot;); \/\/Enter your API secret key here\ns_prdt_ali = &quot;BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML&quot;;\nbroker = ParamStr(&quot;Broker&quot;,&quot;tj&quot;); \/\/Broker Short Code - ab - aliceblue, tj - tradejini, zb - zebu, en - enrich\nver = ParamStr(&quot;API Version&quot;,&quot;1.0&quot;);\n\nRequestTimedRefresh(1,False);\n\nfunction GetOrderBook()\n{\n\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\napi_data =&quot;{\\&quot;uid\\&quot;:\\&quot;&quot;+uid+&quot;\\&quot;,\\&quot;s_prdt_ali\\&quot;:\\&quot;&quot;+s_prdt_ali+&quot;\\&quot;}&quot;;\nresp=algomojo.AMDispatcher(user_apikey, api_secret,&quot;OrderBook&quot;,api_data,broker,ver);\n_TRACE(&quot; Order Book  : &quot; +resp);\nreturn resp;\n}\n\nfunction ExitOrder(Symbol,OrderType,Nstordno)\n{\n\nif(OrderType==&quot;BO&quot;)\n{\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\napi_data =&quot;{\\&quot;nestordernumber\\&quot;:\\&quot;&quot;+Nstordno+&quot;\\&quot;,\\&quot;SyomOrderId\\&quot;:\\&quot;&quot;+&quot;&quot;+&quot;\\&quot;,\\&quot;status\\&quot;:\\&quot;&quot;+&quot;pending&quot;+&quot;\\&quot;}&quot;;\nresp=algomojo.AMDispatcher(user_apikey, api_secret,&quot;ExitBOOrder&quot;,api_data,broker,ver);\n\n}\n\nif(OrderType==&quot;CO&quot;)\n{\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\napi_data =&quot;{\\&quot;uid\\&quot;:\\&quot;&quot;+uid+&quot;\\&quot;,\\&quot;NestOrd\\&quot;:\\&quot;&quot;+Nstordno+&quot;\\&quot;,\\&quot;s_prdt_ali\\&quot;:\\&quot;&quot;+s_prdt_ali+&quot;\\&quot;}&quot;;\nresp=algomojo.AMDispatcher(user_apikey, api_secret,&quot;ExitCOOrder&quot;,api_data,broker,ver);\n\n}\n\n\nreturn resp;\n}\n\nfunction CancelOrder(resp,Tsym,Pcode)\n{\n\n\/\/Initialization\nflag = 0;\n\nstringinfo = &quot;Nstordno&quot;;\n\nfor( item = 0; ( sym = StrExtract( resp, item,&#039;{&#039; )) != &quot;&quot;; item++ )\n{\n\nsym = StrTrim(sym,&quot; &quot;);\nTsym = StrTrim(Tsym,&quot; &quot;);\n\nif(Pcode == &quot;BO&quot;);\n{\nordermatch = StrFind(sym,Tsym) AND StrFind(sym,Pcode) AND StrFind(sym,&quot;complete&quot;);\n}\n\nif(Pcode == &quot;CO&quot;);\n{\nordermatch = StrFind(sym,Tsym) AND StrFind(sym,Pcode) AND (StrFind(sym,&quot;complete&quot;) OR StrFind(sym,&quot;open&quot;));\n}\n\n\nif(ordermatch) \/\/Matches the symbol and \/\/Matches the Order Type\n{\n\nflag = 1; \/\/turn on the flag\n\ndata = sym;\n\n\n\nfor( jitem = 0; ( posdetails = StrExtract( data, jitem,&#039;,&#039; )) != &quot;&quot;; jitem++ )\n{\n  \n  if(StrFind(posdetails,&quot;\\&quot;&quot;+stringinfo+&quot;\\&quot;&quot;))\n  {\n   posdetails = StrExtract(posdetails,1,&#039;:&#039;);\n   Nstordno = StrTrim(posdetails,&quot;\\&quot;&quot;);\n   ExitOrder(Tsym,Pcode,Nstordno);\n  }\n  \n\n} \/\/end of for loop\n} \/\/end of if loop\n\n\n}\/\/end of for loop\n\n\n_TRACE(Tsym+&quot; &quot;+Pcode+&quot; Order Cancelled&quot;);\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Bracket Order and Cover Order are multi-legged orders and canceling the bracket order and cover order for a particular trading instrument there is no direct mechanism available from the API provisions supplied by the brokers. Hence here is the Amibroker AFL code to do the exit by reading the orderbook and processing the pending cover &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/algomojo.com\/blog\/exiting-cover-order-and-bracket-order-for-a-particular-trading-instrument\/\"> <span class=\"screen-reader-text\">Exiting Cover Order and Bracket Order for a Particular Trading Instrument<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[6],"tags":[103,7,109,104,105,106,107,108],"_links":{"self":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/194"}],"collection":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/comments?post=194"}],"version-history":[{"count":3,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/194\/revisions"}],"predecessor-version":[{"id":1590,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/194\/revisions\/1590"}],"wp:attachment":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/media?parent=194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/categories?post=194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/tags?post=194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}