Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and 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. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

How to Send Automated Bracket Order from Trading View Strategy to Algomojo Platform

4 min read

This tutorial explains how to send Automated Bracket orders from Trading view strategies to the Algomojo Trading Platform. Tradingview Supports strategies that contain rules in a pinescript language that instructs the system when to Buy/Sell orders, Modify, Cancel Orders.

Tradingview Strategies allow you to perform backtesting (emulation of a strategy trading on historical data) and forward testing (emulation of a strategy trading on real-time data) according to your algorithms.

What is a Bracket Order?

Bracket order (BO) is a type of order where you can enter a new position along with a target/exit and a stop-loss order. As soon as the main order is executed, the system will place two more orders (profit taking and stop-loss). When one of the two orders (profit taking or stop loss) gets executed, the other order will get cancelled automatically. Bracket orders are essentially algo orders.

Bracket orders can be used only for intraday trades. All bracket orders will be squared off automatically before market close. BOs aren’t allowed on BSE stocks, stock options, currency options, and MCX.

Important Note : Currently you can place the bracket order automatically with predefined target and stoploss/trailing stoploss. However in case if you want to modify the stoploss/exit the bracket order one have to do it manually by going to the algomojo orderbook and exiting manually/modifying the stop or target levels manually from there.

If you are very new to the Algomojo Platform then kickstart with this tutorial

How to Send Automated Option Orders Tutorial

In order to send market orders from Tradingview to Algomojo in the previous tutorial, we have seen how to use Tradingview webhook feature to configure Automated orders using the Tradingview Alert Option.

In the previous tutorial, we also have seen How to Send Orders Dynamically using Tradingview Webhook Facility,Placeholders and Tradingview Pinescript strategy

Among All the Place Holders we are going to use {strategy.order.comment}} which reads the comment from the strategy section and change the order type dynamically based on the supertrend Buy or Sell Signal.

//Alerts
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")

longCondition = buySignal
if (longCondition)
    strategy.entry("BUY", strategy.long, when = window(),comment="B")
shortCondition = sellSignal
if (shortCondition)
    strategy.entry("SELL", strategy.short, when = window(),comment="S")

Comment PlaceHolder

{{strategy.order.comment}} – returns the comment of the executed order (the string used in the comment parameter in one of the function calls generating orders: strategy.entry, strategy.exit or strategy.order). If no comment is specified, then the value of strategy.order.id will be used.

Configuring Alerts

1)Select the required Timeframe.
2)Apply the Pinescript Buy or Sell Trading Strategy and
3)Click on the Alert option to configure alerts to trigger alerts from that particular timeframe.

Under the Alert Condition Tab use the drop down and change to Supertrend Algomojo and enable the Webhook URL option as shown below

Broker - Aliceblue 
https://abapi.algomojo.com/1.0/PlaceBOOrder 

Broker - Tradejini
https://tjapi.algomojo.com/1.0/PlaceBOOrder 

Broker - Zebu
https://zbapi.algomojo.com/1.0/PlaceBOOrder

Configure the Webhook Message with Placeholders

Here is the sample message with necessary placeholders

configure your API Key and API Secret key from the Algomojo portal. Here is the sample code which place Buy and Sell order for Reliance with 2 shares in intraday mode.

{
  "api_key":"8f8cb9504bb91bd5472b68483cbda8bb",
  "api_secret":"845802a70c8881a66cccfde02d83934d",
  "data":
    {
      "strg_name": "BO Strategy",
      "s_prdt_ali":"BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML",
      "TokenNo":"2885",
      "exch":"NSE",
      "Ttranstype":"{{strategy.order.comment}}",
      "Ret":"DAY",
      "qty":"2",
      "discqty":"0",
      "Price":"0",
      "ltpOratp":"NA",
      "SqrOffAbsOrticks":"Ticks",
      "SqrOffvalue":"10",
      "SLAbsOrticks":"Ticks",
      "SLvalue":"10",
      "trailingSL":"0",
      "tSLticks":"10"
    }
}

Copy the above code modify according to your need and paste in the Alert Webhook comments block as shown below

Getting the Token ID for sending Bracket Orders

Token ID is provided by the brokers and it is unique for each and every symbol. Go to the Watchlist in the Algomojo Symbols. Click on the Security Info of that Particular symbol to retrieve the Token ID

Retieve the Token ID corresponding to that particular symbol. As shown below for Reliance Industries the token no is 2885 and it is unique for each and every ttrading symbols which is mandatory for sending Bracket Orders from Tradingview platform to Algomojo.

use the retrieved Token No to configure Webhook Message as shown above.

Here is the Complete Pinescript Strategy and to place automated orders ensure one configures the Alert Settings as mentioned above.


// Algomojo Trading Strategy


//@version=4
strategy("SuperTrend Algomojo Trading Strategy", overlay=true)



//inputs
Periods = input(title="ATR Period", type=input.integer, defval=10)
src = input(hl2, title="Source")
Multiplier = input(title="ATR Multiplier", type=input.float, step=0.1, defval=3.0)
changeATR= input(title="Change ATR Calculation Method ?", type=input.bool, defval=true)
showsignals = input(title="Show Buy/Sell Signals ?", type=input.bool, defval=true)
highlighting = input(title="Highlighter On/Off ?", type=input.bool, defval=true)
barcoloring = input(title="Bar Coloring On/Off ?", type=input.bool, defval=true)

atr2 = sma(tr, Periods)
atr= changeATR ? atr(Periods) : atr2
up=src-(Multiplier*atr)
up1 = nz(up[1],up)
up := close[1] > up1 ? max(up,up1) : up
dn=src+(Multiplier*atr)
dn1 = nz(dn[1], dn)
dn := close[1] < dn1 ? min(dn, dn1) : dn
trend = 1
trend := nz(trend[1], trend)
trend := trend == -1 and close > dn1 ? 1 : trend == 1 and close < up1 ? -1 : trend
upPlot = plot(trend == 1 ? up : na, title="Up Trend", style=plot.style_linebr, linewidth=2, color=color.green)
buySignal = trend == 1 and trend[1] == -1
plotshape(buySignal ? up : na, title="UpTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.green, transp=0)
plotshape(buySignal and showsignals ? up : na, title="Buy", text="Buy", location=location.absolute, style=shape.labelup, size=size.tiny, color=color.green, textcolor=color.white, transp=0)
dnPlot = plot(trend == 1 ? na : dn, title="Down Trend", style=plot.style_linebr, linewidth=2, color=color.red)
sellSignal = trend == -1 and trend[1] == 1
plotshape(sellSignal ? dn : na, title="DownTrend Begins", location=location.absolute, style=shape.circle, size=size.tiny, color=color.red, transp=0)
plotshape(sellSignal and showsignals ? dn : na, title="Sell", text="Sell", location=location.absolute, style=shape.labeldown, size=size.tiny, color=color.red, textcolor=color.white, transp=0)
mPlot = plot(ohlc4, title="", style=plot.style_circles, linewidth=0)
longFillColor = highlighting ? (trend == 1 ? color.green : color.white) : color.white
shortFillColor = highlighting ? (trend == -1 ? color.red : color.white) : color.white
fill(mPlot, upPlot, title="UpTrend Highligter", color=longFillColor)
fill(mPlot, dnPlot, title="DownTrend Highligter", color=shortFillColor)
FromMonth = input(defval = 9, title = "From Month", minval = 1, maxval = 12)
FromDay   = input(defval = 1, title = "From Day", minval = 1, maxval = 31)
FromYear  = input(defval = 2018, title = "From Year", minval = 999)
ToMonth   = input(defval = 1, title = "To Month", minval = 1, maxval = 12)
ToDay     = input(defval = 1, title = "To Day", minval = 1, maxval = 31)
ToYear    = input(defval = 9999, title = "To Year", minval = 999)
start     = timestamp(FromYear, FromMonth, FromDay, 00, 00)  
finish    = timestamp(ToYear, ToMonth, ToDay, 23, 59)       
window()  => time >= start and time <= finish ? true : false


//Alerts
alertcondition(buySignal, title="SuperTrend Buy", message="SuperTrend Buy!")
alertcondition(sellSignal, title="SuperTrend Sell", message="SuperTrend Sell!")

longCondition = buySignal
if (longCondition)
    strategy.entry("BUY", strategy.long, when = window(),comment="B")
shortCondition = sellSignal
if (shortCondition)
    strategy.entry("SELL", strategy.short, when = window(),comment="S")
buy1= barssince(buySignal)
sell1 = barssince(sellSignal)
color1 = buy1[1] < sell1[1] ? color.green : buy1[1] > sell1[1] ? color.red : na
barcolor(barcoloring ? color1 : na)

One the alert triggers automated orders will be placed instantly in algomojo platform

Where to Check the Order in Realtime in Algomojo

You can use order logs to check for any incoming automated orders generating from Tradingview Webhooks in Realtime and can also download the logs for later use.

Rajandran R Telecom Engineer turned Full-time Derivative Trader. Mostly Trading Nifty, Banknifty, USDINR and 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. Writing about Markets, Trading System Design, Market Sentiment, Trading Softwares & Trading Nuances since 2007 onwards. Author of Marketcalls.in)

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

5Paisa – Tradingview Webhook Automation Module

In this tutorial, we will be learning how to use the 5Paisa – Tradingview Webhook feature for automating your Tradingview pinescript strategy. This Module...
Rajandran R
10 min read

Leave a Reply

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