This tutorial helps you to understand how to send cover orders from the Tradingview platform using the webhook method to the Algomojo Trading Platform.
Supported Brokers
Aliceblue, Upstox, Tradejini, Zebu
What is a Cover Order?
Cover Order is a 2 legged Intraday order
1)Leg 1 is the main Buy or Sell Order (Market Order / SL-Limit Order)
2)Leg 2 is a compulsory stop-loss Order (SL-Market Order)
Benefit of Cover Order
1)Intraday Risk is Pre-Defined and Controlled
2)Order Goes to the Exchange and rest there. Even if the Broker Server goes for Outage or Even if the traders internet goes down no matter what Cover Order gets a fill.
TradingView Webhook
Tradingview Webhooks allow you to send a POST request to a certain URL every time the alert is triggered. This feature can be enabled when you create or edit an alert
Various Modes One can send Alerts from Tradingview Platform
1)Horizontal Line Trigger Alerts
2)Price Crossover Alerts
3)Trendline/Channel Alerts
4)Custom Indicator/Strategy Alerts
5)Tradingview Screener Alerts
Free Version or Paid Version in Tradingview?
Both free version and paid version support real-time data for NSE Cash, NSE Futures, MCX Futures with Alerts. However, Webhook Alerts are available in the Tradingview Pro version onwards.
Tradingview Strategy to Place Cover Orders
Tradingview Supports strategies which contains rules in pinescript language which instructs the system when to Buy/Sell orders, Modify, Cancel Orders.
Supertrend with Cover-Order Stoploss Plot
CoverOrder Price for Buy = closing value when buy got triggered – stoploss
CoverOrder Price for Sell = closing value when short got triggered – stoploss
Tradingview Alerts Settings
Supertrend Pinescript Code for Placing Cover Orders
// Algomojo Trading Strategy
//@version=4
strategy("SuperTrend Algomojo Cover Order 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)
plotstop = input(title="Plot Stoploss On/Off ?", type=input.bool, defval=true)
stoploss = input(title="Stoploss", type=input.integer, defval=10)
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 = 2019, 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 = 2100, 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
tradestop = close
tradestop := iff(buySignal, valuewhen(buySignal,close[1]-stoploss,0),iff(sellSignal,valuewhen(sellSignal,close[1]+stoploss,0),tradestop[1]))
plot(tradestop,title="tradestop",color=color.yellow)
//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)
Strategies allow you to perform backtesting (emulation of a strategy trading on historical data) and forwardtesting (emulation of a strategy trading on real-time data) according to your algorithms.
Learn here to send market orders from Tradingview to Algomojo
Now this time we are going to use tradingview placeholders in the webhook Alert option to dynamically send Buy Orders when there is a Buy Signal in Supertrend and Sell Signal when there is a Sell Signal in Supertrend with Dynamic Stoploss (Different stoploss for Buy and Sell Cover Orders)
Webhook URL for placing orders (MKT, LMT, SL, SL-LMT, CO,AMO orders)
Broker - Aliceblue
https://abapi.algomojo.com/1.0/PlaceOrder
Broker - Upstox
https://upapi.algomojo.com/1.0/PlaceOrder
Broker - Tradejini
https://tjapi.algomojo.com/1.0/PlaceOrder
Broker - Zebu
https://zbapi.algomojo.com/1.0/PlaceOrder
Webhook Message Format – Sample Placeorder Example for Aliceblue
{
"api_key":"c1997d92a3bb556a67dca7d1446b70",
"api_secret":"5306433329e81ba41203653417063c",
"data":
{
"strg_name":"Cover Order",
"s_prdt_ali":"BO:BO||CNC:CNC||CO:CO||MIS:MIS||NRML:NRML",
"Tsym":"RELIANCE-EQ",
"exch":"NSE",
"Ttranstype":"{{strategy.order.comment}}",
"Ret":"DAY",
"prctyp":"MKT",
"qty":"75",
"discqty":"0",
"MktPro":"NA",
"Price":"0",
"TrigPrice":"{{plot("tradestop")}}",
"Pcode":"CO",
"AMO":"NO"
}
}
Dynamic Webhook Placeholder used
Among All the Place Holders we are going to use {{strategy.order.comment}} and {{plot(“tradestop”)}} which reads the comment from the strategy section and dynamic tradestop to change the order type dynamically based on the supertrend Buy or Sell Signal with a pre-determined stop loss.
{{strategy.order.comment}} - Which dynamically transmits "B" or "S " values
{{plot("tradestop")}} - Which Dynamically transmits the stop Price. The stop price is computed and plotted using the plot function as shown below
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.
Let me know in comments if you find this tutorial useful or incase if you need more details about algomojo integration with tradingview you can comment your inputs below.