Sometimes you need some Technical Analysis indicators for counting in your code. Maybe, you are interested in how many days ago something happened. An example could be a question like “how many days have we been above the moving average” or “how many days since we had a new high”. This post will show you an easy way to answer these kind of questions.
Bars since the last High
I would like to give you a small piece of code to show you how the programming is done. The question is: How many days has it been, since the market made its last 25 day high. The chart above shows you the result. The indicator always rises 1 per day if there is no new 25 bar high, and is reset to zero if there has been a new high.
Technical Analysis Indicator Code
Technical Analysis indicators can be written with just a few lines of code, like this one.
- First you would define the needed inputs and variables (line 1&2)
- Then define the indicator you want to test (line 6). As I want to know how many days it has been since the last period high, I use the built-in highest function highest(high,period) to calculate the highest high over the given period.
- Line 9 is doing the actual counting. Note the if..then..else construct. If there is a new high, then the counter is reset to 0, otherwise (else) it adds 1 to its previous value.
Finally you will have to plot the result to spark your inspiration.
Indicator usage in a Scanner
Instead of plotting the indicator on a single chart, you can also apply it to a market scanner. Open your favorite list of stocks, add the indicator, load the needed amount of data in your scanner and press start. The screenshot below gives you an idea of the current state of German stocks.
Deutsche Bank has not seen a new 25 day high for 128 days, is this the end?
Tradesignal® is a registered trademark of Tradesignal GmbH. Unauthorized use or misuse is strictly prohibited. Data by Thomson Reuters Eikon.