# vnpy_ctp **Repository Path**: marketmaking/vnpy_ctp ## Basic Information - **Project Name**: vnpy_ctp - **Description**: vn.py框架的CTP交易接口 - **Primary Language**: C++ - **License**: MIT - **Default Branch**: main - **Homepage**: https://www.vnpy.com - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 60 - **Created**: 2022-10-13 - **Last Updated**: 2022-10-13 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # VeighNa框架的CTP底层接口 修改版本 ctp_gateway 修改函数 支持了timestamp 不同数据类型的情况 ```python def onRtnOrder(self, data: dict) -> None: """委托更新推送""" if not self.contract_inited: self.order_data.append(data) return symbol: str = data["InstrumentID"] contract: ContractData = symbol_contract_map[symbol] frontid: int = data["FrontID"] sessionid: int = data["SessionID"] order_ref: str = data["OrderRef"] orderid: str = f"{frontid}_{sessionid}_{order_ref}" timestamp: str = f"{data['InsertDate']} {data['InsertTime']}" # print(timestamp) # dt: datetime = datetime.strptime(timestamp, "%Y%m%d %H:%M:%S") # dt: datetime = CHINA_TZ.localize(dt) if len(str(int(timestamp))) == 8: print(timestamp[:4], timestamp[4:6], timestamp[6:8]) year = int(timestamp[:4]) month = int(timestamp[4:6]) day = int(timestamp[6:8]) now = date(year, month, day) dt: datetime = now.strftime("%Y%m%d %H:%M:%S") # dt: datetime = dt.replace(tzinfo=CHINA_TZ) print(dt) else: dt: datetime = datetime.strptime(timestamp, "%Y%m%d %H:%M:%S") dt: datetime = dt.replace(tzinfo=CHINA_TZ) tp: tuple = (data["OrderPriceType"], data["TimeCondition"], data["VolumeCondition"]) order: OrderData = OrderData( symbol=symbol, exchange=contract.exchange, orderid=orderid, type=ORDERTYPE_CTP2VT[tp], direction=DIRECTION_CTP2VT[data["Direction"]], offset=OFFSET_CTP2VT[data["CombOffsetFlag"]], price=data["LimitPrice"], volume=data["VolumeTotalOriginal"], traded=data["VolumeTraded"], status=STATUS_CTP2VT[data["OrderStatus"]], datetime=dt, gateway_name=self.gateway_name ) self.gateway.on_order(order) self.sysid_orderid_map[data["OrderSysID"]] = orderid ```

## 说明 基于CTP期货版的6.6.7接口封装开发,接口中自带的是【穿透式实盘环境】的dll文件。 ## 安装 安装环境推荐基于3.3.0版本以上的【[**VeighNa Studio**](https://www.vnpy.com)】。 直接使用pip命令: ``` pip install vnpy_ctp ``` 或者下载源代码后,解压后在cmd中运行: ``` pip install . ``` 使用源代码安装时需要进行C++编译,因此在执行上述命令之前请确保已经安装了【Visual Studio(Windows)】或者【GCC(Linux)】编译器。 ## 使用 以脚本方式启动(script/run.py): ``` from vnpy.event import EventEngine from vnpy.trader.engine import MainEngine from vnpy.trader.ui import MainWindow, create_qapp from vnpy_ctp import CtpGateway def main(): """主入口函数""" qapp = create_qapp() event_engine = EventEngine() main_engine = MainEngine(event_engine) main_engine.add_gateway(CtpGateway) main_window = MainWindow(main_engine, event_engine) main_window.showMaximized() qapp.exec() if __name__ == "__main__": main() ```