{"id":233,"date":"2023-02-08T06:46:00","date_gmt":"2023-02-08T06:46:00","guid":{"rendered":"https:\/\/algomojo.com\/blog\/?p=233"},"modified":"2023-09-13T05:42:13","modified_gmt":"2023-09-13T05:42:13","slug":"option-button-trading-to-trade-atm-itm-otm-options","status":"publish","type":"post","link":"https:\/\/algomojo.com\/blog\/option-button-trading-to-trade-atm-itm-otm-options\/","title":{"rendered":"Option Button Trading to Trade ATM, ITM, OTM Options &#8211; Amibroker AFL Code"},"content":{"rendered":"\n<p>Here is a simple button trading module that can be used to send option trading orders with buttons. Users can configure the button to send ATM, ITM, OTM Options, and with one touch one will be able to send small\/large options orders in a split second. Entry Order and Exit order buttons are provided to users.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1860\" height=\"866\" src=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2023\/02\/image.png\" alt=\"\" class=\"wp-image-516\"\/><\/figure>\n\n\n\n<p><strong>Supported Brokers:<\/strong>&nbsp;All Algomojo Supported Brokers<br><strong>AlgoPlatform&nbsp;<\/strong>: Algomojo<br><strong>Supported Amibroker Version<\/strong>&nbsp;: 6.22 or above<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Buttons<\/strong><\/td><td><strong>Type<\/strong><\/td><\/tr><tr><td>Long CE Entry<\/td><td>Entry Order<\/td><\/tr><tr><td>Long CE Exit<\/td><td>Exit Order<\/td><\/tr><tr><td>Long PE Entry<\/td><td>Entry Order<\/td><\/tr><tr><td>Long PE Exit<\/td><td>Exit Order<\/td><\/tr><tr><td>Short CE Entry<\/td><td>Entry Order<\/td><\/tr><tr><td>Short CE Exit<\/td><td>Exit Order<\/td><\/tr><tr><td>Short PE Entry<\/td><td>Entry Order<\/td><\/tr><tr><td>Short PE Exit<\/td><td>Exit Order<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Button Offset Parameter Controls in Amibroker<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td>Option Type<\/td><td>Offset<\/td><\/tr><tr><td>ATM<\/td><td>0<\/td><\/tr><tr><td>OTM<\/td><td>Positive Value<\/td><\/tr><tr><td>ITM<\/td><td>Negative Value<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><strong>Execution Logic for Placing Options Order<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/i1.wp.com\/algomojo.com\/blog\/wp-content\/uploads\/2021\/01\/image-1.png?ssl=1\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Execution Logic for Exiting Options Order<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/i1.wp.com\/algomojo.com\/blog\/wp-content\/uploads\/2021\/01\/image-2.png?ssl=1\" alt=\"\"\/><\/figure>\n\n\n\n<p><strong>Setting up the Options Button Trading AFL File<\/strong><\/p>\n\n\n\n<p>1)Save the AFL file under the name <strong>Algomojo Options Button Trading.afl<\/strong> under Amibroker\/formulas\/Algomojo folder. Create Algomojo Folder if it doesn&#8217;t exist.<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-clike\">\/*\nCreated By : Algomojo\nCreated on : 09 Feb 2023\nWebsite : www.algomojo.com\n*\/\n\n_SECTION_BEGIN(&quot;Algomojo - Options Button Trading Module&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();\n\n\n\n_SECTION_BEGIN(&quot;Amibroker Version Check&quot;);\n\nRequestTimedRefresh(1,False);\n\nVersion(6.22);\n\n\/\/Static Variables will be saved in Amibroker every 30 seconds once\nSetOption(&quot;StaticVarAutoSave&quot;,60);\n\n\/\/ Send orders even if Amibroker is minimized or Chart is not active\nRequestTimedRefresh(1, False); \nEnableTextOutput(False);\n\n_SECTION_END();\n\n\n_SECTION_BEGIN(&quot;Algomojo Options Controls&quot;);\n\n\n\/\/ Send orders even if Amibroker is minimized or Chart is not active\nRequestTimedRefresh(1, False); \n\nbroker =Paramlist(&quot;Broker&quot;,&quot;an|ab|fp|fs|fy|gc|pt|sm|tc|up|zb|ze&quot;,0);;\nver = &quot;v1&quot;;\n\n\/\/input controls\napikey = Paramstr(&quot;ApiKey&quot;,&quot;xxxxxxxxxxxxxxxxx&quot;);\napisecret = Paramstr(&quot;ApiSecret&quot;,&quot;xxxxxxxxxxxxxxxxx&quot;);\nstrategy = ParamStr(&quot;Strategy Name&quot;,&quot;Options Strategy&quot;);\nproduct = ParamList(&quot;product&quot;,&quot;NRML|MIS&quot;,0);\npricetype = ParamList(&quot;pricetype&quot;,&quot;MARKET&quot;,0); \nprice = 0;\ntriggerprice = 0;\nexchange = ParamList(&quot;Exchange&quot;,&quot;NFO&quot;,0); \nsplitorder = ParamList(&quot;Split Order&quot;,&quot;NO|YES&quot;);\nsplit_quantity = Param(&quot;Split Quantity&quot;,1,1,5000,1);\n\n\nEnableAlgo = ParamList(&quot;Algo Mode&quot;,&quot;Disable|Enable&quot;,0); \/\/ Algo Mode\nVoiceAlert = ParamList(&quot;Voice Alerts&quot;,&quot;Disable|Enable&quot;,0); \/\/ Algo Mode\n\n\n\nclear = ParamTrigger(&quot;Clear Static Variables&quot;,&quot;Clear&quot;);\nstatic_var_options = &quot;staticvar_options&quot;+Name()+Interval(2)+GetChartID() + strategy;\n\nif(clear)\n{\n\nStaticVarRemove(static_var_options+&quot;*&quot;);\n_TRACE(&quot;Static Variables Cleared&quot;);\n}\n\nfunction removeinteralmemory(opt_type,legno)\n{\nStaticVarRemove(static_var_options+&quot;Options_Trans&quot;+opt_type+legno);\nStaticVarRemove(static_var_options+&quot;Options_OrderNo&quot;+opt_type+legno);\nStaticVarRemove(static_var_options+&quot;Options_Symbol&quot;+opt_type+legno);\nStaticVarRemove(static_var_options+&quot;Options_OrderStatus&quot;+opt_type+legno);\nStaticVarRemove(static_var_options+&quot;Options_NetQty&quot;+opt_type+legno);\n\n}\n\n\/\/Internal Memory\nopt_type=&quot;CE&quot;;\nlegno = 1;\nprintf(&quot;\\n---------------Leg 1 Long CE Orders Internal Memory----------&quot;);\nprintf(&quot;\\nOrderType : &quot;+StaticVarGetText(static_var_options+&quot;Options_Trans&quot;+opt_type+legno));\nprintf(&quot;\\nOrderNo : &quot;+StaticVarGetText(static_var_options+&quot;Options_OrderNo&quot;+opt_type+legno));\nprintf(&quot;\\nSymbol : &quot;+StaticVarGetText(static_var_options+&quot;Options_Symbol&quot;+opt_type+legno));\nprintf(&quot;\\nStatus : &quot;+StaticVarGetText(static_var_options+&quot;Options_OrderStatus&quot;+opt_type+legno));\nprintf(&quot;\\nNetQty : &quot;+StaticVarGetText(static_var_options+&quot;Options_NetQty&quot;+opt_type+legno));\n\n\nopt_type=&quot;PE&quot;;\nlegno = 2;\nprintf(&quot;\\n---------------Leg 2 Long PE Orders Internal Memory----------&quot;);\nprintf(&quot;\\nOrderType : &quot;+StaticVarGetText(static_var_options+&quot;Options_Trans&quot;+opt_type+legno));\nprintf(&quot;\\nOrderNo : &quot;+StaticVarGetText(static_var_options+&quot;Options_OrderNo&quot;+opt_type+legno));\nprintf(&quot;\\nSymbol : &quot;+StaticVarGetText(static_var_options+&quot;Options_Symbol&quot;+opt_type+legno));\nprintf(&quot;\\nStatus : &quot;+StaticVarGetText(static_var_options+&quot;Options_OrderStatus&quot;+opt_type+legno));\nprintf(&quot;\\nNetQty : &quot;+StaticVarGetText(static_var_options+&quot;Options_NetQty&quot;+opt_type+legno));\n\n\n\nopt_type=&quot;CE&quot;;\nlegno = 3;\nprintf(&quot;\\n---------------Leg 3 Short CE Orders Internal Memory----------&quot;);\nprintf(&quot;\\nOrderType : &quot;+StaticVarGetText(static_var_options+&quot;Options_Trans&quot;+opt_type+legno));\nprintf(&quot;\\nOrderNo : &quot;+StaticVarGetText(static_var_options+&quot;Options_OrderNo&quot;+opt_type+legno));\nprintf(&quot;\\nSymbol : &quot;+StaticVarGetText(static_var_options+&quot;Options_Symbol&quot;+opt_type+legno));\nprintf(&quot;\\nStatus : &quot;+StaticVarGetText(static_var_options+&quot;Options_OrderStatus&quot;+opt_type+legno));\nprintf(&quot;\\nNetQty : &quot;+StaticVarGetText(static_var_options+&quot;Options_NetQty&quot;+opt_type+legno));\n\n\nopt_type=&quot;PE&quot;;\nlegno = 4;\nprintf(&quot;\\n---------------Leg 4 Short PE Orders Internal Memory----------&quot;);\nprintf(&quot;\\nOrderType : &quot;+StaticVarGetText(static_var_options+&quot;Options_Trans&quot;+opt_type+legno));\nprintf(&quot;\\nOrderNo : &quot;+StaticVarGetText(static_var_options+&quot;Options_OrderNo&quot;+opt_type+legno));\nprintf(&quot;\\nSymbol : &quot;+StaticVarGetText(static_var_options+&quot;Options_Symbol&quot;+opt_type+legno));\nprintf(&quot;\\nStatus : &quot;+StaticVarGetText(static_var_options+&quot;Options_OrderStatus&quot;+opt_type+legno));\nprintf(&quot;\\nNetQty : &quot;+StaticVarGetText(static_var_options+&quot;Options_NetQty&quot;+opt_type+legno));\n\n\n\n\n\nfunction getorderno(resp)\n{\n\norderno = &quot;&quot;;\n\n\norderno = StrExtract(resp,2,&#039;{&#039;);\norderno = StrExtract(orderno,2,&#039;,&#039;);\norderno = StrExtract(orderno,1,&#039;:&#039;);\norderno = StrExtract(orderno,1,&#039;&quot;&#039;);\n\n_TRACE(&quot;Order No is : &quot;+orderno);\n\nreturn orderno;\n\n}\n\n\nfunction GetSymbol(resp)\n{\n\ntradingsymbol = &quot;&quot;;\n\n\ntradingsymbol = StrExtract(resp,2,&#039;{&#039;);\ntradingsymbol = StrExtract(tradingsymbol,1,&#039;,&#039;);\ntradingsymbol = StrExtract(tradingsymbol,1,&#039;:&#039;);\ntradingsymbol = StrExtract(tradingsymbol,1,&#039;&quot;&#039;);\n\n_TRACE(&quot;Trading Symbol is : &quot;+tradingsymbol);\n\nreturn tradingsymbol;\n\n}\n\nfunction GetOrderStatus(order_id)\n{\n\n\/\/Creating the Trading Bridge\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\n\napi_data = &quot;{ \\&quot;broker\\&quot;: \\&quot;&quot;+broker+&quot;\\&quot;,\n            \\&quot;order_id\\&quot;:\\&quot;&quot;+order_id+&quot;\\&quot; }&quot;;\n_TRACE(&quot;OpenPositions API Request&quot;+api_data);\n\n\/\/Sending The Broker Request for NetOpenPositions\nresp=algomojo.AMDispatcher(apikey,apisecret,&quot;OrderStatus&quot;,api_data,&quot;am&quot;,ver);\n\norderstatus = StrExtract(resp,2,&#039;{&#039;);\norderstatus = StrExtract(orderstatus,1,&#039;:&#039;);\norderstatus = StrExtract(orderstatus,1,&#039;&quot;&#039;);\n\n_TRACE(&quot;Order Status : &quot;+orderstatus);\nfor(i=0; i&lt;5; i++)  ThreadSleep(100);\nreturn orderstatus;\n\n}\n\n\nfunction OpenPositions(TSymbol)\n{\n\/\/Creating the Trading Bridge\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\n\napi_data = &quot;{ \\&quot;broker\\&quot;: \\&quot;&quot;+broker+&quot;\\&quot;,\n            \\&quot;symbol\\&quot;:\\&quot;&quot;+TSymbol+&quot;\\&quot;,\n            \\&quot;product\\&quot;:\\&quot;&quot;+product+&quot;\\&quot;\n            }&quot;;\n_TRACE(&quot;OpenPositions API Request&quot;+api_data);\n\n\/\/Sending The Broker Request for NetOpenPositions\nresp=algomojo.AMDispatcher(apikey,apisecret,&quot;OpenPositions&quot;,api_data,&quot;am&quot;,ver);\n_TRACE(&quot;OpenPositions API Response : &quot;+resp);\nfor(i=0; i&lt;5; i++)  ThreadSleep(100);\nreturn resp;\n\n}\n\nfunction NetOpenPositions(TSymbol)\n{\nflag = 0;\n\nresp = OpenPositions(TSymbol);\n\nposNetqty =0;\n\nsym = StrExtract( resp, 2,&#039;{&#039; );\n\/\/_TRACE(&quot;Symbol Extract&quot;+sym);\n\nif( sym != &quot;&quot;)\n{\n\nfor( jitem = 0; ( posdetails = StrExtract( sym, jitem,&#039;,&#039; )) != &quot;&quot;; jitem++ )\n{\n\n  if(Strfind(posdetails,&quot;netqty&quot;))\n  {\n   \n   flag = 1; \/\/turn on the flag\n   posdetails = StrExtract(posdetails,1,&#039;:&#039;);\n   posNetqty = StrToNum(StrTrim(posdetails,&quot;\\&quot;&quot;));\n   \n  }\n  \n\n} \/\/end of for loop\n\n\n} \/\/end of if loop\n\n\n\nreturn posNetqty;\n\n}\n\n\n\n\/\/placeoptionorder(1,spot,expiry,iInterval,btn12_qty,&quot;B&quot;,&quot;CE&quot;,offset_btn12); \n\nfunction PlaceOptionsOrder(legno,spot_symbol,expiry_date,strike_int,quantity,action, opt_type, offset)\n{\n\nif(opt_type==&quot;PE&quot;)\n{\noffset = -offset;  \/\/changing the polarity\n}\n\n\n\/\/create the Trading Bridge\nalgomojo = CreateObject(&quot;AMAMIBRIDGE.Main&quot;);  \/\/calling the bridge dll\n\n\/\/Prepart the API data\n\napi_data =  &quot;{\n        \\&quot;broker\\&quot;:\\&quot;&quot;+broker+&quot;\\&quot;,\n        \\&quot;strategy\\&quot;:\\&quot;&quot;+strategy+&quot;\\&quot;,\n        \\&quot;spot_symbol\\&quot;:\\&quot;&quot;+spot_symbol+&quot;\\&quot;,\n        \\&quot;expiry_date\\&quot;:\\&quot;&quot;+expiry_date+&quot;\\&quot;,\n        \\&quot;action\\&quot;:\\&quot;&quot;+action+&quot;\\&quot;,\n        \\&quot;product\\&quot;:\\&quot;&quot;+product+&quot;\\&quot;,\n        \\&quot;pricetype\\&quot;:\\&quot;&quot;+pricetype+&quot;\\&quot;,\n        \\&quot;quantity\\&quot;:\\&quot;&quot;+quantity+&quot;\\&quot;,\n        \\&quot;price\\&quot;:\\&quot;0\\&quot;,\n        \\&quot;triggerprice\\&quot;:\\&quot;0\\&quot;,\n        \\&quot;option_type\\&quot;:\\&quot;&quot;+opt_type+&quot;\\&quot;,\n        \\&quot;strike_int\\&quot;:\\&quot;&quot;+strike_int+&quot;\\&quot;,\n        \\&quot;offset\\&quot;:\\&quot;&quot;+offset+&quot;\\&quot;,\n        \\&quot;splitorder\\&quot;:\\&quot;&quot;+splitorder+&quot;\\&quot;,\n        \\&quot;split_quantity\\&quot;:\\&quot;&quot;+split_quantity+&quot;\\&quot;\n        }&quot;;\n\n_TRACE(&quot;Broker API Request&quot;+api_data);\n\nresp = algomojo.AMDispatcher(apikey,apisecret,&quot;PlaceFOOptionsOrder&quot;,api_data,&quot;am&quot;,ver);\n\n_TRACE(&quot;Broker API Respone&quot;+resp);\n\n\/\/Store the Transaction Type, OrderNo,Trading Symbol,OrderStatus,Netqty\n\nTrans = action;\nStaticVarSetText(static_var_options+&quot;Options_Trans&quot;+opt_type+legno,Trans,True); \nOrderNo = GetOrderNo(resp);\nStaticVarSetText(static_var_options+&quot;Options_OrderNo&quot;+opt_type+legno,OrderNo,True);\nSymbol = GetSymbol(resp);\nStaticVarSetText(static_var_options+&quot;Options_Symbol&quot;+opt_type+legno,Symbol,True);\nOrderstatus = GetOrderStatus(OrderNo);  \/\/Get the Orderstatus from OrderHistory\nStaticVarSetText(static_var_options+&quot;Options_OrderStatus&quot;+opt_type+legno,Orderstatus,True);\nNetQty = NumToStr(NetOpenPositions(Symbol),1);  \/\/Get the Netqty from the Positions Book\nStaticVarSetText(static_var_options+&quot;Options_NetQty&quot;+opt_type+legno,NetQty,True);\n\nif(VoiceAlert == &quot;Enable&quot;)\n\tSay( &quot;Order Placed&quot; );\n\nreturn resp;\n}\n\n\nfunction SquareOffPosition(legno,opt_type)\n{\nresp =&quot;&quot;;\nTType = &quot;&quot;;\nExitSymbol = StaticVarGetText(static_var_options+&quot;Options_Symbol&quot;+opt_type+legno);\n_TRACE(&quot;Exit Symbol :&quot;+ExitSymbol);\n\nif(ExitSymbol!=&quot;&quot;)\n{\n\nNetqty = NetOpenPositions(ExitSymbol);\n\nif(StaticVarGetText(static_var_options+&quot;Options_Trans&quot;+opt_type+legno)==&quot;BUY&quot;)\n{\nTType = &quot;SELL&quot;;\n}\n\nif(StaticVarGetText(static_var_options+&quot;Options_Trans&quot;+opt_type+legno)==&quot;SELL&quot;)\n{\nTType = &quot;BUY&quot;;\n}\n\n\n\n\/\/Creating the Trading Bridge\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\n\/\/Preparing the API data\n\namo = &quot;NO&quot;;\ntrigger_price = &quot;0&quot;;\n\napi_data = &quot;{ \\&quot;broker\\&quot;: \\&quot;&quot;+broker+&quot;\\&quot;,\n            \\&quot;strategy\\&quot;:\\&quot;&quot;+strategy+&quot;\\&quot;,\n            \\&quot;exchange\\&quot;:\\&quot;&quot;+exchange+&quot;\\&quot;,\n            \\&quot;symbol\\&quot;:\\&quot;&quot;+ExitSymbol+&quot;\\&quot;,\n            \\&quot;action\\&quot;:\\&quot;&quot;+TType+&quot;\\&quot;,\n            \\&quot;product\\&quot;:\\&quot;&quot;+product+&quot;\\&quot;,\n            \\&quot;pricetype\\&quot;:\\&quot;&quot;+pricetype+&quot;\\&quot;,\n            \\&quot;quantity\\&quot;:\\&quot;&quot;+abs(Netqty)+&quot;\\&quot;,\n            \\&quot;price\\&quot;:\\&quot;&quot;+price+&quot;\\&quot;,      \n            \\&quot;disclosed_quantity\\&quot;:\\&quot;&quot;+&quot;0&quot;+&quot;\\&quot;,\n            \\&quot;trigger_price\\&quot;:\\&quot;&quot;+trigger_price+&quot;\\&quot;,\n            \\&quot;amo\\&quot;:\\&quot;&quot;+amo+&quot;\\&quot;,\n            \\&quot;splitorder\\&quot;:\\&quot;&quot;+splitorder+&quot;\\&quot;,\n            \\&quot;split_quantity\\&quot;:\\&quot;&quot;+split_quantity+&quot;\\&quot;  }&quot;; \n            \n\n\n_TRACE(&quot;Squareoff API Request&quot;+api_data);\n\/\/Testing\n\/\/if(Netqty==0 AND ExitSymbol!=&quot;&quot;)\n\/\/Live Trading\nif(Netqty!=0 AND ExitSymbol!=&quot;&quot;)\n{\nresp=algomojo.AMDispatcher(apikey, apisecret,&quot;PlaceOrder&quot;,api_data,&quot;am&quot;,ver);\n\t\n}\n\n\n}\nif(resp!=&quot;&quot;)\n{\n_TRACE(&quot;Squareoff API Response : &quot;+resp);\n}\nelse\n{\n_TRACE(&quot;Squareoff Order Not Triggered&quot;);\n}\nremoveinteralmemory(opt_type,legno);\n\nif(VoiceAlert == &quot;Enable&quot;)\n\tSay( &quot;Order Placed&quot; );\n\nreturn resp;\n\n}\n\n\n_SECTION_END();\n\n\n\n\n_SECTION_BEGIN(&quot;Button Controls&quot;);\n\n\n\/\/Default strike is ATM, Positve Offset = OTM, Negative Offset = ITM -&gt; AFL Side\nspot = Paramlist(&quot;spot_symbol&quot;,&quot;NIFTY|BANKNIFTY|FINNIFTY&quot;,0);\nexpiry = ParamStr(&quot;Expiry Date&quot;,&quot;23FEB23&quot;);\niInterval= Param(&quot;Strike Interval&quot;,50,1,5000,1);\nlotsize= Param(&quot;Lot Size&quot;,50,1,5000,1);\nquantity = Param(&quot;Quantity in Lot Size&quot;,1,1,100000,1)*lotsize;\n\n\/\/Long CE Entry and Exit Button Controls\nbtn12_qty = Param(&quot;Long CE Qty&quot;,50,0,10000,1);\noffset_btn12 = Param(&quot;Long CE Offset&quot;,0,-20,20,1);  \noptiontype12 = WriteIf(offset_btn12 == 0, &quot;ATM&quot;, WriteIf(offset_btn12&lt;0,&quot;ITM&quot;,&quot;OTM&quot;));\nbtn1_name = optiontype12+&quot;-&quot;+abs(offset_btn12)+&quot; Long CE Entry &quot;+btn12_qty+&quot; shares&quot;;\nbtn2_name = optiontype12+&quot;-&quot;+abs(offset_btn12)+&quot; Long CE Exit &quot;+btn12_qty+&quot; shares&quot;;\n\nbtn34_qty = Param(&quot;Long PE Qty&quot;,50,0,10000,1);\noffset_btn34 = Param(&quot;Long PE Offset&quot;,0,-20,20,1);  \noptiontype34 = WriteIf(offset_btn34 == 0, &quot;ATM&quot;, WriteIf(offset_btn34&lt;0,&quot;ITM&quot;,&quot;OTM&quot;));\nbtn3_name = optiontype34+&quot;-&quot;+abs(offset_btn34)+&quot; Long PE Entry &quot;+btn34_qty+&quot; shares&quot;;\nbtn4_name = optiontype34+&quot;-&quot;+abs(offset_btn34)+&quot; Long PE Exit &quot;+btn34_qty+&quot; shares&quot;;\n\nbtn56_qty = Param(&quot;Short CE Qty&quot;,50,0,10000,1);\noffset_btn56 = Param(&quot;Short CE Offset&quot;,0,-20,20,1);  \noptiontype56 = WriteIf(offset_btn56 == 0, &quot;ATM&quot;, WriteIf(offset_btn56&lt;0,&quot;ITM&quot;,&quot;OTM&quot;));\n\nbtn5_name = optiontype56+&quot;-&quot;+abs(offset_btn56)+&quot; Short CE Entry &quot;+btn56_qty+&quot; shares&quot;;\nbtn6_name = optiontype56+&quot;-&quot;+abs(offset_btn56)+&quot; Long CE Exit &quot;+btn56_qty+&quot; shares&quot;;\n\nbtn78_qty = Param(&quot;Short PE Qty&quot;,50,0,10000,1);\noffset_btn78 = Param(&quot;Short PE Offset&quot;,0,-20,20,1); \noptiontype78 = WriteIf(offset_btn78 == 0, &quot;ATM&quot;, WriteIf(offset_btn78&lt;0,&quot;ITM&quot;,&quot;OTM&quot;)); \nbtn7_name = optiontype78+&quot;-&quot;+abs(offset_btn78)+&quot; Shortong PE Entry &quot;+btn78_qty+&quot; shares&quot;;\nbtn8_name = optiontype78+&quot;-&quot;+abs(offset_btn78)+&quot; Short PE Exit &quot;+btn78_qty+&quot; shares&quot;;\n\n_SECTION_END();\n\n\n\n_SECTION_BEGIN(&quot;Algo Dashboard&quot;);\n\n\nstatic_name_ = Name()+GetChartID()+interval(2)+strategy;\nstatic_name_algo = Name()+GetChartID()+interval(2)+strategy+&quot;algostatus&quot;;\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_SECTION_END();\n\n\n\n_SECTION_BEGIN(&quot;Algomojo Button GUI Creation and Event Handling&quot;);\n\nresp = &quot;&quot;;\n\n\n\n\n\nfunction CreateGUI( 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 = 1;\n\treturn result;\n\t\n}\n\n\nfunction HandleEvents()\n{\n\n\tresult = 0;\n\tid = GuiGetEvent( 0, 0 );\/\/ receiving button id\n\tevent = GuiGetEvent( 0, 1 );\/\/ receiving notifyflag\n\tclickevent = event == 1;\n\t\n\t\n\t\n\t\n\t\n\tLongCEEntry = id == 1 &amp;&amp; clickevent;\n\tLongCEExit = id == 2 &amp;&amp; clickevent;\n\tLongPEEntry = id == 3 &amp;&amp; clickevent;\n\tLongPEExit = id == 4 &amp;&amp; clickevent;\n\tShortCEEntry = id == 5 &amp;&amp; clickevent;\n\tShortCEExit = id == 6 &amp;&amp; clickevent;\n\tShortPEEntry = id == 7 &amp;&amp; clickevent;\n\tShortPEExit = id == 8 &amp;&amp; clickevent;\n\t\n\t\n\t\n\tresult = 0;\n\t\n\t\n\tif(EnableAlgo == &quot;Enable&quot;)\n\t{\n\t\n\tif( LongCEEntry AND StaticVarGet(Name()+GetChartID()+&quot;LongCEEntry&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;Call Long Entry&quot;);\n\t\torderresponse = PlaceOptionsOrder(1,spot,expiry,iInterval,btn12_qty,&quot;BUY&quot;,&quot;CE&quot;,offset_btn12); \n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;LongCEEntry&quot;,1); \n\t}\n\telse\n\t{\n\t\t\n\t\tStaticVarSet(Name()+GetChartID()+&quot;LongCEEntry&quot;,0);\n\t}\n\t\n\tif( LongCEExit AND StaticVarGet(Name()+GetChartID()+&quot;LongCEExit&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;Call Long Exit&quot;);\n\t\tsqoffstatus = SquareOffPosition(1,&quot;CE&quot;);\n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;LongCEExit&quot;,1); \n\t}\n\telse\n\t{\n\t\tStaticVarSet(Name()+GetChartID()+&quot;LongCEExit&quot;,0);\n\t}\n\t\n\tif( LongPEEntry AND StaticVarGet(Name()+GetChartID()+&quot;LongPEEntry&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;Put Long Entry&quot;);\n\t\torderresponse = PlaceOptionsOrder(2,spot,expiry,iInterval,btn34_qty,&quot;BUY&quot;,&quot;PE&quot;,offset_btn34); \n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;LongPEEntry&quot;,1); \n\t}\n\telse\n\t{\n\t\tStaticVarSet(Name()+GetChartID()+&quot;LongPEEntry&quot;,0);\n\t}\n\t\n\tif( LongPEExit AND StaticVarGet(Name()+GetChartID()+&quot;LongPEExit&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;Put Long Exit&quot;);\n\t\tsqoffstatus = SquareOffPosition(2,&quot;PE&quot;);\n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;LongPEExit&quot;,1); \n\t}\n\telse\n\t{\n\t\tStaticVarSet(Name()+GetChartID()+&quot;LongPEExit&quot;,0);\n\t}\n\t\n\tif( ShortCEEntry AND StaticVarGet(Name()+GetChartID()+&quot;ShortCEEntry&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;Call Short Entry&quot;);\n\t\torderresponse = PlaceOptionsOrder(3,spot,expiry,iInterval,btn56_qty,&quot;SELL&quot;,&quot;CE&quot;,offset_btn56); \n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortCEEntry&quot;,1); \n\t}\n\telse\n\t{\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortCEEntry&quot;,0);\n\t}\n\t\n\tif( ShortCEExit AND StaticVarGet(Name()+GetChartID()+&quot;ShortCEExit&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;Call Short Exit&quot;);\n\t\tsqoffstatus = SquareOffPosition(3,&quot;CE&quot;);\n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortCEExit&quot;,1); \n\t}\n\telse\n\t{\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortCEExit&quot;,0);\n\t}\n\t\n\tif( ShortPEEntry AND StaticVarGet(Name()+GetChartID()+&quot;ShortPEEntry&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;Put Short Entry&quot;);\n\t\t\n\t\torderresponse = PlaceOptionsOrder(4,spot,expiry,iInterval,btn78_qty,&quot;SELL&quot;,&quot;PE&quot;,offset_btn78); \n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortPEEntry&quot;,1); \n\t}\n\telse\n\t{\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortPEEntry&quot;,0);\n\t}\n\t\n\tif( ShortPEExit AND StaticVarGet(Name()+GetChartID()+&quot;ShortPEExit&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;Put Short Exit&quot;);\n\t\tsqoffstatus = SquareOffPosition(4,&quot;PE&quot;);\n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortPEExit&quot;,1); \n\t}\n\telse\n\t{\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortPEExit&quot;,0);\n\t}\n\t}\n\t\n\t\n\treturn result;  \/\/result = 1 - order is successfully placed, 0 - Algotrading is in disabled state or orders are not went through\n\t\n\n\n}\n\n\t\n\tLongCEEntry = CreateGUI( btn1_name, 20, 100, 300, 30 );\n\tLongCEExit = CreateGUI( btn2_name, 300, 100, 300, 30 );\n\t\n\tLongPEEntry = CreateGUI( btn3_name, 20, 150, 300, 30 );\n\tLongPEExit = CreateGUI( btn4_name, 300, 150, 300, 30 );\n\t\n\tShortCEEntry = CreateGUI( btn5_name, 20, 200, 300, 30 );\n\tShortCEExit = CreateGUI( btn6_name, 300, 200, 300, 30 );\n\t\n\tShortPEEntry = CreateGUI( btn7_name, 20, 250, 300, 30 );\n\tShortPEExit = CreateGUI( btn8_name, 300, 250, 300, 30 );\n\t\n\thandleevents();\n\t\n\t\n\t\n\tGuiSetColors( 1, 2, 2, colorGreen, colorBlack, colorGreen, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); \n\tGuiSetColors( 3, 4, 2, colorred, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); \n\n\n\tGuiSetColors( 5, 6, 2, colorBlue, colorBlack, colorBlue, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); \n\tGuiSetColors( 7, 8, 2, colorLavender, colorBlack, colorLavender, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); \n\n_SECTION_END();\n\n_SECTION_BEGIN(&quot;Price&quot;);\n\nSetChartOptions(0, chartShowArrows | chartShowDates); \/\/x-Axis will be plottted\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 ) ) ));\n\/\/plot the candles\nPlot(Close,&quot;Close&quot;,colorDefault,GetPriceStyle() | styleNoTitle);\n\n_SECTION_END();\n\n<\/code><\/pre>\n\n\n\n<p>Now Drag and Drop the Algomojo Options Button Trading to the new blank charts. Configure the API Key, API Secret Key, Broker Name, Spot, Expiry Date, Strike Interval, and the Button Controls, Offset and Quantity.<\/p>\n\n\n\n<p><strong>What are the Internal Memory Tracks?<\/strong><\/p>\n\n\n\n<p>Internal Memory tracks the OrderType, OrderNo, Traded Symbol, Order Status, and Net Quantity. Any fresh entry orders will create internal memory and exiting the positions will automatically erase the internal memory. The Internal Memory is persistently saved every 30 seconds means even if the Amibroker is closed and reopened the internal memory is restored with the help of persistent static variables.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"584\" height=\"597\" src=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2023\/02\/image-1.png\" alt=\"\" class=\"wp-image-517\"\/><\/figure>\n\n\n\n<p><strong>Managing the Internal Memory<\/strong><\/p>\n\n\n\n<p>Press<strong>&nbsp;Clear Static Variables<\/strong>&nbsp;to Reset the Internal Memory if in case you want to start fresh and there are no open positions for the current button trading module.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"396\" height=\"495\" src=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2023\/02\/image-2.png\" alt=\"\" class=\"wp-image-518\"\/><\/figure>\n\n\n\n<p>Bingo! Now with the press of the button you will be able to place options orders automatically.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is a simple button trading module that can be used to send option trading orders with buttons. Users can configure the button to send ATM, ITM, OTM Options, and with one touch one will be able to send small\/large options orders in a split second. Entry Order and Exit order buttons are provided to users.<\/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":[13,7,42,70,122],"_links":{"self":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/233"}],"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=233"}],"version-history":[{"count":7,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/233\/revisions"}],"predecessor-version":[{"id":634,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/233\/revisions\/634"}],"wp:attachment":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/media?parent=233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/categories?post=233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/tags?post=233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}