How to Squareoff All OpenPositions using Tradingview – Automation Tutorial

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

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// @algomojo
// Webhook Url : https://amapi.algomojo.com/v1/SquareOffAllPosition


//@version=5
strategy('Squareoff All OpenPositions Execution Module for Algomojo', overlay=true,process_orders_on_close = true,pyramiding = 25000)

marketSession = input.session(title='Squareoff session', defval='1514-1515', confirm=false, group='Squareoff Controls')

barInSession(sess) =>
    time(timeframe.period, sess) != 0

bool intradaySession = barInSession(marketSession)


sqoff = intradaySession

if sqoff
    strategy.entry('Squareoff', strategy.long,comment='Squareoff All Positions')

Steps to Squareoff All Open Positions

1)Open Tradingview 1 min charts.

2)Apply the Squareoff All Open Positions pinescript code to Tradingview charts.

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 24hour clock timing format

3)Create the Alert with the following alert message. Configure the apikey,apisecret and broker short code

{
    "api_key":"46ad9245cca3dfe957deb235a39d02a3",
    "api_secret":"30ca108cfb91f458babd2681fb6d0817",
    "data":
      {
        "broker":"AN"
      }
}

in the above example apikey and apisecret key is taken from the algomojo MyAPI section and here is the list of broker shortcode

Broker Short Code

AB – Alice Blue
AN – Angel Broking
FS – Firstock
FY – Fyers
PT – Paytm
SM – Samco
TC – Tradejini
UP – Upstox
ZB – Zebu
ZE – Zerodha

For Updated Brokers Short Code visit Algomojo consolidated API docs – appendix section

Once Alert Message is prepared goto the alert box and enter those alert JSON message as shown below

now goto the notifications tab and enter the following webhook URL to squareoff all open positions

https://amapi.algomojo.com/v1/SquareOffAllPosition

Copy

Ensure Webhook URL box is checked and click create to create the alert

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.

Limitations

1)At this moment Bracket Orders (BO) and Cover Orders(CO) orders will not be squareoff using SquareoffAllPosition Function
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.

3)You Demat holdings will not be affected by SquareoffAllPosition Function

Leave a Comment

Your email address will not be published. Required fields are marked *