[Add] download history data from rqdata into sqlite
This commit is contained in:
parent
b99c5ff590
commit
be8f57d039
1
.gitignore
vendored
1
.gitignore
vendored
@ -11,3 +11,4 @@
|
|||||||
# Temp
|
# Temp
|
||||||
build
|
build
|
||||||
dist
|
dist
|
||||||
|
*.local
|
||||||
|
59
tests/backtesting/getdata.py
Normal file
59
tests/backtesting/getdata.py
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
from time import time
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
import rqdatac as rq
|
||||||
|
|
||||||
|
from vnpy.trader.database import DbBarData, DB
|
||||||
|
|
||||||
|
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):
|
||||||
|
""""""
|
||||||
|
bar = DbBarData()
|
||||||
|
|
||||||
|
bar.symbol = symbol
|
||||||
|
bar.exchange = exchange
|
||||||
|
bar.interval = "1m"
|
||||||
|
bar.open_price = row["open"]
|
||||||
|
bar.high_price = row["high"]
|
||||||
|
bar.low_price = row["low"]
|
||||||
|
bar.close_price = row["close"]
|
||||||
|
bar.volume = row["volume"]
|
||||||
|
bar.datetime = row.name.to_pydatetime()
|
||||||
|
bar.gateway_name = "DB"
|
||||||
|
bar.vt_symbol = f"{symbol}.{exchange}"
|
||||||
|
|
||||||
|
return bar
|
||||||
|
|
||||||
|
|
||||||
|
def download_minute_bar(vt_symbol):
|
||||||
|
"""下载某一合约的分钟线数据"""
|
||||||
|
print(f"开始下载合约数据{vt_symbol}")
|
||||||
|
symbol, exchange = vt_symbol.split(".")
|
||||||
|
|
||||||
|
start = time()
|
||||||
|
|
||||||
|
df = rq.get_price(symbol, frequency="1m", fields=FIELDS)
|
||||||
|
|
||||||
|
with DB.atomic():
|
||||||
|
for ix, row in df.iterrows():
|
||||||
|
print(row.name)
|
||||||
|
bar = generate_bar_from_row(row, symbol, exchange)
|
||||||
|
DbBarData.replace(bar.__data__).execute()
|
||||||
|
|
||||||
|
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")
|
Loading…
Reference in New Issue
Block a user