Creating and Testing a Algo (or) Strategy in TradingView using PineScript

Creating and Testing a Algo (or) Strategy in TradingView using PineScript

Sept-Creating and Testing a Algo (or) Strategy in TradingView using PineScript

At the start of the series, I mentioned there are 2 types of pine script.

1. Study

2. Strategy

Study is simply indicators, where as strategies are the real core which can be used for algo-trading.

Whatever we have seen in the last 4 episodes are studies. Now, let’s see how to code a simple strategy in this episode.

To create a strategy, we swap out the study declaration with a strategy declaration.

strategy(“episode 5”, overlay=true)

The first thing we will want to do is create two moving averages and assign the data to variables.

shortSMA = sma(close, 10)

longSMA = sma(close, 30)

We will also create an RSI indicator that will be used to confirm our entries and exits.

rsi = rsi(close, 14)

This strategy will be run on the main chart so we don’t need to use the security() function here.

Next, we want to specify our crossover conditions. Fortunately, TradingView has a built-in function for that already, so we don’t need to code it manually.

longCondition = crossover(shortSMA, longSMA)

shortCondition = crossunder(shortSMA, longSMA)

We have two conditions, the first one is when the short SMA, the 10-period, crosses above the longer 30-period SMA.

The second condition is the opposite as we’ve used the crossunder function as opposed to crossover.

Both these conditions are saved to variables. So when the crossover or crossunder occurs, these variables will get updated to True which is a Boolean value.

We can use an if statement to check if the condition is changed to True, and then execute a trade based if that is the case.

if (longCondition)

strategy.entry(“longtradeN “, strategy.long, 100, when = rsi > 50)

The built-in strategy.entry function is used to enter trades. Here are the parameters that are passed into the function.

1. “longtradeN” — this is a trade ID. We won’t be using it in this example. But, if you plan to close or cancel a trade, it can be done with this ID.

2. strategy.long — this is a built-in variable that tells Pine script that we want to get long.

3. 100 — the number of shares we want to trade

4. when = rsi > 50 — this is an additional parameter that tells pine script to only execute the trade if the RSI is higher than 50.

The syntax for our short entries will follow a very similar format.

if (shortCondition)

strategy.entry(“shorttradeN “, strategy.short, 100, when = rsi < 50)

Since we are running a strategy, we don’t have to plot anything or specify an output.

But we will do so anyway. It would be nice to see the SMA’s on the chart so that we can confirm that trades took place when they should have.

So last step is to plot the SMAs.

plot(shortSMA)

plot(longSMA, color=color.black)

If we save and add to chart, the strategy will run and automatically open the Strategy Tester window which will display some important stats.

This is what our screen looks like.

By default, a new tab opens showing the overview stats for the strategy. You can click through the Performance Summary or List of Trades to see other statistics.

The strategy will run on the time frame that is displayed on your chart.

You can easily cycle through different time frames using the time frame options in the menu at the top of the screen. The strategy will auto-update based on the new time frame chosen.


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

To know more, checkout:

https://youtu.be/8oVaG6VyL1E

 

Leave a Reply

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

error: Content is protected !!