A step-by-step guide to code your first automated strategy in TradingView




TradingView is programmable through a language called Pine Script. This document is a beginner’s guide to learning how to write, execute, and backtest a simple trading strategy.



Financial activity:

Trading

Knowledge level:

Beginner

Reading time: 

12 minutes



Summary
Where to start
How to create a new strategy
The backtest
A little improvement
Conclusions



Where to start?

At this link, you can find the online manual of the language: Pine Script User Manual 4 documentation,


and here is the Reference guide.


If you want to learn to code indicators and automated trading systems, this book (“Learn to program automated trading systems for TradingView from scratch”) can help you to start.


We begin to become familiar with the development environment, namely the Pine Editor.


In the lower part of the window with the graph, choose the Pine Editor tab.


Pine Editor tab in TradingView - Screenshot by Author.


As you can see, there are already lines of code; I tell you right away that they are designed to help in the creation of a new indicator and not a new trading system.

Lines 1, 2, and 4 contain comments.

The content of these lines is not considered by the platform, but they serve us (humans) to get information about the code we are reading.

Line # 5 tells us it's an indicator called "My script" (the names I write in quotes are colored green).

Line #6 tells the platform to draw the closing prices on the chart (so this indicator draws a curve with the closing prices and that's it!). “close” is a Pine Script default variable (and is colored red) and “plot” is a command (and is colored blue).

Click on "Add to Chart" (it is in the menu at the top right, in the Editor window).

Now we should see the indicator drawn in the graph window.


The indicator named “My script” is plotted on the chart of TradingView - Screenshot by Author.



How to create a new strategy

We want to write a trading system, not an indicator (many are already on the platform)!


Let's close it by pressing the button with the "X" at the top right.


Into the “Open” menu choose “CREATE NEW / Strategy”.

We start to create a new strategy - Screenshot by Author.


A complete strategy already appears: it is a crossing of moving averages.



This is the code that appears when I choose to create a new strategy in TradingView - Screenshot by Author.


The first four lines are the same as those seen before.

Line #5 immediately presents a significant difference: “strategy” informs us that we are looking at the code of a trading system and not at an indicator.

In the round parenthesis of “strategy”, we find the name (“My strategy”), which is mandatory, and the command “overlay” (set equal to true) which allows you to draw the strategy on the chart (if I set it equal to false, the drawing disappears), “margin_long = 100” and “margin_short = 100” indicate the maximum margin that can be used in Long and Short orders, respectively.

Line 7 proposes the condition to enter Long (“longConditon”). It is equal to the intersection (“ta.crossover”) between the two moving averages (“ta.sma”) indicated in the round bracket.

The first moving average is calculated on the closing price series (“close”) with a period of 14, while the second is always calculated on the closing prices with a period of 28.

Note: "ta" means "technical analysis".

If we move the mouse over the single words, a help window will appear.

Line #8 tells us that if the "longCondition" is true (this last part is not written, but is taken for granted), then what is reported in line 9 must be done, that is to enter the market thanks to the “strategy.entry” function.

The order with which we enter the market will be called "My Long Entry Id" and will be a buy order because this is indicated by the integrated variable "strategy.long".

Everything we have said for the "longCondition" also applies to the "shortCondition" and it is not worth repeating (lines #11 to #13).



The backtest

Click on "Add to Chart" also for this strategy and the backtest will appear.

Backtest of “My strategy” - Screenshot by Author.

Please, watch the content of all four tabs (Overview, Performance Summary, List of Trades, Properties).



A little improvement

How can I easily change the calculation periods of the two moving averages?
The answer is this: let's add the inputs!

These are the lines of code to add to our trading system:

// Inputs:
lenght_1 = input.int(14, title="SMAfast period")
lenght_2 = input.int(28, title="SMAslow period")

First I specified that the inputs must be integers ("input.int"), then I indicated a name and a title for each variable.

So, in conclusion, by substituting variables for numbers, this is the complete input code:


I have added some inputs to “My strategy” - Screenshot by Author.


This is the window with the inputs that you can change, now.


Strategy inputs - Screenshot by Author.


If you change a value, the platform immediately recalculates all the backtest.



Conclusions

  • Pine Script is a powerful language that you can easily learn.
  • Learning this language, you can backtest your strategies and understand if they have worked or not in the past, so you can decide if it’s a good idea to try to use them in the future.

A sincere wish of good work to all!



Written by F. GRAMOLA (*).

(*) Member of S.I.A.T., the Italian Society of Technical Analysis (member society of I.F.T.A. – International Federation of Technical Analysts).



Warning

We merely cite our personal opinions for educational purposes only.

All trademarks are the property of their respective owners.

Investing and trading are risky. Don't invest or trade money that you cannot afford to lose.

The above trading system is intended for education and research only.

Initial Photo by Luke Chesser on Unsplash.




Post a Comment