Member-only story

How to write your own stock trading strategy / Indicator on TradingView

Sairav Dev
6 min readJan 20, 2022

--

Photo by Negative Space on Pexels.com

Being a trader working with Technical Analysis , we all look at multiple indictors like RSI , MACD , Moving Averages , Bollinger Bands … and the list goes on . In order to confirm our trades , we check these indicators , and combine it with other strategies like support, resistance, volume etc. When they all give a definite signal , we take position.

There is a way we can script(automate) this logic using TradingView’s PineScript. With Pinescript ->

  • We can create a new strategy (combination of multiple indicators and logic)
  • An indicator for that strategy
  • Backtest it (test it against the previous time duration — past candles) — how well it could have performed .

And once the strategy gives us a signal , we can set alarms or in case you are making a trading bot , you can automate the process of taking positions as well.

Writing a simple strategy script (using sma crossover )

Let’s look at an easy example to get a better understanding. Suppose our strategy is -> we get confirmations from the values of SMA(Simple Moving Averages — SMA-9 and SMA-20) , whenever sma9 crosses up sma20 , we long or buy otherwise we sell or short when sma20 crosses up sma9.

TradingView Chart — BTCUSDT , showing Long and Short signals
TradingView Chart — Short and Long Signal

sma 9 — blue

sma20 — red

  • point 1 -> sma20 has crossed up sma9 , hence short
  • point 2 -> sma9 has crossed up sma20 , hence long

Let’s begin :

Go to Pine Editor tab on any chart on tradingview ->

TradingView Chart — BTCUSDT — Open PineEditor below the chart
Open PineEditor

and start writing below code ->

strategy(“sma-x”)sma9 = ta.sma(close,9)
sma20 = ta.sma(close,20)
plot(sma9 , “SMA9” ,color=color.new(#0000ff, 0), linewidth=3)
plot(sma20 , “SMA20”…

--

--

Sairav Dev
Sairav Dev

Written by Sairav Dev

Software Developer | Music Enthusiast | Trader

Responses (2)

Write a response