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:
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.
study("Percentage Change Function", overlay=false)
// Author: Backtest Rookies
// Date: 24 April 2018
// This script provides a simple function that can be copied and pasted into
// either a study or strategy script. It will return the following percentage
// changes over the given lookback period (i.e range):
// - Percentage change from the start of the lookback period to the end
// - Percentage change from the start of the lookback period to the highest
// point of the lookback period. This could be any point in the range
// - Percentage change from the start of the lookback period to the lowest
// point of the range.
// Knowing the highest and lowest points of the range will help identify whether
// price was volatile during the lookback period or whether price moved steadily
// in one direction.
// The returned values will be negative for price drops and positive for price
inp_lkb = input(5, title='Lookback Period')
overall_change = ((close - open[lkb]) / open[lkb]) * 100
highest_high = na
lowest_low = na
for i = lkb to 0
highest_high := i == lkb ? high : high[i] > high[(i + 1)] ? high[i] : highest_high
lowest_low := i == lkb ? low : low[i] < low[(i + 1)] ? low[i] : lowest_low
start_to_high = ((highest_high - open[lkb]) / open[lkb]) * 100
start_to_low = ((lowest_low - open[lkb]) / open[lkb]) * 100
[overall_change, start_to_high, start_to_low]
// Call the function
[overall, to_high, to_low] = perc_change(inp_lkb)
plot(overall, color=black, title='Overall Percentage Change', linewidth=3)
plot(to_high, color=green,title='Percentage Change from Start to High', linewidth=2)
plot(to_low, color=red, title='Percentage Change from Start to Low', linewidth=2)
hline(0, title='Center Line', color=orange, linestyle=solid, linewidth=2)
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
close. 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!
And here is how the plotted results relate to the price chart: