Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Building GenAI Applications. 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

Supertrend v4.0 with EMA Filter – Tradingview Pinescript Code

1 min read

Supertrend with a simple EMA Filter can improve the performance of the signals during a strong trending market and reduces the number of trades as well which in turn reduces the trading cost + unwanted losing trades. i.e overall drawdown reduces and improves the sharp ratio compared to the regular supertrend long/short signals.

Strategy Rules

Buy – if candle closes above supertrend and price above 200 EMA on closing basis
Exit Buy – if price of the candle closes below supertrend.

Strategy Parameters

EMA Filter used: 200 EMA

Strategy Type: Long Only

Recommended Timeframe: 15min

Symbol Backtested: Nifty Futures

Backtesting Period : Jan 2017 – 29th Nov 2022 – Till to date

Supertrend Default Parameters: Multiplier- 3 and ATR Length – 10

Optimization : No

Position Size : 1 lot (50 shares)

Supertrend V4.0 – Nifty Futures 15min Charts

During the sideways markets, it behaves like supertrend, creating noisy signals. However, the overall performance of the super trend improves with reduced drawdown

Equity Curve per lot

Absolute Drawdown per lot

Backtesting Metrics

Tradingview Pinescript Code – Supertrend V4.0

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © algostudio
// Coded by Rajandran R (Founder - Marketcalls and Creator - OpenAlgo)

//@version=5
strategy("Supertrend with EMA as Filter", overlay=true)


//input controls
factor = input.float(2,"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

longEntry = direction == -1 and close > ema200
longExit =  direction == 1 and direction[1] == -1

shortEntry = direction == 1 and close < ema200
shortExit = direction == -1 and direction[1] == 1

supcolor = direction == -1 ? color.green : color.red 

plot(supertrend,"Supertrend",supcolor,linewidth = 2)

if(longEntry)
    strategy.entry("BUY",strategy.long,comment="BUY")
if(longExit)
    strategy.close("BUY",comment="BUY EXIT")
// if(shortEntry)
//     strategy.entry("SHORT",strategy.short,comment="SHORT")
// if(shortExit)
//     strategy.close("SHORT",comment="SHORT EXIT")
Rajandran R Creator of OpenAlgo - OpenSource Algo Trading framework for Indian Traders. Building GenAI Applications. 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

First Order and Second Order Option Greeks in Tradingview…

The provided Tradingview Pine Script™ code is an Options Greeks Calculator tailored for use on the TradingView platform. It provides real-time analysis and visualization...
Rajandran R
12 min read

Tradingview Pinescript – Real-Time Option Greeks with Implied Volatility…

Converting an existing Option Greeks model from Amibroker AFL to TradingView Pinescript was an ambitious project, one that involved dealing with complex calculations like...
Rajandran R
9 min read

Simple Option Straddle Dashboard – Tradingview Pinescript v5

In this blog post, we’ll walk through creating a Simple Option Straddle Dashboard using TradingView’s Pine Script v5. This dashboard will dynamically calculate the...
Rajandran R
3 min read

2 Replies to “Supertrend v4.0 with EMA Filter – Tradingview Pinescript Code”

  1. I have downloaded the nakshatra system for nifty and modified it. It is giving vey good calmar ratio for 6 months but the problem is i don’t have reliable data source for historical data. It would be great if you would share your data with us as you have shared many code. If it is not possible to share your data, please share the nakshatra system for tradingview where we can buy the data and test the code also. Thanks.

Leave a Reply

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