-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSampleDash.py
160 lines (127 loc) · 5.56 KB
/
SampleDash.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
import streamlit as st
import pandas as pd
import numpy as np
import requests
import tweepy
import config
import psycopg2, psycopg2.extras
import plotly.graph_objects as go
auth = tweepy.OAuthHandler(config.TWITTER_CONSUMER_KEY, config.TWITTER_CONSUMER_SECRET)
auth.set_access_token(config.TWITTER_ACCESS_TOKEN, config.TWITTER_ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
connection = psycopg2.connect(host=config.DB_HOST, database=config.DB_NAME, user=config.DB_USER, password=config.DB_PASS)
cursor = connection.cursor(cursor_factory=psycopg2.extras.DictCursor)
option = st.sidebar.selectbox("Which Dashboard?", ('twitter', 'wallstreetbets', 'stocktwits', 'chart', 'pattern'), 3)
st.header(option)
if option == 'twitter':
for username in config.TWITTER_USERNAMES:
user = api.get_user(username)
tweets = api.user_timeline(username)
st.subheader(username)
st.image(user.profile_image_url)
for tweet in tweets:
if '$' in tweet.text:
words = tweet.text.split(' ')
for word in words:
if word.startswith('$') and word[1:].isalpha():
symbol = word[1:]
st.write(symbol)
st.write(tweet.text)
st.image(f"https://finviz.com/chart.ashx?t={symbol}")
if option == 'chart':
symbol = st.sidebar.text_input("Symbol", value='MSFT', max_chars=None, key=None, type='default')
data = pd.read_sql("""
select date(day) as day, open, high, low, close
from daily_bars
where stock_id = (select id from stock where UPPER(symbol) = %s)
order by day asc""", connection, params=(symbol.upper(),))
st.subheader(symbol.upper())
fig = go.Figure(data=[go.Candlestick(x=data['day'],
open=data['open'],
high=data['high'],
low=data['low'],
close=data['close'],
name=symbol)])
fig.update_xaxes(type='category')
fig.update_layout(height=700)
st.plotly_chart(fig, use_container_width=True)
st.write(data)
if option == 'wallstreetbets':
num_days = st.sidebar.slider('Number of days', 1, 30, 3)
cursor.execute("""
SELECT COUNT(*) AS num_mentions, symbol
FROM mention JOIN stock ON stock.id = mention.stock_id
WHERE date(dt) > current_date - interval '%s day'
GROUP BY stock_id, symbol
HAVING COUNT(symbol) > 10
ORDER BY num_mentions DESC
""", (num_days,))
counts = cursor.fetchall()
for count in counts:
st.write(count)
cursor.execute("""
SELECT symbol, message, url, dt, username
FROM mention JOIN stock ON stock.id = mention.stock_id
ORDER BY dt DESC
LIMIT 100
""")
mentions = cursor.fetchall()
for mention in mentions:
st.text(mention['dt'])
st.text(mention['symbol'])
st.text(mention['message'])
st.text(mention['url'])
st.text(mention['username'])
rows = cursor.fetchall()
st.write(rows)
if option == 'pattern':
pattern = st.sidebar.selectbox(
"Which Pattern?",
("engulfing", "threebar")
)
if pattern == 'engulfing':
cursor.execute("""
SELECT *
FROM (
SELECT day, open, close, stock_id, symbol,
LAG(close, 1) OVER ( PARTITION BY stock_id ORDER BY day ) previous_close,
LAG(open, 1) OVER ( PARTITION BY stock_id ORDER BY day ) previous_open
FROM daily_bars
JOIN stock ON stock.id = daily_bars.stock_id
) a
WHERE previous_close < previous_open AND close > previous_open AND open < previous_close
AND day = '2021-02-18'
""")
if pattern == 'threebar':
cursor.execute("""
SELECT *
FROM (
SELECT day, close, volume, stock_id, symbol,
LAG(close, 1) OVER ( PARTITION BY stock_id ORDER BY day ) previous_close,
LAG(volume, 1) OVER ( PARTITION BY stock_id ORDER BY day ) previous_volume,
LAG(close, 2) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_close,
LAG(volume, 2) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_volume,
LAG(close, 3) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_previous_close,
LAG(volume, 3) OVER ( PARTITION BY stock_id ORDER BY day ) previous_previous_previous_volume
FROM daily_bars
JOIN stock ON stock.id = daily_bars.stock_id) a
WHERE close > previous_previous_previous_close
AND previous_close < previous_previous_close
AND previous_close < previous_previous_previous_close
AND volume > previous_volume
AND previous_volume < previous_previous_volume
AND previous_previous_volume < previous_previous_previous_volume
AND day = '2021-02-19'
""")
rows = cursor.fetchall()
for row in rows:
st.image(f"https://finviz.com/chart.ashx?t={row['symbol']}")
if option == 'stocktwits':
symbol = st.sidebar.text_input("Symbol", value='AAPL', max_chars=5)
r = requests.get(f"https://api.stocktwits.com/api/2/streams/symbol/{symbol}.json")
data = r.json()
for message in data['messages']:
st.image(message['user']['avatar_url'])
st.write(message['user']['username'])
st.write(message['created_at'])
st.write(message['body'])