-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from vnpy/dev
1.0.3 Release
- Loading branch information
Showing
15 changed files
with
616 additions
and
903 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
# 1.0.3版本 | ||
|
||
1. 调整模块设计(模板、引擎),只支持单合约算法执行交易 | ||
2. 调整算法模板,增加默认参数和变量字段,以及对应函数传参 | ||
3. 优化算法状态控制,增加状态枚举值,算法支持暂停和恢复运行 | ||
4. 移除DMA算法 | ||
5. 移除算法配置缓存和加载功能 | ||
6. 调整图形界面,优化算法状态信息显示 | ||
|
||
# 1.0.2版本 | ||
|
||
1. 将模块的图标文件信息,改为完整路径字符串 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,121 @@ | ||
from random import uniform | ||
|
||
from vnpy.trader.constant import Offset, Direction | ||
from vnpy.trader.constant import Direction | ||
from vnpy.trader.object import TradeData, OrderData, TickData | ||
from vnpy.trader.engine import BaseEngine | ||
from vnpy.trader.utility import round_to | ||
|
||
from ..template import AlgoTemplate | ||
|
||
|
||
class BestLimitAlgo(AlgoTemplate): | ||
"""""" | ||
|
||
display_name = "BestLimit 最优限价" | ||
|
||
default_setting = { | ||
"vt_symbol": "", | ||
"direction": [Direction.LONG.value, Direction.SHORT.value], | ||
"volume": 0.0, | ||
"min_volume": 0.0, | ||
"max_volume": 0.0, | ||
"volume_change": [ | ||
"1", | ||
"0.1", | ||
"0.01", | ||
"0.001", | ||
"0.0001", | ||
"0.00001" | ||
], | ||
"offset": [ | ||
Offset.NONE.value, | ||
Offset.OPEN.value, | ||
Offset.CLOSE.value, | ||
Offset.CLOSETODAY.value, | ||
Offset.CLOSEYESTERDAY.value | ||
] | ||
"""最优限价算法类""" | ||
|
||
display_name: str = "BestLimit 最优限价" | ||
|
||
default_setting: dict = { | ||
"min_volume": 0, | ||
"max_volume": 0, | ||
} | ||
|
||
variables = [ | ||
"traded", | ||
variables: list = [ | ||
"vt_orderid", | ||
"order_price", | ||
"last_tick", | ||
"order_price" | ||
] | ||
|
||
def __init__( | ||
self, | ||
algo_engine: BaseEngine, | ||
algo_name: str, | ||
vt_symbol: str, | ||
direction: str, | ||
offset: str, | ||
price: float, | ||
volume: float, | ||
setting: dict | ||
): | ||
"""""" | ||
super().__init__(algo_engine, algo_name, setting) | ||
) -> None: | ||
"""构造函数""" | ||
super().__init__(algo_engine, algo_name, vt_symbol, direction, offset, price, volume, setting) | ||
|
||
# 参数 | ||
self.vt_symbol = setting["vt_symbol"] | ||
self.direction = Direction(setting["direction"]) | ||
self.volume = setting["volume"] | ||
self.offset = Offset(setting["offset"]) | ||
|
||
self.min_volume = setting["min_volume"] | ||
self.max_volume = setting["max_volume"] | ||
|
||
if "." in setting["volume_change"]: | ||
self.volume_change = float(setting["volume_change"]) | ||
else: | ||
self.volume_change = int(setting["volume_change"]) | ||
self.min_volume: float = setting["min_volume"] | ||
self.max_volume: float = setting["max_volume"] | ||
|
||
# 变量 | ||
self.vt_orderid = "" | ||
self.traded = 0 | ||
self.last_tick = None | ||
self.order_price = 0 | ||
self.vt_orderid: str = "" | ||
self.order_price: float = 0 | ||
|
||
self.put_parameters_event() | ||
self.put_variables_event() | ||
self.put_event() | ||
|
||
# 检查最大/最小挂单量 | ||
if self.min_volume <= 0: | ||
self.write_log("最小挂单量必须大于0,算法启动失败") | ||
self.stop() | ||
self.finish() | ||
return | ||
|
||
if self.max_volume < self.min_volume: | ||
self.write_log("最大挂单量必须不小于最小委托量,算法启动失败") | ||
self.stop() | ||
self.finish() | ||
return | ||
|
||
self.subscribe(self.vt_symbol) | ||
|
||
def on_tick(self, tick: TickData): | ||
"""""" | ||
self.last_tick = tick | ||
|
||
def on_tick(self, tick: TickData) -> None: | ||
"""Tick行情回调""" | ||
if self.direction == Direction.LONG: | ||
if not self.vt_orderid: | ||
self.buy_best_limit() | ||
elif self.order_price != self.last_tick.bid_price_1: | ||
self.buy_best_limit(tick.bid_price_1) | ||
elif self.order_price != tick.bid_price_1: | ||
self.cancel_all() | ||
else: | ||
if not self.vt_orderid: | ||
self.sell_best_limit() | ||
elif self.order_price != self.last_tick.ask_price_1: | ||
self.sell_best_limit(tick.ask_price_1) | ||
elif self.order_price != tick.ask_price_1: | ||
self.cancel_all() | ||
|
||
self.put_variables_event() | ||
|
||
def on_trade(self, trade: TradeData): | ||
"""""" | ||
self.traded += trade.volume | ||
self.put_event() | ||
|
||
def on_trade(self, trade: TradeData) -> None: | ||
"""成交回调""" | ||
if self.traded >= self.volume: | ||
self.write_log(f"已交易数量:{self.traded},总数量:{self.volume}") | ||
self.stop() | ||
self.finish() | ||
else: | ||
self.put_variables_event() | ||
self.put_event() | ||
|
||
def on_order(self, order: OrderData): | ||
"""""" | ||
def on_order(self, order: OrderData) -> None: | ||
"""委托回调""" | ||
if not order.is_active(): | ||
self.vt_orderid = "" | ||
self.order_price = 0 | ||
self.put_variables_event() | ||
self.put_event() | ||
|
||
def buy_best_limit(self): | ||
"""""" | ||
volume_left = self.volume - self.traded | ||
def buy_best_limit(self, bid_price_1: float) -> None: | ||
"""最优限价买入""" | ||
volume_left: float = self.volume - self.traded | ||
|
||
rand_volume = self.generate_rand_volume() | ||
order_volume = min(rand_volume, volume_left) | ||
rand_volume: int = self.generate_rand_volume() | ||
order_volume: float = min(rand_volume, volume_left) | ||
|
||
self.order_price = self.last_tick.bid_price_1 | ||
self.order_price = bid_price_1 | ||
self.vt_orderid = self.buy( | ||
self.vt_symbol, | ||
self.order_price, | ||
order_volume, | ||
offset=self.offset | ||
) | ||
|
||
def sell_best_limit(self): | ||
"""""" | ||
volume_left = self.volume - self.traded | ||
def sell_best_limit(self, ask_price_1: float) -> None: | ||
"""最优限价卖出""" | ||
volume_left: float = self.volume - self.traded | ||
|
||
rand_volume = self.generate_rand_volume() | ||
order_volume = min(rand_volume, volume_left) | ||
rand_volume: int = self.generate_rand_volume() | ||
order_volume: float = min(rand_volume, volume_left) | ||
|
||
self.order_price = self.last_tick.ask_price_1 | ||
self.order_price = ask_price_1 | ||
self.vt_orderid = self.sell( | ||
self.vt_symbol, | ||
self.order_price, | ||
order_volume, | ||
offset=self.offset | ||
) | ||
|
||
def generate_rand_volume(self): | ||
"""""" | ||
rand_volume = uniform(self.min_volume, self.max_volume) | ||
rand_volume = round_to(rand_volume, self.volume_change) | ||
|
||
if self.volume_change == 1: | ||
rand_volume = int(rand_volume) | ||
|
||
return rand_volume | ||
def generate_rand_volume(self) -> int: | ||
"""随机生成委托数量""" | ||
rand_volume: float = uniform(self.min_volume, self.max_volume) | ||
return int(rand_volume) |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.