In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how Jupyter Notebook can get historical data from the IB interface and write it to the database, strategy back test and firm trading. The editor thinks it is very practical, so I share it with you. I hope you can get something after reading this article.
There happened to be a classmate who asked how to achieve IB earnings historical data acquisition, and strategy back test and firm trading. Thinking of being familiar with vnpy2.0 operation, I ran away with Jupyter Notebook. The overall architectural design of VNPY2.0 is very extensible, and the calls are much more advanced and clear than v1.0, and the engine load calls are very convenient.
Tell me and pay attention to:
Most of the historical data of the IB earnings API are subscribed for a fee. If there is an error message in the charge, find a free one for use here. In addition, vnpy is designed based on a maximum of 6 months of historical data.
There is a small hole in the database definition. I used mongodb. I wrote the wrong password in the trader/setting.py the first time, but later I changed it in trader/setting.py and found that it couldn't be changed. It turns out that after the first maintenance, the configuration will be written to .vntrader / vt_setting, and then the system will only go to .vntrader / vt_setting to read. To change vt_setting, not trader/setting.py.
Use CtaStrategyApp to support adding a new policy, and the system will automatically output json retention policy information; so the second time you run the code, it will indicate that it already exists, not a problem.
I put the back test and the real offer at one time in the code. If you run down directly, you may report an error. It is recommended to annotate the back test when you run the real offer.
Historical subscription data using script_engine is obtained from rqdata by default. The vnpy v2.07 IB API already provides historical data acquisition. Here, HistoryRequest is created to obtain historical data using main_engine.
In order to make it easy to post, change it to .py code format, there is no problem to run directly.
From vnpy.app.script_trader import init_cli_tradingfrom vnpy.app.script_trader.cli import process_log_eventfrom vnpy.gateway.ib import IbGatewayfrom timeimport sleepfrom datetimeimport datetimeimport pandas as pd# connects to server setting = {"TWS address": "127.0.0.1", "TWS port": 7497, "customer number": 8 # each link uses a separate link number An IBAPI supports 32 simultaneous links} engine = init_cli_trading ([IbGateway]) # return Script_engine example And registered gatewayengine.connect_gateway (setting) for main_engine "IB") # Link # query funds-automatic sleep (10) print ("* query funds and positions * *") print (engine.get_all_accounts (use_df = True)) # query position print (engine.get_all_positions (use_df = True)) # subscription quotes from vnpy.trader.constant import Exchangefrom vnpy.trader.object import SubscribeRequest# has problems using Script_engine directly from my test There are too many varieties of IB, and the get_all_contracts command cannot be specified. You need to specify a specific one. Here, use main_engine subscription req1 = SubscribeRequest ("12087792", Exchange.IDEALPRO) # to create a market subscription engine.main_engine.subscribe (req1, "IB") # use script_engine subscription historical data is obtained from rqdata, vnpy v2.07 has provided historical data acquisition, here create HistoryRequest to obtain, # query if there is no endtime, default is current. Return historical data output to database and csv file # for more information about api, please see https://interactivebrokers.github.io/tws-api/historical_bars.htmlprint("*** reading historical data from IB, returning historical data output to database and csv file * * ") from vnpy.trader.object import HistoryRequestfrom vnpy.trader.object import Intervalstart = datetime.strptime ('20190901 output,"% Y%m%d ") historyreq = HistoryRequest (symbol=" 12087792 ") Exchange=Exchange.IDEALPRO, start=start, interval=Interval.MINUTE) # # read historical data And put the historical data BarData into the database bardatalist = engine.main_engine.query_history (historyreq, "IB") from vnpy.trader.database import database_managerdatabase_manager.save_bar_data (bardatalist) # output the historical data BarData to csvpd.DataFrame (bardatalist). To_csv ("C:\ Project\" + str (historyreq.symbol) + ".csv", index=True, header=True) print ("History data export to CSV") # # reference backtesting.ipynb Use the built-in double moving average strategy to test back Wear on the 10th and do more in 60 days. Otherwise, print ("* read historical data from database and perform backtest *") from vnpy.app.cta_strategy.backtesting import BacktestingEnginefrom vnpy.app.cta_strategy.strategies.double_ma_strategy import (DoubleMaStrategy,) btengine = BacktestingEngine () # New backtest engine btengine.set_parameters (vt_symbol= "12087792.IDEALPRO", interval= "1m", start=datetime (2019, 9, 1), end=datetime (2019, 10, 5), rate = 0 Slippage=0.00005, size=1000, pricetick=0.00005, capital=1_000_000,) btengine.add_strategy (DoubleMaStrategy, {"fast_window": 10, "slow_window": 60}) btengine.load_data () btengine.run_backtesting () df = btengine.calculate_result () btengine.calculate_statistics () btengine.show_chart () # load the double moving average strategy into script_engine Run print ("* read preparation data from database, run * *") # use cta transaction engine from vnpy.app.cta_strategy import CtaStrategyAppfrom vnpy.app.cta_strategy.base import EVENT_CTA_LOGengine.event_engine.register (EVENT_CTA_LOG, process_log_event) cta_engine = engine.main_engine.add_app (CtaStrategyApp) # add appcta_engine.init_engine () cta_engine.add_strategy ("DoubleMaStrategy") "DoubleMaStrategy_IB_12087792_v1", "12087792.IDEALPRO", {"fast_window": 10, "slow_window": 50}) sleep (10) cta_engine.init_strategy ("DoubleMaStrategy_IB_12087792_v1") sleep (10) cta_engine.start_strategy ("DoubleMaStrategy_IB_12087792_v1") above is how Jupyter Notebook implements historical data acquisition from IB interface and writing to database, strategy backtesting and firm trading. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.