Merge pull request #1767 from 1122455801/stop_algo_01

[Add] DMA algo
This commit is contained in:
vn.py 2019-05-31 09:16:04 +08:00 committed by GitHub
commit ef8ca962db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 102 additions and 1 deletions

View File

@ -0,0 +1,99 @@
from vnpy.trader.constant import Offset, Direction, OrderType
from vnpy.trader.object import TradeData, OrderData, TickData
from vnpy.trader.engine import BaseEngine
from vnpy.app.algo_trading import AlgoTemplate
class DmaAlgo(AlgoTemplate):
""""""
display_name = "DMA 直接委托"
default_setting = {
"vt_symbol": "",
"direction": [Direction.LONG.value, Direction.SHORT.value],
"order_type": [
OrderType.MARKET.value,
OrderType.LIMIT.value,
OrderType.STOP.value,
OrderType.FAK.value,
OrderType.FOK.value
],
"price": 0.0,
"volume": 0.0,
"offset": [
Offset.NONE.value,
Offset.OPEN.value,
Offset.CLOSE.value,
Offset.CLOSETODAY.value,
Offset.CLOSEYESTERDAY.value
]
}
variables = [
"traded",
"vt_orderid",
"order_status",
]
def __init__(
self,
algo_engine: BaseEngine,
algo_name: str,
setting: dict
):
""""""
super().__init__(algo_engine, algo_name, setting)
# Parameters
self.vt_symbol = setting["vt_symbol"]
self.direction = Direction(setting["direction"])
self.order_type = OrderType(setting["order_type"])
self.price = setting["price"]
self.volume = setting["volume"]
self.offset = Offset(setting["offset"])
# Variables
self.vt_orderid = ""
self.traded = 0
self.order_status = ""
self.subscribe(self.vt_symbol)
self.put_parameters_event()
self.put_variables_event()
def on_tick(self, tick: TickData):
""""""
if not self.vt_orderid:
if self.direction == Direction.LONG:
self.vt_orderid = self.buy(
self.vt_symbol,
self.price,
self.volume,
self.order_type,
self.offset
)
else:
self.vt_orderid = self.sell(
self.vt_symbol,
self.price,
self.volume,
self.order_type,
self.offset
)
self.put_variables_event()
def on_order(self, order: OrderData):
""""""
self.traded = order.traded
self.order_status = order.status
if not order.is_active():
self.stop()
self.put_variables_event()
def on_trade(self, trade: TradeData):
""""""
pass

View File

@ -85,7 +85,7 @@ class StopAlgo(AlgoTemplate):
if tick.limit_down: if tick.limit_down:
price = max(price, tick.limit_down) price = max(price, tick.limit_down)
self.vt_orderid = self.buy( self.vt_orderid = self.sell(
self.vt_symbol, self.vt_symbol,
price, price,
self.volume, self.volume,

View File

@ -49,6 +49,7 @@ class AlgoEngine(BaseEngine):
from .algos.stop_algo import StopAlgo from .algos.stop_algo import StopAlgo
from .algos.best_limit_algo import BestLimitAlgo from .algos.best_limit_algo import BestLimitAlgo
from .algos.grid_algo import GridAlgo from .algos.grid_algo import GridAlgo
from .algos.dma_algo import DmaAlgo
self.add_algo_template(TwapAlgo) self.add_algo_template(TwapAlgo)
self.add_algo_template(IcebergAlgo) self.add_algo_template(IcebergAlgo)
@ -56,6 +57,7 @@ class AlgoEngine(BaseEngine):
self.add_algo_template(StopAlgo) self.add_algo_template(StopAlgo)
self.add_algo_template(BestLimitAlgo) self.add_algo_template(BestLimitAlgo)
self.add_algo_template(GridAlgo) self.add_algo_template(GridAlgo)
self.add_algo_template(DmaAlgo)
def add_algo_template(self, template: AlgoTemplate): def add_algo_template(self, template: AlgoTemplate):
"""""" """"""