TRADINGVIEW_STRATEGY_OVERVIEW

Programming Your First TradingView Strategy

Following our previous post on creating your first indicator on TradingView we will go ahead and turn the indicator into a trading strategy.

Changing from “study” to “strategy”

The main difference between creating an indicator and a strategy is the first line of the code, we will no longer use “study” function to start our script, we will be using “strategy“.

On that first line we will be able to tell how much initial capital we want to use when backtesting our strategy. For this example we will go with 10,000 USD. We will also inform TradingView that we will be opening positions with 20% of our portfolio.

We will also set overlay=true in order to see our SMA plotted on the chart.

//@version=4
strategy(title="SMA CROSS", initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=20, overlay=true)

If you would like to know more about other arguments of the strategy function please check the official documentation.

Define your Entry and Exit Strategy

A simple start would be to BUY when the price crosses over our SMA and SELL when the price crosses under our SMA. To achieve that we will create two variables, one called crossed_over and another one called crossed_under.

We will set crossed_over to true if the current candle close price is ABOVE the SMA and the previous candle close was UNDER the SMA.

We will do exactly the opposite for our crossed_under variable.

sma_value = sma(close, 100)

plot(sma_value, linewidth=4, color=color.green)

crossed_over  = close[0] > sma_value[0] and close[1] < sma_value[1]
crossed_under = close[0] < sma_value[0] and close[1] > sma_value[1]

Opening New Positions

You can open new positions by calling the strategy.entry function. We will be using two arguments, the first one is called id which works as an identifier for our position, while the second argument is used to let TradingView know if we are going LONG or SHORT on the market.

This part of the code is quite simple thanks to the two variables we previously created:

if crossed_over
    strategy.entry("long", strategy.long)

if crossed_under
    strategy.entry("short", strategy.short)

Have a look at the official documentation to see all the possible parameters of the strategy.entry function.

Closing Previously Opened Positions

One thing to keep in mind is that before opening a new LONG or SHORT we want to close any previous position we might have on the market. For that we will be using the strategy.close function to close our SHORT position before we go LONG and also to close our LONG position before we go SHORT.

Note the added strategy.close function:

if crossed_over
    strategy.close("short")
    strategy.entry("long", strategy.long)

if crossed_under
    strategy.close("long")
    strategy.entry("short", strategy.short)

Have a look at the official documentation to see all the possible arguments of the strategy.close function.

Adding the Script to the Chart

Once you have the whole script in the “Pine Editor”, you can simply “Save” then “Add to Chart”. Here is the complete script we have written:

//@version=4
strategy(title="SMA CROSS", initial_capital=10000, default_qty_type=strategy.percent_of_equity, default_qty_value=20, overlay=true)

sma_value = sma(close, 100)

plot(sma_value, linewidth=4, color=color.green)

crossed_over  = close[0] > sma_value[0] and close[1] < sma_value[1]
crossed_under = close[0] < sma_value[0] and close[1] > sma_value[1]

if crossed_over
    strategy.close("short")
    strategy.entry("long", strategy.long)

if crossed_under
    strategy.close("long")
    strategy.entry("short", strategy.short)
Pine Editor – SMA Crossover Strategy

Now you should see your SMA on the chart and some arrows on the chart indicating your entries and exits.

Strategy Tester

Clicking on “Strategy Tester” will give you an Overview of the strategy performance. You can also see more detailed information on the “Performance Summary” and “List of Trades” tab.

Strategy Tester – Overview
Strategy Tester – List of Trades

Next Steps

If you look through the List of Trades you will notice that this strategy is great at profiting when the market is trending in a very clear direction, but not so good when the market moves sideways.

There are many ways we can try avoid those trades when the market is moving sideways. The possibilities are endless and that’s when the strategy gets more interesting – and complicated. We will be looking at some options in the next articles.

Another thing you will notice when looking at the “List of Trades” is that our strategy is backtested until 2013. In some cases we may want to test our strategies during specific periods.

We will be looking at those improvements in the next article. In case you can’t wait for our next article and want to learn more about Pine Script, feel free to jump directly into Pine Script TradingView programming articles on kodify.net.

If you have any questions or requests regarding our TradingView series, please get in touch with us at support@aluna.social or leave a comment below.

Leave a Reply