-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathuiCtaKLine.py
149 lines (131 loc) · 5.24 KB
/
uiCtaKLine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# -*- coding: utf-8 -*-
from uiBasicIO import uiBasicIO
from uiKLine import KLineWidget
# PyQt
from qtpy.QtGui import *
from qtpy.QtCore import *
from qtpy.QtWidgets import *
from qtpy import QtGui,QtCore
from collections import deque
import pandas as pd
########################################################################
class ctaKLine(uiBasicIO):
"""K线向量化回测工具"""
dbClient = None
signal = QtCore.Signal(type({}))
# ----------------------------------------------------------------------
def __init__(self, ctaEngine, eventEngine, parent=None):
"""初始化函数"""
import ctaBase
super(ctaKLine,self).__init__(parent,\
'json\\uiCtaKLine_input.json',\
'json\\uiCtaKLine_button.json') # 输入配置文件,按钮配置文件
# 用于计算因子的数据
self.bars = deque([])
self.pdBars = pd.DataFrame()
self.signals = deque([])
self.signalsOpen = deque([])
self.states = []
self.pnl = []
self.stateData = {}
self.spdData = pd.DataFrame()
self.datas = None
self.vtSymbol = ""
self.vtSymbol1 = ""
self.mode = 'deal'
self.canvas = KLineWidget(self)
self.signal.connect(self.loadData)
self.initUi()
# -----------------------------------------------
def loadData(self,data):
"""载入所有数据"""
self.bars = data['bar']
self.states = data['state']
self.pnl = data['pnl']
self.signals = data['deal']
self.signalsOpen = data['dealOpen']
kTool = self.canvas
for sig in kTool.sigPlots:
kTool.pwKL.removeItem(kTool.sigPlots[sig])
kTool.sigData = {}
kTool.sigPlots = {}
for sig in kTool.subSigPlots:
kTool.pwOI.removeItem(kTool.subSigPlots[sig])
kTool.subSigData = {}
kTool.subSigPlots = {}
print u'正在准备回测结果数据....'
self.canvas.clearData()
self.pdBars = pd.DataFrame(list(self.bars))
self.pdBars = self.pdBars[['datetime','open','close','low','high','volume','openInterest']].set_index('datetime')
self.canvas.loadData(self.pdBars)
self.canvas.updateSig(self.signals,self.signalsOpen)
self.spdData = pd.DataFrame(self.states)
self.stateData = self.spdData.to_records()
allState = filter(lambda s:s not in ['trading','inited','pos'],self.spdData.columns)
allState = filter(lambda s:isinstance(self.spdData[s][0], (int, long, float)),allState)
self.spdData['pnl'] = self.pnl
self.spdData = self.spdData[self.spdData['pnl']!=0][allState+['pnl']]
#self.spdData['pnl'] = ['p' if p > 0 else 'l' for p in self.pnl]
self.editDict['signalName'].clear()
self.editDict['signalName'].addItems(allState)
print u'数据准备完成!'
#-----------------------------------------------
def loadSig(self,data):
"""载入K线信号"""
self.states = data['state']
self.pnl = data['pnl']
self.signals = data['deal']
self.signalsOpen = data['dealOpen']
kTool = self.canvas
for sig in kTool.sigPlots:
kTool.pwKL.removeItem(kTool.sigPlots[sig])
kTool.sigData = {}
kTool.sigPlots = {}
for sig in kTool.subSigPlots:
kTool.pwOI.removeItem(kTool.subSigPlots[sig])
kTool.subSigData = {}
kTool.subSigPlots = {}
self.canvas.updateSig(self.signals)
self.spdData = pd.DataFrame(self.states)
self.stateData = self.spdData.to_records()
allState = filter(lambda s:s not in ['trading','inited','pos'],self.spdData.columns)
allState = filter(lambda s:isinstance(self.spdData[s][0], (int, long, float)),allState)
self.spdData['pnl'] = self.pnl
self.spdData = self.spdData[self.spdData['pnl']!=0][allState+['pnl']]
#self.spdData['pnl'] = ['p' if p > 0 else 'l' for p in self.pnl]
self.editDict['signalName'].clear()
self.editDict['signalName'].addItems(allState)
#----------------------------------------------------------------------
def initUi(self):
"""初始化界面"""
hbox = QHBoxLayout()
hbox.addWidget(self.groupInput)
hbox.addWidget(self.groupProcess)
vbox = QVBoxLayout()
vbox.addLayout(hbox)
vbox.addWidget(self.canvas)
self.setLayout(vbox)
#----------------------------------------------------------------------
def setSymbol(self, symbol):
"""设置合约信息"""
self.vtSymbol = symbol
self.editDict['symbol'].setText(symbol)
if '-' in symbol:
self.vtSymbol = symbol.split('-')[0]
self.vtSymbol1 = symbol.split('-')[1]
else:
self.vtSymbol1 = None
########################################################################
import sys
if __name__ == '__main__':
app = QApplication(sys.argv)
# 界面设置
cfgfile = QtCore.QFile('css.qss')
cfgfile.open(QtCore.QFile.ReadOnly)
styleSheet = cfgfile.readAll()
styleSheet = unicode(styleSheet, encoding='utf8')
app.setStyleSheet(styleSheet)
# K线界面
ui = ctaKLine()
ui.show()
app.exec_()