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.
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.
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.
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!
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 > close candle_color := color.purple if close < close 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 ) is higher than the previous close ( close ), 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 ) is lower than the previous close ( close ), 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!
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!