-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
204 lines (163 loc) · 7.31 KB
/
Copy pathmain.py
File metadata and controls
204 lines (163 loc) · 7.31 KB
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
import sys
import yfinance
import datetime as dt
from PySide6.QtWidgets import QWidget, QMainWindow, QApplication, QTabWidget, QHBoxLayout, QVBoxLayout
from PySide6.QtWidgets import QTableView, QHeaderView, QGridLayout, QSizePolicy, QSpacerItem, QLabel
from PySide6.QtCore import QSortFilterProxyModel, QAbstractTableModel, QSize
from PySide6.QtGui import Qt, QFont
import finplot as fplt
import talib.abstract as talib
class TickerListWidget(QWidget):
def __init__(self, parent, header_list, ticker_list):
super(TickerListWidget, self).__init__(parent)
self.parent = parent
# Creating a QTableView
self.table_view = QTableView()
# Getting the Model
source_model = CustomTableModel(header_list, ticker_list)
# Enable sorting
proxy_model = QSortFilterProxyModel(self)
proxy_model.setSourceModel(source_model)
self.table_view.setModel(proxy_model)
self.table_view.horizontalHeader().setSortIndicator(0, Qt.AscendingOrder)
self.table_view.setSortingEnabled(True)
# Connect the 'clicked' signal
self.table_view.clicked.connect(self.clicked_ticker)
# QTableView Headers
self.horizontal_header = self.table_view.horizontalHeader()
self.vertical_header = self.table_view.verticalHeader()
self.horizontal_header.setSectionResizeMode(QHeaderView.ResizeToContents)
self.vertical_header.setSectionResizeMode(QHeaderView.ResizeToContents)
font = QFont()
font.setBold(True)
self.horizontal_header.setFont(font)
# Set the Tableview Size
self.table_view.setMinimumSize(QSize(60, 500))
self.table_view.setMaximumSize(QSize(60, 16777215))
# QWidget Layout
self.main_layout = QGridLayout()
size = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
# Left layout
self.table_view.setSizePolicy(size)
self.main_layout.addWidget(self.table_view)
# Set the layout to the QWidget
self.setLayout(self.main_layout)
def clicked_ticker(self):
index = {index for index in self.table_view.selectionModel().selectedIndexes()}.pop()
row = index.row()
ticker = self.table_view.model().index(row, 0).data()
self.parent.clicked_ticker_list_widget(ticker)
class CustomTableModel(QAbstractTableModel):
def __init__(self, header_list, ticker_list, *args):
QAbstractTableModel.__init__(self, *args)
self.header = header_list
self.table_data = ticker_list
def rowCount(self, parent):
return len(self.table_data)
def columnCount(self, parent):
return 1
def data(self, index, role):
if not index.isValid():
return None
# Display the values in the cells
if role == Qt.DisplayRole:
return self.table_data[index.row()]
def headerData(self, col, orientation, role):
if orientation == Qt.Horizontal and role == Qt.DisplayRole:
return self.header[col]
class FinplotWidget(QWidget):
def __init__(self, parent, symbol, df):
super(FinplotWidget, self).__init__(parent)
self.parent = parent
# Finplot settings
fplt.display_timezone = dt.timezone.utc
fplt.poc_color = "#fff"
fplt.background = "#000" # Set the background color to black
fplt.odd_plot_background = "#000"
fplt.foreground = "#fff"
fplt.cross_hair_color = "#fff"
fplt.draw_line_color = "#fff"
fplt.side_margin = 1
fplt.clamp_grid = False
fplt.right_margin_candles = 2
# Layout and labels
self.labels = QHBoxLayout()
self.labels.spacer = QSpacerItem(10, 0)
self.labels.addStretch(1)
self.labels.symbol = QLabel()
self.labels.addWidget(self.labels.symbol)
# Add the chart
self.layout = QVBoxLayout(self)
self.fplt_main, self.fplt_study = fplt.create_plot_widget(self.window(), rows=2)
# y axis
self.ax_left = self.fplt_main.getAxis("left")
self.ax_left.setPen(style=Qt.DotLine)
# Show the grid
self.fplt_main.set_visible(xgrid=True, ygrid=True)
self.layout.addLayout(self.labels)
self.layout.addWidget(self.fplt_main.ax_widget, stretch=3)
self.layout.addWidget(self.fplt_study.ax_widget, stretch=1)
self.window().axs = self.fplt_main, self.fplt_study # required property of window
self.setLayout(self.layout)
self.load_df(symbol, df)
def load_df(self, symbol, df):
self.fplt_main.reset()
self.fplt_study.reset()
if len(df.index) != 0: # Ensure there is data in the dataframe
# Main
df.columns = df.columns.str.lower()
df_cols_list = ["open", "close", "high", "low", "volume"]
candlesticks = fplt.candlestick_ochl(df[df_cols_list], ax=self.fplt_main)
candlesticks.colors.update(dict(bull_body="#00AA00", bull_shadow="#fff", bull_frame="#00AA00",
bear_body="#D20000", bear_shadow="#fff", bear_frame="#D20000"))
candlesticks.x_offset = 0.5
# Studies
study_name = "MFI"
talib_function = talib.Function(study_name)
study_df = talib_function(df[df_cols_list])
fplt.plot(study_df, legend=study_name, color="#FFFF66", ax=self.fplt_study)
fplt.refresh()
self.labels.symbol.setText(f"<B>{symbol}</B>") # set the symbol
class ComboStockChartWidget(QWidget):
def __init__(self, parent, header_list, ticker_list, df_dict, initial_symbol):
super(ComboStockChartWidget, self).__init__(parent)
self.df_dict = df_dict
self.ticker_list_widget = TickerListWidget(self, header_list, ticker_list)
df = df_dict[initial_symbol]
self.finplot_widget = FinplotWidget(self, initial_symbol, df)
self.hBox = QHBoxLayout()
self.hBox.addWidget(self.ticker_list_widget)
self.hBox.addWidget(self.finplot_widget)
self.setLayout(self.hBox)
def clicked_ticker_list_widget(self, symbol):
df = self.df_dict[symbol]
self.finplot_widget.load_df(symbol, df)
def instantiate_combo_chart_widget(df_dict):
ticker_list_header_list = ["Symbol"]
ticker_list = list(df_dict.keys())
initial_symbol = list(df_dict.keys())[0] # Take the first item in the dictionary to display it
return ComboStockChartWidget(None, ticker_list_header_list, ticker_list, df_dict, initial_symbol)
class MainWindow(QMainWindow):
def __init__(self, parent, df_dict):
super(MainWindow, self).__init__(parent)
self.setWindowTitle("Stocks")
self.tab_widget = QTabWidget()
self.tab_widget.setMovable(True)
self.setCentralWidget(self.tab_widget)
# Stock Chart
combo_stock_chart_widget = instantiate_combo_chart_widget(df_dict)
self.tab1 = combo_stock_chart_widget
self.tab_widget.addTab(self.tab1, "Stock Chart")
def main():
df_dict = {}
symbols_list = ["AAPL", "GOOGL", "FB"]
period = "180d"
for symbol in symbols_list:
df_dict[symbol] = yfinance.download(symbol, period=period)
app = QApplication(sys.argv)
main_window = MainWindow(None, df_dict)
main_window.showMaximized()
ret = app.exec()
sys.exit(ret)
if __name__ == '__main__':
main()