In Trading View

Trading View provides a great way to write up custom indicators and back test your strategies. With their custom created language “Pine Script”. You can write up a strategy within minutes and back test it on years of data.

Before starting, you should have a basic knowledge in a programming language. Although not too similar, Pine Script reminds me most of Python.

If you’re just getting familiar with Pine Script; here is a great source provided by Trading View going over built in Functions, Variables and language operators: Link

Lets write our first Algorithm and back test it!

Algorithm can be viewed and tested in the link bellow:

Source Code

Overview

  1. Name your strategy
  2. Build your indicators
  3. Create buy and sell conditions
  4. Configure entry function

To start writing, login to your trading view account and open up a chart. On the bottom of the page you will see a few tabs

Click on “Pine Editor” to begin writing your script. This will open up a code editor.

1 – Name your strategy

Every back test needs to have have some basic parameters. The first is the strategy() function.

This function takes in multiple parameters but we will only add the name of our strategy. Now when we execute the code, Trading View knows to run this as a back test

2 – Build your indicators

Now we will need to add some indicators that we can use to write some conditions.

Here we set the variables “ema20” and “ema50” to the the ema() function based off the close price and with the time series of 20 and 50. This simpley gives us a 20 and 50 moving average that we can now use in our strategy.

3 -Lets create some buy and sell conditions

Now we need to set some conditions that we will call long and short. When the long condition is true we will buy and when the short condition is met we will sell. Seem simple enough?

Our “long” condition is true when the 20 EMA is above the 50 EMA and vice versa.

4 – Configure entry function

These functions will place our orders for us, strategy.entry(). We will add the following parameters to these functions:

  • name
  • long or short
  • purchase size
  • condition that activates function

we will set up two of these for our long and short positions

5 – Configure exit function

The exit function is very simlar to the entry function. it just needs to know wich position to close and when. So its first parameter is the name of the entry strategy and the second is the exit condition. For the exit condition we will close the long position when the short parameters are met and buy the close the short position when the long parameters are met.

Here is our full backtest in 13 lines and 5 steps.

Now lets run the strategy and see the results!

Once you completed you strategy you can click on “Add to chart”, it’s right next to the save button on the top right of the pine editor.

This will run your algorithm and give you the results in the “Strategy tester” tab. Here you can rotate between 3 pages (Overview, Performance Summary and List of Trades). You can view all your stats and every trade that strategy took.

You can also view every trade your strategy took right on the chart! I look at this frequently to confirm my strategy took every trade it was supposed to.

 
Contact Us

We're not around right now. But you can send us an email and we'll get back to you, asap.