Home >  > Vn.py学习记录五–Widgets开发

Vn.py学习记录五–Widgets开发

0

一、首先准备图片以及文件

二、demoEngine.py

from vnpy.event import Event
from vnpy.trader.vtEvent import EVENT_TICK

from vnpy.trader.vtObject import VtSubscribeReq

EVENT_DEMO_LOG = "eDemolog"

class DemoEngine(object):
	def __init__(self,mainEngine,eventEngine):
		self.mainEngine = mainEngine
		self.eventEngine = eventEngine

		sefl.symbol = 'rb1805'
		self.priceDict = {}


		self.registerEvent()

	def subscribeData(self):
		req = VtSubscribeReq()
		req.symbol = self.symbol
		slef.mainEngine.subscribe(req,'CTP')

	def registerEvent(self):
		sefl.eventEngine.register(EVENT_TICK,self.processTickEvent)

	def processTickEvent(self,event):
		tick = event.dict_['data']
		last = tick.lastPrice

		if self.priceDict:
			if last >= self.priceDict['ask']:
				result = "外盘"
			elif last <= self.priceDict['bid']:
				result = "内盘"
			else:
				result = "中性"

			self.writeLog("最新Tick的成交价为s%,交易方向为:s%" %(last,result))
		
		self.priceDict['bid'] = tick.bidPrice
		self.priceDict['ask'] = tick.askPrice

	def writeLog(self,msg):
		event = Event(EVENT_DEMO_LOG)
		event.dict_['data'] = msg
		self.eventEngine.put(event)

三、uiDemoWidget.py

from datetime import datetime
from .demoEngine import EVENT_DEMO_LOG
from vnpy.event import event
from vnpy.trader.uiQt import QtCore,QtWidgets

class ClassName(QtWidgets.Qwidget):
	signalLog = QtCore.Signal(type(Event()))



	"""docstring for ClassName"""
	def __init__(self, demoEngine,eventEngine,parent=None):
		super(DemoWidget, self).__init__(parent)
		
		self.demoEngine = demoEngine
		self.eventEngine = eventEngine

		self.initUi()
		self.registerEvent()

	def initUi(self):
		self.setWindowTitle("DemoAPP")

		self.botton = QtWidgets.QPushButton("订阅行情")
		self.button.click.connect(self.demoEngine.subscribeData)

		self.logMoniter = QtWidgets.QTextEdit()
		self.logMoniter.setReadOnly(True)

		#横向排版
		hbox = QtWidgets.QHBoxLayout()
		hbox = addWidget(self.button)
		hbox.addStretch()
        
        #竖向排版
		vbox = QtWidgets.QVBoxLayout()
		vbox = addLayout(hbox)
		vbox.addWidget(self.logMoniter)

		self.selfLayout(vbox)


	def registerEvent():
		self.signalLog.connect(self.processLogEvent)
		sefl.eventEngine.register(EVENT_DEMO_LOG,self.signalLog.emit)


	def processLogEvent(self,event):
		msg = event.dict_['data']
		t = datetime.now()

		mas = str(t)+ "  " + msg
		self.logMoniter.append(msg)	
			

四、init.py

from demoEngine import DemoEngine
from uiDemoWidget import DemoWidget

app_name = "DemoAPP"
appDisplayName = "DemoAPP"
appEngine = DemoEngine
appWidget = DemoWidget
appIco = "demo.ico"

然后再修改run.py,就可以跑起来测试了。

本文暂无标签

发表评论

*

*