Getting started with Algo Trading using PineScript & TradingView

Getting started with Algo Trading using PineScript & TradingView

Based on so many requests we have received from our followers, we are starting a brand new series on PineScript. This will be the second series on Algo-Trading. This will be in addition to the Ami Broker Series, which is already running in parallel.

In this series, I’m going to walk you through the basic knowledge required in order to begin creating your own custom TradingView indicators, trading tools and trading alerts

Pine Script is one of the popular algo-trading option nowadays, especially due to it’s compatibility with tradingview.

In order to develop an algo-trading system, we need 3 important components.

1. Data Feed

2. A well defined Strategy

3. Script/Code and back testing

With Trading view, Item #1, which is the Data Feed is automatically taken care as it already has all the data you need fr all the markets. And most importantly, the data is clutter free and complete with all the adjustments done for splits, bonuses and dividends. So data feed is automatically taken care. We just need a strategy and it’s code, all the backtesting tools are available in the platform itself.

So, with that note, let’s begin PineScript Series. But first things first — let’s start at the very beginning. I’m going to assume you have zero experience with Pine Script.

When you open the Pine Script Editor for the first time, it will look like this:

//@version=4

study(“My Script”)

plot(close)

This is the default template for a basic indicator script.

There are two different script types you can choose to create. One is called a “study” (indicator), the other is called a “strategy” (which essentially behaves the same as a study, except that it allows you to enter and exit mock trades through TradingView’s backtesting system).

We will focus on indicators for now, as strategies require a basic understanding of indicators to implement and are far more complex.

 — — — — — — — -

//@version=4

The first line in the default template code represents a comment, indicating the version of pinescript we are using. Here we are using version 4. Pine Script is a few years old, so it has a few different versions. Each different version has slightly different syntax rules and built-in features. Script developed in some other version won’t be ully compatible with other version, so just careful about it and make the minor tweaks as and when you switch the versions. Also, always use the latest version. And for comments, always use double slash

As we are dealing with price action data (high, low, open, close, indicator values etc), complex scripts can often become very unreadable very fast. A well-written indicator often looks like pure gibberish to the untrained eye.

Using comments is the simplest way to include annotations in your code to explain what the code does — both for yourself, and for anyone else who you might want to read your code later.

The @ symbol flags this line of code as a special comment. These types of comments are not ignored by the compiler, but treated as a kind of meta-data. This comment is not a line of Pine Script code, but rather it tells the TradingView platform which version of Pine Script to use. So this line is extremely important.

So @version=4 in English means “Hey Compiler! Before you start, this script is using version 4 of the Pine Script syntax, so when you compile my code into computer-language, use Pine Script v4.0’s rules to do it.”

This may be a little bit confusing, and you don’t need to understand this concept completely just yet. Just be aware that if you do not include this line in your script, then you will not have access to the latest features of the programming language and as a result, you may run into errors and limitations.

 — — — -

study(“My Script”)

You can think of it like a header or a function which has set of codes.

When creating a strategy script, you would change this line to ‘strategy(“My Script”)’, which would change the capabilities of your script and the default features you can access (eg. allowing you to place mock trades).

What this code does?

It is telling the computer “Create me a ‘study’, and call it My Script”.

When you create new scripts, you can rename them by changing this text to whatever you like.

plot(close)

Now this is where the actual script begins.

In this line of code, we are telling Pine Script to plot the close of the current candle onto our chart. The script will run on every tick for the current candle and every historic candle, so by default, this will plot a blue line on your chart connecting all the candle closes (essentially drawing a price line chart).

This is the best part of Pine Script — how easy it is to paint information directly onto your charts. With just three simple lines of code, we already have the foundation of a simple indicator.

With just a few adjustments, you can make this paint the close of a different timeframe, the high or low of the past 100 candles, or any specific price action data you’d like to see on the screen. You can change the drawing color, the line thickness, and even draw certain symbols on your chart.

Once you have written your code and you want to test it out, click the button ‘Add to Chart’ and TradingView will save your script’s source code and then draw your script on the chart.

It’s that simple to get started. Welcome to Pine Script!

Check Out Script Library to get script used for this episode:
https://marketsecrets.in/script-library/

To know more, checkout:

https://youtu.be/UyvOxkSOYik

 

Leave a Reply

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

error: Content is protected !!