add comment

This commit is contained in:
msincenselee 2015-09-16 15:34:00 +08:00
parent 4ed22c2c33
commit ccde440dd1
9 changed files with 503 additions and 25 deletions

View File

@ -2,13 +2,18 @@
# 系统模块
from Queue import Queue, Empty
print u'eventEngine.py import Queue.Queue/Empty success.'
from threading import Thread
print u'eventEngine.py import threading.Thread success.'
# 第三方模块
from PyQt4.QtCore import QTimer
print u'eventEngine.py import PyQt4.QtCore.QTimer success.'
# 自己开发的模块
from eventType import *
print u'eventEngine.py import eventType.* success.'
########################################################################
@ -52,6 +57,9 @@ class EventEngine:
#----------------------------------------------------------------------
def __init__(self):
"""初始化事件引擎"""
print u'eventEngine.py EventEngine.__init__ begin.'
# 事件队列
self.__queue = Queue()
@ -68,10 +76,14 @@ class EventEngine:
# 这里的__handlers是一个字典用来保存对应的事件调用关系
# 其中每个键对应的值是一个列表,列表中保存了对该事件进行监听的函数功能
self.__handlers = {}
print u'eventEngine.py EventEngine.__init__ end.'
#----------------------------------------------------------------------
def __run(self):
"""引擎运行"""
print u'eventEngine.py EventEngine.__run begin.'
while self.__active == True:
try:
event = self.__queue.get(block = True, timeout = 1) # 获取事件的阻塞时间设为1秒
@ -82,18 +94,22 @@ class EventEngine:
#----------------------------------------------------------------------
def __process(self, event):
"""处理事件"""
print u'eventEngine.py EventEngine.__process begin.'
# 检查是否存在对该事件进行监听的处理函数
if event.type_ in self.__handlers:
# 若存在,则按顺序将事件传递给处理函数执行
[handler(event) for handler in self.__handlers[event.type_]]
# 以上语句为Python列表解析方式的写法对应的常规循环写法为
#for handler in self.__handlers[event.type_]:
#handler(event)
print u'eventEngine.py EventEngine.__process end.'
#----------------------------------------------------------------------
def __onTimer(self):
"""向事件队列中存入计时器事件"""
# 创建计时器事件
event = Event(type_=EVENT_TIMER)
@ -103,6 +119,8 @@ class EventEngine:
#----------------------------------------------------------------------
def start(self):
"""引擎启动"""
print u'eventEngine.py EventEngine.start begin.'
# 将引擎设为启动
self.__active = True
@ -111,10 +129,14 @@ class EventEngine:
# 启动计时器计时器事件间隔默认设定为1秒
self.__timer.start(1000)
print u'eventEngine.py EventEngine.start end.'
#----------------------------------------------------------------------
def stop(self):
"""停止引擎"""
print u'eventEngine.py EventEngine.stop begin.'
# 将引擎设为停止
self.__active = False
@ -123,10 +145,15 @@ class EventEngine:
# 等待事件处理线程退出
self.__thread.join()
print u'eventEngine.py EventEngine.stop end.'
#----------------------------------------------------------------------
def register(self, type_, handler):
"""注册事件处理函数监听"""
print u'eventEngine.py EventEngine.register begin.'
# 尝试获取该事件类型对应的处理函数列表,若无则创建
try:
handlerList = self.__handlers[type_]
@ -137,10 +164,15 @@ class EventEngine:
# 若要注册的处理器不在该事件的处理器列表中,则注册该事件
if handler not in handlerList:
handlerList.append(handler)
print u'eventEngine.py EventEngine.register end.'
#----------------------------------------------------------------------
def unregister(self, type_, handler):
"""注销事件处理函数监听"""
print u'eventEngine.py EventEngine.unregister begin.'
# 尝试获取该事件类型对应的处理函数列表,若无则忽略该次注销请求
try:
handlerList = self.handlers[type_]
@ -153,7 +185,9 @@ class EventEngine:
if not handlerList:
del self.handlers[type_]
except KeyError:
pass
pass
print u'eventEngine.py EventEngine.unregister end.'
#----------------------------------------------------------------------
def put(self, event):
@ -176,8 +210,11 @@ class Event:
def test():
"""测试函数"""
import sys
print u'eventEngine.py test() import sys success.'
from datetime import datetime
print u'eventEngine.py test() import datetime.datetime success.'
from PyQt4.QtCore import QCoreApplication
print u'eventEngine.py test() import PyQt4.QtCore.QCoreApplication success.'
def simpletest(event):
print u'处理每秒触发的计时器事件:%s' % str(datetime.now())
@ -185,11 +222,14 @@ def test():
app = QCoreApplication(sys.argv)
ee = EventEngine()
#注册测试函数simpletest
ee.register(EVENT_TIMER, simpletest)
ee.start()
app.exec_()
# 直接运行脚本可以进行测试
if __name__ == '__main__':

View File

@ -2,7 +2,7 @@
from datetime import datetime
from pymongo import Connection
from pymongo import MongoClient as Connection
from pymongo.errors import *
from eventEngine import *

View File

@ -3,7 +3,7 @@
import shelve
from eventEngine import *
from pymongo import Connection
from pymongo import MongoClient as Connection
from pymongo.errors import *
from strategyEngine import *

File diff suppressed because it is too large Load Diff

View File

@ -27,10 +27,14 @@ class MainEngine:
def __init__(self):
"""Constructor"""
self.ee = EventEngine() # 创建事件驱动引擎
print u'demoEngine.MainEngine create EventEngine() success'
self.md = DemoMdApi(self.ee) # 创建API接口
print u'demoEngine.MainEngine create DemoMdApi() success'
self.td = DemoTdApi(self.ee)
print u'demoEngine.MainEngine create DemoTdApi() success'
self.ee.start() # 启动事件驱动引擎
# 循环查询持仓和账户相关

View File

@ -2,15 +2,21 @@
# 首先写系统内置模块
import sys
print u'demoStrategy.py import sys success'
from datetime import datetime, timedelta, time
print u'demoStrategy.py import datetime.datetime/timedelta/time success'
from time import sleep
print u'demoStrategy.py import time.sleep success'
# 然后是第三方库模块如PyQt4等
import sip
print u'demoStrategy.py import sip success'
from PyQt4 import QtCore
print u'demoStrategy.py import PyQt4.QtCore success'
# 然后是自己编写的模块
from demoEngine import MainEngine
print u'demoStrategy.py import demoEngine.MainEngine success'
from strategyEngine import *
@ -265,9 +271,12 @@ def main():
"""运行在CMD中的演示程度"""
# 创建PyQt4应用对象
app = QtCore.QCoreApplication(sys.argv)
print u'demoStrategy.py Main call QtCore.QCoreApplication(sys.argv) success'
# 创建主引擎对象
me = MainEngine()
print u'demoStrategy.py Main call MainEngine() success'
# 注册事件监听
me.ee.register(EVENT_LOG, print_log)

View File

@ -2,13 +2,20 @@
# 系统模块
from Queue import Queue, Empty
print u'eventEngine.py import Queue.Queue/Empty success'
from threading import Thread
print u'eventEngine.py import threading.Thread success'
# 第三方模块
from PyQt4.QtCore import QTimer
print u'eventEngine.py import PyQt4.QtCore.QTimer success'
# 自己开发的模块
from eventType import *
print u'eventEngine.py import eventType.* success'
########################################################################

View File

@ -1,11 +1,18 @@
# encoding: UTF-8
from datetime import datetime
print u'StragegyEngine.py import datetime.datetime success'
from pymongo import MongoClient as Connection
print u'demoStrategy.py import pymongo.Connection success'
from pymongo import Connection
from pymongo.errors import *
print u'demoStrategy.py import pymongo.errors.* success'
from eventEngine import *
print u'demoStrategy.py import eventEngine.* success'
# 常量定义

178
vnpy.pyproj Normal file
View File

@ -0,0 +1,178 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{5c9c864b-1736-4189-81b1-d32b67b54740}</ProjectGuid>
<ProjectHome />
<StartupFile>vn.event\eventEngine.py</StartupFile>
<SearchPath />
<WorkingDirectory>.</WorkingDirectory>
<OutputPath>.</OutputPath>
<ProjectTypeGuids>{888888a0-9f3d-457c-b088-3a5042f75d52}</ProjectTypeGuids>
<LaunchProvider>Standard Python launcher</LaunchProvider>
<InterpreterId>{2af0f10d-7135-4994-9156-5d01c9c11b7e}</InterpreterId>
<InterpreterVersion>2.7</InterpreterVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
<PropertyGroup Condition="'$(Configuration)' == 'Release'" />
<PropertyGroup>
<VisualStudioVersion Condition=" '$(VisualStudioVersion)' == '' ">10.0</VisualStudioVersion>
<PtvsTargetsFile>$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets</PtvsTargetsFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="vn.cs\vncshshlp\test\cstest.py" />
<Compile Include="vn.ctp\pyscript\ctp_data_type.py" />
<Compile Include="vn.ctp\pyscript\ctp_struct.py" />
<Compile Include="vn.ctp\pyscript\generate_data_type.py" />
<Compile Include="vn.ctp\pyscript\generate_md_functions.py" />
<Compile Include="vn.ctp\pyscript\generate_struct.py" />
<Compile Include="vn.ctp\pyscript\generate_td_functions.py" />
<Compile Include="vn.ctp\vnctpmd\test\mdtest.py" />
<Compile Include="vn.ctp\vnctptd\test\tdtest.py" />
<Compile Include="vn.datayes\api.py" />
<Compile Include="vn.datayes\errors.py" />
<Compile Include="vn.datayes\storage.py" />
<Compile Include="vn.datayes\tests.py" />
<Compile Include="vn.datayes\__init__.py" />
<Compile Include="vn.demo\ctpdemo\ctp_data_type.py" />
<Compile Include="vn.demo\ctpdemo\demoApi.py" />
<Compile Include="vn.demo\ctpdemo\demoEngine.py" />
<Compile Include="vn.demo\ctpdemo\demoMain.py" />
<Compile Include="vn.demo\ctpdemo\demoMain.pyw" />
<Compile Include="vn.demo\ctpdemo\demoUi.py" />
<Compile Include="vn.demo\ctpdemo\eventEngine.py" />
<Compile Include="vn.demo\ctpdemo\eventType.py" />
<Compile Include="vn.demo\ctpdemo\mdtest.py" />
<Compile Include="vn.demo\ctpdemo\tdtest.py" />
<Compile Include="vn.demo\ltsdemo\demoApi.py" />
<Compile Include="vn.demo\ltsdemo\demoEngine.py" />
<Compile Include="vn.demo\ltsdemo\demoMain.py" />
<Compile Include="vn.demo\ltsdemo\demoMain.pyw" />
<Compile Include="vn.demo\ltsdemo\demoUi.py" />
<Compile Include="vn.demo\ltsdemo\eventEngine.py" />
<Compile Include="vn.demo\ltsdemo\eventType.py" />
<Compile Include="vn.demo\ltsdemo\lts_data_type.py" />
<Compile Include="vn.event\eventEngine.py" />
<Compile Include="vn.event\eventType.py" />
<Compile Include="vn.lts\pyscript\generate_data_type.py" />
<Compile Include="vn.lts\pyscript\generate_md_functions.py" />
<Compile Include="vn.lts\pyscript\generate_struct.py" />
<Compile Include="vn.lts\pyscript\generate_td_functions.py" />
<Compile Include="vn.lts\pyscript\l2\generate_data_type.py" />
<Compile Include="vn.lts\pyscript\l2\generate_l2_functions.py" />
<Compile Include="vn.lts\pyscript\l2\generate_struct.py" />
<Compile Include="vn.lts\pyscript\l2\l2_data_type.py" />
<Compile Include="vn.lts\pyscript\l2\l2_struct.py" />
<Compile Include="vn.lts\pyscript\lts_data_type.py" />
<Compile Include="vn.lts\pyscript\lts_struct.py" />
<Compile Include="vn.lts\vnltsl2\test\l2test.py" />
<Compile Include="vn.lts\vnltsmd\test\mdtest.py" />
<Compile Include="vn.lts\vnltstd\test\lts_data_type.py" />
<Compile Include="vn.lts\vnltstd\test\tdtest.py" />
<Compile Include="vn.strategy\backtestingEngine.py" />
<Compile Include="vn.strategy\strategydemo\backtestingEngine.py" />
<Compile Include="vn.strategy\strategydemo\ctp_data_type.py" />
<Compile Include="vn.strategy\strategydemo\demoApi.py" />
<Compile Include="vn.strategy\strategydemo\demoBacktesting.py" />
<Compile Include="vn.strategy\strategydemo\demoEngine.py" />
<Compile Include="vn.strategy\strategydemo\demoStrategy.py" />
<Compile Include="vn.strategy\strategydemo\eventEngine.py" />
<Compile Include="vn.strategy\strategydemo\eventType.py" />
<Compile Include="vn.strategy\strategydemo\strategyEngine.py" />
<Compile Include="vn.strategy\strategyEngine.py" />
<Compile Include="vn.trader\ctpGateway.py" />
<Compile Include="vn.trader\eventEngine.py" />
<Compile Include="vn.trader\eventType.py" />
<Compile Include="vn.trader\gateway.py" />
</ItemGroup>
<ItemGroup>
<Content Include="vn.cs\hshlp\Hsconfig.ini" />
<Content Include="vn.cs\hshlp\t2sdk.ini" />
<Content Include="vn.cs\vncshshlp\test\Hsconfig.ini" />
<Content Include="vn.cs\vncshshlp\test\t2sdk.ini" />
<Content Include="vn.cs\vncshshlp\vncshshlp\ReadMe.txt" />
<Content Include="vn.ctp\vnctpmd\vnctpmd\ReadMe.txt" />
<Content Include="vn.ctp\vnctptd\vnctptd\ReadMe.txt" />
<Content Include="vn.datayes\static\figs\fig1.png" />
<Content Include="vn.datayes\static\figs\fig2.png" />
<Content Include="vn.datayes\static\figs\fig3.png" />
<Content Include="vn.datayes\static\figs\fig4.png" />
<Content Include="vn.datayes\static\figs\fig5.png" />
<Content Include="vn.datayes\static\figs\fig6.png" />
<Content Include="vn.demo\ctpdemo\exe\mdconnection\empty.txt" />
<Content Include="vn.demo\ctpdemo\exe\tdconnection\empty.txt" />
<Content Include="vn.demo\ctpdemo\mdconnection\empty.txt" />
<Content Include="vn.demo\ctpdemo\tdconnection\empty.txt" />
<Content Include="vn.demo\ctpdemo\vnpy.ico" />
<Content Include="vn.demo\ltsdemo\exe\l2connection\empty.txt" />
<Content Include="vn.demo\ltsdemo\exe\mdconnection\empty.txt" />
<Content Include="vn.demo\ltsdemo\exe\tdconnection\empty.txt" />
<Content Include="vn.demo\ltsdemo\l2connection\empty.txt" />
<Content Include="vn.demo\ltsdemo\mdconnection\empty.txt" />
<Content Include="vn.demo\ltsdemo\tdconnection\empty.txt" />
<Content Include="vn.demo\ltsdemo\vnpy.ico" />
<Content Include="vn.lts\vnltsl2\vnltsl2\ReadMe.txt" />
<Content Include="vn.lts\vnltsmd\pyltsmd\ReadMe.txt" />
<Content Include="vn.lts\vnltstd\pyltstd\ReadMe.txt" />
<Content Include="vn.strategy\strategydemo\mdconnection\empty.txt" />
<Content Include="vn.strategy\strategydemo\tdconnection\empty.txt" />
<Content Include="vn.strategy\strategydemo\vnpy.ico" />
</ItemGroup>
<ItemGroup>
<Folder Include="vn.cs\" />
<Folder Include="vn.cs\hshlp" />
<Folder Include="vn.cs\vncshshlp\" />
<Folder Include="vn.cs\vncshshlp\test" />
<Folder Include="vn.cs\vncshshlp\vncshshlp" />
<Folder Include="vn.ctp\" />
<Folder Include="vn.ctp\pyscript" />
<Folder Include="vn.ctp\vnctpmd\" />
<Folder Include="vn.ctp\vnctpmd\test" />
<Folder Include="vn.ctp\vnctpmd\vnctpmd" />
<Folder Include="vn.ctp\vnctptd\" />
<Folder Include="vn.ctp\vnctptd\test" />
<Folder Include="vn.ctp\vnctptd\vnctptd" />
<Folder Include="vn.datayes" />
<Folder Include="vn.datayes\static\" />
<Folder Include="vn.datayes\static\figs" />
<Folder Include="vn.demo\" />
<Folder Include="vn.demo\ctpdemo" />
<Folder Include="vn.demo\ctpdemo\exe\" />
<Folder Include="vn.demo\ctpdemo\exe\mdconnection" />
<Folder Include="vn.demo\ctpdemo\exe\tdconnection" />
<Folder Include="vn.demo\ctpdemo\mdconnection" />
<Folder Include="vn.demo\ctpdemo\tdconnection" />
<Folder Include="vn.demo\ltsdemo" />
<Folder Include="vn.demo\ltsdemo\exe\" />
<Folder Include="vn.demo\ltsdemo\exe\l2connection" />
<Folder Include="vn.demo\ltsdemo\exe\mdconnection" />
<Folder Include="vn.demo\ltsdemo\exe\tdconnection" />
<Folder Include="vn.demo\ltsdemo\l2connection" />
<Folder Include="vn.demo\ltsdemo\mdconnection" />
<Folder Include="vn.demo\ltsdemo\tdconnection" />
<Folder Include="vn.event" />
<Folder Include="vn.lts\" />
<Folder Include="vn.lts\pyscript" />
<Folder Include="vn.lts\pyscript\l2" />
<Folder Include="vn.lts\vnltsl2\" />
<Folder Include="vn.lts\vnltsl2\test" />
<Folder Include="vn.lts\vnltsl2\vnltsl2" />
<Folder Include="vn.lts\vnltsmd\" />
<Folder Include="vn.lts\vnltsmd\pyltsmd" />
<Folder Include="vn.lts\vnltsmd\test" />
<Folder Include="vn.lts\vnltstd\" />
<Folder Include="vn.lts\vnltstd\pyltstd" />
<Folder Include="vn.lts\vnltstd\test" />
<Folder Include="vn.strategy" />
<Folder Include="vn.strategy\strategydemo" />
<Folder Include="vn.strategy\strategydemo\mdconnection" />
<Folder Include="vn.strategy\strategydemo\tdconnection" />
<Folder Include="vn.trader" />
</ItemGroup>
<ItemGroup>
<InterpreterReference Include="{2af0f10d-7135-4994-9156-5d01c9c11b7e}\2.7" />
</ItemGroup>
<Import Project="$(PtvsTargetsFile)" Condition="Exists($(PtvsTargetsFile))" />
<Import Project="$(MSBuildToolsPath)\Microsoft.Common.targets" Condition="!Exists($(PtvsTargetsFile))" />
</Project>