vnpy_spreadtrading 1.2.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (23) hide show
  1. vnpy_spreadtrading-1.2.6/LICENSE +21 -0
  2. vnpy_spreadtrading-1.2.6/PKG-INFO +65 -0
  3. vnpy_spreadtrading-1.2.6/README.md +37 -0
  4. vnpy_spreadtrading-1.2.6/setup.cfg +45 -0
  5. vnpy_spreadtrading-1.2.6/setup.py +4 -0
  6. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/__init__.py +59 -0
  7. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/algo.py +170 -0
  8. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/backtesting.py +895 -0
  9. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/base.py +561 -0
  10. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/engine.py +1120 -0
  11. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/strategies/__init__.py +0 -0
  12. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/strategies/basic_spread_strategy.py +193 -0
  13. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/strategies/statistical_arbitrage_strategy.py +180 -0
  14. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/template.py +780 -0
  15. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/ui/__init__.py +1 -0
  16. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/ui/spread.ico +0 -0
  17. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading/ui/widget.py +886 -0
  18. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading.egg-info/PKG-INFO +65 -0
  19. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading.egg-info/SOURCES.txt +22 -0
  20. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading.egg-info/dependency_links.txt +1 -0
  21. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading.egg-info/not-zip-safe +1 -0
  22. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading.egg-info/requires.txt +3 -0
  23. vnpy_spreadtrading-1.2.6/vnpy_spreadtrading.egg-info/top_level.txt +1 -0
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015-present, Xiaoyou Chen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,65 @@
1
+ Metadata-Version: 2.4
2
+ Name: vnpy_spreadtrading
3
+ Version: 1.2.6
4
+ Summary: Spread trading application for VeighNa quant trading framework.
5
+ Home-page: https://www.vnpy.com
6
+ Author: Xiaoyou Chen
7
+ Author-email: xiaoyou.chen@mail.vnpy.com
8
+ License: MIT
9
+ Keywords: quant,quantitative,investment,trading,algotrading
10
+ Classifier: Development Status :: 5 - Production/Stable
11
+ Classifier: Operating System :: OS Independent
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Topic :: Office/Business :: Financial :: Investment
18
+ Classifier: Programming Language :: Python :: Implementation :: CPython
19
+ Classifier: License :: OSI Approved :: MIT License
20
+ Classifier: Natural Language :: Chinese (Simplified)
21
+ Requires-Python: >=3.10
22
+ Description-Content-Type: text/markdown
23
+ License-File: LICENSE
24
+ Requires-Dist: numpy
25
+ Requires-Dist: pandas
26
+ Requires-Dist: plotly
27
+ Dynamic: license-file
28
+
29
+ # VeighNa框架的价差交易模块
30
+
31
+ <p align="center">
32
+ <img src ="https://vnpy.oss-cn-shanghai.aliyuncs.com/vnpy-logo.png"/>
33
+ </p>
34
+
35
+ <p align="center">
36
+ <img src ="https://img.shields.io/badge/version-1.2.6-blueviolet.svg"/>
37
+ <img src ="https://img.shields.io/badge/platform-windows|linux|macos-yellow.svg"/>
38
+ <img src ="https://img.shields.io/badge/python-3.10|3.11|3.12|3.13-blue.svg" />
39
+ <img src ="https://img.shields.io/github/license/vnpy/vnpy.svg?color=orange"/>
40
+ </p>
41
+
42
+ ## 说明
43
+
44
+ 针对多条合约腿价差交易设计的应用模块,覆盖价差盘口计算、价差算法执行、价差策略开发的全流程业务功能。
45
+
46
+ ## 注意
47
+
48
+ 不支持使用交易所套利单组成的价差交易合约。
49
+
50
+ ## 安装
51
+
52
+ 安装环境推荐基于3.9.0版本以上的【[**VeighNa Studio**](https://www.vnpy.com)】。
53
+
54
+ 直接使用pip命令:
55
+
56
+ ```
57
+ pip install vnpy_spreadtrading
58
+ ```
59
+
60
+
61
+ 或者下载源代码后,解压后在cmd中运行:
62
+
63
+ ```
64
+ pip install .
65
+ ```
@@ -0,0 +1,37 @@
1
+ # VeighNa框架的价差交易模块
2
+
3
+ <p align="center">
4
+ <img src ="https://vnpy.oss-cn-shanghai.aliyuncs.com/vnpy-logo.png"/>
5
+ </p>
6
+
7
+ <p align="center">
8
+ <img src ="https://img.shields.io/badge/version-1.2.6-blueviolet.svg"/>
9
+ <img src ="https://img.shields.io/badge/platform-windows|linux|macos-yellow.svg"/>
10
+ <img src ="https://img.shields.io/badge/python-3.10|3.11|3.12|3.13-blue.svg" />
11
+ <img src ="https://img.shields.io/github/license/vnpy/vnpy.svg?color=orange"/>
12
+ </p>
13
+
14
+ ## 说明
15
+
16
+ 针对多条合约腿价差交易设计的应用模块,覆盖价差盘口计算、价差算法执行、价差策略开发的全流程业务功能。
17
+
18
+ ## 注意
19
+
20
+ 不支持使用交易所套利单组成的价差交易合约。
21
+
22
+ ## 安装
23
+
24
+ 安装环境推荐基于3.9.0版本以上的【[**VeighNa Studio**](https://www.vnpy.com)】。
25
+
26
+ 直接使用pip命令:
27
+
28
+ ```
29
+ pip install vnpy_spreadtrading
30
+ ```
31
+
32
+
33
+ 或者下载源代码后,解压后在cmd中运行:
34
+
35
+ ```
36
+ pip install .
37
+ ```
@@ -0,0 +1,45 @@
1
+ [metadata]
2
+ name = vnpy_spreadtrading
3
+ version = 1.2.6
4
+ url = https://www.vnpy.com
5
+ license = MIT
6
+ author = Xiaoyou Chen
7
+ author_email = xiaoyou.chen@mail.vnpy.com
8
+ description = Spread trading application for VeighNa quant trading framework.
9
+ long_description = file: README.md
10
+ long_description_content_type = text/markdown
11
+ keywords =
12
+ quant
13
+ quantitative
14
+ investment
15
+ trading
16
+ algotrading
17
+ classifiers =
18
+ Development Status :: 5 - Production/Stable
19
+ Operating System :: OS Independent
20
+ Programming Language :: Python :: 3
21
+ Programming Language :: Python :: 3.10
22
+ Programming Language :: Python :: 3.11
23
+ Programming Language :: Python :: 3.12
24
+ Programming Language :: Python :: 3.13
25
+ Topic :: Office/Business :: Financial :: Investment
26
+ Programming Language :: Python :: Implementation :: CPython
27
+ License :: OSI Approved :: MIT License
28
+ Natural Language :: Chinese (Simplified)
29
+
30
+ [options]
31
+ packages = find:
32
+ zip_safe = False
33
+ python_requires = >=3.10
34
+ install_requires =
35
+ numpy
36
+ pandas
37
+ plotly
38
+
39
+ [options.package_data]
40
+ * = *.ico
41
+
42
+ [egg_info]
43
+ tag_build =
44
+ tag_date = 0
45
+
@@ -0,0 +1,4 @@
1
+ from setuptools import setup
2
+
3
+
4
+ setup()
@@ -0,0 +1,59 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2015-present, Xiaoyou Chen
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in all
13
+ # copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ # SOFTWARE.
22
+
23
+ from pathlib import Path
24
+
25
+ import importlib_metadata
26
+ from vnpy.trader.app import BaseApp
27
+ from vnpy.trader.object import (
28
+ OrderData,
29
+ TradeData,
30
+ TickData,
31
+ BarData
32
+ )
33
+
34
+ from .engine import (
35
+ SpreadEngine,
36
+ APP_NAME,
37
+ SpreadData,
38
+ LegData,
39
+ SpreadStrategyTemplate,
40
+ SpreadAlgoTemplate
41
+ )
42
+
43
+
44
+ try:
45
+ __version__ = importlib_metadata.version("vnpy_spreadtrading")
46
+ except importlib_metadata.PackageNotFoundError:
47
+ __version__ = "dev"
48
+
49
+
50
+ class SpreadTradingApp(BaseApp):
51
+ """"""
52
+
53
+ app_name: str = APP_NAME
54
+ app_module: str = __module__
55
+ app_path: Path = Path(__file__).parent
56
+ display_name: str = "价差交易"
57
+ engine_class: SpreadEngine = SpreadEngine
58
+ widget_name: str = "SpreadManager"
59
+ icon_name: str = str(app_path.joinpath("ui", "spread.ico"))
@@ -0,0 +1,170 @@
1
+ from typing import TYPE_CHECKING, Optional
2
+
3
+ from vnpy.trader.constant import Direction
4
+ from vnpy.trader.object import TickData, OrderData, TradeData, ContractData
5
+ from vnpy.trader.utility import round_to
6
+
7
+ from .template import SpreadAlgoTemplate
8
+ from .base import SpreadData, LegData
9
+
10
+ if TYPE_CHECKING:
11
+ from .engine import SpreadAlgoEngine
12
+
13
+
14
+ class SpreadTakerAlgo(SpreadAlgoTemplate):
15
+ """"""
16
+ algo_name: str = "SpreadTaker"
17
+
18
+ def __init__(
19
+ self,
20
+ algo_engine: "SpreadAlgoEngine",
21
+ algoid: str,
22
+ spread: SpreadData,
23
+ direction: Direction,
24
+ price: float,
25
+ volume: float,
26
+ payup: int,
27
+ interval: int,
28
+ lock: bool,
29
+ extra: dict
30
+ ) -> None:
31
+ """"""
32
+ super().__init__(
33
+ algo_engine,
34
+ algoid,
35
+ spread,
36
+ direction,
37
+ price,
38
+ volume,
39
+ payup,
40
+ interval,
41
+ lock,
42
+ extra
43
+ )
44
+
45
+ def on_tick(self, tick: TickData) -> None:
46
+ """"""
47
+ # Return if there are any existing orders
48
+ if not self.is_order_finished():
49
+ return
50
+
51
+ # Hedge if active leg is not fully hedged
52
+ if not self.is_hedge_finished():
53
+ self.hedge_passive_legs()
54
+ return
55
+
56
+ # Return if tick not inited
57
+ if not self.spread.bid_volume or not self.spread.ask_volume:
58
+ return
59
+
60
+ # Otherwise check if should take active leg
61
+ if self.direction == Direction.LONG:
62
+ if self.spread.ask_price <= self.price:
63
+ self.take_active_leg()
64
+ else:
65
+ if self.spread.bid_price >= self.price:
66
+ self.take_active_leg()
67
+
68
+ def on_order(self, order: OrderData) -> None:
69
+ """"""
70
+ # Only care active leg order update
71
+ if order.vt_symbol != self.spread.active_leg.vt_symbol:
72
+ return
73
+
74
+ # Do nothing if still any existing orders
75
+ if not self.is_order_finished():
76
+ return
77
+
78
+ # Hedge passive legs if necessary
79
+ if not self.is_hedge_finished():
80
+ self.hedge_passive_legs()
81
+
82
+ def on_trade(self, trade: TradeData) -> None:
83
+ """"""
84
+ pass
85
+
86
+ def on_interval(self) -> None:
87
+ """"""
88
+ if not self.is_order_finished():
89
+ self.cancel_all_order()
90
+
91
+ def take_active_leg(self) -> None:
92
+ """"""
93
+ active_symbol: str = self.spread.active_leg.vt_symbol
94
+
95
+ # Calculate spread order volume of new round trade
96
+ spread_volume_left: float = self.target - self.traded
97
+
98
+ if self.direction == Direction.LONG:
99
+ spread_order_volume: float = self.spread.ask_volume
100
+ spread_order_volume = min(spread_order_volume, spread_volume_left)
101
+ else:
102
+ spread_order_volume: float = -self.spread.bid_volume
103
+ spread_order_volume = max(spread_order_volume, spread_volume_left)
104
+
105
+ # Calculate active leg order volume
106
+ leg_order_volume: float = self.spread.calculate_leg_volume(
107
+ active_symbol,
108
+ spread_order_volume
109
+ )
110
+
111
+ # Check active leg volume left
112
+ active_volume_target: float = self.spread.calculate_leg_volume(
113
+ active_symbol,
114
+ self.target
115
+ )
116
+ active_volume_traded: float = self.leg_traded[active_symbol]
117
+ active_volume_left: float = active_volume_target - active_volume_traded
118
+
119
+ # Limit order volume to total volume left of the active leg
120
+ if active_volume_left > 0:
121
+ leg_order_volume: float = min(leg_order_volume, active_volume_left)
122
+ else:
123
+ leg_order_volume: float = max(leg_order_volume, active_volume_left)
124
+
125
+ # Send active leg order
126
+ self.send_leg_order(
127
+ active_symbol,
128
+ leg_order_volume
129
+ )
130
+
131
+ def hedge_passive_legs(self) -> None:
132
+ """
133
+ Send orders to hedge all passive legs.
134
+ """
135
+ # Calcualte spread volume to hedge
136
+ active_leg: LegData = self.spread.active_leg
137
+ active_traded: float = self.leg_traded[active_leg.vt_symbol]
138
+ active_traded: float = round_to(active_traded, self.spread.min_volume)
139
+
140
+ hedge_volume: float = self.spread.calculate_spread_volume(
141
+ active_leg.vt_symbol,
142
+ active_traded
143
+ )
144
+
145
+ # Calculate passive leg target volume and do hedge
146
+ for leg in self.spread.passive_legs:
147
+ passive_traded: float = self.leg_traded[leg.vt_symbol]
148
+ passive_traded: float = round_to(passive_traded, self.spread.min_volume)
149
+
150
+ passive_target: float = self.spread.calculate_leg_volume(
151
+ leg.vt_symbol,
152
+ hedge_volume
153
+ )
154
+
155
+ leg_order_volume: float = passive_target - passive_traded
156
+ if leg_order_volume:
157
+ self.send_leg_order(leg.vt_symbol, leg_order_volume)
158
+
159
+ def send_leg_order(self, vt_symbol: str, leg_volume: float) -> None:
160
+ """"""
161
+ leg: LegData = self.spread.legs[vt_symbol]
162
+ leg_tick: Optional[TickData] = self.get_tick(vt_symbol)
163
+ leg_contract: Optional[ContractData] = self.get_contract(vt_symbol)
164
+
165
+ if leg_volume > 0:
166
+ price: float = leg_tick.ask_price_1 + leg_contract.pricetick * self.payup
167
+ self.send_order(leg.vt_symbol, price, abs(leg_volume), Direction.LONG)
168
+ elif leg_volume < 0:
169
+ price: float = leg_tick.bid_price_1 - leg_contract.pricetick * self.payup
170
+ self.send_order(leg.vt_symbol, price, abs(leg_volume), Direction.SHORT)