kotak-logo
TradingView Simplified Logo Light Mode

Kotak

Stockshaala

Module 7
Pine Script – Build & Backtest
Course Index
Read in
English
हिंदी

Chapter 4 | 3 min read

Strategy Logic Building (Example: RSI + Supertrend)

You’ve built your first custom indicator. Now it’s time to take things up a notch:

  • Create a trading strategy with actual buy/sell logic
  • Test it on real historical data
  • See if it works before risking any money

In this chapter, you’ll build a strategy that:

  • Enters a buy trade when RSI is oversold and Supertrend turns bullish.
  • Exits when RSI goes above a certain level or Supertrend turns bearish.

Unlike indicator scripts, strategy scripts use the broker emulator logic to determine price movement within a bar and by default wait for the bar to close to execute orders, which is the key difference compared to indicator types.

Buy Entry Conditions:

  • RSI < 30 (oversold)
  • Supertrend is in uptrend mode (bullish)
  • RSI > 70 (overbought)
  • OR Supertrend turns bearish

Unlike indicator() scripts, a strategy() script in Pine Script™ uses TradingView’s broker emulator to simulate trades on historical data. It models how price moves within each bar and, by default, waits for the bar to close before executing an order — making it ideal for backtesting and performance evaluation.

//@version=6
strategy("RSI + Supertrend Strategy", overlay=true)

// === INPUTS ===
rsiPeriod = input.int(14, title="RSI Period")
rsiBuyLevel = input.int(30, title="RSI Buy Threshold")
rsiSellLevel = input.int(70, title="RSI Sell Threshold")
atrPeriod = input.int(10, title="Supertrend ATR Period")
factor = input.float(3.0, title="Supertrend Multiplier")

// === INDICATORS ===
// RSI
rsi = ta.rsi(close, rsiPeriod)

// Supertrend function
[supertrend, direction] = ta.supertrend(factor, atrPeriod)
isBullish = direction == 1
isBearish = direction == -1

// === STRATEGY CONDITIONS ===
// Entry when RSI < 30 and trend is bullish
longCondition = (rsi < rsiBuyLevel) and isBullish
if (longCondition)
strategy.entry("Buy", strategy.long)

// Exit when RSI > 70 or trend turns bearish
exitCondition = (rsi > rsiSellLevel) or isBearish
if (exitCondition)
strategy.close("Buy")

// === VISUALS ===
plot(supertrend, title="Supertrend", color=isBullish ? color.green : color.red, linewidth=1)

  1. Paste the code in TradingView’s Pine Editor
  2. Click “Add to Chart”
  3. Open the Strategy Tester tab
  4. Check metrics like:
  • Net Profit
  • Win Rate
  • Drawdown
  • Number of Trades
  • Profit Factor

You can also change timeframes (15-min, 1H, Daily) to see how performance varies.

  • Replace RSI with Stochastic or MACD
  • Add a stop-loss using strategy.exit
  • Test with EMA filters or multi-timeframe confirmation
  • Add shorting logic: strategy.entry("Sell", strategy.short)

You’ve now written a working strategy that combines two popular indicators - RSI and Supertrend and even backtested it using real data.

It’s a major step toward building strategies you trust, test, and refine without risking money upfront.

Note: Pine Script™ is a trademark of TradingView Inc.

Is this chapter helpful?
Previous
Hands-On: Create Your First Indicator
Next
Backtesting Do’s & Don’ts (Bias, Interpretation, Metrics)

Disclaimer: This article is for informational purposes only and does not constitute financial advice. It is not produced by the desk of the Kotak Neo Research Team, nor is it a report published by the Kotak Neo Research Team. The information presented is compiled from several secondary sources available on the internet and may change over time. Investors should conduct their own research and consult with financial professionals before making any investment decisions. Read the full disclaimer here.

Investments in securities market are subject to market risks, read all the related documents carefully before investing. Brokerage will not exceed SEBI prescribed limit. The securities are quoted as an example and not as a recommendation. SEBI Registration No-INZ000200137 Member Id NSE-08081; BSE-673; MSE-1024, MCX-56285, NCDEX-1262.

Discover our extensive knowledge center

Explore our comprehensive video library that blends expert market insights with Kotak's innovative financial solutions to support your goals.

PreviousCourse IndexNext