Plotting Renko bars in Python

Renko bars can be used as an alternative to the standard fixed-time candles. They smooth out a time-series without introducing lag. Most trading platforms don’t seem to support them natively but can be created using scripting. In this post I used DeepThought to generate the bars and Python’s matplotlib to display.

Generating the Renko Bars

From the commandline, Renko bars can be generated by

DeepThought --generate-bars EURUSDm1 --bar-type const-price-2 --price-movement 0.002 --output-filename renko-type-2.csv

This will generate a CSV file containing the Renko bars. Two types are possible, documented here: Deep Thought Renko Bars.

Alternatively you could write some script in your trading platform to generate the bars.

Python Script

Matplotlib does not have an in-build plot type for Renko bars, however using Rectangle and add_patches we can create a nice-looking Renko plot. Below is the script I used to visualise the bars:


import matplotlib.pyplot as plt
import pandas as pd
import sys

def PlotRenko(filename):
    # Turn interactive mode off
    plt.ioff()
    df = pd.read_csv(filename, parse_dates=[['date','time']])

    # number of bars to display in the plot
    num_bars = 100

    # get the last num_bars
    df = df.tail(num_bars)
    renkos = zip(df['open'],df['close'])

    # compute the price movement in the Renko
    price_move = abs(df.iloc[1]['open'] - df.iloc[1]['close'])

    # create the figure
    fig = plt.figure(1)
    fig.clf()
    axes = fig.gca()

    # plot the bars, blue for 'up', red for 'down'
    index = 1
    for open_price, close_price in renkos:
        if (open_price < close_price):
            renko = matplotlib.patches.Rectangle((index,open_price), 1, close_price-open_price, edgecolor='darkblue', facecolor='blue', alpha=0.5)
            axes.add_patch(renko)
        else:
            renko = matplotlib.patches.Rectangle((index,open_price), 1, close_price-open_price, edgecolor='darkred', facecolor='red', alpha=0.5)
            axes.add_patch(renko)
        index = index + 1

    # adjust the axes
    plt.xlim([0, num_bars])
    plt.ylim([min(min(df['open']),min(df['close'])), max(max(df['open']),max(df['close']))])
    fig.suptitle('Bars from ' + min(df['date_time']).strftime("%d-%b-%Y %H:%M") + " to " + max(df['date_time']).strftime("%d-%b-%Y %H:%M") \
        + '\nPrice movement = ' + str(price_move), fontsize=14)
    plt.xlabel('Bar Number')
    plt.ylabel('Price')
    plt.grid(True)
    plt.show()

    # save the figure as a png file
    fig.savefig("Renko.png")

if __name__ == "__main__":
    if (len(sys.argv) == 1):
        print "Usage: python analyse_backtest_results.py <configuration file location>"
        exit()
    else:
        filename = sys.argv[1]

    PlotRenko(filename)

The final output is:

Renko_EURUSD_2

Compare this to H4 candles covering the same period:

ConstTimeH4_EURUSD

If you want to experiment yourself, you can download a CSV file of Renko bars here: DeepThoughtRenkoBars.zip

Advertisement
Posted in Deep Thought, Python, Renko
2 comments on “Plotting Renko bars in Python
  1. skymex chih says:

    Hello, i found this intereseting, do you know who I can work with to develop a strategy with renko bars ??

  2. […] segment, there’s somewhat slightly of code. The credit score for this section is going to the code I found here which I’ve changed somewhat […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: