![](/img/trans.png)
[英]Adding a Time Condition to an order in Interactive Broker's API
[英]Format Interactive Broker data to ProRealTime's daily bars
当我从盈透证券 (IB) 下载外汇每日开盘高低收盘 (OHLC) 蜡烛时,我得到的值与 ProRealTime (PRT) 的蜡烛不匹配(至少在法国)。
我弄清楚了 PRT 是如何构建蜡烛的(根据 IB 数据):
因此,我想使用熊猫从 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))
在这篇文章的同时,我提供了一段为我完成这项工作的代码。
我花了一些时间来弄清楚这一切,所以我分享了我对这个问题的解决方案,另外我想知道是否有更清晰的方法来实现相同的目标:
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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.