In the previous tutorial, we discussed configuring the Long/Short Stop and Reverse Tradingview Automation Module for Dhan. In this module, we will explore strategies that come with defined Long Entry(Buy), Long Exit(Sell), Short Entry(Short), and Short Exit(Cover) rules in Tradingview Pinescript.
For Configuring the Webhook, Tradingview Webhook Message kindly refer the tutorial Dhan – Tradingview Webhook Automation – Long Short Trading Strategy Module
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Trading Block Description
//Module Name : Buy/Sell/Short/Cover Module
//Coded by Rajandran R (Founder - www.marketcalls.in)
//Version : v1.0
//Coded Date : 20th Dec 2023
//Block 1 : Backtesting Controls & Target and Stoploss Controls
//Block 2 : Trading Strategy and Controls (write your trading strategy in the block
//Block 3 : Intraday Function and Buy and Sell Signal Mapping (Signal Mapping is required)
//Block 4 : Strategy Execution Module
///////////////////////////////////////////////////////////////////////////////////////////////////////////
//@version=5
strategy('SuperTrend Trading Strategy with EMA Filter Target/Stoploss/TrailingStoploss', shorttitle='Supertrend EMA Filter Strategy', overlay=true)
//Block 1 : Backtesting Controls & Target Stoploss Controls
FromDay = input.int(defval=1, title='From Day', minval=1, maxval=31, group='Backtesting')
FromMonth = input.int(defval=1, title='From Month', minval=1, maxval=12, group='Backtesting')
FromYear = input.int(defval=2018, title='From Year', minval=999, group='Backtesting')
ToDay = input.int(defval=1, title='To Day', minval=1, maxval=31, group='Backtesting')
ToMonth = input.int(defval=1, title='To Month', minval=1, maxval=12, group='Backtesting')
ToYear = input.int(defval=9999, title='To Year', minval=999, group='Backtesting')
start = timestamp(FromYear, FromMonth, FromDay, 00, 00)
finish = timestamp(ToYear, ToMonth, ToDay, 23, 59)
window() =>
time >= start and time <= finish ? true : false
Mode = input.string(title='Algo Mode', defval='ENABLE', options=['ENABLE', 'LONGONLY', 'SHORTONLY'], group='Backtesting')
highlighting = input.bool(title='Highlighter On/Off ?', defval=true, group='Intraday Controls')
barcoloring = input.bool(title='Bar Coloring On/Off ?', defval=true, group='Intraday Controls')
intraday = input.bool(title='Intraday On/Off ?', defval=false, group='Intraday Controls')
marketSession = input.session(title='Market session', defval='0915-1500', confirm=false, group='Intraday Controls')
var longCondition = false
var shortCondition = false
//Tradingview alert_message
string alert_buy = 'B'
string alert_sell = 'S'
////////////////////////////////////////Block 1 Module Ends////////////////////////////////////////////////////////////////////////
//Block 2 : Trading Strategy
//input controls
factor = input.float(2.5,"Factor",1.0,10.0,0.5)
length = input.int(10,"ATR Length",1,100,1)
[supertrend,direction] = ta.supertrend(factor,length)
ema200 = ta.ema(close,200)
plot(ema200,"EMA 200",color.yellow,linewidth = 2)
//direction variables contains series of +1 and -1
//supertrend variable contains the supertrend line
supcolor = direction == -1 ? color.green : color.red
plot(supertrend,"Supertrend",supcolor,linewidth = 2)
//Trading Logic
buySignal = direction == -1 and close > ema200
sellSignal = direction == 1
shortSignal = direction == 1 and close < ema200
coverSignal = direction == -1
////////////////////////////////////////Block 2 Module Ends////////////////////////////////////////////////////////////////////////
//Block 3 : Intraday Function and Buy and Sell Signal Mapping
//Remove the comments to do the long/short signal mapping
//buySignal = tbuy
//sellSignal = tsell
//shortSignal = tshort
//coverSignal = tcover
barInSession(marketSession) =>
time(timeframe.period, marketSession) != 0
bool intradaySession = barInSession(marketSession)
buy = buySignal
sell = sellSignal
short = shortSignal
cover = coverSignal
buy1 = buy[1]
sell1 = sell[1]
short1 = short[1]
cover1 = cover[1]
//assign signals
if(not intraday)
buy := buySignal
short := shortSignal
if(intraday)
buy := buy and intradaySession
short := short and intradaySession
////////////////////////////////////////Block 3 Module Ends////////////////////////////////////////////////////////////////////////
//Block 4 : Execution Controls
if(Mode=="ENABLE")
//Fresh Long Entry
if buy and not cover and strategy.position_size == 0 and window()
strategy.entry('buy',strategy.long,comment='BUY',alert_message = alert_buy)
if buy and cover and strategy.position_size == 0 and window()
strategy.entry('buy',strategy.long,comment='BUY',alert_message = alert_buy)
//Stop and Reverse to Buy
if buy and cover and strategy.position_size < 0 and window()
strategy.entry('buy',strategy.long,comment='BUY',alert_message = alert_buy)
//Long Exit
if sell and not short and strategy.position_size > 0 and window()
strategy.close('buy',comment='SELL',alert_message = alert_sell)
//Fresh Short Entry
if short and not sell and strategy.position_size == 0 and window()
strategy.entry('short',strategy.short,comment='SHORT',alert_message = alert_sell)
if short and sell and strategy.position_size == 0 and window()
strategy.entry('short',strategy.short,comment='SHORT',alert_message = alert_sell)
//Stop and Reverse to Buy
if short and sell and strategy.position_size > 0 and window()
strategy.entry('short',strategy.short,comment='SHORT',alert_message = alert_sell)
//Short Exit
if cover and not buy and strategy.position_size < 0 and window()
strategy.close('short',comment='COVER',alert_message = alert_buy)
if(Mode=="LONGONLY")
//Fresh Long Entry
if buy and not cover and strategy.position_size == 0 and window()
strategy.entry('buy',strategy.long,comment='BUY',alert_message = alert_buy)
if buy and cover and strategy.position_size == 0 and window()
strategy.entry('buy',strategy.long,comment='BUY',alert_message = alert_buy)
//Stop and Reverse to Buy
if buy and cover and strategy.position_size < 0 and window()
strategy.entry('buy',strategy.long,comment='BUY',alert_message = alert_buy)
//Long Exit
if sell and not short and strategy.position_size > 0 and window()
strategy.close('buy',comment='SELL',alert_message = alert_sell)
if(Mode=="SHORTONLY")
//Fresh Short Entry
if short and not sell and strategy.position_size == 0 and window()
strategy.entry('short',strategy.short,comment='SHORT',alert_message = alert_sell)
if short and sell and strategy.position_size == 0 and window()
strategy.entry('short',strategy.short,comment='SHORT',alert_message = alert_sell)
//Stop and Reverse to Buy
if short and sell and strategy.position_size > 0 and window()
strategy.entry('short',strategy.short,comment='SHORT',alert_message = alert_sell)
//Short Exit
if cover and not buy and strategy.position_size > 0 and window()
strategy.close('short',comment='COVER',alert_message = alert_buy)
if(intraday)
longsquareOff = not intradaySession and strategy.position_size > 0
if(longsquareOff)
strategy.close(id='BUY', comment='Square-off',alert_message = alert_sell)
shortsquareOff = not intradaySession and strategy.position_size < 0
if(shortsquareOff)
strategy.close(id='SELL', comment='Square-off',alert_message = alert_buy)
////////////////////////////////////////Block 4 Module Ends////////////////////////////////////////////////////////////////////////
Now configure the Strategy for Alerts and place the dynamic alert message
Here is the Sample Dynamic Strategy Alerts
To the Dhan JSON format message add dynamic strategy placeholders to control the transaction type (Buy/Sell) and the quantity as shown below.
{
"secret": "iF4yz",
"alertType": "multi_leg_order",
"order_legs": [
{
"transactionType": "{{strategy.order.alert_message}}",
"orderType": "MKT",
"quantity": "{{strategy.order.contracts}}",
"exchange": "NSE",
"symbol": "NIFTY1!",
"instrument": "FUT",
"productType": "M",
"sort_order": "1",
"price": "0"
}
]
}
Alert Configuration on TradingView:
1.After applying the strategy to the charts, create a strategy alert.
2.Enter the strategy name and the configured TradingView alert message.
3.In the alert box, go to the Notifications tab, enable webhook, and enter the Dhan webhook URL.
Trade Execution
Once configured, TradingView will send automated orders to Dhan based on the alerts set up by your trading strategy.
Rajandran – Is there any article on how I can combine this with ICICIDirect’s Breeze API?
Hi it is nothing to do with API here. Here Dhan provides a webhook interface directly to place direct automation orders. Other brokers are not providing yet.
In order to use Breeze API to send automation orders you need to create a intermediate bridge in python which receives the signal from tradingview and calls the breeze API. But there are no direct options to place orders from tradingview to ICICI direct.
Can I get a template for consecutive bar up and down strategy…
can a get a template for consecutive up down strategy