Commodity Investment Strategies with TradingView and Pine Script: Backtesting and Rule-Based Approach

Commodities are difficult to trade profitably, with few strategies offering any statistical edge. Despite this, finding effective commodities strategies might be worth the effort, as they allow for profits that are uncorrelated with those of other strategies and tend to work well in times of geopolitical risk and upheaval. Every trader should consider adding a few commodity trading strategies to their portfolio. Can you find commodity investment strategies using TradingView?

Yes, the extensive built-in tools in TradingView offer indicators and ready-made strategies that might be useful for traders and investors. Using TradingView, we were able to find some promising commodity investment strategies.

TradingView is a platform many traders favor due to its extensive range of tools. While QuantifiedStrategies has no association with TradingView, we recognize its popularity among traders and plan to write several forthcoming articles exploring its practical use. This article examines how to utilize TradingView’s tools and their native Pine Script to discover (profitable) commodity investment strategies.

Related reading: 200+ Investment Strategies

No strategy in this article is recommended by us. The article is only meant as an example of utilizing TradingView and its in-built strategies. Using the methods outlined in this article, we were able to find several profitable commodity investment strategies, but it remains to be seen if they held up for live trading and investing.

(Our previous article on Commodity Trading Strategies will give you a comprehensive introduction to commodity trading in general and is worth reading before returning to this one.)

Finding and organizing commodities on TradingView

The first thing we need to do in order to get started with commodity investment strategies on TradingView, is to create an organized list of commodities which we can then research further.

For this, we will make use of TradingView’s Watchlist function. Click the ‘Create new list button, located in the panel to the right:

Finding and organizing commodities on TradingView

We’ll name this list “Commodities” (or any other title you may like).

Once done, we’ll need to add the commodities one by one by  pressing the ‘Add symbol’ button in the top right of the watchlist:

Finding and organizing symbols and tickers on TradingView

This opens a search interface, which looks like this when we search for our first commodity, Gold:

Symbol list on TradingView

Keep in mind that column two shows whether the ticker/symbol is futures, stock, CFD, or ETF. That is important info! In this case, the Gold symbol is a CFD, not the futures contract.

For each commodity you keep adding, you should look at the price history of the symbol you add to ensure that it has price data for several years. In the case of gold, we can safely add the topmost option, as TVC gives us price data going all the way back to January 1833. Symbols by data providers Pepperstone or TVC usually work well for other commodities.

Keep adding symbols until you have all the commodities you need. A more or less complete watchlist of tradable commodities should look something like this:

Complete list of commodities TradingView

Other commodities are also available on some exchanges, but be aware that these are often difficult to trade profitably.

Backtesting successful commodity investment strategies on TradingView

Now that we have our list of commodities, we can use TradingView’s built-in tools to do some simple backtesting. It is important to note that we are still in the idea stage here, and every trading possibility we find at this point would need further consideration before trading live. You might divide your data into an out-of-sample period, or even better keep your strategies in incubation for at least 6 months, preferably 12 months.

TradingView comes with a large set of pre-made strategies that let you backtest in a plug-and-play fashion. These strategies are all modeled in TradingView’s coding language, which is called Pine Script. In future articles, we will show how to make simple changes to a strategy in Pine Script in order to enhance backtesting further. But for now, let’s stick with the basics.

Where to find strategies for backtesting on TradingView?

To find strategies we can backtest in TradingView on our various commodities, we first click on ‘Indicators’.

Where to find strategies for backtesting on TradingView

We then click on ‘Technicals’:

Indicators, metrics, and strategies in TradingView

And finally, clicking ‘Strategies’, bringing up the following window:

Backtesting successful commodity investment strategies in TradingView

Clicking on any of the listed strategy scripts will automatically display the results for this strategy for the current symbol and time frame, covering a significant portion of the available data range.

We’ll get into the trading logic behind these strategies later. For now, let’s make sure that we view gold on a daily time frame and add the first strategy we find, the ‘BarUpDn Strategy’. Before we bother reading the results, we’ll make some simple changes by opening the input interface:

Default strategies in TradingView

Then we click on ‘Properties’, and change Order Size to 100% of equity:

How to backtest using TradingView

This allows us to see the fully compounded end result of the trading strategy. Just ensure you don’t trade with anywhere near 100% of your full equity in live trading.

You might notice in the background at this stage that the results of the strategy on gold are looking disastrous, mildly speaking.

We head on over to ‘Inputs,’ where it turns out that the BarUpDn Strategy is set to ‘Max Intraday Loss: 1%’. This, in effect, places a very tight stop loss on our strategy. We change it to 10, which is a more suitable number for this sort of research:

Setting stop loss in TradingView

You will be coming back to the ‘Inputs’ menu many times as you go through the various strategies, symbols, and time frames. Don’t be afraid of experimenting with different inputs, as this is part of the backtesting process. Just be aware of curve fitting, especially if using a strategy with many modifiable inputs.

Easy backtesting of strategies on TradingView

We can now finally look at our backtesting results for gold on the daily timeframe, using the BarUpDn Strategy:

Easy backtesting of strategies on TradingView

The results for gold are not great. But no worries, this is where the fun starts. Almost without noticing, we have now access to at least four variables we can play around with to seek better performance. We can:

  • Change the backtesting strategy we are using.
  • Change the inputs for the strategy we are using.
  • Change the commodity we are looking at.
  • Change the timeframe we are looking at.

These four variables create nearly limitless combinations for backtesting commodity strategies, all easily accessible through TradingView’s backtesting functionality.

For now, we will stick with the BarUpDn Strategy with its current inputs, on the daily time frame, and see if other commodities yield different results.

This is where the watchlist of commodities we previously created comes in handy, as it makes it easy-peasy to scroll through the whole list of commodities, enabling us to take in the results of the current strategic configuration at a glance.

Going down the list this way gives us many combinations that simply don’t work. If you doubt that most strategies have zero trading edges across most traded symbols, this process will serve as a wake-up call.

Then we get to CORN, and something happens:

Backtesting on TradingView

In corn, we find a combination of symbol, strategy, time frame, and inputs that seems to work right off the bat.

This combination has many of the requirements we often discuss here on QuantifiedStrategies, such as a good Win Ratio (Percent Profitable), a good Profit Factor, and a low Drawdown. In other words, it could have potential if we choose to develop it further.

Trading is at least 80% backtesting

Now, this doesn’t mean that you stop backtesting as soon as you stumble upon your first profitable configuration. Keep exploring, varying the four variables in all combinations you can think of, and have fun while you’re at it. Keep in mind that trading is at least 80% backtesting and research, and your chances of success increase dramatically if you can find some measure of happiness in doing the work involved.

This concludes our introduction to commodity investment strategies using TradingView. In future articles, we will be further exploring potentially profitable trading strategies we have found using this method, and the trading logic behind them. We’ll round off this article by exploring the trade logic for the strategy we just found in corn.

BarUpDn Strategy Trading Logic on TradingView

When we use a pre-made strategy in TradingView, the underlying Pine Script calculates everything for us, giving us immediate results. However, regardless of whether we want to do our trading manually or automatically, we need to understand the underlying trade logic.

To find the trade logic for the BarUpDn Strategy, we can use the ‘Show Description’ function:

BarUpDn Strategy Trading Logic on TradingView

From here, we can easily read the logic in plain English. Simultaneously observing what goes on in the price chart, we get the following trading rules:

  • If the current bar is green and opens above the close of the previous bar, we enter a long position on the open of the subsequent bar.
  • If the current bar is red and opens below the close of the previous bar, we enter a short position on the open of the subsequent bar.
  • If the Max Intraday Loss (%) in the input interface is reached, we exit the position immediately.

It is also important to observe what is actually going on in the price chart, and whether the trades taken through the strategy match your understanding of its logic. Luckily, TradingView makes this easy for us, by displaying the entries and exits in the price chart:

TradingView displays your buys and sells

Blue arrows indicate long entries (BarUp), while red arrows indicate short entries (BarDn). By reading the trade logic and trading rules and comparing it to the chart above, you will be able to understand why each entry occurs where it does. It is important to stick with this until you genuinely understand what is going on, and to never trade a strategy you don’t understand.

BarUpDn Strategy on Corn on TradingView

With our new understanding of how the BarUpDn Strategy works, we can also improve our understanding of how it performs on corn specifically, and whether any optimizations need to be made.

The BarUpDn strategy on corn has a few apparent flaws, but we need to keep in mind the relatively low number of trades, as we only use data for the last two years only. This, combined with a relatively low Sharpe Ratio of 0.4, means that these results could result from random chance. On the other hand, if the edge is real, we have the chance of entering into the strategy at an early point, which is important due to the fact that all trading strategies stop working eventually.

Let’s try some backtesting optimization to see if we can get better performance. One thing to notice is that in the current iteration of the strategy, no trades are exited due to our Stop Loss being triggered. This happens because we set the Max Intraday Loss (%) to 10 earlier, which was a high number. By varying the Max Intraday Loss (%) in the input interface, we can change the outcome of the strategy. Experimenting with various numbers, we get the following result if we set it to 5%:

Trading optimization on TradingView

Is this better than the last iteration? Net profits and Profit Factor have both gone down, but so has Max Drawdown. This is perhaps a slight improvement, especially considering that the lower drawdown will probably lead to fewer behavioral mistakes.

What else can we do to make this strategy better? Changing the stop loss mechanism to something like a Trailing Stop Loss or an ATR-based Stop Loss comes to mind. We could also add a filter, such as the ever-popular 200-period Exponential Moving Average, to make sure that we only take long trades in an uptrend, and only take short trades in a downtrend.

Doing any of these optimizations would require us to do some simple programming in Pine Script, but this is beyond the scope of this article, so we will leave it at that for now. Future articles will deal with these aspects.

In the meantime, we encourage you to go backtesting for good commodity investment strategies on TradingView.

FAQ:

Why is TradingView recommended for finding commodity investment strategies?

TradingView is a popular platform among traders, offering extensive tools for technical analysis. It’s recommended for finding commodity investment strategies due to its built-in indicators and ready-made strategies that can be backtested. Traders can utilize TradingView and its in-built strategies.

Why should every trader consider adding commodity trading strategies to their portfolio?

Commodity trading strategies provide an opportunity for profits that are uncorrelated with other strategies. They also tend to perform well during times of geopolitical risk and upheaval, offering diversification benefits.Commodities, especially tangible assets like precious metals and agricultural products, have historically demonstrated resilience against inflationary pressures.

How can I backtest commodity investment strategies on TradingView?

The key variables include changing the backtesting strategy, adjusting inputs, exploring different commodities, and varying timeframes. These variables offer numerous combinations for backtesting commodity strategies. Use TradingView’s built-in tools for backtesting. The platform offers pre-made strategies in Pine Script, allowing users to easily backtest various combinations of strategies, symbols, time frames, and inputs.

Similar Posts