{"id":135,"date":"2021-01-13T07:10:08","date_gmt":"2021-01-13T07:10:08","guid":{"rendered":"https:\/\/algomojo.com\/blog\/?p=135"},"modified":"2023-09-13T06:40:53","modified_gmt":"2023-09-13T06:40:53","slug":"multi-legged-options-execution-amibroker-module-button-style-trading","status":"publish","type":"post","link":"https:\/\/algomojo.com\/blog\/multi-legged-options-execution-amibroker-module-button-style-trading\/","title":{"rendered":"Multi Legged Options Execution \u2013 Amibroker Module \u2013 Button Style Trading"},"content":{"rendered":"\n<p>This tutorial provides you a basic framework for sending multi-legged options module and how to exit the multi-legged option module using a simple button trading based example. However, this module can be extended to trade any sort of neutral trading strategy using Amibroker.<\/p>\n\n\n\n<p>This button trading module also comes with providing priority to execute the Long Option Orders followed by Option Selling Orders in order to effectively manage the Margin as per the&nbsp;<a href=\"https:\/\/www.marketcalls.in\/analysis\/new-margin-framework-for-options-trading-and-market-outlook-for-june-2020.html\">new margin framework for Derivatives<\/a>.<\/p>\n\n\n\n<p><strong>Supported Brokers<\/strong>&nbsp;: Aliceblue, Tradejini, Zebu<\/p>\n\n\n\n<p><strong>Supported Amibroker Version<\/strong>&nbsp;: Amibroker Version 6.22 onwards<\/p>\n\n\n\n<p><strong>Supported Algomojo API Version : 1.0<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2021\/01\/image-6.png\"><img loading=\"lazy\" width=\"1914\" height=\"1034\" src=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2021\/01\/image-6.png\" alt=\"\" class=\"wp-image-136\"\/><\/a><\/figure>\n\n\n\n<p>In case if you are new to the option execution module then kindly kickstart with the basic tutorial on&nbsp;<a href=\"https:\/\/algomojo.com\/blog\/sending-option-orders-and-exiting-option-orders-in-algomojo-platform-using-amibroker\/\">Sending Option Orders from Amibroker using Algomojo Platform<\/a><\/p>\n\n\n\n<p>In case if you are looking for sending single-leg option orders from Amibroker based on Signals from Futures\/Spot then kickstart with the basic tutorial on<a href=\"https:\/\/algomojo.com\/blog\/send-option-orders-from-futures-or-spot-signals-in-amibroker-using-algomojo-platform\/\">&nbsp;Send Option Orders from Futures or Spot Signals in Amibroker using Algomojo Platform<\/a><\/p>\n\n\n\n<p><strong>To Build Button Trading module we need 2 Major components<\/strong><\/p>\n\n\n\n<p>1)Header Option Execution Module that needs to be placed inside the&nbsp;<strong>Amibroker\\Formulas\\Include<\/strong>&nbsp;folder<\/p>\n\n\n\n<p>2)Main Options Entry\/Exit Button Execution Module that needs to be drag and dropped over the Blank Charts<\/p>\n\n\n\n<p><strong>1)Header Multi Legged Options Execution Module<\/strong><\/p>\n\n\n\n<p>Copy the Header Multi Leg Execution Module to&nbsp;<strong>Amibroker\\Formulas\\include<\/strong>&nbsp;folder. Save the AFL under the name&nbsp;<strong>algomojomultioptions.afl<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-javascript\">\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/Multi Broker Amibroker Option Execution Module\n\/\/Coded by Algomojo\n\/\/Date : 30\/12\/2020\n\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\n\n\n\/\/Use this code only for Single Legged Long Only Options and Exiting Long Only Options\n\n\n_SECTION_BEGIN(&quot;Algomojo Multi legged Options&quot;);\n\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;4a94db82ea4fa140afaa2f039efffd18&quot;); \/\/Enter your API secret key here\ns_prdt_ali = &quot;BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML&quot;;\nprctyp = ParamList(&quot;prctyp&quot;,&quot;MKT|L|SL|SL-M&quot;,0);\nPcode = ParamList(&quot;Pcode&quot;,&quot;NRML|CO|MIS&quot;,2);\nPrice = ParamList(&quot;Price&quot;,&quot;0&quot;);\nTrigPrice = ParamList(&quot;TrigPrice&quot;,&quot;0&quot;);\n\nstgy_name = ParamStr(&quot;Strategy Name&quot;, &quot;Options&quot;);\nbroker = ParamStr(&quot;Broker&quot;,&quot;ab&quot;); \/\/Broker Short Code - ab - aliceblue, tj - tradejini, zb - zebu, en - enrich\nver = ParamStr(&quot;API Version&quot;,&quot;1.0&quot;);\nfpath = ParamStr(&quot;Symbol Filepath&quot;, &quot;C:\\\\Program Files (x86)\\\\AmiBroker\\\\Formulas\\\\Algomojo\\\\&quot;);\ntimer = 1; \/\/3 seconds for providing delay after placing order\n\nRequestTimedRefresh(1,False);\n\n\nfunction getnestorderno(response)\n{\n\nNOrdNo = &quot;&quot;;\n\n\nif(StrFind(response,&quot;NOrdNo&quot;)) \/\/Matches the orderstatus\n{\nNOrdNo = StrTrim( response, &quot;{\\&quot;NOrdNo\\&quot;:&quot; );\nNOrdNo = StrTrim( NOrdNo, &quot;\\&quot;,\\&quot;stat\\&quot;:\\&quot;Ok\\&quot;}&quot; );\n}\n\nreturn NOrdNo;\n}\n\nfunction getorderhistory(orderno)\n{\n\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\napi_data = &quot;{\\&quot;uid\\&quot;:\\&quot;&quot;+uid+&quot;\\&quot;,\\&quot;NOrdNo\\&quot;:\\&quot;&quot;+orderno+&quot;\\&quot;,\\&quot;s_prdt_ali\\&quot;:\\&quot;&quot;+s_prdt_ali+&quot;\\&quot;}&quot;;\nresp=algomojo.AMDispatcher(user_apikey, api_secret,&quot;OrderHistory&quot;,api_data,broker,ver);\n\nreturn resp;\n\n\n}\n\n\nfunction getsymbol(orderno)\n{\n\nordresp = getorderhistory(orderno);\n\ndata = &quot;&quot;;\nTrsym=&quot;&quot;;\n\nfor( item = 0; ( sym = StrExtract( ordresp, item,&#039;{&#039; )) != &quot;&quot;; item++ )\n{\n\nsym = StrTrim(sym,&quot; &quot;); \/\/Trim Whitespaces\n\n\n\n\nif(StrFind(sym,&quot;complete&quot;) OR StrFind(sym,&quot;rejected&quot;)) \/\/Matches the orderstatus\n{\n\nflag = 1; \/\/turn on the flag\n\ndata = sym;\n\nfor( jitem = 0; ( ohistory = StrExtract( data, jitem,&#039;,&#039; )) != &quot;&quot;; jitem++ )\n{\n\nif(Strfind(ohistory,&quot;Trsym&quot;))\n  {\n   Trsym = StrExtract(ohistory,1,&#039;:&#039;);\n   Trsym = StrTrim(Trsym,&quot;\\&quot;&quot;);\n   \n  }\n\n}\n\n}\n\n}\n\nreturn Trsym;\n\n}\n\nfunction getorderstatus(orderno)\n{\norderstatus=&quot;&quot;;\nordresp = getorderhistory(orderno);\n\ndata = &quot;&quot;;\n\nfor( item = 0; ( sym = StrExtract( ordresp, item,&#039;{&#039; )) != &quot;&quot;; item++ )\n{\n\nsym = StrTrim(sym,&quot; &quot;); \/\/Trim Whitespaces\n\n\nif(StrFind(sym,&quot;complete&quot;) OR StrFind(sym,&quot;rejected&quot;)) \/\/Matches the orderstatus\n{\n\nflag = 1; \/\/turn on the flag\n\ndata = sym;\n\nfor( jitem = 0; ( ohistory = StrExtract( data, jitem,&#039;,&#039; )) != &quot;&quot;; jitem++ )\n{\n\nif(Strfind(ohistory,&quot;Status&quot;))\n  {\n   orderstatus = StrExtract(ohistory,1,&#039;:&#039;);\n   orderstatus = StrTrim(orderstatus,&quot;\\&quot;&quot;);\n   \n  }\n\n}\n\n}\n\n}\n\nreturn orderstatus;\n}\/\/end function\n\n\nfunction gettoken(symbol)\n{\nstoken=&quot;&quot;;\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\napi_data = &quot;{\\&quot;s\\&quot;:\\&quot;&quot;+symbol+&quot;\\&quot;}&quot;;\nresp=algomojo.AMDispatcher(user_apikey, api_secret,&quot;fetchsymbol&quot;,api_data,broker,ver);\n\nfor( item = 0; ( sym = StrExtract( resp, item,&#039;{&#039; )) != &quot;&quot;; item++ )\n{\n\nsym = StrTrim(sym,&quot; &quot;); \/\/Trim Whitespaces\n\ndata = sym;\n\nfor( jitem = 0; ( ofetch = StrExtract( data, jitem,&#039;,&#039; )) != &quot;&quot;; jitem++ )\n{\n\nif(Strfind(ofetch,&quot;symbol_token&quot;))\n  {\n   stoken = StrExtract(ofetch,1,&#039;:&#039;);\n   stoken = StrTrim(stoken,&quot;\\&quot;&quot;);\n   \n  }\n\n}\n\n}\nreturn stoken;\n}\n\nfunction writetofile(filepath,ordno,symbol,ostatus)\n{\n    result =0;\n\tif(ostatus==&quot;complete&quot; OR ostatus==&quot;rejected&quot;)\n\t{\n    fh = fopen( filepath, &quot;w&quot;); \/\/Filepath of csv file with symbols\n\tif(fh)\n    {\n\t\t\n\t\tfputs(ordno + &quot;,&quot;, fh);\n\t\tfputs(symbol + &quot;,&quot;, fh);\n\t\tfputs(ostatus+&quot;,&quot;, fh);\n\t\tfclose(fh);\n\t\tresult =1;\n    } \n    }\n    \n    return result; \n}\n\nfunction placeoptionorder(spot_sym,expiry_dt,strike_int,qty,Ttranstype,opt,off,leg)\n{\n\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\napi_data = &quot;{\\&quot;strg_name\\&quot;:\\&quot;&quot;+stgy_name+&quot;\\&quot;,\\&quot;spot_sym\\&quot;:\\&quot;&quot;+spot_sym+&quot;\\&quot;,\\&quot;expiry_dt\\&quot;:\\&quot;&quot;+expiry_dt+&quot;\\&quot;,\\&quot;opt_type\\&quot;:\\&quot;&quot;+opt+&quot;\\&quot;,\\&quot;Ttranstype\\&quot;:\\&quot;&quot;+Ttranstype+&quot;\\&quot;,\\&quot;prctyp\\&quot;:\\&quot;&quot;+prctyp+&quot;\\&quot;,\\&quot;qty\\&quot;:\\&quot;&quot;+qty+&quot;\\&quot;,\\&quot;Price\\&quot;:\\&quot;&quot;+Price+&quot;\\&quot;,\\&quot;TrigPrice\\&quot;:\\&quot;&quot;+TrigPrice+&quot;\\&quot;,\\&quot;Pcode\\&quot;:\\&quot;&quot;+Pcode+&quot;\\&quot;,\\&quot;strike_int\\&quot;:\\&quot;&quot;+strike_int+&quot;\\&quot;,\\&quot;offset\\&quot;:\\&quot;&quot;+off+&quot;\\&quot;}&quot;;\nresp=algomojo.AMDispatcher(user_apikey, api_secret,&quot;PlaceFOOptionsOrder&quot;,api_data,broker,ver);\n_TRACE(&quot;\\n&quot;+api_data);\n\/\/Get Nest Order Number\nnestorderno = getnestorderno(resp);\n_TRACE(&quot;\\nNest Order No : &quot;+nestorderno);\n\ntradetime=GetPerformanceCounter()\/1000; \n\nwhile ((GetPerformanceCounter()\/1000 - tradetime) &lt; timer)\n{            \n\n\/\/Get Trading Symbol\nTsym = getsymbol(nestorderno);\n}\n_TRACE(&quot;\\nTrading Symbol : &quot;+Tsym);\n\n\/\/Get Order Status\norderstatus = getorderstatus(nestorderno);\n_TRACE(&quot;\\nOrder Status : &quot;+orderstatus);\n\/\/Get Token Number\n\/\/token = gettoken(Tsym);\n\/\/_TRACE(&quot;\\nSymbol Token : &quot;+token);\nif(opt==&quot;CE&quot;)\n{\npath = fpath+leg+&quot;AlgomojoCE.csv&quot;;\n}\nif(opt==&quot;PE&quot;)\n{\npath = fpath+leg+&quot;AlgomojoPE.csv&quot;;\n}\n\nwritestatus = writetofile(path,nestorderno,Tsym,orderstatus);\n_TRACEF(WriteIf(writestatus,&quot;\\nWriting to File - Success&quot;,&quot;\\nWriting to File - Failure&quot;));\n\/\/resp = resp+&quot;\\nNest Order No : &quot;+nestorderno+&quot;\\nTrading Symbol : &quot;+Tsym+&quot;\\nOrder Status : &quot;+orderstatus+&quot;\\nSymbol Token : &quot;+token;\nreturn Tsym;\n\n}\n\n\nfunction getquantity(Tsym)\n{\n\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\napi_data =&quot;{\\&quot;uid\\&quot;:\\&quot;&quot;+uid+&quot;\\&quot;,\\&quot;actid\\&quot;:\\&quot;&quot;+uid+&quot;\\&quot;,\\&quot;type\\&quot;:\\&quot;&quot;+&quot;NET&quot;+&quot;\\&quot;,\\&quot;s_prdt_ali\\&quot;:\\&quot;&quot;+s_prdt_ali+&quot;\\&quot;}&quot;;\nresp=algomojo.AMDispatcher(user_apikey, api_secret,&quot;PositionBook&quot;,api_data,broker,ver);\n\n\/\/Initialization\nflag = 0;\npossym = &quot;&quot;;\nposNetqty =0;\n\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(Strfind(sym,Tsym) AND StrFind(sym,Pcode)) \/\/Matches the symbol and \/\/Matches the Order Type\n{\n\nflag = 1; \/\/turn on the flag\n\ndata = sym;\n\n_TRACE(&quot; Position Book  : &quot; +data);\n\nfor( jitem = 0; ( posdetails = StrExtract( data, jitem,&#039;,&#039; )) != &quot;&quot;; jitem++ )\n{\n\n  if(Strfind(posdetails,&quot;Netqty&quot;))\n  {\n   posdetails = StrExtract(posdetails,1,&#039;:&#039;);\n   posNetqty = StrToNum(StrTrim(posdetails,&quot;\\&quot;&quot;));\n   _TRACE(&quot;\\nNetQty : &quot;+posNetqty);\n  }\n  \n\n} \/\/end of for loop\n}\n\n}\/\/end of for loop\n\nif(flag==0)\n{\n_TRACE(&quot;\\nTrading Symbol Not Found&quot;);\n}\n\nreturn posNetqty;\n\n}\n\n\nfunction squareoffoptions(opt,Ttranstype,leg)\n{\n\nttype=&quot;&quot;;\n\nif(Ttranstype==&quot;B&quot;)\n{\n\nttype = &quot;S&quot;;\n\n}\n\nif(Ttranstype==&quot;S&quot;)\n{\n\nttype = &quot;B&quot;;\n\n}\n\n\nordno = &quot;&quot;;\nSymbol = &quot;&quot;;\nostatus =&quot;&quot;;\n\n\tif(opt==&quot;CE&quot;)\n\t{\n\tfpath = fpath+leg+&quot;AlgomojoCE.csv&quot;;\n\t}\n\tif(opt==&quot;PE&quot;)\n\t{\n\tfpath = fpath+leg+&quot;AlgomojoPE.csv&quot;;\n\t}\n\t\n\tfh = fopen(fpath, &quot;r&quot;); \n\tif(fh)\n\t{\n\t\t\n\t\tread = fgets(fh);\n\t\tordno = StrTrim(StrExtract(read,0),&quot; &quot;);\n\t\tSymbol = StrTrim(StrExtract(read,1),&quot; &quot;);\n\t\tostatus = StrTrim(StrExtract(read,2),&quot; &quot;);\n\t\t\t\t\t\n\t\tfclose(fh);\n\t}\n\t\n\texitqty = getquantity(Symbol);\n\t_TRACE(&quot;Net Quantity in the OrderBook for the Symbol : &quot;+Symbol+&quot; is : &quot;+exitqty);\n\t\n\tif(ostatus==&quot;complete&quot; AND exitqty!=0)\n\t{\n\talgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\n\tapi_data = &quot;{\\&quot;strg_name\\&quot;:\\&quot;&quot;+stgy_name+&quot;\\&quot;,\\&quot;s_prdt_ali\\&quot;:\\&quot;&quot;+s_prdt_ali+&quot;\\&quot;,\\&quot;Tsym\\&quot;:\\&quot;&quot;+Symbol+&quot;\\&quot;,\\&quot;exch\\&quot;:\\&quot;&quot;+&quot;NFO&quot;+&quot;\\&quot;,\\&quot;Ttranstype\\&quot;:\\&quot;&quot;+ttype+&quot;\\&quot;,\\&quot;Ret\\&quot;:\\&quot;&quot;+&quot;DAY&quot;+&quot;\\&quot;,\\&quot;prctyp\\&quot;:\\&quot;&quot;+prctyp+&quot;\\&quot;,\\&quot;qty\\&quot;:\\&quot;&quot;+exitqty+&quot;\\&quot;,\\&quot;discqty\\&quot;:\\&quot;&quot;+&quot;0&quot;+&quot;\\&quot;,\\&quot;MktPro\\&quot;:\\&quot;&quot;+&quot;NA&quot;+&quot;\\&quot;,\\&quot;Price\\&quot;:\\&quot;&quot;+&quot;0&quot;+&quot;\\&quot;,\\&quot;TrigPrice\\&quot;:\\&quot;&quot;+&quot;0&quot;+&quot;\\&quot;,\\&quot;Pcode\\&quot;:\\&quot;&quot;+Pcode+&quot;\\&quot;,\\&quot;AMO\\&quot;:\\&quot;&quot;+&quot;NO&quot;+&quot;\\&quot;}&quot;;\n\tresp=algomojo.AMDispatcher(user_apikey, api_secret,&quot;PlaceOrder&quot;,api_data,broker,ver);\n\t\/\/Get Nest Order Number\n\tnestorderno = getnestorderno(resp);\n\t_TRACE(&quot;\\nNest Order No : &quot;+nestorderno);\n\n\ttradetime=GetPerformanceCounter()\/1000; \n\n\twhile ((GetPerformanceCounter()\/1000 - tradetime) &lt; timer)\n\t{            \n\n\t\/\/Get Trading Symbol\n\tTsym = getsymbol(nestorderno);\n\t}\n\t_TRACE(&quot;\\nTrading Symbol : &quot;+Tsym);\n\n\t\/\/Get Order Status\n\torderstatus = getorderstatus(nestorderno);\n\t_TRACE(&quot;\\nOrder Status : &quot;+orderstatus);\n\t\n\t}\n\telse\n\t{\n\t\n\tresp = &quot;Not Squared Off. Either No Open Position Exist or Prev Signal Status Not in Completed State&quot;;\n\t\n\t}\n\t\n\treturn resp;\n}\n_SECTION_END();\n<\/code><\/pre>\n\n\n\n<p><strong>2)Main Options Enty\/Exit Button Execution Module<\/strong><\/p>\n\n\n\n<p>Copy the Main Execution Module to Amibroker\\Formulas\\Algomojo Platform. Save the AFL under the name&nbsp;<strong>Algomojo Button Trading Multi Legged Options.afl<\/strong><\/p>\n\n\n\n<p>Now drag and Drop the Module on top of your Charting with Buy\/Sell Trading System<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-javascript\">\/*\nCreated By : Algomojo\nCreated on : 01 Apr 2020.\nWebsite : www.algomojo.com\n*\/\n\n#include &lt; algomojomultioptions.afl &gt;\n\n\n_SECTION_BEGIN(&quot;Button Trading - Multi Broker - Algomojo&quot;);\n\n\/\/Notes\n\/\/This afl code works only on Amibroker 6.22 and higher version\n\/\/Requires Algomojo Trading account\n\/\/Replace the API Key and API Secrety key with yours\n\nVersion(6.22);\n\nRequestTimedRefresh(1, False); \/\/ Send orders even if Amibroker is minimized or Chart is not active\n\nspot_sym = ParamStr(&quot;spot_sym&quot;,&quot;NIFTY&quot;); \/\/Enter the symbol name here\nstrike_int = ParamStr(&quot;strike_int&quot;,&quot;50&quot;);\nlotsize = Param(&quot;Symbol Lot Size&quot;,75,1,50000);\n\nleg1 = ParamToggle(&quot;1st Leg&quot;,&quot;Disable|Enable&quot;);\nleg1expiry_dt = ParamStr(&quot;Leg 1 expiry_dt&quot;, &quot;28JAN21&quot;);\nleg1qty = Param(&quot;Leg 1 Lot Size&quot;,1)*lotsize;\nleg1Ttranstype = ParamList(&quot;Leg 1 Transaction Type&quot;,&quot;B|S&quot;);\nleg1opt_type = ParamList(&quot;Leg 1 Option Type&quot;,&quot;CE|PE&quot;,0);\nleg1offset = ParamStr(&quot;Leg 1 Offset&quot;,&quot;0&quot;); \n\nleg2 = ParamToggle(&quot;2ng Leg&quot;,&quot;Disable|Enable&quot;);\nleg2expiry_dt = ParamStr(&quot;Leg 2 expiry_dt&quot;, &quot;28JAN21&quot;);\nleg2qty = Param(&quot;Leg 2 Lot Size&quot;,1)*lotsize;\nleg2Ttranstype = ParamList(&quot;Leg 2 Transaction Type&quot;,&quot;B|S&quot;);\nleg2opt_type = ParamList(&quot;Leg 2 Option Type&quot;,&quot;CE|PE&quot;,0);\nleg2offset = ParamStr(&quot;Leg 2 Offset&quot;,&quot;0&quot;); \n\n\nleg3 = ParamToggle(&quot;3rd Leg&quot;,&quot;Disable|Enable&quot;);\nleg3expiry_dt = ParamStr(&quot;Leg 3 expiry_dt&quot;, &quot;28JAN21&quot;);\nleg3qty = Param(&quot;Leg 3 Lot Size&quot;,1)*lotsize;\nleg3Ttranstype = ParamList(&quot;Leg 3 Transaction Type&quot;,&quot;B|S&quot;);\nleg3opt_type = ParamList(&quot;Leg 3 Option Type&quot;,&quot;CE|PE&quot;,0);\nleg3offset = ParamStr(&quot;Leg 3 Offset&quot;,&quot;0&quot;); \n\nleg4 = ParamToggle(&quot;4th Leg&quot;,&quot;Disable|Enable&quot;);\nleg4expiry_dt = ParamStr(&quot;Leg 4 expiry_dt&quot;, &quot;28JAN21&quot;);\nleg4qty = Param(&quot;Leg 4 Lot Size&quot;,1)*lotsize;\nleg4Ttranstype = ParamList(&quot;Leg 4 Transaction Type&quot;,&quot;B|S&quot;);\nleg4opt_type = ParamList(&quot;Leg 4 Option Type&quot;,&quot;CE|PE&quot;,0);\nleg4offset = ParamStr(&quot;Leg 4 Offset&quot;,&quot;0&quot;); \n\n\ntradedelay = Param(&quot;Trade Delay&quot;,0);\nEnableAlgo = ParamList(&quot;Algo Mode&quot;,&quot;Disable|Enable&quot;,0); \/\/ Algo Mode\n\n\nstatic_name_ = Name()+GetChartID()+interval(2)+stgy_name;\nstatic_name_algo = Name()+GetChartID()+interval(2)+stgy_name+&quot;algostatus&quot;;\n\/\/StaticVarSet(static_name_algo, -1); \nGfxSelectFont( &quot;BOOK ANTIQUA&quot;, 14, 100 );\nGfxSetBkMode( 1 );\nif(EnableAlgo == &quot;Enable&quot;)\n{\nAlgoStatus = &quot;Algo Enabled&quot;;\nGfxSetTextColor( colorGreen ); \nGfxTextOut( &quot;Algostatus : &quot;+AlgoStatus , 20, 40); \nif(Nz(StaticVarGet(static_name_algo),0)!=1)\n{\n_TRACE(&quot;Algo Status : Enabled&quot;);\nStaticVarSet(static_name_algo, 1);\n}\n}\nif(EnableAlgo == &quot;Disable&quot;)\n{\nAlgoStatus = &quot;Algo Disabled&quot;;\nGfxSetTextColor( colorRed ); \nGfxTextOut( &quot;Algostatus : &quot;+AlgoStatus , 20, 40); \nif(Nz(StaticVarGet(static_name_algo),0)!=0)\n{\n_TRACE(&quot;Algo Status : Disabled&quot;);\nStaticVarSet(static_name_algo, 0);\n}\n}\nif(EnableAlgo == &quot;LongOnly&quot;)\n{\nAlgoStatus = &quot;Long Only&quot;;\nGfxSetTextColor( colorYellow ); \nGfxTextOut( &quot;Algostatus : &quot;+AlgoStatus , 20, 40); \nif(Nz(StaticVarGet(static_name_algo),0)!=2)\n{\n_TRACE(&quot;Algo Status : Long Only&quot;);\nStaticVarSet(static_name_algo, 2);\n}\n}\nif(EnableAlgo == &quot;ShortOnly&quot;)\n{\nAlgoStatus = &quot;Short Only&quot;;\nGfxSetTextColor( colorYellow ); \nGfxTextOut( &quot;Algostatus : &quot;+AlgoStatus , 20, 40); \nif(Nz(StaticVarGet(static_name_algo),0)!=3)\n{\n_TRACE(&quot;Algo Status : Short Only&quot;);\nStaticVarSet(static_name_algo, 3);\n}\n}\n\n\n\nresp = &quot;&quot;;\n\n\n\n\nfunction Buyorder()\n{\n\n\/\/Managing the efficient Margin according to new margin regulations. Giving Priority for Long Options first followed by option short positions\n\tif(leg1 AND leg1Ttranstype==&quot;B&quot;)\n\t{\n    leg1response = placeoptionorder(spot_sym,leg1expiry_dt,strike_int,leg1qty,leg1Ttranstype,leg1opt_type,leg1offset,1);\n    _TRACE(&quot;Leg 1 Buy Order Placed Successfully&quot;);\n    }\n    if(leg2 AND leg2Ttranstype==&quot;B&quot;)\n\t{\n    leg2response = placeoptionorder(spot_sym,leg2expiry_dt,strike_int,leg2qty,leg2Ttranstype,leg2opt_type,leg2offset,2);\n    _TRACE(&quot;Leg 2 Buy Order Placed Successfully&quot;);\n    }\n    if(leg3  AND leg3Ttranstype==&quot;B&quot;)\n\t{\n    leg3response = placeoptionorder(spot_sym,leg3expiry_dt,strike_int,leg3qty,leg3Ttranstype,leg3opt_type,leg3offset,3);\n    _TRACE(&quot;Leg 3 Buy Order Placed Successfully&quot;);\n    }\n    if(leg4  AND leg4Ttranstype==&quot;B&quot;)\n\t{\n    leg4response = placeoptionorder(spot_sym,leg4expiry_dt,strike_int,leg4qty,leg4Ttranstype,leg4opt_type,leg4offset,4);\n    _TRACE(&quot;Leg 4 Buy Order Placed Successfully&quot;);\n    }\n    \n    \n    if(leg1 AND leg1Ttranstype==&quot;S&quot;)\n\t{\n    leg1response = placeoptionorder(spot_sym,leg1expiry_dt,strike_int,leg1qty,leg1Ttranstype,leg1opt_type,leg1offset,1);\n    _TRACE(&quot;Leg 1 Sell Order Placed Successfully&quot;);\n    }\n    if(leg2 AND leg2Ttranstype==&quot;S&quot;)\n\t{\n    leg2response = placeoptionorder(spot_sym,leg2expiry_dt,strike_int,leg2qty,leg2Ttranstype,leg2opt_type,leg2offset,2);\n    _TRACE(&quot;Leg 2 Sell Order Placed Successfully&quot;);\n    }\n    if(leg3  AND leg3Ttranstype==&quot;S&quot;)\n\t{\n    leg3response = placeoptionorder(spot_sym,leg3expiry_dt,strike_int,leg3qty,leg3Ttranstype,leg3opt_type,leg3offset,3);\n    _TRACE(&quot;Leg 3 Sell Order Placed Successfully&quot;);\n    }\n    if(leg4  AND leg4Ttranstype==&quot;S&quot;)\n\t{\n    leg4response = placeoptionorder(spot_sym,leg4expiry_dt,strike_int,leg4qty,leg4Ttranstype,leg4opt_type,leg4offset,4);\n    _TRACE(&quot;Leg 4 Sell Order Placed Successfully&quot;);\n    }\n        \n\tSay( &quot;Order Placed&quot; ); \n}\n\nfunction Sellorder()\n{\n\tif(leg1)\n\t{\n    sqoff1status = squareoffoptions(leg1opt_type,leg1Ttranstype,1);\n\t_TRACE(&quot;Leg 1 Exit Response :&quot;+sqoff1status);\n\t}\n\t\n\tif(leg2)\n\t{\n\tsqoff2status = squareoffoptions(leg2opt_type,leg2Ttranstype,2);\n\t_TRACE(&quot;Leg 2 Exit Response :&quot;+sqoff2status);\n\t}\n\tif(leg3)\n\t{\n\tsqoff3status = squareoffoptions(leg3opt_type,leg3Ttranstype,3);\n\t_TRACE(&quot;Leg 3 Exit Response :&quot;+sqoff3status);\n\t}\n\tif(leg4)\n\t{\n\tsqoff4status = squareoffoptions(leg4opt_type,leg4Ttranstype,4);\n\t_TRACE(&quot;Leg 4 Exit Response :&quot;+sqoff4status);\n\t}\n\t\n    _TRACE(&quot;Order Placed Successfully&quot;);\n            \n    Say( &quot;Order Placed&quot; );\n}\n\nfunction GuiButtonTrigger( ButtonName, x, y, width, Height ) {\n\t\/\/\/ @link http:\/\/forum.amibroker.com\/t\/guibuttons-for-everyone\/1716\/4\n\t\/\/\/ by beaver &amp; fxshrat\n\t\/\/\/ version 1.1\n\tglobal IDset;\n\tlocal id, event, clickeven;\n\t\n\tif( typeof( IDset ) == &quot;undefined&quot; ) IDset = 0; \n\n\t\/\/_TRACEF( &quot;IDset before: %g&quot;, IDset );\t\n\tGuiButton( ButtonName, ++IDset, x, y, width, height, 7 ); \n\t\/\/_TRACEF( &quot;IDset after: %g&quot;, IDset );\n\tresult = 0;\n\tid = GuiGetEvent( 0, 0 );\/\/ receiving button id\n\tevent = GuiGetEvent( 0, 1 );\/\/ receiving notifyflag\n\tclickevent = event == 1;\n\tBuyClicked = id == 1 &amp;&amp; clickevent;\n\tSellClicked = id == 2 &amp;&amp; clickevent;\n\tif( BuyClicked AND StaticVarGet(Name()+GetChartID()+&quot;buyAlgo&quot;)==0 ) \n\t{\n\t\tBuyOrder();\n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;buyAlgo&quot;,1); \n\t}\n\telse\n\t{\n\t\tStaticVarSet(Name()+GetChartID()+&quot;buyAlgo&quot;,0);\n\t}\n\tif( SellClicked AND StaticVarGet(Name()+GetChartID()+&quot;sellAlgo&quot;)==0 ) \n\t{\n\t\tSellOrder();\n\t\tresult = -1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;sellAlgo&quot;,1); \n\t}\n\telse\n\t{\n\t\tStaticVarSet(Name()+GetChartID()+&quot;sellAlgo&quot;,0); \n\t}\n\treturn result;\n\t\n}\n\t\n\tBuyTrigger = GuiButtonTrigger( &quot;Enter Options&quot;, 0, 100, 200, 30 );\n\tSellTrigger = GuiButtonTrigger( &quot;Exit Options&quot;, 200, 100, 200, 30 );\n\tGuiSetColors( 1, 3, 2, colorRed, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); \n\nTitle = &quot;Trigger: &quot; + WriteIf(BuyTrigger==1,&quot;Buy Triggered&quot;,WriteIf(BuyTrigger==-1,&quot;Sell Triggered&quot;,&quot;0&quot;));\n\nSetChartOptions(0 , chartShowArrows | chartShowDates);\nPlot(Close,&quot;Candle&quot;, colorDefault, styleCandle);\n\n_SECTION_END();\n<\/code><\/pre>\n\n\n\n<p>3)Now right-click over the charts and set the Client ID, user_apikey, api_secretkey,broker and set the required quantity<\/p>\n\n\n\n<p>4)Ensure the&nbsp;<strong>Symbol File Path Exists<\/strong>. This is the path where the executed symbols CE and PE Options will get saved in a CSV file for later squareoff.<\/p>\n\n\n\n<p>5)Enable\/Disable Multiple Leg Options, Set the Symbol, Lot Size, Strike Interval (Difference between two immediate strike price), Set the Expiry Date, Lot Size and Offset for Multiple Legs as shown below<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-markdown\">Ensure while entering the Option Expiry Format. Login to Algomojo Terminal and check out the weekly and monthly option format and enter the expiry date accordingly.\n\nFor Aliceblue account holders Monthly Option Symbol Format: NIFTY21JAN14500CE\n\nHence the Expiry Date needs to be entered as 21JAN\n\nFor Aliceblue Account holders weekly Option Symbol Format: NIFTY2111414500CE\n\nHence the Expiry Date needs to be entered as 21114\n\nFor Tradejini and Zebu Account Holders Monthly Option Symbol Format: NIFTY28JAN2114500CE\n\nHence the Expiry Date needs to be entered as 28JAN21\n\nFor Tradejini and Zebu Account Holders Monthly Option Symbol Format: NIFTY14JAN2114500CE\n\nHence the Expiry Date needs to be entered as 14JAN21<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2021\/01\/image-7.png\"><img loading=\"lazy\" width=\"455\" height=\"1034\" src=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2021\/01\/image-7.png\" alt=\"\" class=\"wp-image-137\"\/><\/a><\/figure>\n\n\n\n<p>6)Ensure Log Window is open to capture the Trace Logs<\/p>\n\n\n\n<p>7)Enable to Algo Mode : Enable State<\/p>\n\n\n\n<p>7)Bingo Now on the click of Enter Options you will be able to send Multi legged options as per your settings and you can also see that long option orders take priority in execution compared to short legged options to effectively manage the margin.<\/p>\n\n\n\n<p>8)on the Press of Exit Options button automatically all the multi-legged option orders get squared off. Square off happens only if the position exists in the position book. If no position is found one will get the following log notification<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Not Squared Off. Either No Open Position Exist or Prev Signal Status Not in Completed State<\/p><\/blockquote>\n\n\n\n<p>This module can be extended further to place multi legged option orders autmatically from Amibroker by doing customization according to traders requirement.<\/p>\n\n\n\n<p>If in case you need a customized multi-legged options requirement you can send your requirement to support@algomojo.com<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial provides you a basic framework for sending multi-legged options module and how to exit the multi-legged option module using a simple button trading based example. However, this module can be extended to trade any sort of neutral trading strategy using Amibroker.<\/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,15],"tags":[13,75,71,7,76,17,70],"_links":{"self":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/135"}],"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=135"}],"version-history":[{"count":8,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/135\/revisions"}],"predecessor-version":[{"id":653,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/135\/revisions\/653"}],"wp:attachment":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/media?parent=135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/categories?post=135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/tags?post=135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}