【问题标题】:Format Interactive Broker data to ProRealTime's daily bars将 Interactive Broker 数据格式化为 ProRealTime 的每日柱形图
【发布时间】:2021-01-13 10:13:17
【问题描述】:

当我从盈透证券 (IB) 下载外汇每日高低收盘 (OHLC) 蜡烛图时,我得到的值与 ProRealTime (PRT) 的蜡烛图不匹配(至少在法国这里)。

我弄清楚了 PRT 是如何构建它的蜡烛的(根据 IB 数据):

  • 周一-PRT = 周日(23:15) -> 周二(02:00)
  • 周二-PRT = 周二(02:00) -> 周三(02:00)
  • 星期三-PRT = 星期三(02:00) -> 星期四(02:00)
  • 星期四-PRT = 星期四(02:00) -> 星期五(02:00)
  • 星期二-PRT = 星期五(02:00) -> 星期五(22:00)

因此,我想使用 pandas 从 IB-hourly-candles 重建 PRT-daily-candles。

下面提供了带有 IB-hourly-candles 的起始代码:

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))

在这篇文章的同时,我提供了一段代码,为我完成了这项工作。

【问题讨论】:

    标签: python pandas interactive-brokers


    【解决方案1】:

    我花了一些时间才弄清楚这一切,所以我分享了我对这个问题的解决方案,另外我想知道是否有更清晰的方法来实现相同的目标:

    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]
    

    【讨论】:

      猜你喜欢
      • 2019-07-22
      • 2016-11-08
      • 1970-01-01
      • 1970-01-01
      • 2023-02-18
      • 1970-01-01
      • 2019-05-31
      • 1970-01-01
      • 2014-03-13
      相关资源
      最近更新 更多