Algorithmic Trading & Indicators Intermediate

Convert Time-Series OHLC Data into Pure Price-Movement Renko Bricks in Python

Filter out time-based sideways noise by converting standard OHLCV price series into fixed or ATR-sized Renko bricks (+1 Bull / -1 Bear) in Python.

#pandas#numpy

Problem Statement

Time-based candles print new bars even when the market moves 0 points for two hours. Renko bricks only form when price moves by a strict threshold (`brick_size`), isolating pure price momentum.

Formula / Core Logic

New Bull Brick (+1) forms when Price - Last_Brick_Close >= Brick_Size New Bear Brick (-1) forms when Last_Brick_Close - Price >= Brick_Size

Installation

pip install pandas numpy
renko-chart-atr-brick-builder-python-pandas.py
import pandas as pd

def build_renko_bricks(close_series, brick_size=50.0):
    bricks = []
    last_price = close_series.iloc[0]
    for dt, price in close_series.items():
        diff = price - last_price
        if abs(diff) >= brick_size:
            num_bricks = int(abs(diff) // brick_size)
            direction = 1 if diff > 0 else -1
            for _ in range(num_bricks):
                last_price += direction * brick_size
                bricks.append({'Timestamp': dt, 'Brick_Close': round(last_price, 2), 'Direction': direction})
    return pd.DataFrame(bricks)

Get the Complete 100+ Python Quant & Automation Bundle

Instant lifetime access to all production-ready scripts, valuation models, and trading bots ($19).

Buy Full Bundle on Gumroad ($19) →

Frequently Asked Questions

Q: How should I choose `brick_size` for stocks of different prices?

Set `brick_size` equal to the 14-period Average True Range (ATR) or 0.5% to 1.0% of the asset's spot price.