简体   繁体   中英

Format Interactive Broker data to ProRealTime's daily bars

When I download Forex Daily Open-High-Low-Close(OHLC) candles from Interactive Broker (IB), the values I get do not match the candles of ProRealTime (PRT) (Here in France, at least).

I figured out how PRT was building its candles (in terms of IB data):

  • Monday-PRT = Sunday(23:15) -> Tuesday(02:00)
  • Tuesday-PRT = Tuesday(02:00) -> Wednesday(02:00)
  • Wednesday-PRT = Wednesday(02:00) -> Thursday(02:00)
  • Thursday-PRT = Thursday(02:00) -> Friday(02:00)
  • Tuesday-PRT = Friday(02:00) -> Friday(22:00)

Hence I would like to rebuild PRT-daily-candles from IB-hourly-candles using pandas.

A starting code with IB-hourly-candles is provided herebelow:

import pandas as pd
import dateutils

df = pd.read_csv(
    'https://gist.githubusercontent.com/marc-moreaux/d6a910952b8c8243a4fcb8e377cc2de9/raw/d811445bbb782743e71193dbbe31f84adc6f8a5f/EURUSD-daily.csv',
    index_col=0,
    usecols=[1, 2, 3, 4, 5],
    parse_dates=True,
    date_parser=dateutil.parser.isoparse))

Parallel to this post I provide a piece of code that did the job for me.

It took me some time to figure all this out, so I share my solution for this problem, plus I wonder if there is a clearer way to achieve the same goal:

import pandas as pd
import dateutils

# Read the csv
df = pd.read_csv(
    'https://gist.githubusercontent.com/marc-moreaux/d6a910952b8c8243a4fcb8e377cc2de9/raw/d811445bbb782743e71193dbbe31f84adc6f8a5f/EURUSD-daily.csv',
    index_col=0,
    usecols=[1, 2, 3, 4, 5],
    parse_dates=True,
    date_parser=dateutil.parser.isoparse))

# OHLC Agglomeration scheme
ohlc_agg = {
    'open': 'first',
    'high': 'max',
    'low': 'min',
    'close': 'last' }

# Set data to Central European Time (+1/+2) and convert it to UTC (+0)
df = (df.tz_localize('CET', ambiguous='infer')
      .tz_convert(None)
      .resample('1d')
      .agg(ohlc_agg)
      .dropna())

# Identify Sundays and Mondays by their weekday
df['wd'] = df.index.weekday

# Aggregate 1 week of data where only Sundays and Mondays exists; shift these
# aggregated values to corresponding monday; and update df.
df.update(df[(df.wd == 6) | (df.wd == 0)]
          .ressample('1W-MON')
          .agg(ohlc_agg))

# Finally delete sundays
df = df[df.wd != 6]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM