{"id":532,"date":"2023-02-16T12:41:31","date_gmt":"2023-02-16T12:41:31","guid":{"rendered":"https:\/\/algomojo.com\/blog\/?p=532"},"modified":"2023-09-13T04:30:20","modified_gmt":"2023-09-13T04:30:20","slug":"how-to-squareoff-all-openpositions-using-tradingview-automation-tutorial","status":"publish","type":"post","link":"https:\/\/algomojo.com\/blog\/how-to-squareoff-all-openpositions-using-tradingview-automation-tutorial\/","title":{"rendered":"How to Squareoff All OpenPositions using Tradingview &#8211; Automation Tutorial"},"content":{"rendered":"\n<p>One of the most demanded requests from Tradingview-based automated traders is how to square-off all open positions when a particular time is reached. There is a simple workaround for Algomojo users to implement square-off all open positions using Tradingview Pinescript.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2023\/02\/image-3.png\"><img loading=\"lazy\" width=\"1825\" height=\"806\" src=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2023\/02\/image-3.png\" alt=\"\" class=\"wp-image-533\"\/><\/a><\/figure>\n\n\n\n<p><strong>Tradingview Pinescript Code to Squareoff All Open Positions<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-clike\">\/\/ This source code is subject to the terms of the Mozilla Public License 2.0 at https:\/\/mozilla.org\/MPL\/2.0\/\n\/\/ @algomojo\n\/\/ Webhook Url : https:\/\/amapi.algomojo.com\/v1\/SquareOffAllPosition\n\n\n\/\/@version=5\nstrategy(&#039;Squareoff All OpenPositions Execution Module for Algomojo&#039;, overlay=true,process_orders_on_close = true,pyramiding = 25000)\n\nmarketSession = input.session(title=&#039;Squareoff session&#039;, defval=&#039;1514-1515&#039;, confirm=false, group=&#039;Squareoff Controls&#039;)\n\nbarInSession(sess) =&gt;\n    time(timeframe.period, sess) != 0\n\nbool intradaySession = barInSession(marketSession)\n\n\nsqoff = intradaySession\n\nif sqoff\n    strategy.entry(&#039;Squareoff&#039;, strategy.long,comment=&#039;Squareoff All Positions&#039;)\n<\/code><\/pre>\n\n\n\n<p><strong>Steps to Squareoff All Open Positions<\/strong><\/p>\n\n\n\n<p>1)Open Tradingview 1 min charts.<\/p>\n\n\n\n<p>2)Apply the Squareoff All Open Positions pinescript code to Tradingview charts. <\/p>\n\n\n\n<p>2)Go to the parameter settings and set the square-off session timings with 1-minute difference as shown below. In this example, 15:14 and 15:15 is the timing displayed with 1minute difference which will square-off the openposition at 15:15.  Entered Timings should be in <a href=\"https:\/\/simple.wikipedia.org\/wiki\/24-hour_clock\">24hour clock timing format<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"478\" height=\"342\" src=\"https:\/\/algomojo.com\/blog\/wp-content\/uploads\/2023\/02\/image-5.png\" alt=\"\" class=\"wp-image-535\"\/><\/figure>\n\n\n\n<p>3)<strong>Create the Alert&nbsp;<\/strong>with the following alert message. Configure the apikey,apisecret and broker short code<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-json\">{\n    &quot;api_key&quot;:&quot;46ad9245cca3dfe957deb235a39d02a3&quot;,\n    &quot;api_secret&quot;:&quot;30ca108cfb91f458babd2681fb6d0817&quot;,\n    &quot;data&quot;:\n      {\n        &quot;broker&quot;:&quot;AN&quot;\n      }\n}<\/code><\/pre>\n\n\n\n<p>in the above example apikey and apisecret key is taken from the algomojo&nbsp;<strong>MyAPI&nbsp;<\/strong>section and here is the list of broker shortcode<\/p>\n\n\n\n<h4>Broker Short Code<\/h4>\n\n\n\n<p>AB \u2013 Alice Blue<br>AN \u2013 Angel Broking<br>FS \u2013 Firstock<br>FY \u2013 Fyers<br>PT \u2013 Paytm<br>SM \u2013 Samco<br>TC \u2013 Tradejini<br>UP \u2013 Upstox<br>ZB \u2013 Zebu<br>ZE \u2013 Zerodha<\/p>\n\n\n\n<p>For Updated Brokers Short Code visit&nbsp;<a href=\"https:\/\/amapi.algomojo.com\/v1\/docs\/\">Algomojo consolidated API docs \u2013 appendix section<\/a><\/p>\n\n\n\n<p>Once Alert Message is prepared goto the alert box and enter those alert JSON message as shown below<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/i0.wp.com\/www.marketcalls.in\/wp-content\/uploads\/2023\/02\/image-27.png?resize=530%2C600&amp;ssl=1\" alt=\"\" class=\"wp-image-57323\"\/><\/figure>\n\n\n\n<p>now goto the notifications tab and enter the following webhook URL to squareoff all open positions<\/p>\n\n\n\n<pre class=\"wp-block-prismatic-blocks\"><code class=\"language-json\">https:\/\/amapi.algomojo.com\/v1\/SquareOffAllPosition<\/code><\/pre>\n\n\n\n<p>Copy<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img src=\"https:\/\/i0.wp.com\/www.marketcalls.in\/wp-content\/uploads\/2023\/02\/image-28.png?resize=530%2C620&amp;ssl=1\" alt=\"\" class=\"wp-image-57324\"\/><\/figure>\n\n\n\n<p>Ensure Webhook URL box is checked and click create to create the alert<\/p>\n\n\n\n<p>4)Now once the square-off time, tradingview will trigger the alert to square-off all open positions and you can witness from your trading terminal\/Algomojo Terminal that all the open positions square-off automatically.<\/p>\n\n\n\n<p><strong>Limitations<\/strong><\/p>\n\n\n\n<p>1)At this moment Bracket Orders (BO) and Cover Orders(CO) orders will not be squareoff using SquareoffAllPosition Function<br>2)If the trade quantity is more than the freeze quantity in Derivatives it will not be square-off which needs to be manually squaredoff at this moment.<\/p>\n\n\n\n<p>3)You Demat holdings will not be affected by SquareoffAllPosition Function<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most demanded requests from Tradingview-based automated traders is how to square-off all open positions when a particular time is reached. There is a simple workaround for Algomojo users to implement square-off all open positions using Tradingview Pinescript. Tradingview Pinescript Code to Squareoff All Open Positions Steps to Squareoff All Open Positions 1)Open &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"https:\/\/algomojo.com\/blog\/how-to-squareoff-all-openpositions-using-tradingview-automation-tutorial\/\"> <span class=\"screen-reader-text\">How to Squareoff All OpenPositions using Tradingview &#8211; Automation Tutorial<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[91],"tags":[227,33,169],"_links":{"self":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/532"}],"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=532"}],"version-history":[{"count":2,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/532\/revisions"}],"predecessor-version":[{"id":590,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/posts\/532\/revisions\/590"}],"wp:attachment":[{"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/media?parent=532"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/categories?post=532"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/algomojo.com\/blog\/wp-json\/wp\/v2\/tags?post=532"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}