From 4f94988a6e678aeaad5d13dbb0447e232fdc0f57 Mon Sep 17 00:00:00 2001 From: xldistance <819873074@qq.com> Date: Fri, 7 Jun 2019 16:00:13 +0800 Subject: [PATCH 1/7] =?UTF-8?q?ctaengine=E4=BB=B7=E6=A0=BC=E5=8F=96?= =?UTF-8?q?=E6=95=B4=E5=88=B0=E6=9C=80=E5=B0=8F=E4=BB=B7=E6=A0=BC=E5=8F=98?= =?UTF-8?q?=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ctaengine价格取整到最小价格变动 --- vnpy/app/cta_strategy/engine.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vnpy/app/cta_strategy/engine.py b/vnpy/app/cta_strategy/engine.py index e17f49ec..fd3d3c46 100644 --- a/vnpy/app/cta_strategy/engine.py +++ b/vnpy/app/cta_strategy/engine.py @@ -36,7 +36,7 @@ from vnpy.trader.constant import ( Offset, Status ) -from vnpy.trader.utility import load_json, save_json, extract_vt_symbol +from vnpy.trader.utility import load_json, save_json, extract_vt_symbol,round_to from vnpy.trader.database import database_manager from vnpy.trader.rqdata import rqdata_client @@ -464,7 +464,7 @@ class CtaEngine(BaseEngine): if not contract: self.write_log(f"委托失败,找不到合约:{strategy.vt_symbol}", strategy) return "" - + price = round_to(price,contract.pricetick) if stop: if contract.stop_supported: return self.send_server_stop_order(strategy, contract, direction, offset, price, volume, lock) From b79212c4d95bcf7d1d38f48d29d78a53f96c6c44 Mon Sep 17 00:00:00 2001 From: xldistance <819873074@qq.com> Date: Sun, 9 Jun 2019 08:49:27 +0800 Subject: [PATCH 2/7] Update engine.py --- vnpy/app/cta_strategy/engine.py | 1 + 1 file changed, 1 insertion(+) diff --git a/vnpy/app/cta_strategy/engine.py b/vnpy/app/cta_strategy/engine.py index fd3d3c46..98b52bfe 100644 --- a/vnpy/app/cta_strategy/engine.py +++ b/vnpy/app/cta_strategy/engine.py @@ -465,6 +465,7 @@ class CtaEngine(BaseEngine): self.write_log(f"委托失败,找不到合约:{strategy.vt_symbol}", strategy) return "" price = round_to(price,contract.pricetick) + volume = round_to(volume,contract.min_volume) if stop: if contract.stop_supported: return self.send_server_stop_order(strategy, contract, direction, offset, price, volume, lock) From 4fadcd901646ed6fdd951b6d3d9e87cfc06fc61f Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 10 Jun 2019 13:25:06 +0800 Subject: [PATCH 3/7] [Mod] add comment for round_to --- vnpy/app/cta_strategy/engine.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/vnpy/app/cta_strategy/engine.py b/vnpy/app/cta_strategy/engine.py index 98b52bfe..ff2e8ef9 100644 --- a/vnpy/app/cta_strategy/engine.py +++ b/vnpy/app/cta_strategy/engine.py @@ -36,7 +36,7 @@ from vnpy.trader.constant import ( Offset, Status ) -from vnpy.trader.utility import load_json, save_json, extract_vt_symbol,round_to +from vnpy.trader.utility import load_json, save_json, extract_vt_symbol, round_to from vnpy.trader.database import database_manager from vnpy.trader.rqdata import rqdata_client @@ -464,8 +464,11 @@ class CtaEngine(BaseEngine): if not contract: self.write_log(f"委托失败,找不到合约:{strategy.vt_symbol}", strategy) return "" - price = round_to(price,contract.pricetick) - volume = round_to(volume,contract.min_volume) + + # Round order price and volume to nearest incremental value + price = round_to(price, contract.pricetick) + volume = round_to(volume, contract.min_volume) + if stop: if contract.stop_supported: return self.send_server_stop_order(strategy, contract, direction, offset, price, volume, lock) From 8f5f187fcd875f89203f628d2c05a33885943580 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 10 Jun 2019 13:29:03 +0800 Subject: [PATCH 4/7] [Mod] clear previously loaded history data --- vnpy/app/cta_strategy/backtesting.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/vnpy/app/cta_strategy/backtesting.py b/vnpy/app/cta_strategy/backtesting.py index 115881cc..456ab7fa 100644 --- a/vnpy/app/cta_strategy/backtesting.py +++ b/vnpy/app/cta_strategy/backtesting.py @@ -214,6 +214,8 @@ class BacktestingEngine: if not self.end: self.end = datetime.now() + self.history_data.clear() # Clear previously loaded history data + # Load 30 days of data each time and allow for progress update progress_delta = timedelta(days=30) total_delta = self.end - self.start From a833576ebb26ec3ec539bb220588c93779a13aaa Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 10 Jun 2019 14:15:08 +0800 Subject: [PATCH 5/7] [Mod] update gateway document for using ConId as contract symbol at IB Gateway --- docs/gateway.md | 37 +++++++++++++++++++---------------- tests/trader/run.py | 8 ++++---- vnpy/gateway/ib/ib_gateway.py | 3 +++ 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/docs/gateway.md b/docs/gateway.md index 714ca42b..1f183ab5 100644 --- a/docs/gateway.md +++ b/docs/gateway.md @@ -71,23 +71,23 @@ def main(): ## 接口分类 -| 接口 |类型 | -| ------ | :------: | -| CTP | 期货 | -| FEMAS | 期货 | -| OES | 国内股票 | -| XTP | 国内股票、指数、基金、债券、期权、融资融券 | -| IB | 外盘股票、期货、期权 | -| TAP | 外盘股票、期货、期权 | -| FUTU | 国内股票、港股、美股 | -| TIGER | 国内股票、港股、美股 | -| BITFINEX | 数字货币 | -| BITMEX | 数字货币 | -| OKEX | 数字货币 | -| OKEXF | 数字货币 | -| HUOBI | 数字货币 | -| HBDM | 数字货币 | -| ONETOKEN | 数字货币 | +| 接口 | 类型 | +| -------- | :----------------------------------------: | +| CTP | 期货 | +| FEMAS | 期货 | +| OES | 国内股票 | +| XTP | 国内股票、指数、基金、债券、期权、融资融券 | +| IB | 外盘股票、期货、期权 | +| TAP | 外盘股票、期货、期权 | +| FUTU | 国内股票、港股、美股 | +| TIGER | 国内股票、港股、美股 | +| BITFINEX | 数字货币 | +| BITMEX | 数字货币 | +| OKEX | 数字货币 | +| OKEXF | 数字货币 | +| HUOBI | 数字货币 | +| HBDM | 数字货币 | +| ONETOKEN | 数字货币 | @@ -270,8 +270,11 @@ main_engine.add_gateway(IbGateway)   #### 其他特点 + 可交易品种几乎覆盖全球的股票、期权、期权;手续费相对较低。 +注意IB接口的合约代码较为特殊,请前往官网的产品查询板块查询,VN Trader中使用的是盈透证券对于每个合约在某一交易所的唯一标识符ConId来作为合约代码,而非Symbol或者LocalName。 +   diff --git a/tests/trader/run.py b/tests/trader/run.py index 099d7a55..26b48183 100644 --- a/tests/trader/run.py +++ b/tests/trader/run.py @@ -5,7 +5,7 @@ from vnpy.trader.ui import MainWindow, create_qapp # from vnpy.gateway.bitmex import BitmexGateway # from vnpy.gateway.futu import FutuGateway -# from vnpy.gateway.ib import IbGateway +from vnpy.gateway.ib import IbGateway # from vnpy.gateway.ctp import CtpGateway # # from vnpy.gateway.ctptest import CtptestGateway # from vnpy.gateway.femas import FemasGateway @@ -18,7 +18,7 @@ from vnpy.trader.ui import MainWindow, create_qapp # from vnpy.gateway.okexf import OkexfGateway # from vnpy.gateway.xtp import XtpGateway # from vnpy.gateway.hbdm import HbdmGateway -from vnpy.gateway.tap import TapGateway +# from vnpy.gateway.tap import TapGateway # from vnpy.app.cta_strategy import CtaStrategyApp # from vnpy.app.csv_loader import CsvLoaderApp @@ -39,7 +39,7 @@ def main(): # main_engine.add_gateway(CtpGateway) # # main_engine.add_gateway(CtptestGateway) # main_engine.add_gateway(FemasGateway) - # main_engine.add_gateway(IbGateway) + main_engine.add_gateway(IbGateway) # main_engine.add_gateway(FutuGateway) # main_engine.add_gateway(BitmexGateway) # main_engine.add_gateway(TigerGateway) @@ -50,7 +50,7 @@ def main(): # main_engine.add_gateway(OnetokenGateway) # main_engine.add_gateway(OkexfGateway) # main_engine.add_gateway(HbdmGateway) - main_engine.add_gateway(TapGateway) + # main_engine.add_gateway(TapGateway) # main_engine.add_app(CtaStrategyApp) # main_engine.add_app(CtaBacktesterApp) diff --git a/vnpy/gateway/ib/ib_gateway.py b/vnpy/gateway/ib/ib_gateway.py index 0a7d0fb8..19a84774 100644 --- a/vnpy/gateway/ib/ib_gateway.py +++ b/vnpy/gateway/ib/ib_gateway.py @@ -416,6 +416,9 @@ class IbApi(EWrapper): accountName, ) + if not contract.exchange: + return + ib_size = contract.multiplier if not ib_size: ib_size = 1 From 65133141f8cfe43539a7d50ba2e6878bab09af73 Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 10 Jun 2019 14:46:36 +0800 Subject: [PATCH 6/7] [Add] HbdmGateway auto resubscribe market data when connected --- tests/trader/run.py | 8 ++++---- vnpy/gateway/hbdm/hbdm_gateway.py | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/trader/run.py b/tests/trader/run.py index 26b48183..b4e1db91 100644 --- a/tests/trader/run.py +++ b/tests/trader/run.py @@ -5,7 +5,7 @@ from vnpy.trader.ui import MainWindow, create_qapp # from vnpy.gateway.bitmex import BitmexGateway # from vnpy.gateway.futu import FutuGateway -from vnpy.gateway.ib import IbGateway +# from vnpy.gateway.ib import IbGateway # from vnpy.gateway.ctp import CtpGateway # # from vnpy.gateway.ctptest import CtptestGateway # from vnpy.gateway.femas import FemasGateway @@ -17,7 +17,7 @@ from vnpy.gateway.ib import IbGateway # from vnpy.gateway.onetoken import OnetokenGateway # from vnpy.gateway.okexf import OkexfGateway # from vnpy.gateway.xtp import XtpGateway -# from vnpy.gateway.hbdm import HbdmGateway +from vnpy.gateway.hbdm import HbdmGateway # from vnpy.gateway.tap import TapGateway # from vnpy.app.cta_strategy import CtaStrategyApp @@ -39,7 +39,7 @@ def main(): # main_engine.add_gateway(CtpGateway) # # main_engine.add_gateway(CtptestGateway) # main_engine.add_gateway(FemasGateway) - main_engine.add_gateway(IbGateway) + # main_engine.add_gateway(IbGateway) # main_engine.add_gateway(FutuGateway) # main_engine.add_gateway(BitmexGateway) # main_engine.add_gateway(TigerGateway) @@ -49,7 +49,7 @@ def main(): # main_engine.add_gateway(BitfinexGateway) # main_engine.add_gateway(OnetokenGateway) # main_engine.add_gateway(OkexfGateway) - # main_engine.add_gateway(HbdmGateway) + main_engine.add_gateway(HbdmGateway) # main_engine.add_gateway(TapGateway) # main_engine.add_app(CtaStrategyApp) diff --git a/vnpy/gateway/hbdm/hbdm_gateway.py b/vnpy/gateway/hbdm/hbdm_gateway.py index fe0d93dc..9c786ec8 100644 --- a/vnpy/gateway/hbdm/hbdm_gateway.py +++ b/vnpy/gateway/hbdm/hbdm_gateway.py @@ -957,6 +957,9 @@ class HbdmDataWebsocketApi(HbdmWebsocketApiBase): def on_connected(self): """""" self.gateway.write_log("行情Websocket API连接成功") + + for ws_symbol in self.ticks.keys(): + self.subscribe_data(ws_symbol) def subscribe(self, req: SubscribeRequest): """""" @@ -979,7 +982,11 @@ class HbdmDataWebsocketApi(HbdmWebsocketApiBase): gateway_name=self.gateway_name, ) self.ticks[ws_symbol] = tick - + + self.subscribe_data(ws_symbol) + + def subscribe_data(self, ws_symbol: str): + """""" # Subscribe to market depth update self.req_id += 1 req = { From fa9f06b2aafb16f2bf404bf9da85c37aebc2786c Mon Sep 17 00:00:00 2001 From: "vn.py" Date: Mon, 10 Jun 2019 14:54:41 +0800 Subject: [PATCH 7/7] [Mod] Close #1708 --- vnpy/trader/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vnpy/trader/engine.py b/vnpy/trader/engine.py index 9b60bae0..9a65e90d 100644 --- a/vnpy/trader/engine.py +++ b/vnpy/trader/engine.py @@ -299,7 +299,7 @@ class LogEngine(BaseEngine): file_path = log_path.joinpath(filename) file_handler = logging.FileHandler( - file_path, mode="w", encoding="utf8" + file_path, mode="a", encoding="utf8" ) file_handler.setLevel(self.level) file_handler.setFormatter(self.formatter)