{"id":154,"date":"2021-01-18T17:23:26","date_gmt":"2021-01-18T17:23:26","guid":{"rendered":"https:\/\/algomojo.com\/blog\/?p=154"},"modified":"2023-09-13T06:34:54","modified_gmt":"2023-09-13T06:34:54","slug":"algomojo-index-straddle-strangle-execution-module-with-intraday-stoploss","status":"publish","type":"post","link":"https:\/\/algomojo.com\/blog\/algomojo-index-straddle-strangle-execution-module-with-intraday-stoploss\/","title":{"rendered":"Algomojo Index Straddle\/Strangle Execution Module with Intraday Stoploss"},"content":{"rendered":"\n<p>This tutorial focus on how to automate your index straddle\/strangle strategy with intraday stop-loss levels with time-based entry and exits using Algomojo Platform and Amibroker.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/i2.wp.com\/www.marketcalls.in\/wp-content\/uploads\/2021\/01\/image-30-1024x541.png?resize=1024%2C541&amp;ssl=1\" alt=\"\"\/><\/figure>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Trading Activity<\/strong><\/td><td><strong>Orders to be Placed<\/strong><\/td><\/tr><tr><td>Time Based Entry<\/td><td>Enter Straddle\/Strangle at 9.30a.m (Two legged Order)<\/td><\/tr><tr><td>Calculate the Short Call Average Price<\/td><td>Calculate the Short call Stoploss<\/td><\/tr><tr><td>Calculate the Short Put Average Price<\/td><td>Calculate the Short Put Stoploss<\/td><\/tr><tr><td>Place Stoploss Orders<\/td><td>Place the Stoploss for both Short call and Short Put if the entry order status is completed<\/td><\/tr><tr><td>Check Open Positions<\/td><td>Check the Open Positions for the Qty traded<\/td><\/tr><tr><td>Time Based Exit<\/td><td>Square of Short Call and Short Put legs for the qty present in the open positions around 3.15p.m.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>upported Brokers<\/strong>&nbsp;: Aliceblue, Tradejini, Zebu<br><strong>Supported Trading Instruments&nbsp;<\/strong>: Index Options<\/p>\n\n\n\n<p><strong>To Build this module we need 2 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<br>2)Main Option Execution Module that needs to be drag and dropped over the Blank Chart<\/p>\n\n\n\n<p><strong>1)Header Include Option Execution Module<\/strong><\/p>\n\n\n\n<p>Copy the Header Execution Module to Amibroker\\Formulas\\include folder. Save the AFL under the name&nbsp;<strong>algomojostraddle.afl<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-javascript\">\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\/\/Multi Broker Amibroker Option Straddle Execution Module\n\/\/Coded by Algomojo\n\/\/Date : 18\/01\/2021\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;);\nexch = &quot;NFO&quot;; \/\/Exchange\nRet = &quot;DAY&quot;; \/\/Retention\nAMO = &quot;NO&quot;; \/\/AMO Order\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\nfunction placestoplossorder(SLTsym,orderaction,averageprice,stoppercent,orderqty)\n{\n\t\/\/rounded of to nearest tick\n\tTickSize = 0.05;\n\t\n\tslprice = int(StrToNum(averageprice) + (StrToNum(averageprice) * stoppercent\/100));\n\tslprice = Prec(slprice,2);\n\trem = slprice % TickSize;\n\tslprice = slprice - rem;  \/\/rounded of to nearest tick size\t\n\n\n\talgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\n    api_data =&quot;{\\&quot;stgy_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;+SLTsym+&quot;\\&quot;,\\&quot;exch\\&quot;:\\&quot;&quot;+exch+&quot;\\&quot;,\\&quot;Ttranstype\\&quot;:\\&quot;&quot;+orderaction+&quot;\\&quot;,\\&quot;Ret\\&quot;:\\&quot;&quot;+Ret+&quot;\\&quot;,\\&quot;prctyp\\&quot;:\\&quot;&quot;+&quot;SL-M&quot;+&quot;\\&quot;,\\&quot;qty\\&quot;:\\&quot;&quot;+orderqty+&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;+slprice+&quot;\\&quot;,\\&quot;Pcode\\&quot;:\\&quot;&quot;+Pcode+&quot;\\&quot;,\\&quot;AMO\\&quot;:\\&quot;&quot;+AMO+&quot;\\&quot;}&quot;;\n    _TRACE(&quot;&quot;);\n    _TRACE(&quot;API Request&quot;+api_data);\n    resp=algomojo.AMDispatcher(user_apikey, api_secret,&quot;PlaceOrder&quot;,api_data,broker,ver);\n    Say( &quot;Stoploss Order is Placed&quot; ); \n    return resp;\n\n\n}\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 getaverageprice(orderno)\n{\n\nordresp = getorderhistory(orderno);\n\ndata = &quot;&quot;;\naverageprice=&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;averageprice&quot;))\n  {\n   averageprice = StrExtract(ohistory,1,&#039;:&#039;);\n   averageprice = StrTrim(averageprice,&quot;\\&quot;&quot;);\n   \n  }\n\n}\n\n}\n\n}\n\nif(averageprice==&quot;0.0&quot;)\n{\naverageprice = &quot;100&quot;;\n}\n\nreturn averageprice;\n\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,averageprice)\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\tfputs(averageprice+&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,stoppercent)\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\n\nif(orderstatus==&quot;complete&quot; OR orderstatus==&quot;rejected&quot;)\n{\naverageprice = getaverageprice(nestorderno);\n_TRACE(&quot;Average Price: &quot;+averageprice);\n}\n\nif(orderstatus==&quot;complete&quot;) \/\/changed to complete in a live environment and rejected for testing purpose\n{\n\/\/Stoploss order x% from the average price\n\nslresponse = placestoplossorder(Tsym,&quot;B&quot;,averageprice,stoppercent,qty);\n_TRACE(&quot;Leg &quot;+leg+&quot; Stoploss Response: &quot;+slresponse);\n}\n\n\n\n\n\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,averageprice);\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\taverageprice = StrTrim(StrExtract(read,3),&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>3)Main Option 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>Short Straddle &#8211; Strangle Execution.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\">_SECTION_BEGIN(&quot;Algomojo - Time Based Short Straddle\/Strangle Execution Module with Stoploss&quot;);\n\n\n#include &lt; algomojostraddle.afl &gt;\n\n\nRequestTimedRefresh(1, False);\n\nfunction GetSecondNum()\n{\n    Time = Now( 4 );\n    return Time;\n}\n\nEntryTime = ParamTime(&quot;Execution Time&quot;, &quot;09:30:00&quot;, 0);\nSqOffTime = ParamTime(&quot;SquareoffTime&quot;, &quot;15:15:00&quot;, 0);\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\n\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;S&quot;);\nleg1opt_type = ParamList(&quot;Leg 1 Option Type&quot;,&quot;PE&quot;,0);\nleg1offset = ParamStr(&quot;Leg 1 Offset&quot;,&quot;0&quot;); \n\n\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;S&quot;);\nleg2opt_type = ParamList(&quot;Leg 2 Option Type&quot;,&quot;CE&quot;,0);\nleg2offset = ParamStr(&quot;Leg 2 Offset&quot;,&quot;0&quot;); \n\nSL = Param(&quot;Stop Percentage&quot;, 20,1,100,1);\n\n\n\nstgy_name = ParamStr(&quot;Strategy Name&quot;,&quot;Test Strategy Chart&quot;);\nstatic_name = Name()+GetChartID()+interval(2)+stgy_name;\nstatic_name_algo = Name()+GetChartID()+interval(2)+stgy_name+&quot;algostatus&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;\n\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\nif(EnableAlgo == &quot;Enable&quot;)\n{\nSetChartBkColor(colorDarkGrey);\nif(GetsecondNum() == EntryTime AND StaticVarGet(static_name + EntryTime) == 0)\n{\n\t\/\/Entry Short Straddle\/Strangle at the Entry Time\n    StaticVarSet(static_name + EntryTime, 1);\n    leg1response = placeoptionorder(spot_sym,leg1expiry_dt,strike_int,leg1qty,leg1Ttranstype,leg1opt_type,leg1offset,1,SL);\n    _TRACE(&quot;Leg 1 Short Order Placed Successfully&quot;);\n    leg2response = placeoptionorder(spot_sym,leg2expiry_dt,strike_int,leg2qty,leg2Ttranstype,leg2opt_type,leg2offset,2,SL);\n    _TRACE(&quot;Leg 2 Short Order Placed Successfully&quot;);\n    \n}\nelse if(GetsecondNum() != EntryTime)\n{\n     StaticVarSet(static_name + EntryTime, 0);\n}\n\n\nif(GetsecondNum() == SqOffTime AND StaticVarGet(static_name + SqOffTime) == 0)\n{\n\n\t\/\/Exit Short Straddle \/ Short Strangle\n    StaticVarSet(static_name + SqOffTime, 1);\n    sqoff1status = squareoffoptions(leg1opt_type,leg1Ttranstype,1);\n\t_TRACE(&quot;Leg 1 Exit Response :&quot;+sqoff1status);\n\t\n\tsqoff2status = squareoffoptions(leg2opt_type,leg2Ttranstype,2);\n\t_TRACE(&quot;Leg 2 Exit Response :&quot;+sqoff2status);\n    \n}\nelse if(GetsecondNum() != SqOffTime)\n{\n     StaticVarSet(static_name + SqOffTime, 0);\n}\n\n\n}\n\n_SECTION_BEGIN(&quot;Price&quot;);\nSetChartOptions(0,chartShowArrows|chartShowDates);\n_N(Title = StrFormat(&quot;{{NAME}} - {{INTERVAL}} {{DATE}} Open %g, Hi %g, Lo %g, Close %g (%.1f%%) {{VALUES}}&quot;, O, H, L, C, SelectedValue( ROC( C, 1 ) ) ));\nPlot( C, &quot;Close&quot;, ParamColor(&quot;Color&quot;, colorDefault ), styleNoTitle | ParamStyle(&quot;Style&quot;) | GetPriceStyle() ); \n_SECTION_END();<\/code><\/pre>\n\n\n\n<p>4)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>5)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<blockquote class=\"wp-block-quote\"><p>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.<\/p><p>For Aliceblue account holders Monthly Option Symbol Format: NIFTY21JAN14500CE<\/p><p>Hence the Expiry Date needs to be entered as 21JAN<\/p><p>For Aliceblue Account holders weekly Option Symbol Format: NIFTY2111414500CE<\/p><p>Hence the Expiry Date needs to be entered as 21114<\/p><p>For Tradejini and Zebu Account Holders Monthly Option Symbol Format: NIFTY28JAN2114500CE<\/p><p>Hence the Expiry Date needs to be entered as 28JAN21<\/p><p>For Tradejini and Zebu Account Holders Monthly Option Symbol Format: NIFTY14JAN2114500CE<\/p><p>Hence the Expiry Date needs to be entered as 14JAN21<\/p><\/blockquote>\n\n\n\n<p>1AlgomojoCE.csv and 1AlgomojoPE.csv and 2AlgomojoCE.csv and 2AlgomojoPE.csv will be saved whenever the straddle or strangle are executed and files will be saved only if the execution status is in&nbsp;<strong>complete&nbsp;or in rejected<\/strong>&nbsp;mode<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/i0.wp.com\/www.marketcalls.in\/wp-content\/uploads\/2021\/01\/image-31.png?resize=460%2C869&amp;ssl=1\" alt=\"\"\/><\/figure>\n\n\n\n<p>7)Ensure Log Window is open to capture the Trace Logs<\/p>\n\n\n\n<p>8)Bingo Now you have setup the complete end to end automated straddle\/strangle execution. Straddle\/Strangle Control can be controlled via offset parameters.<\/p>\n\n\n\n<p>9)Code is designed in such a way that stop loss is placed for individual price legs and not for the combined premium of Straddle\/Strangle Spreads. And Stoploss orders will be placed only if the entry order goes to a completed state.<\/p>\n\n\n\n<p>10)Make the Algo Enable and send time based straddle execution (Supports both entry and exit conditions).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This tutorial focus on how to automate your index straddle\/strangle strategy with intraday stop-loss levels with time-based entry and exits using Algomojo Platform and 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,85,86,87],"_links":{"self":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/154"}],"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=154"}],"version-history":[{"count":5,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/154\/revisions"}],"predecessor-version":[{"id":648,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/154\/revisions\/648"}],"wp:attachment":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/media?parent=154"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/categories?post=154"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/tags?post=154"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}