[Add]algo trading app

This commit is contained in:
vn.py 2019-04-06 20:36:04 +08:00
parent 566638426c
commit be62354c1f
9 changed files with 264 additions and 0 deletions

View File

@ -0,0 +1,17 @@
from pathlib import Path
from vnpy.trader.app import BaseApp
from .engine import AlgoEngine, APP_NAME
class CtaStrategyApp(BaseApp):
""""""
app_name = APP_NAME
app_module = __module__
app_path = Path(__file__).parent
display_name = "算法交易"
engine_class = AlgoEngine
widget_name = "AlgoManager"
icon_name = "algo.ico"

View File

@ -0,0 +1,59 @@
# encoding: UTF-8
'''
动态载入所有的策略类
'''
from __future__ import print_function
import os
import importlib
import traceback
# 用来保存算法类和控件类的字典
ALGO_DICT = {}
WIDGET_DICT = {}
#----------------------------------------------------------------------
def loadAlgoModule(path, prefix):
"""使用importlib动态载入算法"""
for root, subdirs, files in os.walk(path):
for name in files:
# 只有文件名以Algo.py结尾的才是算法文件
if len(name)>7 and name[-7:] == 'Algo.py':
try:
# 模块名称需要模块路径前缀
moduleName = prefix + name.replace('.py', '')
module = importlib.import_module(moduleName)
# 获取算法类和控件类
algo = None
widget = None
for k in dir(module):
# 以Algo结尾的类是算法
if k[-4:] == 'Algo':
algo = module.__getattribute__(k)
# 以Widget结尾的类是控件
if k[-6:] == 'Widget':
widget = module.__getattribute__(k)
# 保存到字典中
if algo and widget:
ALGO_DICT[algo.templateName] = algo
WIDGET_DICT[algo.templateName] = widget
except:
print ('-' * 20)
print ('Failed to import strategy file %s:' %moduleName)
traceback.print_exc()
# 遍历algo目录下的文件
path1 = os.path.abspath(os.path.dirname(__file__))
loadAlgoModule(path1, 'vnpy.trader.app.algoTrading.algo.')
# 遍历工作目录下的文件
path2 = os.getcwd()
loadAlgoModule(path2, '')

View File

View File

@ -0,0 +1,65 @@
from vnpy.event import EventEngine
from vnpy.trader.engine import BaseEngine, MainEngine
from vnpy.trader.event import (EVENT_TICK, EVENT_TIMER, EVENT_ORDER, EVENT_TRADE)
class AlgoEngine(BaseEngine):
""""""
def __init__(self, main_engine: MainEngine, event_engine: EventEngine):
"""Constructor"""
super().__init__(main_engine, event_engine)
self.algos = {}
self.symbol_algo_map = {}
self.orderid_algo_map = {}
self.register_event()
def register_event(self):
""""""
self.event_engine.register(EVENT_TICK, self.process_tick_event)
self.event_engine.register(EVENT_TIMER, self.process_timer_event)
self.event_engine.register(EVENT_ORDER, self.process_order_event)
self.event_engine.register(EVENT_TRADE, self.process_trade_event)
def process_tick_event(self):
""""""
pass
def process_timer_event(self):
""""""
pass
def process_trade_event(self):
""""""
pass
def process_order_event(self):
""""""
pass
def start_algo(self, setting: dict):
""""""
pass
def stop_algo(self, algo_name: dict):
""""""
pass
def stop_all(self):
""""""
pass
def subscribe(self, algo, vt_symbol):
""""""
pass
def send_order(
self,
algo,
vt_symbol
):
""""""
pass

View File

@ -0,0 +1,123 @@
from vnpy.trader.engine import BaseEngine
from vnpy.trader.object import TickData, OrderData, TradeData
from vnpy.trader.constant import OrderType, Offset
class AlgoTemplate:
""""""
count = 0
def __init__(
self,
algo_engine: BaseEngine,
algo_name: str,
setting: dict
):
"""Constructor"""
self.algo_engine = algo_engine
self.algo_name = algo_name
self.active = False
self.active_orders = {} # vt_orderid:order
@staticmethod
def new(cls, algo_engine:BaseEngine, setting: dict):
"""Create new algo instance"""
cls.count += 1
algo_name = f"{cls.__name__}_{cls.count}"
algo = cls(algo_engine, algo_name, setting)
def update_tick(self, tick: TickData):
""""""
if self.active:
self.on_tick(tick)
def update_order(self, order: OrderData):
""""""
if self.active:
if order.is_active():
self.active_orders[order.vt_orderid] = order
elif order.vt_orderid in self.active_orders:
self.active_orders.pop(order.vt_orderid)
self.on_order(order)
def update_trade(self, trade: TradeData):
""""""
if self.active:
self.on_trade(trade)
def update_timer(self):
""""""
if self.active:
self.on_timer()
def on_start(self):
""""""
pass
def on_stop(self):
""""""
pass
def on_tick(self, tick: TickData):
""""""
pass
def on_order(self, order: OrderData):
""""""
pass
def on_trade(self, trade: TradeData):
""""""
pass
def on_timer(self):
""""""
pass
def start(self):
""""""
pass
def stop(self):
""""""
pass
def buy(
self,
vt_symbol,
price,
volume,
order_type: OrderType = OrderType.LIMIT,
offset: Offset = Offset.NONE
):
""""""
return self.algo_engine.buy(
vt_symbol,
price,
volume,
order_type,
offset
)
def sell(
self,
vt_symbol,
price,
volume,
order_type: OrderType = OrderType.LIMIT,
offset: Offset = Offset.NONE
):
""""""
return self.algo_engine.buy(
vt_symbol,
price,
volume,
order_type,
offset
)

Binary file not shown.

After

Width:  |  Height:  |  Size: 66 KiB

View File