Renko Brick Size Optimization

Sergey Malchevskiy
Towards Data Science
8 min readMar 31, 2018

--

Subscribe to our Telegram channel for more insights into the world of trading.”

Hi everyone!

I carry out research on financial time series. We work on algorithmic strategies problems in stock and crypto markets. The Renko chart is very popular with my clients as a tool for representing financial price data.

Today, I’m going to overview a research about how to do a noise reduction in financial time series using Renko chart. The purpose of the article is an answer to this question “Is there an approach that is better than the well-known ones to determine an optimal brick size?”.

What is Renko chart?

Renko chart is chart type that is only concerned with price movement, time and volume are not included. Renko chart doesn’t show each price movement. You should set these parameters before Renko chart building:

  • What brick size of a chart to choose that represents the magnitude of price movement? If a brick size is small, chart has more movements, but this chart is noisier.
  • What type of price will be used to build a chart (e.g. open, close, etc.)? Also, a timeframe should be chosen, it can be days, hours, minutes, and ticks. Tick data are more accurate because movements are not missed.
Renko charts built on different brick size

The basic principles of Renko chart building:

  • New brick is built only if the price movement has exceeded the specified threshold level.
  • The brick size is always the same. For example, if the size is 10 points, and the price has increased by 20, then 2 bricks will be drawn. If the price increases, the brick will be green (light), if the price falls — red (dark).
  • Next value of the chosen variant of the price is compared with the maximum and minimum of the previous brick. The new rectangle is always displayed to the right of the previous one.
  • If the price exceeds the top of the previous brick by the brick size or more, draw the corresponding number of green (light) rectangles that the lower border of the new brick corresponds to the upper boundary of the previous one.
  • If the price falls below the bottom of the previous brick by the brick size or more, draw the corresponding number of red (dark) rectangles that the upper boundary of the new brick corresponds to the lower boundary of the previous one.

This article on Medium by Chartist Ranga contains an introduction to Renko charts.

In the next parts of this article, the analysis will be carrying out using pyrenko module that I implemented.

Existing approaches to determining a brick size

  1. Traditional. This approach uses a pre-defined absolute value for brick size.
  2. ATR. This approach uses the values generated by the Average True Range (ATR) indicator. The ATR is used to filter out the normal noise or volatility of a financial instrument. The ATR method “automatically” determines a good brick size. It calculates what the ATR value would be in a regular candlestick chart and then makes this value the brick size.

The first ATR value is calculated using the arithmetic mean formula:

Quality estimation and score function

It would be good to measure a quality of Renko chart. Let’s assume a simple strategy which uses these trend following rules: your position should be “long” while the current brick is green (light). Change to the “short” position if the color of brick has been changed. Using this logic, we have the following parameters:

  • balance is a sum of positive and negative deals. If the current brick has the same direction as the previous one, the balance value should be increased by +1. If the direction has been changed, the balance value should be decreased -2. Positive value is good. Greater value is better.
  • sign_changes is a number of that how many times a trend has been changed. Less value is better.
  • price_ratio is a ratio of a number of original price bars to a number of renko bricks. Value greater than 1 is good. Greater value is better.

The score function tries to integrate these parameters into a single value. Positive value is good. Greater value is better.

If sign_changes is equal to 0, it should be set to 1 before calculation.

Let’s try to simulate and visualize this score function in 3D space. Size of sphere corresponds to a score value. Red spheres have score equals to -1 value.

Import the modules and packages:

Imported modules and packages

Generate, calculate, and visualize the result of score function:

Simulating and visualizing of score function

Score values are multiplied by constants to obtain a more expressed size of spheres.

Score function 3D visalization

The key idea of this research is to test the hypothesis that we can get brick size better than current approaches (ATR approach) using the score function optimization. Given result will be tested on significance by statistical methods.

Experiment

Experiment consists of three parts.

Experiment pipeline

Data preparation

  • Getting the list of stocks (S&P 500 Index) from Wikipedia.
  • Receiving prices (High, Low, Close) of each day for last 3 years.
  • Removing stocks that have missing values in data.

Code block of this part:

Downloading and cleaning the stocks

As a result of this part is the dataset contained 470 stocks.

Brick size optimization and Renko chart evaluation

This part is responsible for Renko chart evaluation. The optimal brick size is determining on train set which contains 70% of days. Renko chart evaluation is carrying out on the rest 30% of days using score function.

Train test split method

These both operations (optimization and evaluation) are performing for two approaches (ATR and score function optimization). In ATR case, the last ATR value of sequence is considered to be optimal brick size.

In score function optimization case, the optimal brick size is which give a maximum value of score function.

Score(brick_size) plot

This approach uses one-dimensional function minimization based on Brent’s method. Score function with minus sign should be used because it is inverse problem.

This code contains the function that should be optimized, and scoring / optimization process:

Optimization process

Brent’s method is a root-finding algorithm combining the bisection method, the secant method and inverse quadratic interpolation. It has the reliability of bisection but it can be as quick as some of the less-reliable methods. The algorithm tries to use the potentially fast-converging secant method or inverse quadratic interpolation if possible, but it falls back to the more robust bisection method if necessary. Boundary points can be given as lowest and highest ATR values on train set, it would be good as an initial approximation. More information about Brent’s method here.

Brent’s method visualization
Brent’s method algorithm

Let’s get the list of ordering stocks by score values for two approaches. Score values are calculated on test set.

Draw the Renko charts of stocks that have best scores (AVY stock for ATR, AFL stock for score function optimization).

Renko charts of highest score stocks

Code block of result visualization:

Result visualization

Analysis of result

This scatter plot demonstrates how price_ratio corresponds to balance:

Scatter plots
Visualization of how price_ratio corresponds to balance

We can conclude that score function optimization approach compresses the data better because price_ratio has the tendency to be greater.

Draw a histogram that shows how many positive / neutral / negative results of stocks on test set: positive: score > 0; neutral: score = 0; negative: score < 0.

Code that counts and visualizes the result:

Positive / Neutral / Negative histogram
Histogram of result distribution for two approaches

Frequencies have some differences, but the shapes of distributions are similar. Let’s show a score histogram of two approaches, and distribution of their difference:

Score histograms
Score histogram for two approaches
Score difference histogram

Will be good to estimate a confidence interval of score difference. This would make it clear which approach on an average gives a better score.

Test the null hypothesis that a sample comes from a normal distribution. The p-value is 1.085e-11, that is very low, it means that sample comes from non-normal distribution:

Test on normal distribution

It is unacceptable to calculate a confidence interval on non-normal distribution.

We can use the bootstrapping to get a confidence interval. Bootstrapping is practical computer method for studying the distribution of statistics based on multiple generations of samples by the Monte Carlo method based on the available sample. Allows you to quickly and easily evaluate a variety of statistics (confidence intervals, variance, correlation, etc.) for complex models.

Boostrapping alogorithm

Bootstrapped sample is shown below. This sample is distributed according to the normal distribution, p-value is 0.9816.

Code block of resampling:

Bootstrapping
Bootstrapped sample of score difference

Almost the all mean values are greater than 0. The p-value of t-test is 0.0 (null-hypothesis: score_diff = 0.0), we reject the null-hypothesis. These calculations confirm that average score difference between score function optimization and ATR approaches is greater for the proposed one.

This is particular example of the difference in approaches (UNH stock).

Renko charts of UNH for two approaches

Conclusions

  1. Formalized the score function that estimates a quality of built Renko chart.
  2. Described a process of score optimization.
  3. The proposed approach got better quality than classic ATR. This advantage is statistically significant.
  4. pyrenko is a module that is demonstrated in this analysis, and it can be used by any researcher.
  5. Demonstrated a full cycle of noise reduction process using Renko. Code included.
  6. Theoretically, balance parameter multiplied by brick_size can be interpreted as a profit on test set. This simple trend following strategy is a good baseline to further research. Transaction costs must be included for more accurate profit estimation (broker’s fee, spread, slippage, etc.).
  7. The developed approach can be used in strategies, such as pair trading, trend following, support & resistance, and etc. Processed noiseless chart becomes understandable, and trend lines are more clearly.

Let me know if you apply Renko chart in stock analysis or plan to use one. Also, it would be interesting what approach do you use to determine a brick size?

Update

The next chapter of research here.

Best regards,

Sergey

--

--