Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

How to Squareoff All OpenPositions using Tradingview – Automation Tutorial

1 min read

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

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

Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, High Liquid Stock Derivatives. Trading the Markets Since 2006 onwards. Using Market Profile and Orderflow for more than a decade. Designed and published 100+ open source trading systems on various trading tools. Strongly believe that market understanding and robust trading frameworks are the key to the trading success. Building Algo Platforms, Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in

Introducing OpenAlgo V1.0: The Ultimate Open-Source Algorithmic Trading Framework…

OpenAlgo V1.0 is an open-source algorithmic trading platform to automate your trading strategies using Amibroker, Tradingview, Metatrader, Python etc. Designed with the modern trader...
Rajandran R
2 min read

Dive Into the TradingView Paper Trading Competition: A Chance…

Welcome to an exhilarating opportunity presented by TradingView – a paper trading competition that not only tests your trading acumen but also offers a...
Rajandran R
1 min read

Introducing PineGPT – To Build Better Tradingview Pinescript Codes

PineGPT is a customGPT for ChatGPT4 users designed to provide expert guidance on creating and understanding TradingView Pine Script indicators and trading strategies. PineGPT...
Rajandran R
1 min read

Leave a Reply

Get Notifications, Alerts on Market Updates, Trading Tools, Automation & More