Programming your first TradingView indicator

Anyone looking into automated crypto trading, or been on crypto twitter or telegram has likely stumbled upon a TradingView chart. Sooner or later will be wondering how to write indicators and test their trading strategies, and that’s exactly what we will be exploring in some of our next articles.

tradingview-cryptocurrency-markets-charts-ticker-pine-script-automated-crypto-trading
https://www.tradingview.com

What is Pine Script?

If you looked into indicators and trading strategies on TradingView you probably have heard about Pine Script before. Pine Script is TradingView‘s programming language which can be used for programming indicators and even strategies, which later can be backtested.

You can find a complete manual for Pine Script on the Pine Script Language Reference Manual which looks overwhelming at first but after you get familiar with it it’s incredibly useful!

Creating your first TradingView indicator

Once you click “Chart” on the main menu you will be taken into the “Chart” page. Clicking the ticker on the top left will allow you to chose any market that is available on the platform.

tradingview-chart-ticker-btcusd-bitcoin-automated-crypto-trading
Select a ticker on TradingView chart e.g. BTCUSD

Clicking “Pine Editor” on the bottom will open a simple script editor, that’s where you will be able to write your indicators and even trading strategies.

Since this is our first indicator we will start simple, by creating a Simple Moving Average of our close price.

//@version=4
study( "My first indicator", overlay=true )

plot( sma( close, 20 ), linewidth=4, color=color.green )

After you click “Save”, TradingView will ask you to log in to save this indicator on your account. You can log in with any social network account and enjoy TradingView’s free basic plan.

Once you have your code saved, you can click “Add to Chart” and you should be able to see a new thick green line traversing the price.

The script we just wrote will be executed for every candle, on every candle we will be using the plot function from Pine script to plot the value of the SMA of 20 periods, using the “close” price as input for the moving average.

You can see more on Pine’s sma and plot documentation page.

tradingview-pine-editor-pine-script-indicator-sma-20
The green line represents our 20 periods SMA

The number of periods on the SMA will define how quickly the line will follow the price. Note how increasing the value ( for instance setting it up to 40 ) will make the line move “slower” while setting smaller values such as 5 will make the line move closer to the price!

tradingview-pine-editor-pine-script-indicator-sma-5
Changing the number of periods to 5 brings our SMA much closer to the price

Changing candle colours

Just for some fun, we will also change the candle colours, and paint the green candles purple and red candles blue!

//@version=4
study( "My first indicator", overlay=true )

plot( sma( close, 20 ), linewidth=4, color=color.green )

candle_color = color.white

if close[0] > close[1]
    candle_color := color.purple
    
if close[0] < close[1]
    candle_color := color.blue
    
barcolor( candle_color )

First, we create a variable called candle_color which we initialize with the white colour.

Later we use if conditions to check if the current close ( close[0] ) is higher than the previous close ( close[1] ), if the answer is positive, we assign the colour purple to the candle.

We then do the opposite, check if the close of current candle ( close[0] ) is lower than the previous close ( close[1] ), if that’s the case assign the colour blue to our variable.

And to finish our first TradingView indicator, we use the barcolor function to paint our candle!

tradingview-pine-script-indicator-change-candle-color
You can easily customise candlestick bar colours

That’s it for today, stay tuned for the next part of this Automated Crypto Trading series. In the next article, we will leverage what we just learnt and start writing a basic Trading Strategy on TradingView.

Have any questions about this article? Please leave a reply below!

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *