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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
//@version=3 study("Percentage Change Function", overlay=false) //////////////////////////// // Author: Backtest Rookies // Date: 24 April 2018 // // Description // ======================== // 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 // increases. // inp_lkb = input(5, title='Lookback Period') perc_change(lkb) => overall_change = ((close[0] - 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[1] lowest_low := i == lkb ? low : low[i] < low[(i + 1)] ? low[i] : lowest_low[1] 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) |
Commentary
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 close
at the end of the range. Some people may prefer to calculate from close
toclose
. 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:
I think this will work with volume aswell
Hopefully these fixes will help someone else, as there are errors since pinescript v4 came out.
Use these changes for v4:
float highest_high = na
float lowest_low = na
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=color.orange, linestyle=hline.style_solid, linewidth=2)
I missed an error in the plot lines. Change their colors statements to:
color=color.black
color=color.red
color=color.orange
Do you know how I could make a script detect the inclination of a moving average? I use multiple moving averages and want to identify strong trending moments using a steep incline