Skip to content

srlcarlg/rangedf

Repository files navigation

rangedf

Transform Tick Data into OHLCV Range Dataframe!

SegmentLocal


Installation

   pip install rangedf

or just copy rangedf.py file to your project!

Content

Usage

There are two classes available:

  • Range(df, range_size)
    To create Range OHLCV dataframe with existing Ticks data.
  • RangeWS(timestamp, price, range_size, external_df, ts_unit)
    To create real-time Range charts, usually over a WebSocket connection.

Let's start with the first class, load a Pandas DataFrame containing Ticks Data, for example:

import pandas as pd
df_ticks = pd.read_parquet('examples/data/EURGBP_T1_cT.parquet')
df_ticks.rename(columns={'bid': 'close'}, inplace=True)
df_ticks.head(5)
ask close spread
datetime
2025-05-08 00:00:00.678 0.85047 0.85043 0.00004
2025-05-08 00:00:00.848 0.85047 0.85045 0.00002
2025-05-08 00:00:02.000 0.85046 0.85045 0.00001
2025-05-08 00:00:02.246 0.85048 0.85046 0.00002
2025-05-08 00:00:02.447 0.85048 0.85045 0.00003

Only two columns are required:

  • close: Mandatory.
  • datetime: If is not present, the index will be used.

After importing rangedf and setting range_size, just call range_df() with the choosen mode.
See all available modes in rangedf_modes.ipynb

from rangedf import Range
r = Range(df_ticks, range_size=0.0003)
df = r.range_df(utils_columns=False) # utils-columns => [direction, tick_index_open, tick_index_close]
df.head(5)
open high low close volume
datetime
2025-05-08 00:20:02.244 0.85052 0.85077 0.85047 0.85077 560
2025-05-08 00:37:55.846 0.85077 0.85103 0.85073 0.85103 358
2025-05-08 00:55:00.448 0.85105 0.85112 0.85082 0.85082 532
2025-05-08 00:57:40.643 0.85081 0.85096 0.85066 0.85066 246
2025-05-08 01:08:23.446 0.85066 0.85079 0.85049 0.85049 716

You can use mpf.plot() or r.plot(), as in the example below.

import mplfinance as mpf
mpf.plot(df.iloc[:130], type='candle', volume=True, style="charles",
         title=f"Range: normal\nrange_size: 0.0003")
mpf.show()
# same as:
# r.plot('normal')

png

As described in rangedf_modes.ipynb, we can have multiple dataframes of different modes from the same instance.

df_normal = r.range_df()
df_nongap = r.range_df('inner-gap')

fig = mpf.figure(style='charles', figsize=(12.5,9))
fig.subplots_adjust(hspace=0.1, wspace=0.01)
ax1 = fig.add_subplot(2,2,1)
ax2 = fig.add_subplot(2,2,2)

mpf.plot(df_normal.iloc[:130],type='candle',ax=ax1,axtitle='normal')
mpf.plot(df_nongap.iloc[:130],type='candle',ax=ax2,axtitle='inner-gap')
mpf.show()

png

Animation/Real-time Range Chart

To run the animation examples, clone this repository, then into the rangedf/examples folder, run:,

  • python ws_animchart_demo.py,
  • python ws_multichart_demo.py,
  • python ws_externaldf_demo.py

NOTE: There are comments, in each of above example files, talking about situations or recommendations to be applied in real cases.

Performance-wise

From v2.0, the Range calculation is powered by numpy arrays instead of python dicts/list as before, the OHLC variations (modes) are also calculated all at once, leading to a massive speed-up in Range Charts creation.

Regarding "backtest vs real-time" charts, there are 2 tests about the OHLCV reliability between them.
The tests includes creating charts from scratch or using external data (Range.to_rws())

Is the Range chart calculated correctly?

I asked myself the same question, how about we see for ourselves?
We are going to do this based on Spotware's FX/CFD Trading Platform called cTrader,
using IC Markets/Trading as Price Provider.

RESUME: Despite the possible occurrence of more/less bars in cases of gap, the range calculation is practically the same or very approximate.

Non-Affiliation Disclaimer

I'm not endorsed by, directly affiliated with, maintained, authorized, or sponsored by any company previously mentioned. All product and company names are the registered trademarks of their original owners. The use of any trade name or trademark is for identification and reference purposes only and does not imply any association with the trademark holder of their product brand.

About

Transform Tick Data into OHLCV Range Dataframe!

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages