Code Stationary Returns Indicator in TradingView — Step by Step

Calculating extremums detection indicator with Pine Script

Rick Hardy
Quant Factory
5 min readSep 6, 2022

--

Photo by Chris Liverani on Unsplash

Today we will be doing an improved version of calculating a stock returns. I’ve called it stationary as when done it seems like the returns got a stationary properties. For coding it, we will be using TradingView programming language called — Pine Script.

Prerequisites

For you to be able to use TradingView charts and to code in Pine Script you must create an account, which is completely free. After you click the Chart button on the home screen, choose any asset you like. The indicator will calculate based on the chart data of the symbol you are currently on. On the bottom of the screen, you will see a Pine Editor window, click on it and get ready to do some coding. If you haven’t registered for TradingView yet, you can use my referral link and get up to 30$ in credits.

The Stationary Returns Indicator

In Stationary Returns indicator, is a simple and yet can be very powerful tool. We will be looking for extremums in both positive or negative returns and try catch swings of the curve. We will starting with getting the stock mid-price as well as a rolling moving average of the mid-prices for a two week rolling window. Then to transform those into returns we will be using natural logarithm. After that we have to substract the returns from the rolling moving average of the returns. We will be then calculating this on a daily timeframe and further create one more moving average of the stationary returns and add bands, so that we can look for extremums and search for possible swings or mean-reversion.

Coding the Stationary Returns in Pine Script

Let’s first start with setting up the version of Pine Script. We will be using version 5, which is the latest one and has a lot of new powerful additions.

Then we have to set the name of our indicator which would be Stationary returns. We also have to set the overlay parameter to false, as the scale would be above and below zero. By settings it to false we specify that our new indicator will be in a separate window and it won’t mess up or chart.

Next we have to create two variables where we will store the mid-prices and the rolling moving average to use later for the returns formula.

We use the build in variable hl2 which stands for (high-low)/2 i.e the mid price of the candle. The ta.sma() stands for ta. (technical analysis package) sma(simple moving average), we are gonna use this to calculate our 2 week rolling moving average. In the brackets we specify hl2 which means that as a source we will use the mid prices and 10 stands for the length.

We then calculate the stationary returns, with log returns of the mid-prices from which we substract the log returns of the rolling window.

The math.log() is a build in function from the Mathematical library and log stands for Natural logarithm.

Now that we have the stationary returns function we have to calculate it on a daily timeframe. There is no point of calculating returns on an intraday data, as this indicator tries to capture multi-daily swings. For this we will use the request.security() function which is used of multi-timeframe or multi-symbol data usage.

The request.security() function requires at least 3 variables, the symbol, the timeframe and the expression we will be using. The first one syminfo.tickerid means that we will be using the symbol we are currently on. “D” stands for Daily Timeframe and the stationary_returns is the operation we want to run on the daily timeframe data.

The final step will be to create the moving average of the stationary returns, I’ve chose 20 as a length, but you can put here any number you want to use as a mean.

Now we will add the bands. They will help us with detecting potential extremums in the price. They will be input based, so you can easily change them within the Settings menu.

Only thing left is to plot the results and the bands and we are ready.

You can copy/paste the whole code directly in the Pine Editor to use the indicator yourself.

This is how the chart should look when you apply it to the chart.

If you want to create a custom indicator, but don’t have the time and energy to learn Pine Script, feel free to contact me through the link below. I am also a professional developer and I would love to help you out with your project.

https://www.fiverr.com/rickhardypro

Final Thoughts

Technical Indicators can be very powerful and help you a lot in your analysis. Through them you can get a valuable insights of a potential future moves and direction.

However, I would like to recommend you always follow the the below steps whenever you come across an investment analysis, trading technique or strategy:

  • Have a critical mindset about every analysis you make.
  • Make sure you have backtested it using real life simulation and conditions.
  • If there is good looking potential, run it through forward test.

That’s all, folks. As always, feel free to share your thoughts and suggestions. And if you would like to see more on investment analysis, algorithmic trading, quantitative techniques and Pine Script programming, just follow me to receive the most recent updates!

Cheers!

A Message from QuantFactory:

Thank you for being part of our community! Before you go:

  • Become A Quant Trader 👉 here.
  • Join our exclusive Stocks Newsletter 👉 here.
  • If you liked the story feel free to clap 👏 and follow the author
  • For more stories from the Algorithmic Trading and Quantitative Analysis world you should follow the Quant Factory Publication

*Note that this article does not provide personal investment advice and I am not a qualified licensed investment advisor. All information found here is for entertainment or educational purposes only and should not be construed as personal investment advice.

--

--

Rick Hardy
Quant Factory

Top writer in Investing, Finance, Data Science | Co-founder of Quant Factory