{"id":381,"date":"2022-04-19T17:47:41","date_gmt":"2022-04-19T17:47:41","guid":{"rendered":"https:\/\/algomojo.com\/blog\/?p=381"},"modified":"2023-09-13T06:09:53","modified_gmt":"2023-09-13T06:09:53","slug":"smart-button-trading-module-trade-automation-amibroker-afl-code","status":"publish","type":"post","link":"https:\/\/algomojo.com\/blog\/smart-button-trading-module-trade-automation-amibroker-afl-code\/","title":{"rendered":"Smart Button Trading Module &#8211; Trade Automation &#8211; Amibroker AFL Code"},"content":{"rendered":"\n<p>This Algomojo Button trading module provides 4 modes of the button to place orders with a single click directly from Amibroker using simple smart order controls.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1917\" height=\"1010\" src=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2022\/04\/image.png\" alt=\"\" class=\"wp-image-382\"\/><\/figure>\n\n\n\n<p><strong>Four Button Types<\/strong><\/p>\n\n\n\n<p>BE \u2013 Buy Entry Button \u2013 Enter longs for the selected quantity<br>BX \u2013 Buy Exit Button \u2013 Exit All Long Open Positions only if the Position is running long else do nothing<br>SE \u2013 Short Entry Button \u2013 Enter Shorts for the selected quantity<br>SX \u2013 Short Exit Button \u2013 Exit All Short Open Positions only if the Position is running shorts else do nothing<\/p>\n\n\n\n<p><strong>Supported Amibroker Version<\/strong><\/p>\n\n\n\n<p>Amibroker v6.22 or higher<\/p>\n\n\n\n<p><strong>Supported Broker<\/strong>&nbsp;\u2013 AngelOne<\/p>\n\n\n\n<p><strong>Automation Platform<\/strong>&nbsp;\u2013 Algomojo Trading Terminal<\/p>\n\n\n\n<p><strong>Smart Button Trading Module \u2013 Amibroker AFL Code<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-clike\">\/\/ Angel Broking - Smart Button Trading Module\n\/\/ Developer: Algomojo\n\/\/ Date: 19-Apr-2022\n\/\/ Website: algomojo.com\n\n\/\/BE - Buy Entry Button\n\/\/BX - Buy Exit Button - Exit All Long Open Positions only if the Position is running longs\n\/\/SE - Short Entry Button\n\/\/SX - Short Exit Button - Exit All Short Open Positions only if the Position is running shorts\n\n_SECTION_BEGIN(&quot;Angel Broking - Broker Controls&quot;);\n\nVersion(6.22); \/\/Gui Function Compatibility check\n\nRequestTimedRefresh(1, False); \n\nbroker =ParamStr(&quot;Broker&quot;,&quot;an&quot;); \/\/Broker Short Code - an- angel broking\nver = ParamStr(&quot;API Version &quot;,&quot;1.0&quot;);\nclnt_id = ParamStr(&quot;Client ID&quot;,&quot;xxxx&quot;); \/\/Enter your trading client ID\nuser_apikey = ParamStr(&quot;apikey&quot;,&quot;xxxxxxxxxxxxxxxxxxxxx&quot;); \/\/Enter your API key here\napi_secretkey = ParamStr(&quot;secretkey&quot;,&quot;xxxxxxxxxxxxxxxxxxxxx&quot;); \/\/Enter your API secret key here\n\n_SECTION_END();\n\n_SECTION_BEGIN(&quot;Angel Broking - Order Controls&quot;);\n\nstgy_name = ParamStr(&quot;Strategy Name&quot;, &quot;Test Strategy&quot;);\nvariety = ParamList(&quot;Variety&quot;,&quot;NORMAL|STOPLOSS|AMO&quot;,0); \ntradingsymbol = ParamStr(&quot;Trading Symbol&quot;,&quot;RELIANCE-EQ&quot;);\n\n\nexchange = ParamList(&quot;Exchange&quot;,&quot;NSE|NFO|BSE|MCX&quot;,0); \nordertype = ParamList(&quot;Order Type&quot;,&quot;MARKET|LIMIT|STOPLOSS_LIMIT|STOPLOSS_MARKET&quot;,0);\nproducttype = ParamList(&quot;Product Type&quot;,&quot;DELIVERY|CARRYFORWARD|MARGIN|INTRADAY|AMO_MARGIN|AMO_DELIVERY|AMO_CARRYFORWARD&quot;,3);\n\nprice = &quot;0&quot;; \ntriggerprice = &quot;0&quot;; \nquantity = Param(&quot;quanity&quot;,1,0,10000);\n\nsquareoff = &quot;0&quot;; \nstoploss = &quot;0&quot;; \ntrailingStopLoss = &quot;0&quot;; \ndisclosedquantity = &quot;0&quot;; \nduration = &quot;DAY&quot;;\n\n\n\n\nEnableAlgo = ParamList(&quot;AlgoStatus&quot;,&quot;Disable|Enable|LongOnly|ShortOnly&quot;,0);\nresp = &quot;&quot;;\n\n\nstatic_name_ = Name()+GetChartID()+interval(2)+stgy_name;\nstatic_name_algo = Name()+GetChartID()+interval(2)+stgy_name+&quot;algostatus&quot;;\n\n\nfunction GetPositionsBook()\n{\n\/\/Creating the Trading Bridge\nalgomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\n\napi_data = &quot;{ }&quot;;\n_TRACE(&quot;PositionBook API Request&quot;+api_data);\n\n\/\/Sending The Broker Request for NetOpenPositions\nresp=algomojo.AMDispatcher(user_apikey,api_secretkey,&quot;Positions&quot;,api_data,broker,ver);\n_TRACE(&quot;PositionBook API Response : &quot;+resp);\n\nreturn resp;\n\n}\n\nfunction NetOpenPositions()\n{\nflag = 0;\n\nresp = GetPositionsBook();\n\nposNetqty =0;\n\nfor( item = 1; ( sym = StrExtract( resp, item,&#039;{&#039; )) != &quot;&quot;; item++ )\n{\n\n\n\nif(Strfind(sym,tradingsymbol) AND StrFind(sym,productType)) \/\/Matches the symbol and \/\/Matches the Order Type\n{\n\nflag = 1; \/\/turn on the flag\n\nfor( jitem = 0; ( posdetails = StrExtract( sym, jitem,&#039;,&#039; )) != &quot;&quot;; jitem++ )\n{\n\n if(Strfind(posdetails,&quot;tradingsymbol&quot;))\n  {\n   posdetails = StrExtract(posdetails,1,&#039;:&#039;);\n   possym = StrTrim(posdetails,&quot;\\&quot;&quot;);\n    _TRACE(&quot;\\nTrading Symbol : &quot;+possym);\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\n\nif(flag==0)\n{\n_TRACE(&quot;\\nTrading Symbol Not Found&quot;);\n posNetqty =0;\n_TRACE(&quot;\\nNetQty : &quot;+posNetqty);\n}\n\nreturn posNetqty;\n\n}\n\n\/\/Buy and Sell Order Functions\nfunction Placeorder(TransType,orderqty)\n{\n\t\n    algomojo=CreateObject(&quot;AMAMIBRIDGE.Main&quot;);\n    symboltoken = &quot;&quot;;\n    api_data =&quot;{\\&quot;stgy_name\\&quot;: \\&quot;&quot;+stgy_name+&quot;\\&quot;,  \\&quot;variety\\&quot;:\\&quot;&quot;+variety+&quot;\\&quot;,  \\&quot;tradingsymbol\\&quot;:\\&quot;&quot;+tradingsymbol+&quot;\\&quot;,      \\&quot;symboltoken\\&quot;:\\&quot;&quot;+symboltoken+&quot;\\&quot;,         \\&quot;transactiontype\\&quot;:\\&quot;&quot;+TransType+&quot;\\&quot;,         \\&quot;exchange\\&quot;:\\&quot;&quot;+exchange+&quot;\\&quot;,         \\&quot;ordertype\\&quot;:\\&quot;&quot;+ordertype+&quot;\\&quot;,         \\&quot;producttype\\&quot;:\\&quot;&quot;+producttype+&quot;\\&quot;,         \\&quot;duration\\&quot;:\\&quot;&quot;+duration+&quot;\\&quot;,         \\&quot;price\\&quot;:\\&quot;&quot;+price+&quot;\\&quot;,         \\&quot;squareoff\\&quot;:\\&quot;&quot;+squareoff+&quot;\\&quot;,         \\&quot;stoploss\\&quot;:\\&quot;&quot;+stoploss+&quot;\\&quot;,         \\&quot;quantity\\&quot;:\\&quot;&quot;+orderqty+&quot;\\&quot;,         \\&quot;triggerprice\\&quot;: \\&quot;&quot;+triggerprice+&quot;\\&quot;,  \\&quot;trailingStopLoss\\&quot;: \\&quot;&quot;+trailingStopLoss+&quot;\\&quot;, \\&quot;disclosedquantity\\&quot;:\\&quot;&quot;+disclosedquantity+&quot;\\&quot; }&quot;;\n   \n    _TRACE(&quot;API Request&quot;+api_data);\n    resp=algomojo.AMDispatcher(user_apikey, api_secretkey,&quot;PlaceOrder&quot;,api_data,broker,ver);\n    _TRACE(&quot;API Response : &quot;+resp);\n    _TRACE(&quot;Strategy Name : &quot;+stgy_name+&quot;  AlgoStatus : &quot;+ EnableAlgo);\n    _TRACE(&quot;Chart Symbol : &quot;+ Name() +&quot;  Trading Symbol : &quot;+  tradingsymbol +&quot;  qty : &quot;+ orderqty +&quot;  Signal : Buy Signal  TimeFrame : &quot;+ Interval(2)+&quot;  ChardId : &quot;+ GetChartID() + &quot; LTP : &quot;+LastValue(C));\n\tSay( &quot;Order Placed&quot; ); \n}\n\n\n\n_SECTION_BEGIN(&quot;Algo Dashboard&quot;);\n\n\nstatic_name_ = Name()+GetChartID()+interval(2)+stgy_name;\nstatic_name_algo = Name()+GetChartID()+interval(2)+stgy_name+&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}\n\n\n\n_SECTION_END();\n\n\/\/Button Controls\n\n\nbtn1_name = &quot;BE&quot;; \/\/BE- Buy Entry\nbtn2_name = &quot;BX&quot;; \/\/BX - Buy Exit - close all the open positions\nbtn3_name = &quot;SE&quot;; \/\/SE - Short Entry\nbtn4_name = &quot;SX&quot;; \/\/SX - Short Exit - close all the open positions\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; \/\/button creation is successful\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\tBE = id == 1 &amp;&amp; clickevent;\n\tBX = id == 2 &amp;&amp; clickevent;\n\tSE = id == 3 &amp;&amp; clickevent;\n\tSX = id == 4 &amp;&amp; clickevent;\n\t\n\t\n\t\n\tresult = 0;\n\t\n\tif(EnableAlgo == &quot;Enable&quot;)\n\t{\n\t\n\tif( BE AND StaticVarGet(Name()+GetChartID()+&quot;BuyEntry&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;BuyEntry is pressed&quot;);\n\t\torderresponse = PlaceOrder(&quot;BUY&quot;,quantity);\n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;BuyEntry&quot;,1); \n\t}\n\telse\n\t{\n\t\t\n\t\tStaticVarSet(Name()+GetChartID()+&quot;BuyEntry&quot;,0);\n\t}\n\t\n\tif( BX AND StaticVarGet(Name()+GetChartID()+&quot;BuyExit&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;BuyExit is pressed&quot;);\n\t\tif((qty=NetOpenPositions())&gt;0) \/\/if long positons are open\n\t\t{\n\t\torderresponse = PlaceOrder(&quot;SELL&quot;,qty);\n\t\tresult = 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t_TRACE(&quot;No Long Positions Open&quot;);\n\t\t}\n\t\t\n\t\tStaticVarSet(Name()+GetChartID()+&quot;BuyExit&quot;,1); \n\t}\n\telse\n\t{\n\t\t\n\t\tStaticVarSet(Name()+GetChartID()+&quot;BuyExit&quot;,0);\n\t}\n\t\n\tif( SE AND StaticVarGet(Name()+GetChartID()+&quot;ShortEntry&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;BShortEntry is pressed&quot;);\n\t\torderresponse = PlaceOrder(&quot;SELL&quot;,quantity);\n\t\tresult = 1;\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortEntry&quot;,1); \n\t}\n\telse\n\t{\n\t\t\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortEntry&quot;,0);\n\t}\n\t\n\n\t\n\tif( SX AND StaticVarGet(Name()+GetChartID()+&quot;ShortExit&quot;)==0 ) \n\t{\n\t\t_TRACE(&quot;ShortExit is pressed&quot;);\n\t\tif((qty=NetOpenPositions())&lt;0) \/\/if short positons are open\n\t\t{\n\t\torderresponse = PlaceOrder(&quot;BUY&quot;,abs(qty));\n\t\tresult = 1;\n\t\t}\n\t\telse\n\t\t{\n\t\t_TRACE(&quot;No Short Positions Open&quot;);\n\t\t}\n\t\t\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortExit&quot;,1); \n\t}\n\telse\n\t{\n\t\t\n\t\tStaticVarSet(Name()+GetChartID()+&quot;ShortExit&quot;,0);\n\t}\n\t\n\t} \n\n\nreturn result;  \/\/result = 1 - order is successfully placed, 0 - Algotrading is in disabled state or orders are not went through\n\t\n}\/\/end of function\n\t\n\tButtonXOffset = Param(&quot;Button X-Offset&quot;,0,50,1000,50);\n\tButtonYOffset = Param(&quot;Button Y-Offset&quot;,100,0,1000,50);\n\t\n\tBuyEntry = CreateGUI( btn1_name, ButtonXOffset, ButtonYOffset, 50, 50 );\n\tBuyExit = CreateGUI( btn2_name, ButtonXOffset+50, ButtonYOffset, 50, 50 );\n\t\n\tShortEntry = CreateGUI( btn3_name, ButtonXOffset , ButtonYOffset+50, 50, 50 );\n\tShortExit = CreateGUI( btn4_name, ButtonXOffset +50, ButtonYOffset+50, 50, 50 );\n\n\t\n\t\n\thandleevents();\n\t\n\t\n\t\n\tGuiSetColors( 1, 1, 2, colorGreen, colorBlack, colorGreen, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); \n\tGuiSetColors( 2, 2, 2, colorred, colorBlack, colorRed, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); \n\n\n\tGuiSetColors( 3, 3, 2, colorOrange, colorBlack, colorOrange, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); \n\tGuiSetColors( 4, 4, 2, colorPaleGreen, colorBlack, colorPaleGreen, colorWhite, colorBlue, colorYellow, colorRed, colorBlack, colorYellow ); \n\n_SECTION_END();\t\n\t\n_SECTION_BEGIN(&quot;Plot Candles&quot;);\t\n\nSetChartOptions(0 , chartShowArrows | chartShowDates);\nPlot(Close,&quot;Candle&quot;, colorDefault, styleCandle);\n\n_SECTION_END();\n\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This Algomojo Button trading module provides 4 modes of the button to place orders with a single click directly from Amibroker using simple smart order controls.<\/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,13,7,109,42,156],"_links":{"self":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/381"}],"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=381"}],"version-history":[{"count":3,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/381\/revisions"}],"predecessor-version":[{"id":638,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/381\/revisions\/638"}],"wp:attachment":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/media?parent=381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/categories?post=381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/tags?post=381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}