- Pine Script is a free cloud-based forward and back-testing tool for investors
- The Pine Script library has 100s of built-in strategies like the moving average to be used in seconds
- Pine Script is a “high level” programming language, meaning that it reads and writes like normal English
- This is the best language for non-coders who want to run complex tests on investments
Pine Script Is a Free Back-Testing and Forward-Testing Tool That You Can Start Using in 5 Minutes
Trading View has designed its scripting language called Pine Script. It allows users to create customized indicators and run both forward and back-testing on a cloud server. The goal is for accessibility and simplicity.
You Can Use Pine Script for Free
- Go to tradingview.com (click here)
- Sign up for a free account
- Click on the “Charts” button
- Click on “Pine Editor” at the bottom
Pine Script is the Simplest Back-testing Tool to Use
The script is always being updated by the developers and is currently at version 4. You can open a blank Pine Script editor, which is just a web application that allows you to enter or alter the text. By selecting “Pine Editor” on any chart and then “New” then “blank indicator script”, the script below will now automatically appear in your text box:
- study(“My Script”)
Line 1 Defines What Version You Are Using
Line 1 of the code tells the compiler, the program that turns your instructions into machine code, to use version 4. This helps the computer decode your writing and execute it onto a chart. There are currently 4 versions of Pine Script and all are incompatible with each other. Therefore, it is important to state what version you are using. For this guide, we will use version 4.
Line 2 Tells the Computer Whether You Want to Plot a Strategy or Run a Strategy
Line 2 of the code describes the computer that you plan to do with the code. Pine Script can plot a strategy, run testing, and even execute a strategy automatically from online brokers. “Study,” tells the computer that you only plan to plot a strategy. In this example, we will not be able to run any testing or execute a strategy. When we want to run a strategy, we replace “Study” with “Strategy” and add a couple more lines of code.
Inside the parentheses inline 2, we also see the word “My Script”. The double quote here tells the computer that this is a string. Strings are just used to represent text! Computers can’t tell when you are trying to name something, and this helps it figure things out. You can also use a single quote and achieve the same result. On the chart itself, you will see a blue line tagged with the name “My Script”.
Line 3 Is a Mini Program That Plots Whatever Is in It
Line 3 plots the strategy. Here, the plot function “plots” the candle’s closing price on the chart. A function is defined by a set of parentheses. Functions are important because it is a mini-program that allows you to put something in and split something new out:
We Can Back Test a Moving Average Strategy in Minutes
Pine Script has many built-in functions, so it is easy to test common strategies like simple moving average in just a few minutes. We can open a new Pine Script Editor by selecting “Blank Strategy”. This plots a Moving Average strategy on our chart that we can run backtesting. Make sure to remove our previous indictor by right-clicking on top of the current blue line and clicking “remove”. The new line of code in our editor will be:
strategy(“My Strategy”, overlay=true)
longCondition = crossover(sma(close, 14), sma(close, 28))
strategy.entry(“My Long Entry Id”, strategy.long)
shortCondition = crossunder(sma(close, 14), sma(close, 28))
strategy.entry(“My Short Entry Id”, strategy.short)
We can test this strategy right away. First click “Add to Chart”, and then click the “Strategy Tester” tab. You will notice our Net Profit was less than $10. When you scroll down, you will also see a “Buy & Hold Return” of 45,000%.
The reason we see a low Net Profit is because, during each “Buy” signal, we only purchase one contract. You can change this by hovering over the strategy named “My Strategy” on the top right corner of the screen. A gear icon will appear. You can click the gear icon, which represents settings, and change the order size to X number of contracts or a % of equity. You can also change the initial capital here too. Play around with the settings and if you make a mistake, you can just open a new blank strategy.
Line 1 and Line 2 Are Almost the Same as Before
We still have version 4 of Pine Script initiated. But now we use the “Strategy” function instead of the “Study” function. “Strategy” allows us to run backtesting! You can see all the tools we can use from the “Strategy” function by hovering our mouse above it. You can use tools like “Overlay” here. “Overlay” plots our indicator on top of our stock candles.
In our default example, “Overlay” is already included but if you wanted to add it, you just have to follow the same format listed in the default example. First, let the computer know what tools you want to use. In this example, we type “overlay”. Then we add an equal sign and type “true”. True is a Boolean statement or an expression that can only be true or false. The application is Boolean expression is imbedded in every programming language. We use it here to tell the computer that we want to move our plotted moving average directly on top of our chart. Feel free to remove it by changing it to “false”.
Line 3 and 4 Tells Us to Buy When the 14-Day SMA Is Greater Than the 28-Day SMA and Short When the Opposite Occur
We will examine line 3 here since it is the same concept as line 4. We first defined a variable called “LongCondition”. “LongCondition” is just a name and you can name it whatever you want but when you change it you must change it in all its instances or else the program will break. “LongCondition” is equal to “Crossover”, another built-in function. Crossover lets us define a particular instance when 14 day SMA or greater than 28 days SMA. Here is the definition of SMA:
Simple Moving Average Is Just the Average of the Last X Days
The simple moving average (SMA) is the average price of the most recent X number of days. For example, a 2-day moving average would look like this: if the price is $100 today and $50 yesterday, then the simple moving average for today would be ($100 + $50 / 2 days) = $75.
Traders often use SMA as a signal to enter or exit a trade. When the faster SMA (2 days) crosses over the slower SMA (10 days), traders enter a trade. They believe that the trend is now changed because the more recent average price is greater than the longer period average price. The formula is listed here below:
(P1 + P2 + P3 + P4 + … + Pn) / n
n=the number of periods
Pine Script has the SMA function Built In
The beauty of Pine Script is that we do not have to code this function ourselves. With Python, even something as simple as running SMA on stock can take a lot of time. Here we just need to add 2 things into the SMA function: “What price to use?” and “How many days do we want to average?”. “Close” is a built-in variable that represents closing candle prices. 14 here represents that we want the average of the last 14 days.
Strategy.Entry Executes a “Buy”
We use an ‘If” statement here to decide when to buy. “If” statements are logical operators that execute when a requirement is met. The logic is the same as an “If” statement in Excel. The requirement here is our 14-day SMA crossing over our 28-day SMA. And our executable action is the strategy.entry function, which is named “My Long Entry Id” and longs or buys because of the “Strategy.long” variable. In total, we told the computer what a long condition looks like. We ask it to execute if that condition happens and executing our conditions means to buy a stock. The same logic can be applied to line 4. In just a couple of lines of code, we can backtest a moving average strategy!
You can play around with Pine Script and learn more about its potential use. We will release more articles about it in the future! The language itself is similar to Python and has hundreds of built-in indicators to help new coders learn to invest smartly.