2019-01-29 07:37:06 +00:00
|
|
|
from time import time
|
|
|
|
|
|
|
|
import rqdatac as rq
|
|
|
|
|
2019-04-16 05:39:06 +00:00
|
|
|
from vnpy.trader.object import BarData
|
|
|
|
from vnpy.trader.constant import Exchange, Interval
|
|
|
|
from vnpy.trader.database import database_manager
|
2019-01-29 07:37:06 +00:00
|
|
|
|
|
|
|
USERNAME = ""
|
|
|
|
PASSWORD = ""
|
|
|
|
FIELDS = ["open", "high", "low", "close", "volume"]
|
|
|
|
|
|
|
|
rq.init(USERNAME, PASSWORD, ("rqdatad-pro.ricequant.com", 16011))
|
|
|
|
|
|
|
|
|
|
|
|
def generate_bar_from_row(row, symbol, exchange):
|
|
|
|
""""""
|
2019-04-16 05:39:06 +00:00
|
|
|
bar = BarData(
|
|
|
|
symbol=symbol,
|
|
|
|
exchange=Exchange(exchange),
|
|
|
|
interval=Interval.MINUTE,
|
|
|
|
open_price=row["open"],
|
|
|
|
high_price=row["high"],
|
|
|
|
low_price=row["low"],
|
|
|
|
close_price=row["close"],
|
|
|
|
volume=row["volume"],
|
|
|
|
datetime=row.name.to_pydatetime(),
|
|
|
|
gateway_name="DB"
|
|
|
|
)
|
2019-01-29 07:37:06 +00:00
|
|
|
return bar
|
|
|
|
|
|
|
|
|
|
|
|
def download_minute_bar(vt_symbol):
|
|
|
|
"""下载某一合约的分钟线数据"""
|
|
|
|
print(f"开始下载合约数据{vt_symbol}")
|
|
|
|
symbol, exchange = vt_symbol.split(".")
|
|
|
|
|
|
|
|
start = time()
|
|
|
|
|
2019-04-16 05:39:06 +00:00
|
|
|
df = rq.get_price(
|
|
|
|
symbol,
|
|
|
|
frequency="1m",
|
|
|
|
fields=FIELDS,
|
|
|
|
start_date='20100416',
|
|
|
|
end_date='20190416'
|
|
|
|
)
|
2019-01-29 07:37:06 +00:00
|
|
|
|
2019-04-12 08:41:56 +00:00
|
|
|
bars = []
|
|
|
|
for ix, row in df.iterrows():
|
|
|
|
bar = generate_bar_from_row(row, symbol, exchange)
|
|
|
|
bars.append(bar)
|
2019-04-16 05:39:06 +00:00
|
|
|
|
|
|
|
database_manager.save_bar_data(bars)
|
2019-01-29 07:37:06 +00:00
|
|
|
|
|
|
|
end = time()
|
|
|
|
cost = (end - start) * 1000
|
|
|
|
|
|
|
|
print(
|
|
|
|
"合约%s的分钟K线数据下载完成%s - %s,耗时%s毫秒"
|
|
|
|
% (symbol, df.index[0], df.index[-1], cost)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
download_minute_bar("IF88.CFFEX")
|