-
Notifications
You must be signed in to change notification settings - Fork 0
/
apptest.py
153 lines (111 loc) · 3.95 KB
/
apptest.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
# 앱 시작
import streamlit as st
import pandas as pd
import sqlite3
from typing import List
from sqlite3 import Connection
from datetime import datetime, timedelta
import yfinance as yf
import sqlalchemy
import pandas_datareader.data as web
import requests
import plotly.figure_factory as ff
import plotly.graph_objects as go
import numpy as np
Stock_sqlite_db = "stock.db"
def memoize(func):
cache = dict()
def memoized_func(*args):
if args in cache:
return cache[args]
result = func(*args)
cache[args] = result
return result
return memoized_func
def main():
st.title('stock analysis')
st.markdown("Search the history of the companies' stock data easily.")
conn = get_connection(Stock_sqlite_db)
conn = get_connection(Stock_sqlite_db)
conn = get_connection(Stock_sqlite_db)
conn = get_connection(Stock_sqlite_db)
conn = get_connection(Stock_sqlite_db)
conn = get_connection(Stock_sqlite_db)
conn = get_connection(Stock_sqlite_db)
conn = get_connection(Stock_sqlite_db)
#init_db(conn)
display_data(conn)
def display_data(conn: Connection):
if st.checkbox("Display data in sqlite database"):
#st.dataframe(get_data(conn))
st.dataframe(get_data_demo(['AAPL', 'TSLA', 'BRK-B', 'DIS', 'AMZN', 'WMT', 'COST', 'AMAT']))
val = st.selectbox("How would you like to be contacted?",
("Apple (AAPL)", "Tesla (TSLA)", "Berkshire Hathaway (BRK-B)", "Walt Disney (DIS)", "Amazon (AMZN)", "Walmart (WMT)", "Costco (COST)", "Applied Materials (AMAT)")
)
val = val.split('(')[1][:-1]
df = get_price(conn, val)
st.dataframe(df)
st.line_chart(df)
def get_price(conn: Connection, company_names: str):
companies = company_names.split(',')
output = []
start = datetime.now() - timedelta(days=730)
end = datetime.now()
for name in companies:
ticker = yf.Ticker(name)
df = ticker.history(start=start, end=end, interval='60m')
output.append(df['Open'].rename(name))
df_all = pd.DataFrame(output)
return df_all.T
def get_price2(conn: Connection, company_name):
curs = conn.cursor()
a = curs.execute('''
SELECT now_price FROM stock
WHERE company_name = ?
''', (company_name,))
return pd.DataFrame(a, columns=['now_price'])
@st.cache
def get_data_demo(params: List[any]):
df = pd.DataFrame(params)
return df
def get_data(conn: Connection):
df = pd.read_sql("SELECT company_name FROM stock", con=conn)
return df
@memoize
def get_connection(path: str):
"""Put the connection in cache unless path does not change between streamlit returns"""
print('create connection')
return sqlite3.connect(path, check_same_thread=False)
# Set sample stock symbol to instrument variable
symbol = st.text_input('Enter Stock Symbol', 'QQQ')
API_URL = "https://www.alphavantage.co/query"
data = { "function": "TIME_SERIES_DAILY",
"symbol": symbol,
"outputsize" : "compact",
"datatype": "json",
"apikey": "XXXXXXXXXXXX" } #ENTER YOUR ALPHAVANTAGE KEY HERE
#https://www.alphavantage.co/query/
response = requests.get(API_URL, data).json()
data = pd.DataFrame.from_dict(response['Time Series (Daily)'], orient= 'index').sort_index(axis=1)
data = data.rename(columns={ '1. open': 'Open', '2. high': 'High', '3. low': 'Low', '4. close': 'Close', '5. volume': 'Volume'})
data = data[['Open', 'High', 'Low', 'Close', 'Volume']]
data['Date'] = data.index
fig = go.Figure(data=[go.Candlestick(x=data['Date'],
open=data['Open'],
high=data['High'],
low=data['Low'],
close=data['Close'],
name=symbol)])
fig.update_layout(
title=symbol+ ' Daily Chart',
xaxis_title="Date",
yaxis_title="Price ($)",
font=dict(
family="Courier New, monospace",
size=12,
color="black"
)
)
st.plotly_chart(fig, use_container_width=True)
if __name__ == "__main__":
main()