-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock_Dashboard.py
399 lines (341 loc) · 14.5 KB
/
Stock_Dashboard.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
import streamlit as st
import sqlite3
import pandas as pd
import yfinance as yf
import plotly.express as px
import plotly.graph_objects as go
import numpy as np
from datetime import date
from alpha_vantage.fundamentaldata import FundamentalData
from stocknews import StockNews
import pandas_ta as ta
# Initialize database tables
def init_db():
conn = sqlite3.connect('trading_app.db')
cursor = conn.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS portfolio (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ticker TEXT,
quantity INTEGER,
buy_price REAL,
buy_date DATE
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS trade_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
ticker TEXT,
quantity INTEGER,
trade_type TEXT,
trade_price REAL,
trade_date DATE DEFAULT CURRENT_DATE
)
''')
conn.commit()
conn.close()
# Initialize session state variables
if 'page' not in st.session_state:
st.session_state.page = "Dashboard"
if 'current_balance' not in st.session_state:
st.session_state.current_balance = 100000.0 # Example initial balance
# Utility function to reset database tables
def reset_table(table_name):
conn = sqlite3.connect('trading_app.db')
cursor = conn.cursor()
try:
cursor.execute(f'DELETE FROM {table_name}')
conn.commit()
st.success(f"{table_name.capitalize()} reset successfully!")
except sqlite3.Error as e:
st.error(f"Database error: {e}")
finally:
conn.close()
# Utility function to reset balance
def reset_balance():
st.session_state.current_balance = 100000.0
st.success("Balance reset to $100,000!")
# Utility function to get stock price
def get_stock_price(ticker):
try:
data = yf.download(ticker, period='1d')
return data['Close'].iloc[-1] # Get the latest closing price
except Exception:
return None
# Buy stock and update portfolio
def buy_stock(ticker, quantity):
current_price = get_stock_price(ticker)
if current_price is None:
st.error(f"Failed to fetch price for {ticker}.")
return
total_cost = current_price * quantity
if total_cost > st.session_state.current_balance:
st.error(f"Insufficient funds! You have ${st.session_state.current_balance:.2f} available.")
return
conn = sqlite3.connect('trading_app.db')
cursor = conn.cursor()
try:
cursor.execute('''
INSERT INTO portfolio (ticker, quantity, buy_price, buy_date)
VALUES (?, ?, ?, ?)
''', (ticker, quantity, current_price, date.today()))
cursor.execute('''
INSERT INTO trade_history (ticker, quantity, trade_type, trade_price)
VALUES (?, ?, 'BUY', ?)
''', (ticker, quantity, current_price))
conn.commit()
# Update balance
st.session_state.current_balance -= total_cost
st.success(f"Bought {quantity} shares of {ticker} at ${current_price:.2f} each.")
except sqlite3.Error as e:
st.error(f"Database error: {e}")
finally:
conn.close()
# Sell stock and update portfolio
def sell_stock(ticker, quantity):
current_price = get_stock_price(ticker)
if current_price is None:
st.error(f"Failed to fetch price for {ticker}.")
return
conn = sqlite3.connect('trading_app.db')
cursor = conn.cursor()
try:
cursor.execute('SELECT * FROM portfolio WHERE ticker = ?', (ticker,))
portfolio_data = cursor.fetchall()
total_shares_owned = sum(row[2] for row in portfolio_data)
if total_shares_owned < quantity:
st.error("You do not have enough shares to sell.")
return
shares_sold = 0
for row in portfolio_data:
remaining = quantity - shares_sold
if remaining == 0:
break
owned_shares = row[2]
if owned_shares <= remaining:
st.session_state.current_balance += owned_shares * current_price
cursor.execute('DELETE FROM portfolio WHERE id = ?', (row[0],))
shares_sold += owned_shares
else:
st.session_state.current_balance += remaining * current_price
cursor.execute('UPDATE portfolio SET quantity = quantity - ? WHERE id = ?', (remaining, row[0]))
shares_sold += remaining
cursor.execute('''
INSERT INTO trade_history (ticker, quantity, trade_type, trade_price)
VALUES (?, ?, 'SELL', ?)
''', (ticker, quantity, current_price))
conn.commit()
st.success(f"Sold {quantity} shares of {ticker} at ${current_price:.2f} each.")
except sqlite3.Error as e:
st.error(f"Database error: {e}")
finally:
conn.close()
# View portfolio
def view_portfolio():
conn = sqlite3.connect('trading_app.db')
cursor = conn.cursor()
portfolio_data = cursor.execute('SELECT * FROM portfolio').fetchall()
conn.close()
if not portfolio_data:
st.info("Your portfolio is empty.")
return
portfolio_df = pd.DataFrame(portfolio_data, columns=['ID', 'Ticker', 'Quantity', 'Buy Price', 'Buy Date'])
portfolio_df['Current Price'] = portfolio_df['Ticker'].apply(get_stock_price)
portfolio_df['Profit/Loss'] = (portfolio_df['Current Price'] - portfolio_df['Buy Price']) * portfolio_df['Quantity']
st.write(f"**Available Balance:** ${st.session_state.current_balance:.2f}")
st.write(portfolio_df[['Ticker', 'Quantity', 'Buy Price', 'Current Price', 'Profit/Loss']])
total_pl = portfolio_df['Profit/Loss'].sum()
st.write(f"**Total Portfolio P/L:** ${total_pl:.2f}")
# View trade history
def view_trade_history():
conn = sqlite3.connect('trading_app.db')
cursor = conn.cursor()
trade_history_data = cursor.execute('SELECT * FROM trade_history').fetchall()
conn.close()
if not trade_history_data:
st.info("No trade history available.")
return
trade_history_df = pd.DataFrame(trade_history_data, columns=['ID', 'Ticker', 'Quantity', 'Trade Type', 'Trade Price', 'Trade Date'])
st.write(trade_history_df[['Ticker', 'Quantity', 'Trade Type', 'Trade Price', 'Trade Date']])
def add_to_watchlist(ticker):
conn = sqlite3.connect('watchlist.db')
cursor = conn.cursor()
try:
cursor.execute('INSERT INTO watchlist (ticker) VALUES (?)', (ticker,))
if cursor.fetchone():
st.info(f'{ticker} is already in your watchlist!')
else:
# Insert the ticker if it doesn't exist
cursor.execute('INSERT INTO watchlist (ticker) VALUES (?)', (ticker,))
conn.commit()
st.success(f'Successfully added {ticker} to the watchlist!')
except sqlite3.Error as e:
st.error(f'Error adding {ticker} to the watchlist: {e}')
finally:
conn.close()
def remove_from_watchlist(ticker):
conn = sqlite3.connect('watchlist.db')
cursor = conn.cursor()
try:
cursor.execute('DELETE FROM watchlist WHERE ticker = ?', (ticker,))
conn.commit()
st.success(f'Successfully removed {ticker} from the watchlist!')
except sqlite3.Error as e:
st.error(f'Error removing {ticker} from the watchlist: {e}')
finally:
conn.close()
def load_watchlist():
conn = sqlite3.connect('watchlist.db')
cursor = conn.cursor()
watchlist_data = cursor.execute('SELECT ticker FROM watchlist').fetchall()
watchlist_with_price = []
for row in watchlist_data:
ticker = row[0]
try:
data = yf.download(ticker, period='1d')
current_price = data['Close'].iloc[-1]
watchlist_with_price.append((ticker, f"${current_price:.2f}"))
except Exception as e:
st.error(f"Error fetching current price for {ticker}: {e}")
conn.close()
return watchlist_with_price
# Initialize database
init_db()
# Navigation
col1, col2, col3, col4 = st.columns(4)
with col1:
if st.button("Dashboard"):
st.session_state.page = "Dashboard"
with col2:
if st.button("Portfolio"):
st.session_state.page = "Portfolio"
with col3:
if st.button("Trade History"):
st.session_state.page = "Trade History"
with col4:
if st.button("Watchlist"):
st.session_state.page = "Watchlist"
# Page content
if st.session_state.page == "Dashboard":
st.title("Stock Dashboard")
ticker_input = st.sidebar.text_input("Enter Ticker Symbol")
start_date = st.sidebar.date_input('Start Date', value=date(2020, 1, 1))
end_date = st.sidebar.date_input('End Date', value=date.today())
quantity = st.sidebar.number_input("Quantity", min_value=1, step=1)
if st.sidebar.button("Buy Stock"):
buy_stock(ticker_input, quantity)
if st.sidebar.button("Sell Stock"):
sell_stock(ticker_input, quantity)
if ticker_input:
# Fetch the stock data
data = yf.download(ticker_input, start=start_date, end=end_date)
# Create tabs for displaying charts
line_chart, candle_chart = st.tabs(["Line Chart", "Candle Chart"])
with line_chart:
st.header('Line Chart')
fig = px.line(data, x=data.index, y=data['Adj Close'], title=ticker_input)
st.plotly_chart(fig)
with candle_chart:
st.header('Candle Chart')
fig = go.Figure(data=[go.Candlestick(x=data.index,
open=data['Open'],
high=data['High'],
low=data['Low'],
close=data['Close'])])
st.plotly_chart(fig)
# Create additional tabs for other sections
pricing_data, fundamental_data, news, tech_indicator = st.tabs(["Pricing Data",
"Fundamental Data",
"News",
"Technical Indicator"])
with pricing_data:
st.header('Pricing Movements')
data2 = data.copy()
data2['% Change'] = data['Adj Close'] / data['Adj Close'].shift(1) - 1
st.write(data2)
annual_return = data2['% Change'].mean() * 252 * 100
st.write('Annual Return is ', annual_return, '%')
stdev = np.std(data2['% Change']) * np.sqrt(252) * 100
st.write('Standard Deviation is', stdev, '%')
st.write('Risk Adj. Return is', annual_return / stdev)
with fundamental_data:
st.header('Fundamental Data')
key = 'OW1639L63B5UCYYL'
fd = FundamentalData(key, output_format='pandas')
balance_sheet = fd.get_balance_sheet_annual(ticker_input)[0]
bs = balance_sheet.T[2:]
bs.columns = list(balance_sheet.T.iloc[0])
st.write(bs)
with news:
st.header(f'News of {ticker_input}')
sn = StockNews(ticker_input, save_news=False)
df_news = sn.read_rss()
for i in range(10):
st.subheader(f'News {i+1}')
st.write(df_news['published'][i])
st.write(df_news['title'][i])
st.write(df_news['summary'][i])
title_sentiment = df_news['sentiment_title'][i]
st.write(f'Title Sentiment {title_sentiment}')
news_sentiment = df_news['sentiment_summary'][i]
st.write(f'News Sentiment {news_sentiment}')
with tech_indicator:
st.subheader('Technical Analysis Dashboard')
df = pd.DataFrame()
ind_list = df.ta.indicators(as_list=True)
technical_indicator = st.selectbox('Tech Indicator', options=ind_list)
method = technical_indicator
indicator = pd.DataFrame(getattr(ta, method)(low=data['Low'],
close=data['Close'],
high=data['High'],
volume=data['Volume']))
indicator['Close'] = data['Close']
# Display indicator
figw_ind_new = px.line(indicator)
st.plotly_chart(figw_ind_new)
st.write(indicator)
elif st.session_state.page == "Portfolio":
st.title("Portfolio")
st.subheader(f"Available Balance: ${st.session_state.current_balance:.2f}")
view_portfolio()
if st.button("Reset Portfolio"):
reset_table('portfolio')
reset_balance()
elif st.session_state.page == "Trade History":
st.title("Trade History")
view_trade_history()
if st.button("Reset Trade History"):
reset_table('trade_history')
elif st.session_state.page == "Watchlist":
st.title("My Watchlist")
# Load watchlist data
watchlist_data = load_watchlist()
if watchlist_data:
# Create a table-like display with headers
col1, col2, col3 = st.columns([2, 2, 1])
col1.subheader("Stock Name")
col2.subheader("Stock Price")
col3.subheader("Remove")
# Display each stock in the watchlist
for index, (ticker, price) in enumerate(watchlist_data):
col1, col2, col3 = st.columns([2, 2, 1])
with col1:
st.write(ticker)
with col2:
st.write(price)
with col3:
if st.button(f"Remove", key=f"remove_{ticker}"):
remove_from_watchlist(ticker)
st.experimental_rerun()
else:
st.info("Your watchlist is empty.")
# Input to add a ticker to the watchlist
ticker_to_add = st.text_input("Add a Ticker to Watchlist")
if st.button("Add to Watchlist"):
if ticker_to_add:
add_to_watchlist(ticker_to_add)
st.experimental_rerun()
else:
st.error("Please enter a ticker to add.")
if st.button("Reset Watchlist"):
reset_table('watchlist')