forked from Skinok/backtrader-pyqt-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userInterface.py
882 lines (659 loc) · 34 KB
/
userInterface.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
###############################################################################
#
# Copyright (C) 2021 - Skinok
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
###############################################################################
from PyQt6 import QtWidgets
from PyQt6 import QtGui
from PyQt6 import QtCore
from numpy import NaN
from pyqtgraph.dockarea import DockArea, Dock
import os
import backtrader as bt
# Ui made with Qt Designer
import strategyTesterUI
import strategyResultsUI
import indicatorParametersUI
# Import Chart lib
import finplotWindow
import qdarkstyle
import functools
class UserInterface:
#########
#
#########
def __init__(self, controller):
self.controller = controller # SkinokBacktraderUI
# It does not finish by a "/"
self.current_dir_path = os.path.dirname(os.path.realpath(__file__))
# Qt
self.app = QtWidgets.QApplication([])
self.win = QtWidgets.QMainWindow()
# All dock area of each time frame
self.dockAreaTimeframes = {}
self.dock_charts= {}
self.dock_rsi= {}
self.dock_stochastic= {}
self.dock_stochasticRsi= {}
self.fpltWindow = {}
self.timeFramePB = {}
# Resize windows properties
self.win.resize(1600,1100)
self.win.setWindowTitle("Skinok Backtrader UI v0.3")
# Set width/height of QSplitter
self.app.setStyleSheet(qdarkstyle.load_stylesheet())
pass
def initialize(self):
# Docks
self.createMainDocks()
# Create all buttons above the chart window
self.createControlPanel()
self.createUIs()
# Enable run button
self.strategyTesterUI.runBacktestBtn.setEnabled(False)
self.strategyTesterUI.initialize()
pass
#########
# Create all chart dock for ONE timeframe
#########
def createChartDock(self, timeframe):
# this need to be changed later
self.current_timeframe = timeframe
# Each time frame has its own dock area
self.dockAreaTimeframes[timeframe] = DockArea()
self.stackedCharts.addWidget(self.dockAreaTimeframes[timeframe])
# Create Chart widget
self.dock_charts[timeframe] = Dock("dock_chart_"+timeframe, size = (1000, 500), closable = False, hideTitle=True)
self.dockAreaTimeframes[timeframe].addDock(self.dock_charts[timeframe])
# Create Order widget
self.dock_rsi[timeframe] = Dock("RSI", size = (1000, 120), closable = False, hideTitle=True)
self.dockAreaTimeframes[timeframe].addDock(self.dock_rsi[timeframe], position='bottom', relativeTo=self.dock_charts[timeframe])
self.dock_rsi[timeframe].hide()
self.dock_stochastic[timeframe] = Dock("Stochastic", size = (1000, 120), closable = False, hideTitle=True)
self.dockAreaTimeframes[timeframe].addDock(self.dock_stochastic[timeframe], position='bottom', relativeTo=self.dock_charts[timeframe])
self.dock_stochastic[timeframe].hide()
self.dock_stochasticRsi[timeframe] = Dock("Stochastic Rsi", size = (1000, 120), closable = False, hideTitle=True)
self.dockAreaTimeframes[timeframe].addDock(self.dock_stochasticRsi[timeframe], position='bottom', relativeTo=self.dock_charts[timeframe])
self.dock_stochasticRsi[timeframe].hide()
# Create finplot Window
self.fpltWindow[timeframe] = finplotWindow.FinplotWindow(self.dockAreaTimeframes[timeframe], self.dock_charts[timeframe], self)
self.fpltWindow[timeframe].createPlotWidgets(timeframe)
self.fpltWindow[timeframe].show()
# Create timeframe button
self.timeFramePB[timeframe] = QtWidgets.QRadioButton(self.controlPanel)
self.timeFramePB[timeframe].setText(timeframe)
self.timeFramePB[timeframe].setCheckable(True)
self.timeFramePB[timeframe].setMaximumWidth(100)
self.timeFramePB[timeframe].toggled.connect(lambda: self.toogleTimeframe(timeframe) )
self.timeFramePB[timeframe].toggle()
self.controlPanelLayout.insertWidget(0,self.timeFramePB[timeframe])
# init checked after connecting the slot
if self.darkmodeCB.isChecked():
self.dark_mode_toggle()
pass
def deleteChartDock(self, timeframe):
del self.dockAreaTimeframes[timeframe]
del self.dock_charts[timeframe]
del self.dock_rsi[timeframe]
self.fpltWindow[timeframe].resetPlots()
del self.fpltWindow[timeframe]
self.controlPanelLayout.removeWidget(self.timeFramePB[timeframe])
del self.timeFramePB[timeframe]
pass
#########
# Create all main window docks
#########
def createMainDocks(self):
self.dockArea = DockArea()
self.win.setCentralWidget(self.dockArea)
# Create Stacked widget
self.dock_stackedCharts = Dock("dock_stackedCharts", size = (1000, 500), closable = False, hideTitle=True )
self.dockArea.addDock(self.dock_stackedCharts, position='above')
self.stackedCharts = QtWidgets.QStackedWidget(self.dock_stackedCharts)
self.dock_stackedCharts.addWidget( self.stackedCharts, 1 , 0 )
# Create Strategy Tester Tab
self.dock_strategyTester = Dock("Strategy Tester", closable = False, hideTitle=True)
self.dockArea.addDock(self.dock_strategyTester, position='left')
# Create Strategy Tester Tab
self.dock_strategyResultsUI = Dock("Strategy Tester", size = (1000, 200), closable = False, hideTitle=True)
self.dockArea.addDock(self.dock_strategyResultsUI, position='bottom')
self.dock_strategyResultsUI.hide()
pass
#########
# Create all dock contents
#########
def createUIs(self):
self.createStrategyTesterUI()
self.createTradesUI()
#self.createOrdersUI()
self.createSummaryUI()
self.createActions()
self.createMenuBar()
pass
#########
# Quick menu actions
#########
def createActions(self):
# Data sources
#self.backtestDataActionGroup = QtGui.QActionGroup(self.win)
#self.openCSVAction = QtGui.QAction(QtGui.QIcon(""),"Open CSV File", self.backtestDataActionGroup)
# AI
#self.aiActionGroup = QtGui.QActionGroup(self.win)
#self.loadTFModelAction = QtGui.QAction(QtGui.QIcon(""),"Load Tensorflow Model", self.aiActionGroup)
#self.loadTFModelAction.triggered.connect( self.loadTFModel )
#self.loadTorchModelAction = QtWidgets.QAction(QtGui.QIcon(""),"Load Torch Model", self.aiActionGroup)
#self.loadTorchModelAction.triggered.connect( self.loadTorchModel )
#self.loadStableBaselines3Action = QtGui.QAction(QtGui.QIcon(""),"Load Stable Baselines 3 Model", self.aiActionGroup)
#self.loadStableBaselines3Action.triggered.connect( self.loadStableBaselinesModel )
# Options
#self.optionsActionGroup = QtGui.QActionGroup(self.win)
pass
#########
# UI : main window menu bar
#########
def createMenuBar(self):
#self.menubar = self.win.menuBar()
#self.indicatorsMenu = self.menubar.addMenu("Indicators")
#self.indicatorsMenu.addActions(self.indicatorsActionGroup.actions())
#self.backtestDataMenu = self.menubar.addMenu("Backtest Data")
#self.backtestDataMenu.addActions(self.backtestDataActionGroup.actions())
#self.aiMenu = self.menubar.addMenu("Artificial Intelligence")
#self.aiMenu.addActions(self.aiActionGroup.actions())
#self.optionsMenu = self.menubar.addMenu("Options")
#self.optionsMenu.addActions(self.optionsActionGroup.actions())
pass
#########
# Strategy results : trades tab
#########
def createTradesUI(self):
self.tradeTableWidget = QtWidgets.QTableWidget(self.strategyResultsUI.TradesGB)
self.tradeTableWidget.setColumnCount(7)
labels = [ "Trade id","Direction", "Date Open", "Date Close", "Price", "Commission", "Profit Net" ]
self.tradeTableWidget.setHorizontalHeaderLabels( labels )
self.tradeTableWidget.verticalHeader().setVisible(False)
self.tradeTableWidget.horizontalHeader().setStretchLastSection(True)
self.tradeTableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeMode.Stretch)
#self.tradeTableWidget.setStyleSheet("alternate-background-color: #AAAAAA;background-color: #CCCCCC;")
self.tradeTableWidget.setAlternatingRowColors(True)
self.tradeTableWidget.setSortingEnabled(True)
self.tradeTableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectionBehavior.SelectRows)
self.tradeTableWidget.setEditTriggers(QtWidgets.QAbstractItemView.EditTrigger.NoEditTriggers)
self.strategyResultsUI.ResultsTabWidget.widget(0).layout().addWidget(self.tradeTableWidget)
pass
def fillTradesUI(self, trades):
# Delete all previous results by settings row count to 0
self.tradeTableWidget.setRowCount(0)
for key, values in trades:
self.tradeTableWidget.setRowCount(len(values[0]))
row = 0
for trade in values[0]:
if not trade.isopen:
# Trade id
item = QtWidgets.QTableWidgetItem( str(trade.ref) )
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.tradeTableWidget.setItem(row,0,item)
item = QtWidgets.QTableWidgetItem( "Buy" if trade.long else "Sell" )
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.tradeTableWidget.setItem(row,1,item)
item = QtWidgets.QTableWidgetItem( str(bt.num2date(trade.dtopen)) )
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.tradeTableWidget.setItem(row,2,item)
item = QtWidgets.QTableWidgetItem( str(bt.num2date(trade.dtclose)) )
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.tradeTableWidget.setItem(row,3,item)
item = QtWidgets.QTableWidgetItem( str(trade.price) )
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.tradeTableWidget.setItem(row,4,item)
item = QtWidgets.QTableWidgetItem( str(trade.commission) )
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.tradeTableWidget.setItem(row,5,item)
item = QtWidgets.QTableWidgetItem( str(trade.pnlcomm) )
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignCenter)
self.tradeTableWidget.setItem(row,6,item)
row += 1
# Click on a trade line will move the chart to the corresponding trade
self.tradeTableWidget.cellClicked.connect( self.tradeClicked );
pass
# Slot when a trade has been clicked
def tradeClicked(self, row, column):
selectedItems = self.tradeTableWidget.selectedItems() # QList<QTableWidgetItem *>
if len(selectedItems) > 4:
dateTradeOpen = selectedItems[2].text()
dateTradeClose = selectedItems[3].text()
self.fpltWindow[self.current_timeframe].zoomTo(dateTradeOpen,dateTradeClose)
pass
#########
# Strategy results : Order tab
#########
def createOrdersUI(self):
self.orderTableWidget = QtWidgets.QTableWidget(self.dock_orders)
self.orderTableWidget.setColumnCount(8)
labels = [ "Order ref" , "Direction", "Date Open", "Date Close", "Execution Type", "Size", "Price", "Profit" ]
self.orderTableWidget.setHorizontalHeaderLabels( labels )
self.orderTableWidget.horizontalHeader().setStretchLastSection(True)
self.orderTableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeMode.Stretch)
self.orderTableWidget.setStyleSheet("alternate-background-color: #AAAAAA;background-color: #CCCCCC;")
self.orderTableWidget.setAlternatingRowColors(True)
self.orderTableWidget.setSortingEnabled(True)
self.orderTableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectionBehavior.SelectRows)
self.orderTableWidget.setEditTriggers(QtWidgets.QAbstractItemView.EditTrigger.NoEditTriggers)
self.dock_orders.addWidget(self.orderTableWidget)
pass
def fillOrdersUI(self, orders):
self.orderTableWidget.setRowCount(len(orders))
for i in range(len(orders)):
order = orders[i]
self.orderTableWidget.setItem(i,0,QtWidgets.QTableWidgetItem( str(order.ref ) ))
self.orderTableWidget.setItem(i,1,QtWidgets.QTableWidgetItem( "Buy" if order.isbuy() else "Sell"))
self.orderTableWidget.setItem(i,2,QtWidgets.QTableWidgetItem( str(bt.num2date(order.created.dt)) ))
self.orderTableWidget.setItem(i,3,QtWidgets.QTableWidgetItem( str(bt.num2date(order.executed.dt)) ))
self.orderTableWidget.setItem(i,4,QtWidgets.QTableWidgetItem( str(order.exectype) ))
self.orderTableWidget.setItem(i,5,QtWidgets.QTableWidgetItem( str(order.size ) ))
self.orderTableWidget.setItem(i,6,QtWidgets.QTableWidgetItem( str(order.price ) ))
self.orderTableWidget.setItem(i,7,QtWidgets.QTableWidgetItem( str(order.executed.pnl) ))
pass
#########
# UI parameters for testing stategies
#########
def createStrategyTesterUI(self):
self.strategyTesterUI = strategyTesterUI.StrategyTesterUI(self.controller, self.win)
self.dock_strategyTester.addWidget(self.strategyTesterUI)
self.strategyResultsUI = strategyResultsUI.StrategyResultsUI(self.controller, self.win)
self.dock_strategyResultsUI.addWidget(self.strategyResultsUI)
#
self.strategyTesterUI.startingCashLE.setText(str(self.controller.cerebro.broker.cash))
validator = QtGui.QDoubleValidator(-9999999, 9999999, 6, self.strategyTesterUI.startingCashLE)
validator.setLocale(QtCore.QLocale("en"))
self.strategyTesterUI.startingCashLE.setValidator( validator )
self.strategyTesterUI.startingCashLE.textChanged.connect( self.controller.cashChanged )
pass
#########
# Strategy results : Summary UI
#########
def createSummaryUI(self):
self.summaryTableWidget = self.strategyResultsUI.summaryTableWidget
self.summaryTableWidget.setColumnCount(2)
self.summaryTableWidget.verticalHeader().hide()
self.summaryTableWidget.horizontalHeader().hide()
self.summaryTableWidget.setShowGrid(False)
self.summaryTableWidget.setStyleSheet("QTableWidget { border: 0; }")
pass
def fillSummaryUI(self, brokerCash, brokerValue, tradeAnalysis):
# Delete all previous rows
self.summaryTableWidget.setRowCount(0)
self.summaryTableWidget.setRowCount(8)
self.summaryTableWidget.setItem(0,0,QtWidgets.QTableWidgetItem("Cash"))
item = QtWidgets.QTableWidgetItem(str(round(brokerCash,2)))
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.summaryTableWidget.setItem(0,1,item)
self.summaryTableWidget.setItem(1,0,QtWidgets.QTableWidgetItem("Value"))
item = QtWidgets.QTableWidgetItem(str(round(brokerValue,2)))
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.summaryTableWidget.setItem(1,1,item)
# if there are some trades
if len(tradeAnalysis) > 1:
self.summaryTableWidget.setItem(2,0,QtWidgets.QTableWidgetItem("Profit total"))
item = QtWidgets.QTableWidgetItem(str(round(tradeAnalysis["pnl"]["net"]["total"],2)))
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.summaryTableWidget.setItem(2,1, item )
self.summaryTableWidget.setItem(3,0,QtWidgets.QTableWidgetItem("Number of trades"))
item = QtWidgets.QTableWidgetItem(str(round(tradeAnalysis["total"]["total"],2)))
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.summaryTableWidget.setItem(3,1,item)
self.summaryTableWidget.setItem(4,0,QtWidgets.QTableWidgetItem("Won"))
item = QtWidgets.QTableWidgetItem(str(round(tradeAnalysis["won"]['total'],2)))
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.summaryTableWidget.setItem(4,1,item)
self.summaryTableWidget.setItem(5,0,QtWidgets.QTableWidgetItem("Lost"))
item = QtWidgets.QTableWidgetItem(str(round(tradeAnalysis["lost"]['total'],2)))
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.summaryTableWidget.setItem(5,1,item)
self.summaryTableWidget.setItem(6,0,QtWidgets.QTableWidgetItem("Long"))
item = QtWidgets.QTableWidgetItem(str(round(tradeAnalysis["long"]["total"],2)))
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.summaryTableWidget.setItem(6,1,item)
self.summaryTableWidget.setItem(7,0,QtWidgets.QTableWidgetItem("Short"))
item = QtWidgets.QTableWidgetItem(str(round(tradeAnalysis["short"]["total"],2)))
item.setTextAlignment(QtCore.Qt.AlignmentFlag.AlignHCenter)
self.summaryTableWidget.setItem(7,1,item)
self.summaryTableWidget.horizontalHeader().setStretchLastSection(True)
self.summaryTableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeMode.Stretch)
self.summaryTableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectionBehavior.SelectRows)
pass
#########
# Show all
#########
def show(self):
#self.fpltWindow[timeframe].show() # prepares plots when they're all setup
self.win.show()
self.app.exec_()
pass
#########
# Get strategy running progress bar
#########
def getProgressBar(self):
return self.strategyTesterUI.runningStratPB
#########
# Draw chart
#########
def drawChart(self, data, timeframe):
self.fpltWindow[timeframe].setChartData(data)
self.fpltWindow[timeframe].updateChart()
pass
#########
# Draw orders on chart
#########
def setOrders(self, orders):
for timeframe,fpltWindow in self.fpltWindow.items():
fpltWindow.drawOrders(orders)
pass
#########
# Draw PnL chart
# A python expert could do waaaaay better optimized on this function
# But anyway... it works...
#########
def displayPnL(self, pnl_dataframe):
# draw charts
for timeframe, fpltwindow in self.fpltWindow.items():
fpltwindow.drawPnL(pnl_dataframe)
self.togglePnLWidget()
pass
#########
# Control panel overlay on top/above of the finplot window
#########
def createControlPanel(self):
self.controlPanel = QtWidgets.QWidget(self.dock_stackedCharts)
self.dock_stackedCharts.addWidget(self.controlPanel,0,0)
self.controlPanelLayout = QtWidgets.QHBoxLayout(self.controlPanel)
'''
panel.symbol = QtWidgets.QComboBox(panel)
[panel.symbol.addItem(i+'USDT') for i in 'BTC ETH XRP DOGE BNB SOL ADA LTC LINK DOT TRX BCH'.split()]
panel.symbol.setCurrentIndex(1)
layout.addWidget(panel.symbol, 0, 0)
panel.symbol.currentTextChanged.connect(change_asset)
layout.setColumnMinimumWidth(1, 30)
panel.interval = QtWidgets.QComboBox(panel)
[panel.interval.addItem(i) for i in '1d 4h 1h 30m 15m 5m 1m'.split()]
panel.interval.setCurrentIndex(6)
layout.addWidget(panel.interval, 0, 2)
panel.interval.currentTextChanged.connect(change_asset)
layout.setColumnMinimumWidth(3, 30)
'''
# Rest
self.ResetPB = QtWidgets.QPushButton(self.controlPanel)
self.ResetPB.setText("Reset")
self.ResetPB.setCheckable(True)
self.ResetPB.setMaximumWidth(100)
self.ResetPB.toggled[bool].connect(self.resetChart)
self.controlPanelLayout.addWidget(self.ResetPB)
# Spacer
spacer = QtWidgets.QSpacerItem(50,20,QtWidgets.QSizePolicy.Policy.Minimum)
self.controlPanelLayout.addSpacerItem(spacer)
# SMA
self.SmaPB = QtWidgets.QPushButton(self.controlPanel)
self.SmaPB.setText("SMA")
self.SmaPB.setCheckable(True)
self.SmaPB.setMaximumWidth(100)
self.SmaPB.toggled[bool].connect(self.addSma)
self.controlPanelLayout.addWidget(self.SmaPB)
# EMA
self.EmaPB = QtWidgets.QPushButton(self.controlPanel)
self.EmaPB.setText("EMA")
self.EmaPB.setCheckable(True)
self.EmaPB.setMaximumWidth(100)
self.EmaPB.toggled[bool].connect(self.addEma)
self.controlPanelLayout.addWidget(self.EmaPB)
# Spacer
spacer = QtWidgets.QSpacerItem(50,20,QtWidgets.QSizePolicy.Policy.Minimum)
self.controlPanelLayout.addSpacerItem(spacer)
# RSI
self.RsiPB = QtWidgets.QPushButton(self.controlPanel)
self.RsiPB.setText("RSI")
self.RsiPB.setCheckable(True)
self.RsiPB.setMaximumWidth(100)
self.RsiPB.toggled[bool].connect(self.toogleRsi)
self.controlPanelLayout.addWidget(self.RsiPB)
# Stochastic
self.StochasticPB = QtWidgets.QPushButton(self.controlPanel)
self.StochasticPB.setText("Stochastic")
self.StochasticPB.setCheckable(True)
self.StochasticPB.setMaximumWidth(100)
self.StochasticPB.toggled[bool].connect(self.toogleStochastic)
self.controlPanelLayout.addWidget(self.StochasticPB)
# Stochastic RSI
self.StochasticRsiPB = QtWidgets.QPushButton(self.controlPanel)
self.StochasticRsiPB.setText("Stochastic RSI")
self.StochasticRsiPB.setCheckable(True)
self.StochasticRsiPB.setMaximumWidth(100)
self.StochasticRsiPB.toggled.connect(self.toogleStochasticRsi)
self.controlPanelLayout.addWidget(self.StochasticRsiPB)
# Ichimoku
self.IchimokuPB = QtWidgets.QPushButton(self.controlPanel)
self.IchimokuPB.setText("Ichimoku")
self.IchimokuPB.setCheckable(True)
self.IchimokuPB.setMaximumWidth(100)
self.IchimokuPB.toggled.connect(self.toogleIchimoku)
self.controlPanelLayout.addWidget(self.IchimokuPB)
# Spacer
spacer = QtWidgets.QSpacerItem(50,20,QtWidgets.QSizePolicy.Policy.Minimum)
self.controlPanelLayout.addSpacerItem(spacer)
# Dark mode
self.darkmodeCB = QtWidgets.QCheckBox(self.controlPanel)
self.darkmodeCB.setText('Dark mode')
self.darkmodeCB.toggled.connect(self.dark_mode_toggle)
self.darkmodeCB.setChecked(True)
self.controlPanelLayout.addWidget(self.darkmodeCB)
# Volumes
self.volumesCB = QtWidgets.QCheckBox(self.controlPanel)
self.volumesCB.setText('Volumes')
self.volumesCB.toggled.connect(self.volumes_toggle)
# init checked after connecting the slot
self.volumesCB.setChecked(False)
self.controlPanelLayout.addWidget(self.volumesCB)
# Spacer
self.controlPanelLayout.insertSpacerItem(0, QtWidgets.QSpacerItem( 0,0, hPolicy=QtWidgets.QSizePolicy.Policy.Expanding, vPolicy=QtWidgets.QSizePolicy.Policy.Preferred) )
return self.controlPanel
#########
# Toggle anther UI Theme
#########
def dark_mode_toggle(self):
for key,window in self.fpltWindow.items():
window.activateDarkMode(self.darkmodeCB.isChecked())
pass
##########
# INDICATORS
##########
def toogleTimeframe(self, timeframe):
if self.timeFramePB[timeframe].isChecked():
print("Display " + timeframe)
self.current_timeframe = timeframe
index = self.stackedCharts.indexOf( self.dockAreaTimeframes[timeframe])
self.stackedCharts.setCurrentIndex( index )
self.togglePnLWidget()
pass
def togglePnLWidget(self):
# hide all PnL windows & Show the good one
for tf, fpltWindow in self.fpltWindow.items():
if tf != self.current_timeframe:
fpltWindow.hidePnL()
else:
fpltWindow.showPnL()
def resetChart(self):
self.fpltWindow[self.current_timeframe].resetChart()
self.fpltWindow[self.current_timeframe].updateChart()
pass
# On chart indicators
def addSma(self):
# Show indicator parameter dialog
paramDialog = indicatorParametersUI.IndicatorParametersUI()
paramDialog.setWindowFlags(QtCore.Qt.CustomizeWindowHint)
paramDialog.setTitle("SMA Indicator parameters")
paramDialog.addParameter("SMA Period", 14)
paramDialog.addParameter("Plot width", 1)
paramDialog.addParameterColor("Plot color", "#FFFF00")
paramDialog.adjustSize()
if paramDialog.exec() == QtWidgets.QDialog.DialogCode.Accepted:
period = paramDialog.getValue("SMA Period")
width = paramDialog.getValue("Plot width")
qColor = paramDialog.getColorValue("Plot color")
self.fpltWindow[self.current_timeframe].drawSma( period, qColor, width)
pass
# On chart indicators
def addEma(self):
# Show indicator parameter dialog
paramDialog = indicatorParametersUI.IndicatorParametersUI()
paramDialog.setWindowFlags(QtCore.Qt.CustomizeWindowHint)
paramDialog.setTitle("EMA Indicator parameters")
paramDialog.addParameter("EMA Period", 9)
paramDialog.addParameter("Plot width", 1)
paramDialog.addParameterColor("Plot color", "#FFFF00")
paramDialog.adjustSize()
if paramDialog.exec() == QtWidgets.QDialog.DialogCode.Accepted:
period = paramDialog.getValue("EMA Period")
width = paramDialog.getValue("Plot width")
qColor = paramDialog.getColorValue("Plot color")
self.fpltWindow[self.current_timeframe].drawEma( period, qColor, width )
pass
# indicators in external windows
def toogleRsi(self):
if self.RsiPB.isChecked():
# Show indicator parameter dialog
paramDialog = indicatorParametersUI.IndicatorParametersUI()
paramDialog.setWindowFlags(QtCore.Qt.WindowType.CustomizeWindowHint)
paramDialog.setTitle("RSI Indicator parameters")
paramDialog.addParameter("RSI Period", 14)
paramDialog.addParameterColor("Plot color", "#FFFF00")
paramDialog.adjustSize()
if paramDialog.exec() == QtWidgets.QDialog.DialogCode.Accepted:
period = paramDialog.getValue("RSI Period")
qColor = paramDialog.getColorValue("Plot color")
self.fpltWindow[self.current_timeframe].drawRsi( period, qColor )
self.dock_rsi[self.current_timeframe].show()
else:
# Cancel
self.RsiPB.setChecked(False)
self.dock_rsi[self.current_timeframe].hide()
else:
self.dock_rsi[self.current_timeframe].hide()
pass
def toogleStochastic(self):
if self.StochasticPB.isChecked():
# Show indicator parameter dialog
paramDialog = indicatorParametersUI.IndicatorParametersUI()
paramDialog.setWindowFlags(QtCore.Qt.WindowType.CustomizeWindowHint)
paramDialog.setTitle("Stochastic Indicator parameters")
paramDialog.addParameter("Stochastic Period K", 14)
paramDialog.addParameter("Stochastic Smooth K", 3)
paramDialog.addParameter("Stochastic Smooth D", 3)
paramDialog.adjustSize()
if paramDialog.exec() == QtWidgets.QDialog.DialogCode.Accepted:
period = paramDialog.getValue("Stochastic Period K")
smooth_k = paramDialog.getValue("Stochastic Smooth K")
smooth_d = paramDialog.getValue("Stochastic Smooth D")
self.fpltWindow[self.current_timeframe].drawStochastic( period, smooth_k, smooth_d )
self.dock_stochastic[self.current_timeframe].show()
else:
# Cancel
self.RsiPB.setChecked(False)
self.dock_stochastic[self.current_timeframe].hide()
else:
self.dock_stochastic[self.current_timeframe].hide()
pass
def toogleStochasticRsi(self):
if self.StochasticRsiPB.isChecked():
# Show indicator parameter dialog
paramDialog = indicatorParametersUI.IndicatorParametersUI()
paramDialog.setWindowFlags(QtCore.Qt.WindowType.CustomizeWindowHint)
paramDialog.setTitle("Stochastic Indicator parameters")
paramDialog.addParameter("Stochastic Rsi Period K", 14)
paramDialog.addParameter("Stochastic Rsi Smooth K", 3)
paramDialog.addParameter("Stochastic Rsi Smooth D", 3)
paramDialog.adjustSize()
if (paramDialog.exec() == QtWidgets.QDialog.DialogCode.Accepted ):
period = paramDialog.getValue("Stochastic Rsi Period K")
smooth_k = paramDialog.getValue("Stochastic Rsi Smooth K")
smooth_d = paramDialog.getValue("Stochastic Rsi Smooth D")
self.fpltWindow[self.current_timeframe].drawStochasticRsi( period, smooth_k, smooth_d)
self.dock_stochasticRsi[self.current_timeframe].show()
else:
# Cancel
self.RsiPB.setChecked(False)
self.dock_stochasticRsi[self.current_timeframe].hide()
else:
self.dock_stochasticRsi[self.current_timeframe].hide()
pass
# On chart indicators
def toogleIchimoku(self):
self.fpltWindow[self.current_timeframe].setIndicator("Ichimoku", self.IchimokuPB.isChecked() )
pass
def volumes_toggle(self):
self.fpltWindow[self.current_timeframe].setIndicator("Volumes", self.volumesCB.isChecked())
pass
#########
# Obsolete (Strategy results : transcations tab)
#########
def createTransactionsUI(self, trades):
self.transactionTableWidget = QtWidgets.QTableWidget(self.dock_trades)
self.transactionTableWidget.setRowCount(len(trades))
self.transactionTableWidget.setColumnCount(4)
labels = [ "Date","Size", "Price", "Value" ]
self.transactionTableWidget.setHorizontalHeaderLabels( labels )
row = 0
for date,values in trades:
#for trade in trades:
self.transactionTableWidget.setItem(row, 0, QtWidgets.QTableWidgetItem(date.strftime("%Y/%m/%d %H:%M:%S")))
self.transactionTableWidget.setItem(row, 1, QtWidgets.QTableWidgetItem(str(values[0][0])))
self.transactionTableWidget.setItem(row, 2, QtWidgets.QTableWidgetItem(str(values[0][1])))
self.transactionTableWidget.setItem(row, 3, QtWidgets.QTableWidgetItem(str(values[0][2])))
row += 1
self.transactionTableWidget.horizontalHeader().setStretchLastSection(True)
self.transactionTableWidget.horizontalHeader().setSectionResizeMode(QtWidgets.QHeaderView.ResizeMode.Stretch)
self.transactionTableWidget.setStyleSheet("alternate-background-color: #AAAAAA;background-color: #CCCCCC;")
self.transactionTableWidget.setAlternatingRowColors(True)
self.transactionTableWidget.setSortingEnabled(True)
self.transactionTableWidget.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectionBehavior.SelectRows)
self.transactionTableWidget.setEditTriggers(QtWidgets.QAbstractItemView.EditTrigger.NoEditTriggers)
self.dock_transactions.addWidget(self.transactionTableWidget)
pass
def fillStrategyParameters(self, strategy):
# Rest widget rows
for indexRow in range(self.strategyTesterUI.parametersLayout.rowCount()):
self.strategyTesterUI.parametersLayout.removeRow(0)
# Insert parameters
row = 0
for parameterName, parameterValue in strategy.params._getitems():
if parameterName is not 'model':
label = QtWidgets.QLabel(parameterName)
if type(parameterValue) == bool:
checkBox = QtWidgets.QCheckBox(str())
checkBox.setObjectName(parameterName)
checkBox.setChecked(parameterValue)
checkBox.stateChanged.connect(functools.partial(self.controller.strategyParametersChanged, checkBox, parameterName, parameterValue))
self.strategyTesterUI.parametersLayout.addRow(label, checkBox )
else:
lineEdit = QtWidgets.QLineEdit(str(parameterValue))
lineEdit.setObjectName(parameterName)
# Connect the parameter changed slot
lineEdit.textChanged.connect(functools.partial(self.controller.strategyParametersChanged, lineEdit, parameterName, parameterValue))
self.strategyTesterUI.parametersLayout.addRow(label, lineEdit )
# Save the parameter to inject it in the addStrategy method
self.controller.strategyParametersSave(parameterName, parameterValue)
row = row + 1
pass
# Parameter box size
self.strategyTesterUI.parametersLayout.update()
self.strategyTesterUI.parametersScrollArea.adjustSize()
pass