-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemoUi.py
1287 lines (1032 loc) · 46.1 KB
/
demoUi.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# encoding: UTF-8
"""
该文件中包含的是交易平台的上层UI部分,
通过图形界面调用中间层的主动函数,并监控相关数据更新。
Monitor主要负责监控数据,有部分包含主动功能。
Widget主要用于调用主动功能,有部分包含数据监控。
"""
from __future__ import division
import time
import sys
import shelve
from collections import OrderedDict
import sip
from PyQt4 import QtCore, QtGui
from eventEngine import *
########################################################################
class LogMonitor(QtGui.QTableWidget):
"""用于显示日志"""
signal = QtCore.pyqtSignal(type(Event()))
#----------------------------------------------------------------------
def __init__(self, eventEngine, parent=None):
"""Constructor"""
super(LogMonitor, self).__init__(parent)
self.__eventEngine = eventEngine
self.initUi()
self.registerEvent()
#----------------------------------------------------------------------
def initUi(self):
"""初始化界面"""
self.setWindowTitle(u'日志')
self.setColumnCount(2)
self.setHorizontalHeaderLabels([u'时间', u'日志'])
self.verticalHeader().setVisible(False) # 关闭左边的垂直表头
self.setEditTriggers(QtGui.QTableWidget.NoEditTriggers) # 设为不可编辑状态
# 自动调整列宽
self.horizontalHeader().setResizeMode(0, QtGui.QHeaderView.ResizeToContents)
self.horizontalHeader().setResizeMode(1, QtGui.QHeaderView.Stretch)
#----------------------------------------------------------------------
def registerEvent(self):
"""注册事件监听"""
# Qt图形组件的GUI更新必须使用Signal/Slot机制,否则有可能导致程序崩溃
# 因此这里先将图形更新函数作为Slot,和信号连接起来
# 然后将信号的触发函数注册到事件驱动引擎中
self.signal.connect(self.updateLog)
self.__eventEngine.register(EVENT_LOG, self.signal.emit)
#----------------------------------------------------------------------
def updateLog(self, event):
"""更新日志"""
# 获取当前时间和日志内容
t = time.strftime('%H:%M:%S',time.localtime(time.time()))
log = event.dict_['log']
# 在表格最上方插入一行
self.insertRow(0)
# 创建单元格
cellTime = QtGui.QTableWidgetItem(t)
cellLog = QtGui.QTableWidgetItem(log)
# 将单元格插入表格
self.setItem(0, 0, cellTime)
self.setItem(0, 1, cellLog)
########################################################################
class AccountMonitor(QtGui.QTableWidget):
"""用于显示账户"""
signal = QtCore.pyqtSignal(type(Event()))
dictLabels = OrderedDict()
dictLabels['AccountID'] = u'投资者账户'
dictLabels['PreBalance'] = u'昨结'
dictLabels['Withdraw'] = u'出金'
dictLabels['Deposit'] = u'入金'
dictLabels['FrozenCash'] = u'冻结资金'
dictLabels['FrozenMargin'] = u'冻结保证金'
dictLabels['FrozenCommission'] = u'冻结手续费'
dictLabels['FrozenTransferFee'] = u'冻结过户费'
dictLabels['FrozenStampTax'] = u'冻结印花税'
dictLabels['Commission'] = u'手续费'
dictLabels['TransferFee'] = u'过户费'
dictLabels['StampTax'] = u'印花税'
dictLabels['CurrMargin'] = u'当前保证金'
dictLabels['Available'] = u'可用资金'
dictLabels['WithdrawQuota'] = u'可取资金'
#----------------------------------------------------------------------
def __init__(self, eventEngine, parent=None):
"""Constructor"""
super(AccountMonitor, self).__init__(parent)
self.__eventEngine = eventEngine
self.dictAccount = {} # 用来保存账户对应的单元格
self.initUi()
self.registerEvent()
#----------------------------------------------------------------------
def initUi(self):
""""""
self.setWindowTitle(u'账户')
self.setColumnCount(len(self.dictLabels))
self.setHorizontalHeaderLabels(self.dictLabels.values())
self.verticalHeader().setVisible(False) # 关闭左边的垂直表头
self.setEditTriggers(QtGui.QTableWidget.NoEditTriggers) # 设为不可编辑状态
#----------------------------------------------------------------------
def registerEvent(self):
""""""
self.signal.connect(self.updateAccount)
self.__eventEngine.register(EVENT_ACCOUNT, self.signal.emit)
#----------------------------------------------------------------------
def updateAccount(self, event):
""""""
data = event.dict_['data']
accountid = data['AccountID']
# 如果之前已经收到过这个账户的数据, 则直接更新
if accountid in self.dictAccount:
d = self.dictAccount[accountid]
for label, cell in d.items():
cell.setText(str(data[label]))
# 否则插入新的一行,并更新
else:
self.insertRow(0)
d = {}
for col, label in enumerate(self.dictLabels.keys()):
cell = QtGui.QTableWidgetItem(str(data[label]))
self.setItem(0, col, cell)
d[label] = cell
self.dictAccount[accountid] = d
########################################################################
class TradeMonitor(QtGui.QTableWidget):
"""用于显示成交记录"""
signal = QtCore.pyqtSignal(type(Event()))
dictLabels = OrderedDict()
dictLabels['InstrumentID'] = u'合约代码'
dictLabels['ExchangeInstID'] = u'交易所合约代码'
dictLabels['ExchangeID'] = u'交易所'
dictLabels['Direction'] = u'方向'
dictLabels['OffsetFlag'] = u'开平'
dictLabels['TradeID'] = u'成交编号'
dictLabels['TradeTime'] = u'成交时间'
dictLabels['Volume'] = u'数量'
dictLabels['Price'] = u'价格'
dictLabels['OrderRef'] = u'报单号'
dictLabels['OrderSysID'] = u'报单系统号'
dictDirection = {}
dictDirection['0'] = u'买'
dictDirection['1'] = u'卖'
dictDirection['2'] = u'ETF申购'
dictDirection['3'] = u'ETF赎回'
dictDirection['4'] = u'ETF现金替代'
dictDirection['5'] = u'债券入库'
dictDirection['6'] = u'债券出库'
dictDirection['7'] = u'配股'
dictDirection['8'] = u'转托管'
dictDirection['9'] = u'信用账户配股'
dictDirection['A'] = u'担保品买入'
dictDirection['B'] = u'担保品卖出'
dictDirection['C'] = u'担保品转入'
dictDirection['D'] = u'担保品转出'
dictDirection['E'] = u'融资买入'
dictDirection['F'] = u'融资卖出'
dictDirection['G'] = u'卖券还款'
dictDirection['H'] = u'买券还券'
dictDirection['I'] = u'直接还款'
dictDirection['J'] = u'直接换券'
dictDirection['K'] = u'余券划转'
dictDirection['L'] = u'OF申购'
dictDirection['M'] = u'OF赎回'
dictDirection['N'] = u'SF拆分'
dictDirection['O'] = u'SF合并'
dictDirection['P'] = u'备兑'
dictDirection['Q'] = u'证券冻结/解冻'
dictDirection['R'] = u'行权'
dictOffset = {}
dictOffset['0'] = u'开仓'
dictOffset['1'] = u'平仓'
dictOffset['2'] = u'强平'
dictOffset['3'] = u'平今'
dictOffset['4'] = u'平昨'
dictOffset['5'] = u'强减'
dictOffset['6'] = u'本地强平'
#----------------------------------------------------------------------
def __init__(self, eventEngine, parent=None):
"""Constructor"""
super(TradeMonitor, self).__init__(parent)
self.__eventEngine = eventEngine
self.initUi()
self.registerEvent()
#----------------------------------------------------------------------
def initUi(self):
""""""
self.setWindowTitle(u'成交')
self.setColumnCount(len(self.dictLabels))
self.setHorizontalHeaderLabels(self.dictLabels.values())
self.verticalHeader().setVisible(False) # 关闭左边的垂直表头
self.setEditTriggers(QtGui.QTableWidget.NoEditTriggers) # 设为不可编辑状态
#----------------------------------------------------------------------
def registerEvent(self):
""""""
self.signal.connect(self.updateTrade)
self.__eventEngine.register(EVENT_TRADE, self.signal.emit)
#----------------------------------------------------------------------
def updateTrade(self, event):
""""""
data = event.dict_['data']
self.insertRow(0)
for col, label in enumerate(self.dictLabels.keys()):
if label == 'Direction':
try:
value = self.dictDirection[data[label]]
except KeyError:
value = u'未知类型'
elif label == 'OffsetFlag':
try:
value = self.dictOffset[data[label]]
except KeyError:
value = u'未知类型'
else:
value = str(data[label])
cell = QtGui.QTableWidgetItem(value)
self.setItem(0, col, cell)
########################################################################
class PositionMonitor(QtGui.QTableWidget):
"""用于显示持仓"""
signal = QtCore.pyqtSignal(type(Event()))
dictLabels = OrderedDict()
dictLabels['InstrumentID'] = u'合约代码'
dictLabels['PosiDirection'] = u'方向'
dictLabels['Position'] = u'持仓'
dictLabels['LongFrozen'] = u'多头冻结'
dictLabels['ShortFrozen'] = u'空头冻结'
dictLabels['PositionCost'] = u'持仓成本'
dictLabels['OpenCost'] = u'开仓成本'
dictLabels['StockValue'] = u'证券价值'
dictPosiDirection = {}
dictPosiDirection['1'] = u'净'
dictPosiDirection['2'] = u'多'
dictPosiDirection['3'] = u'空'
#----------------------------------------------------------------------
def __init__(self, eventEngine, parent=None):
"""Constructor"""
super(PositionMonitor, self).__init__(parent)
self.__eventEngine = eventEngine
self.dictPosition = {} # 用来保存持仓对应的单元格
self.initUi()
self.registerEvent()
#----------------------------------------------------------------------
def initUi(self):
""""""
self.setWindowTitle(u'持仓')
self.setColumnCount(len(self.dictLabels))
self.setHorizontalHeaderLabels(self.dictLabels.values())
self.verticalHeader().setVisible(False) # 关闭左边的垂直表头
self.setEditTriggers(QtGui.QTableWidget.NoEditTriggers) # 设为不可编辑状态
#----------------------------------------------------------------------
def registerEvent(self):
""""""
self.signal.connect(self.updatePosition)
self.__eventEngine.register(EVENT_POSITION, self.signal.emit)
#----------------------------------------------------------------------
def updatePosition(self, event):
""""""
data = event.dict_['data']
# 过滤返回值为空的情况
if data['InstrumentID']:
posid = data['InstrumentID'] + '.' + data['PosiDirection']
# 如果之前已经收到过这个账户的数据, 则直接更新
if posid in self.dictPosition:
d = self.dictPosition[posid]
for label, cell in d.items():
if label == 'PosiDirection':
try:
value = self.dictPosiDirection[data[label]]
except KeyError:
value = u'未知类型'
else:
value = str(data[label])
cell.setText(value)
# 否则插入新的一行,并更新
else:
self.insertRow(0)
d = {}
for col, label in enumerate(self.dictLabels.keys()):
if label == 'PosiDirection':
try:
value = self.dictPosiDirection[data[label]]
except KeyError:
value = u'未知类型'
else:
value = str(data[label])
cell = QtGui.QTableWidgetItem(value)
self.setItem(0, col, cell)
d[label] = cell
self.dictPosition[posid] = d
########################################################################
class OrderMonitor(QtGui.QTableWidget):
"""用于显示所有报单"""
signal = QtCore.pyqtSignal(type(Event()))
dictLabels = OrderedDict()
dictLabels['OrderRef'] = u'报单号'
dictLabels['OrderSysID'] = u'系统编号'
dictLabels['InstrumentID'] = u'合约代码'
dictLabels['ExchangeInstID'] = u'交易所合约代码'
dictLabels['Direction'] = u'方向'
dictLabels['CombOffsetFlag'] = u'开平'
dictLabels['LimitPrice'] = u'价格'
dictLabels['VolumeTotalOriginal'] = u'委托数量'
dictLabels['VolumeTraded'] = u'成交数量'
dictLabels['InsertTime'] = u'委托时间'
dictLabels['CancelTime'] = u'撤销时间'
dictLabels['StatusMsg'] = u'状态信息'
dictDirection = {}
dictDirection['0'] = u'买'
dictDirection['1'] = u'卖'
dictDirection['2'] = u'ETF申购'
dictDirection['3'] = u'ETF赎回'
dictDirection['4'] = u'ETF现金替代'
dictDirection['5'] = u'债券入库'
dictDirection['6'] = u'债券出库'
dictDirection['7'] = u'配股'
dictDirection['8'] = u'转托管'
dictDirection['9'] = u'信用账户配股'
dictDirection['A'] = u'担保品买入'
dictDirection['B'] = u'担保品卖出'
dictDirection['C'] = u'担保品转入'
dictDirection['D'] = u'担保品转出'
dictDirection['E'] = u'融资买入'
dictDirection['F'] = u'融资卖出'
dictDirection['G'] = u'卖券还款'
dictDirection['H'] = u'买券还券'
dictDirection['I'] = u'直接还款'
dictDirection['J'] = u'直接换券'
dictDirection['K'] = u'余券划转'
dictDirection['L'] = u'OF申购'
dictDirection['M'] = u'OF赎回'
dictDirection['N'] = u'SF拆分'
dictDirection['O'] = u'SF合并'
dictDirection['P'] = u'备兑'
dictDirection['Q'] = u'证券冻结/解冻'
dictDirection['R'] = u'行权'
dictOffset = {}
dictOffset['0'] = u'开仓'
dictOffset['1'] = u'平仓'
dictOffset['2'] = u'强平'
dictOffset['3'] = u'平今'
dictOffset['4'] = u'平昨'
dictOffset['5'] = u'强减'
dictOffset['6'] = u'本地强平'
#----------------------------------------------------------------------
def __init__(self, eventEngine, mainEngine, parent=None):
"""Constructor"""
super(OrderMonitor, self).__init__(parent)
self.__eventEngine = eventEngine
self.__mainEngine = mainEngine
self.dictOrder = {} # 用来保存报单号对应的单元格对象
self.dictOrderData = {} # 用来保存报单数据
self.initUi()
self.registerEvent()
#----------------------------------------------------------------------
def initUi(self):
""""""
self.setWindowTitle(u'报单')
self.setColumnCount(len(self.dictLabels))
self.setHorizontalHeaderLabels(self.dictLabels.values())
self.verticalHeader().setVisible(False) # 关闭左边的垂直表头
self.setEditTriggers(QtGui.QTableWidget.NoEditTriggers) # 设为不可编辑状态
#----------------------------------------------------------------------
def registerEvent(self):
""""""
self.signal.connect(self.updateOrder)
self.__eventEngine.register(EVENT_ORDER, self.signal.emit)
self.itemDoubleClicked.connect(self.cancelOrder)
#----------------------------------------------------------------------
def updateOrder(self, event):
""""""
data = event.dict_['data']
orderref = data['OrderRef']
self.dictOrderData[orderref] = data
# 如果之前已经收到过这个订单的数据, 则直接更新
if orderref in self.dictOrder:
d = self.dictOrder[orderref]
for label, cell in d.items():
if label == 'Direction':
try:
value = self.dictDirection[data[label]]
except KeyError:
value = u'未知类型'
elif label == 'CombOffsetFlag':
try:
value = self.dictOffset[data[label]]
except KeyError:
value = u'未知类型'
elif label == 'StatusMsg':
value = data[label].decode('gbk')
else:
value = str(data[label])
cell.setText(value)
# 否则插入新的一行,并更新
else:
self.insertRow(0)
d = {}
for col, label in enumerate(self.dictLabels.keys()):
if label == 'Direction':
try:
value = self.dictDirection[data[label]]
except KeyError:
value = u'未知类型'
elif label == 'CombOffsetFlag':
try:
value = self.dictOffset[data[label]]
except KeyError:
value = u'未知类型'
elif label == 'StatusMsg':
value = data[label].decode('gbk')
else:
value = str(data[label])
cell = QtGui.QTableWidgetItem(value)
self.setItem(0, col, cell)
d[label] = cell
cell.orderref = orderref # 动态绑定报单号到单元格上
self.dictOrder[orderref] = d
#----------------------------------------------------------------------
def cancelOrder(self, cell):
"""双击撤单"""
print u'撤单指令'
orderref = cell.orderref
order = self.dictOrderData[orderref]
# 撤单前检查报单是否已经撤销或者全部成交
if not (order['OrderStatus'] == '0' or order['OrderStatus'] == '5'):
self.__mainEngine.cancelOrder(order['InstrumentID'],
order['ExchangeID'],
orderref,
order['FrontID'],
order['SessionID'])
print u'发出撤单'
#----------------------------------------------------------------------
def cancelAll(self):
"""全撤"""
print u'全撤'
for order in self.dictOrderData.values():
if not (order['OrderStatus'] == '0' or order['OrderStatus'] == '5'):
self.__mainEngine.cancelOrder(order['InstrumentID'],
order['ExchangeID'],
order['OrderRef'],
order['FrontID'],
order['SessionID'])
########################################################################
class MarketDataMonitor(QtGui.QTableWidget):
"""用于显示行情"""
signal = QtCore.pyqtSignal(type(Event()))
dictLabels = OrderedDict()
dictLabels['Name'] = u'合约名称'
dictLabels['InstrumentID'] = u'合约代码'
dictLabels['ExchangeInstID'] = u'合约交易所代码'
dictLabels['BidPrice1'] = u'买一价'
dictLabels['BidVolume1'] = u'买一量'
dictLabels['AskPrice1'] = u'卖一价'
dictLabels['AskVolume1'] = u'卖一量'
dictLabels['LastPrice'] = u'最新价'
dictLabels['Volume'] = u'成交量'
dictLabels['UpdateTime'] = u'更新时间'
#----------------------------------------------------------------------
def __init__(self, eventEngine, mainEngine, parent=None):
"""Constructor"""
super(MarketDataMonitor, self).__init__(parent)
self.__eventEngine = eventEngine
self.__mainEngine = mainEngine
self.dictData = {}
self.initUi()
self.registerEvent()
#----------------------------------------------------------------------
def initUi(self):
""""""
self.setWindowTitle(u'行情')
self.setColumnCount(len(self.dictLabels))
self.setHorizontalHeaderLabels(self.dictLabels.values())
self.verticalHeader().setVisible(False) # 关闭左边的垂直表头
self.setEditTriggers(QtGui.QTableWidget.NoEditTriggers) # 设为不可编辑状态
#----------------------------------------------------------------------
def registerEvent(self):
""""""
self.signal.connect(self.updateData)
self.__eventEngine.register(EVENT_MARKETDATA, self.signal.emit)
#----------------------------------------------------------------------
def updateData(self, event):
""""""
data = event.dict_['data']
instrumentid = data['InstrumentID']
# 如果之前已经收到过这个账户的数据, 则直接更新
if instrumentid in self.dictData:
d = self.dictData[instrumentid]
for label, cell in d.items():
if label != 'Name':
value = str(data[label])
else:
value = self.getName(data['InstrumentID'])
cell.setText(value)
# 否则插入新的一行,并更新
else:
row = self.rowCount()
self.insertRow(row)
d = {}
for col, label in enumerate(self.dictLabels.keys()):
if label != 'Name':
value = str(data[label])
cell = QtGui.QTableWidgetItem(value)
self.setItem(row, col, cell)
d[label] = cell
else:
name = self.getName(data['InstrumentID'])
cell = QtGui.QTableWidgetItem(name)
self.setItem(row, col, cell)
d[label] = cell
self.dictData[instrumentid] = d
#----------------------------------------------------------------------
def getName(self, instrumentid):
"""获取名称"""
instrument = self.__mainEngine.selectInstrument(instrumentid)
if instrument:
return instrument['InstrumentName'].decode('GBK')
else:
return ''
########################################################################
class LoginWidget(QtGui.QDialog):
"""登录"""
#----------------------------------------------------------------------
def __init__(self, mainEngine, parent=None):
"""Constructor"""
super(LoginWidget, self).__init__()
self.__mainEngine = mainEngine
self.initUi()
self.loadData()
#----------------------------------------------------------------------
def initUi(self):
"""初始化界面"""
self.setWindowTitle(u'登录')
# 设置组件
labelUserID = QtGui.QLabel(u'账号:')
labelMdPassword = QtGui.QLabel(u'行情密码:')
labelTdPassword = QtGui.QLabel(u'交易密码:')
labelMdAddress = QtGui.QLabel(u'行情服务器:')
labelTdAddress = QtGui.QLabel(u'交易服务器:')
self.editUserID = QtGui.QLineEdit()
self.editMdPassword = QtGui.QLineEdit()
self.editTdPassword = QtGui.QLineEdit()
self.editMdAddress = QtGui.QLineEdit()
self.editTdAddress = QtGui.QLineEdit()
self.editUserID.setMinimumWidth(200)
buttonLogin = QtGui.QPushButton(u'登录')
buttonCancel = QtGui.QPushButton(u'取消')
buttonLogin.clicked.connect(self.login)
buttonCancel.clicked.connect(self.close)
# 设置布局
buttonHBox = QtGui.QHBoxLayout()
buttonHBox.addStretch()
buttonHBox.addWidget(buttonLogin)
buttonHBox.addWidget(buttonCancel)
grid = QtGui.QGridLayout()
grid.addWidget(labelUserID, 0, 0)
grid.addWidget(labelMdPassword, 1, 0)
grid.addWidget(labelTdPassword, 2, 0)
grid.addWidget(labelMdAddress, 3, 0)
grid.addWidget(labelTdAddress, 4, 0)
grid.addWidget(self.editUserID, 0, 1)
grid.addWidget(self.editMdPassword, 1, 1)
grid.addWidget(self.editTdPassword, 2, 1)
grid.addWidget(self.editMdAddress, 3, 1)
grid.addWidget(self.editTdAddress, 4, 1)
grid.addLayout(buttonHBox, 5, 0, 1, 2)
self.setLayout(grid)
#----------------------------------------------------------------------
def login(self):
"""登录"""
userid = str(self.editUserID.text())
mdPassword = str(self.editMdPassword.text())
tdPassword = str(self.editTdPassword.text())
mdAddress = str(self.editMdAddress.text())
tdAddress = str(self.editTdAddress.text())
brokerid = '2011'
self.__mainEngine.login(userid, mdPassword, tdPassword, brokerid, mdAddress, tdAddress)
self.close()
#----------------------------------------------------------------------
def loadData(self):
"""读取数据"""
f = shelve.open('setting.vn')
try:
setting = f['login']
userid = setting['userid']
mdPassword = setting['mdPassword']
tdPassword = setting['tdPassword']
mdAddress = setting['mdAddress']
tdAddress = setting['tdAddress']
self.editUserID.setText(userid)
self.editMdPassword.setText(mdPassword)
self.editTdPassword.setText(tdPassword)
self.editMdAddress.setText(mdAddress)
self.editTdAddress.setText(tdAddress)
except KeyError:
pass
f.close()
#----------------------------------------------------------------------
def saveData(self):
"""保存数据"""
setting = {}
setting['userid'] = str(self.editUserID.text())
setting['mdPassword'] = str(self.editMdPassword.text())
setting['tdPassword'] = str(self.editTdPassword.text())
setting['mdAddress'] = str(self.editMdAddress.text())
setting['tdAddress'] = str(self.editTdAddress.text())
f = shelve.open('setting.vn')
f['login'] = setting
f.close()
#----------------------------------------------------------------------
def closeEvent(self, event):
"""关闭事件处理"""
# 当窗口被关闭时,先保存登录数据,再关闭
self.saveData()
event.accept()
########################################################################
class ControlWidget(QtGui.QWidget):
"""调用查询函数"""
#----------------------------------------------------------------------
def __init__(self, mainEngine, parent=None):
"""Constructor"""
super(ControlWidget, self).__init__()
self.__mainEngine = mainEngine
self.initUi()
#----------------------------------------------------------------------
def initUi(self):
""""""
self.setWindowTitle(u'测试')
buttonAccount = QtGui.QPushButton(u'查询账户')
buttonInvestor = QtGui.QPushButton(u'查询投资者')
buttonPosition = QtGui.QPushButton(u'查询持仓')
buttonAccount.clicked.connect(self.__mainEngine.getAccount)
buttonInvestor.clicked.connect(self.__mainEngine.getInvestor)
buttonPosition.clicked.connect(self.__mainEngine.getPosition)
hBox = QtGui.QHBoxLayout()
hBox.addWidget(buttonAccount)
hBox.addWidget(buttonInvestor)
hBox.addWidget(buttonPosition)
self.setLayout(hBox)
########################################################################
class TradingWidget(QtGui.QWidget):
"""交易"""
signal = QtCore.pyqtSignal(type(Event()))
dictDirection = OrderedDict()
dictDirection['0'] = u'买'
dictDirection['1'] = u'卖'
dictDirection['2'] = u'ETF申购'
dictDirection['3'] = u'ETF赎回'
dictDirection['L'] = u'OF申购'
dictDirection['M'] = u'OF赎回'
dictDirection['N'] = u'SF拆分'
dictDirection['O'] = u'SF合并'
dictDirection['P'] = u'备兑'
dictDirection['Q'] = u'证券冻结/解冻'
dictDirection['R'] = u'行权'
dictOffset = OrderedDict()
dictOffset['0'] = u'开仓'
dictOffset['1'] = u'平仓'
dictPriceType = OrderedDict()
dictPriceType['1'] = u'市价'
dictPriceType['2'] = u'限价'
dictPriceType['3'] = u'最优五档剩余撤销'
dictPriceType['4'] = u'最优五档剩余限价'
dictPriceType['5'] = u'全部成交或撤销'
dictPriceType['6'] = u'本方最优市价'
dictPriceType['7'] = u'对方最优市价'
# 反转字典
dictDirectionReverse = {value:key for key,value in dictDirection.items()}
dictOffsetReverse = {value:key for key, value in dictOffset.items()}
dictPriceTypeReverse = {value:key for key, value in dictPriceType.items()}
#----------------------------------------------------------------------
def __init__(self, eventEngine, mainEngine, orderMonitor, parent=None):
"""Constructor"""
super(TradingWidget, self).__init__()
self.__eventEngine = eventEngine
self.__mainEngine = mainEngine
self.__orderMonitor = orderMonitor
self.instrumentid = ''
self.initUi()
self.registerEvent()
#----------------------------------------------------------------------
def initUi(self):
"""初始化界面"""
self.setWindowTitle(u'交易')
# 左边部分
labelID = QtGui.QLabel(u'代码')
labelName = QtGui.QLabel(u'名称')
labelDirection = QtGui.QLabel(u'委托类型')
labelOffset = QtGui.QLabel(u'开平')
labelPrice = QtGui.QLabel(u'价格')
labelVolume = QtGui.QLabel(u'数量')
labelPriceType = QtGui.QLabel(u'价格类型')
self.lineID = QtGui.QLineEdit()
self.lineName = QtGui.QLineEdit()
self.comboDirection = QtGui.QComboBox()
self.comboDirection.addItems(self.dictDirection.values())
self.comboOffset = QtGui.QComboBox()
self.comboOffset.addItems(self.dictOffset.values())
self.spinPrice = QtGui.QDoubleSpinBox()
self.spinPrice.setDecimals(4)
self.spinPrice.setMinimum(0)
self.spinPrice.setMaximum(10000)
self.spinVolume = QtGui.QSpinBox()
self.spinVolume.setMinimum(0)
self.spinVolume.setMaximum(1000000)
self.comboPriceType = QtGui.QComboBox()
self.comboPriceType.addItems(self.dictPriceType.values())
gridleft = QtGui.QGridLayout()
gridleft.addWidget(labelID, 0, 0)
gridleft.addWidget(labelName, 1, 0)
gridleft.addWidget(labelDirection, 2, 0)
gridleft.addWidget(labelOffset, 3, 0)
gridleft.addWidget(labelPrice, 4, 0)
gridleft.addWidget(labelVolume, 5, 0)
gridleft.addWidget(labelPriceType, 6, 0)
gridleft.addWidget(self.lineID, 0, 1)
gridleft.addWidget(self.lineName, 1, 1)
gridleft.addWidget(self.comboDirection, 2, 1)
gridleft.addWidget(self.comboOffset, 3, 1)
gridleft.addWidget(self.spinPrice, 4, 1)
gridleft.addWidget(self.spinVolume, 5, 1)
gridleft.addWidget(self.comboPriceType, 6, 1)
# 右边部分
labelBid1 = QtGui.QLabel(u'买一')
labelBid2 = QtGui.QLabel(u'买二')
labelBid3 = QtGui.QLabel(u'买三')
labelBid4 = QtGui.QLabel(u'买四')
labelBid5 = QtGui.QLabel(u'买五')
labelAsk1 = QtGui.QLabel(u'卖一')
labelAsk2 = QtGui.QLabel(u'卖二')
labelAsk3 = QtGui.QLabel(u'卖三')
labelAsk4 = QtGui.QLabel(u'卖四')
labelAsk5 = QtGui.QLabel(u'卖五')
self.labelBidPrice1 = QtGui.QLabel()
self.labelBidPrice2 = QtGui.QLabel()
self.labelBidPrice3 = QtGui.QLabel()
self.labelBidPrice4 = QtGui.QLabel()
self.labelBidPrice5 = QtGui.QLabel()
self.labelBidVolume1 = QtGui.QLabel()
self.labelBidVolume2 = QtGui.QLabel()
self.labelBidVolume3 = QtGui.QLabel()
self.labelBidVolume4 = QtGui.QLabel()
self.labelBidVolume5 = QtGui.QLabel()
self.labelAskPrice1 = QtGui.QLabel()
self.labelAskPrice2 = QtGui.QLabel()
self.labelAskPrice3 = QtGui.QLabel()
self.labelAskPrice4 = QtGui.QLabel()
self.labelAskPrice5 = QtGui.QLabel()
self.labelAskVolume1 = QtGui.QLabel()
self.labelAskVolume2 = QtGui.QLabel()
self.labelAskVolume3 = QtGui.QLabel()
self.labelAskVolume4 = QtGui.QLabel()
self.labelAskVolume5 = QtGui.QLabel()
labelLast = QtGui.QLabel(u'最新')
self.labelLastPrice = QtGui.QLabel()
self.labelReturn = QtGui.QLabel()
self.labelLastPrice.setMinimumWidth(60)
self.labelReturn.setMinimumWidth(60)
gridRight = QtGui.QGridLayout()
gridRight.addWidget(labelAsk5, 0, 0)
gridRight.addWidget(labelAsk4, 1, 0)
gridRight.addWidget(labelAsk3, 2, 0)
gridRight.addWidget(labelAsk2, 3, 0)
gridRight.addWidget(labelAsk1, 4, 0)
gridRight.addWidget(labelLast, 5, 0)
gridRight.addWidget(labelBid1, 6, 0)
gridRight.addWidget(labelBid2, 7, 0)
gridRight.addWidget(labelBid3, 8, 0)
gridRight.addWidget(labelBid4, 9, 0)
gridRight.addWidget(labelBid5, 10, 0)
gridRight.addWidget(self.labelAskPrice5, 0, 1)
gridRight.addWidget(self.labelAskPrice4, 1, 1)
gridRight.addWidget(self.labelAskPrice3, 2, 1)
gridRight.addWidget(self.labelAskPrice2, 3, 1)
gridRight.addWidget(self.labelAskPrice1, 4, 1)
gridRight.addWidget(self.labelLastPrice, 5, 1)
gridRight.addWidget(self.labelBidPrice1, 6, 1)
gridRight.addWidget(self.labelBidPrice2, 7, 1)
gridRight.addWidget(self.labelBidPrice3, 8, 1)
gridRight.addWidget(self.labelBidPrice4, 9, 1)
gridRight.addWidget(self.labelBidPrice5, 10, 1)
gridRight.addWidget(self.labelAskVolume5, 0, 2)
gridRight.addWidget(self.labelAskVolume4, 1, 2)
gridRight.addWidget(self.labelAskVolume3, 2, 2)
gridRight.addWidget(self.labelAskVolume2, 3, 2)
gridRight.addWidget(self.labelAskVolume1, 4, 2)
gridRight.addWidget(self.labelReturn, 5, 2)
gridRight.addWidget(self.labelBidVolume1, 6, 2)
gridRight.addWidget(self.labelBidVolume2, 7, 2)
gridRight.addWidget(self.labelBidVolume3, 8, 2)
gridRight.addWidget(self.labelBidVolume4, 9, 2)
gridRight.addWidget(self.labelBidVolume5, 10, 2)
# 发单按钮
buttonSendOrder = QtGui.QPushButton(u'发单')
buttonCancelAll = QtGui.QPushButton(u'全撤')
# 整合布局
hbox = QtGui.QHBoxLayout()
hbox.addLayout(gridleft)
hbox.addLayout(gridRight)
vbox = QtGui.QVBoxLayout()
vbox.addLayout(hbox)
vbox.addWidget(buttonSendOrder)
vbox.addWidget(buttonCancelAll)
self.setLayout(vbox)
# 关联更新
buttonSendOrder.clicked.connect(self.sendOrder)
buttonCancelAll.clicked.connect(self.__orderMonitor.cancelAll)
self.lineID.returnPressed.connect(self.updateID)
#----------------------------------------------------------------------
def updateID(self):
"""合约变化"""
instrumentid = str(self.lineID.text())
# 获取合约
instrument = self.__mainEngine.selectInstrument(instrumentid)
if instrument:
self.lineName.setText(instrument['InstrumentName'].decode('GBK'))
# 清空价格数量
self.spinPrice.setValue(0)
self.spinVolume.setValue(0)
# 清空行情显示
self.labelBidPrice1.setText('')
self.labelBidPrice2.setText('')
self.labelBidPrice3.setText('')
self.labelBidPrice4.setText('')
self.labelBidPrice5.setText('')
self.labelBidVolume1.setText('')
self.labelBidVolume2.setText('')