Tradingview: First Script

Tradingview Pine Script Beginner Tutorial:

Tradingview is fast becoming one of the most popular charting tools in the industry. With its’ easy to use drawing tools, indicators and social network integration, traders have a complete set of tools to perform technical analysis and share ideas. In addition to this, Tradingview have also developed their own scripting language that allows traders to create custom indicators and develop strategies.
Before I go into the code, first things first. If you have not already, I suggest you sign up for an account at the Tradingview website. Once you have an account you will be able to save your scripts.
When I started to take a look at Tradingview for backtesting one thing that immediately struck me is how simple the platform is. This makes it an excellent choice for us backtest rookies to get started. Not only is the pine script language quite simple to understand, Tradingview also make accessing help information a breeze.
For example:
  • Mouse over functions for help. Provides access short form help information.
  • Builtin version control. Every time you hit the save button, Tradingview saves the script as a new version. This makes life very easy if you accidentally go too far off track and need to revert.
  • CTL + Click to see detailed documentation (option + click for Mac users). One nice thing about the popup help window is that you can still type in the script pane behind it. No need to constantly open and close the help screen as you work through input parameters of a function.
Gallery (click on an image to enlarge it)
One negative I noticed (at the time of writing) is that I could not find an option to print strings to the output console. This can make debugging a little tricky. Especially in instances where you want to check the value of a certain variable.

The Platform

If you are completely new to trading view, it would be helpful to go over where everything is and how to access the pine script code editor.
First load up a chart:
  • Open Tradingview (Use this affiliate link if you want to be kind – http://tradingview.go2cloud.org/SHpH)
  • Click on chart on the home page
  • If this is the first time you have used the charting feature, you will be taken to a chart. If not, you will get a drop down menu with an “unnamed” chart template. If that applies to you, click on that.
 Tradingview homepage
  • The pine editor pops up from the bottom of the page. Click on the pine editor tab.

Image showing the main chart area on trading view and highlights where to select the pine script editor.

When creating a strategy, do not start from the default pine script page. This template is intended for indicators. You can get a better template for strategies. The default indicator template looks like this:
 Shows the default indicator template on tradingview
As you can see it is a bit spartan. To get a strategy based template, click on “New –> Blank strategy script“. This will provide a basic template to get you up and running.
Highlighting which options for select in order to create a new blank script.
This will give you a code template that looks like this:
 Image showing the default strategy template on tradingview.
You can see this actually gives you everything you need to run a script:
  • The strategy is initialized
  • A trigger to go long is provided
  • A trigger to go short is provided
If you want, you can go ahead and press the “Add to Chart” button and consider yourself done. However, if you want to go just a little further and make some edits, keep reading on.

The Strategy

This strategy we are going to implement follows the same rules as in the Backtrader: First Script. I have done this for consistency and so that those still deciding which platform to select for backtesting can have an apples to apples introduction comparison. It is a super simple long only strategy that aims to stay on the right side of a long bull market in equities.
Entry
  • Long Entry when RSI < 30
Exit
  • Long Exit when RSI > 70
 
Trade Management and Position Sizing
  • No trade management shall be implemented. No scaling in / out. Just simple buying and selling with a single open position at a time.
  • Position size wise, we will keep things simple and keep the Tradingview default which is to buy and sell 1 contract.
Indicator Settings
  • Period = 21
  • We will use a longer look back period than the default 14. In theory this should result in less false signals and price should have to come down / rise much further before it is considered overbought / over sold. 

 The Code:

Look how short and sweet that code is! If you compare this to the Backtrader: First Script, you will appreciate just how much easier it is to get up and running! What you give up in features and control, you get back in simplicity and time.
 
If you compare it to the blank strategy template shown above, you might notice the following changes:
  • The short entry has been converted to a close function so that we do not go short (in accordance with the strategy rules)
  • The SMA crossover functions have been changed to check whether an RSI indicator is above or below a certain level.
  • The if statement for the long condition has been extended.
First lets take a little look at the Initialization of the strategy and talk about another really nice feature. There are very few mandatory parameters needed when setting up the strategy. In fact, only the title is required and this is so the strategy can be saved. All of the strategy parameters can be edited visually via a form in the strategies tab. Pretty nice right? Well it gets better, the parameters can be adjusted here at anytime and the results in the performance summary automatically update as you make the changes.
During the initialization of the strategy, I added some optional parameters:
These option parameters (initial_capital and currency) set the default values you will see when pressing the configuration button in the strategy tester panel:
Image showing the strategy configuration option and how they relate to the parameters set in the script.
Note in the above image the values for Initial Capital and Base Currency.
Another change I made, was to create some variables that are used to assess whether the year is above or below a particular number. I did this as I prefer to focus on particular periods of time when back testing so that a long profitable run does not mask a period of serious under performance. It is a personal preference. I would not like to sit through years of under performance even if a strategy works over 30 years! 😃
What is happening here? 
Each time a new candle is made, a check is done to see if the year is greater than 2014 or less than 2016. If it is, the Boolean value “true” is saved to the fromYear and toYear variables. This can then be used in the IF statement to check whether we can consider opening a trade:
We are checking to see IF the the RSI is below 30 AND the date is after 2014 AND the date is before 2016.
The closing of the strategy is more simple. It just waits to see if we are above 70 and closes it.

Using this code on Tradingview

Copy the code then click here to open up a chart window on Tradingview. Paste it into the pine script editor, Now you can click on the add to chart button in order to start the test and see visually where the strategy bought and sold.
Don’t forget to click save, so you can come back to the strategy later! You can apply this strategy to any instrument. All loaded strategies appear in the strategies tab on the bottom pane of the charting area. To load strategies, you can select “open” from the pine editor tab.

The results

Time for the grand results……..A net profit of $10.56! Not so impressive, but that is not the point. We are now setup and backtesting on Tradingview!
pine script beginner tutorial results
Have a play around with the code, change the indicators, change the strategy parameters and get used to the platform. I hope this post helps in getting you started!