diff --git a/.gitignore b/.gitignore index 57b07e0b..21009318 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ # Temp build dist +*.local diff --git a/tests/backtesting/getdata.py b/tests/backtesting/getdata.py new file mode 100644 index 00000000..ffa2fc12 --- /dev/null +++ b/tests/backtesting/getdata.py @@ -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")