Tradingview: Code Snippet Percentage Change Function

This post contains a useful little code snippet that can be copied and pasted into your own strategies and indicators to easily calculate some interesting percentage change levels within a given lookback period. In other words, we won’t just look the change from the start to the end of the period, we shall also look at some levels which can give you a picture of what happened between the start and end.

This post is not for absolute beginners in terms of understanding the code. Having said that, beginners will still be able to take something away from this post since the code can be easily reused without needing to understand the inner-workings. For those who do want to dive a bit deeper, the code in this script touches on the following concepts:

The Idea

Knowing the price change from a given point prior to an event or trigger can help add an extra layer of confirmation or filter undesirable setups. For example, in a mean-reversion strategy, you might only want to consider a long setup if price has already dropped 5%.  In this scenario, we might look back over the last 10 bars to see if price dropped to our desired level. When we do this, we would just look at the difference between the start and the end of the range.

This is generally fine because most people are only interested in the change over the period. However, can be other useful information that we can take away from what happened during those 10 bars.  For example, let’s say you are looking for an 8% rise over the last 10 bars, well price might have risen gracefully from the start of the range to the end or it might have dropped 5% over 5 bars before rising 13% over the last 5.

The code snippet contained in this post attempts to take a first step in addressing this. In addition to providing the overall change between the start and the end of the range, the function shall also return:

  • The percentage change from the start of the range to the highest point in the range
  • The percentage change from the start of the range to the lowest point in the range

Using the 3 data points you will be able to see how price moved between the start and end of the period.

The Code


The commentary for this code snippet shall be short and sweet. To understand the fundamental concepts in the code, you can check out the tutorials linked above.

The bulk of the code is packed into a single function so it can be easily transferred to other scripts. The function will return the 3 values of interest. Notice that when the function is called that we must place our variables in a list [overall, to_high, to_low]? This allows us to assign the returned values to each variable contained in the list respectively.

As for the function itself, we simply calculate the overall change before looping through the range to identify the highest high and lowest low within the range. Once we have these two values, we are then able to calculate the maximum bullish and bearish movements within the range.

Finally, it is worth pointing out that this function calculates the difference from the open at the beginning of the range. In other words, it is calculated from the open at the start of the range to the closeat the end of the range. Some people may prefer to calculate from closetoclose. If that is you, you can easily make this change.

On The Charts

Although the script is intended to be used as a snippet which can be copied into other more complex strategies and indicators, it doesn’t look too bad as a standalone indicator!

Percentage Change Indicator on the chart

And here is how the plotted results relate to the price chart:

Percentage Change: Full Chart

Find This Post Useful?

If this post saved you time and effort, please consider support the site! There are many ways to support us and some won’t even cost you a penny. 

Backtest Rookies is a registered with Brave publisher!

Brave users can drop us a tip.

Alternatively, support us by switching to Brave using this referral link and we will receive some BAT!

Referral Link

Enjoying the content and thinking of subscribing to Tradingview? Support this site by clicking the referral link before you sign up!

Donate with PayPal using any payment method you are comfortable with!