Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,30 +1,23 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
from
|
|
|
4 |
import datetime
|
5 |
-
import os
|
6 |
-
###from urllib.request import urlopen # for getting data from FMP API
|
7 |
-
import json # for parsing data from FMP API
|
8 |
-
import urllib.request
|
9 |
|
|
|
|
|
10 |
|
11 |
# For parsing data from API from JSON to a Python Dictionary
|
12 |
def get_jsonparsed_data(url):
|
13 |
-
|
14 |
headers = {'User-Agent': 'Mozilla/5.0'}
|
15 |
request = urllib.request.Request(url, headers=headers)
|
16 |
response = urllib.request.urlopen(request)
|
17 |
data = response.read().decode("utf-8")
|
18 |
return json.loads(data)
|
19 |
-
|
20 |
-
|
21 |
-
###response = urlopen(url)
|
22 |
-
###data = response.read().decode("utf-8")
|
23 |
-
###return json.loads(data)
|
24 |
|
25 |
# Get FMP API stored as environment variable
|
26 |
-
|
27 |
-
apiKey = os.getenv['FMP_API_KEY']
|
28 |
if not apiKey:
|
29 |
st.error("API Key not found. Please set the FMP_API_KEY environment variable.")
|
30 |
st.stop()
|
@@ -32,16 +25,14 @@ if not apiKey:
|
|
32 |
# Financialmodelingprep (FMP) api base url
|
33 |
base_url = "https://financialmodelingprep.com/api/v3/"
|
34 |
|
35 |
-
|
36 |
# Get today's date and add 3 months to it
|
37 |
# Convert both today's date and the 3 months later date to strings (for input into API endpoint URL later)
|
38 |
# This is the date range within which we want to get our earnings dates
|
39 |
-
|
40 |
today = datetime.datetime.today()
|
41 |
today_string = today.strftime('%Y-%m-%d')
|
42 |
future_string = (today + relativedelta(months=3)).strftime('%Y-%m-%d')
|
43 |
|
44 |
-
# This is the full API endpoint to get the earnings dates from today to
|
45 |
url = f"{base_url}earning_calendar?from={today_string}&to={future_string}&apikey={apiKey}"
|
46 |
|
47 |
# This decorator ensures that the call to the FMP API will only run once at the start of this app
|
@@ -51,22 +42,18 @@ url = f"{base_url}earning_calendar?from={today_string}&to={future_string}&apikey
|
|
51 |
def get_earnings_dates(url):
|
52 |
events = get_jsonparsed_data(url)
|
53 |
return events
|
54 |
-
|
55 |
-
events = get_earnings_dates(url)
|
56 |
|
|
|
57 |
|
58 |
with st.sidebar:
|
59 |
st.title("미국 주식 어닝 달력")
|
60 |
st.header("알고싶은 미국주식 티커를 입력해주세요~")
|
61 |
-
#tickers = ['GOOG', 'META', 'TSLA', 'NET', 'V', 'MA', 'BA', 'C']
|
62 |
-
|
63 |
-
# For users to enter tickers of interest
|
64 |
tickers_string = st.text_area('달력에 포함될 티커를 ,로 구분해서 입력해주세요~ \
|
65 |
예 "NVDA, META, AMZN," 입력후 컨트롤 + 엔터 또는 "적용" 버튼 클릭 ',
|
66 |
-
value
|
67 |
st.button("적용")
|
68 |
st.write("기본세팅: ")
|
69 |
-
st.write("TSMC, Nvidia, AMD, Intel, Micron, Apple, 알파벳,FirstSolar, ARM, Qualcomm, MS, Applied mat, Lam research, Synopsys, Cadence design, Tesla, Eaton,Fluence energy, Arista net, Nextracker, GE베르노바, SalesForce")
|
70 |
st.write("참고: Earnings 날짜는 오늘부터 3개월 이내로 제한됨")
|
71 |
st.write('')
|
72 |
st.write("달력은 미국시간 기준입니다.")
|
@@ -75,18 +62,6 @@ with st.sidebar:
|
|
75 |
st.write("[장후: 한국시간 익일 아침]")
|
76 |
st.write('')
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
#st.markdown("## [Explanatory Article](https://medium.datadriveninvestor.com/build-a-stock-earnings-calendar-of-your-favorite-stocks-in-python-36bba1950a61)")
|
81 |
-
st.write('')
|
82 |
-
|
83 |
-
# Where the data came from
|
84 |
-
#st.markdown("## [Financial Modeling Prep API](https://intelligence.financialmodelingprep.com/pricing-plans?couponCode=damianboh&utm_campaign=damianboh&utm_medium=blog&utm_source=medium)\
|
85 |
-
#\n\nEarnings Dates for all tickers are obtained from the FinancialModelingPrep API, feel free to sign up\
|
86 |
-
#[here](https://intelligence.financialmodelingprep.com/pricing-plans?couponCode=damianboh&utm_campaign=damianboh&utm_medium=blog&utm_source=medium)\
|
87 |
-
#if you wish.")
|
88 |
-
|
89 |
-
# Parse user input into a list
|
90 |
tickers_string = tickers_string.replace(' ', '')
|
91 |
tickers = tickers_string.split(',')
|
92 |
|
@@ -96,30 +71,27 @@ for event in events:
|
|
96 |
if event['symbol'] in tickers:
|
97 |
calendar_event = {}
|
98 |
calendar_event['title'] = event['symbol']
|
99 |
-
if event['time'] == 'bmo':
|
100 |
calendar_event['title'] = '장전' + calendar_event['title']
|
101 |
-
elif event['time'] == 'amc':
|
102 |
-
calendar_event['title'] = '장후'
|
103 |
calendar_event['start'] = event['date']
|
104 |
calendar_events.append(calendar_event)
|
105 |
|
106 |
st.header("미국 주식 어닝 달력")
|
107 |
|
108 |
-
|
109 |
calendar_options = {
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
custom_css="""
|
123 |
.fc-event-past {
|
124 |
opacity: 0.8;
|
125 |
}
|
@@ -134,5 +106,4 @@ custom_css="""
|
|
134 |
}
|
135 |
"""
|
136 |
|
137 |
-
|
138 |
calendar = calendar(events=calendar_events, options=calendar_options, custom_css=custom_css)
|
|
|
1 |
+
|
2 |
+
import streamlit as st # for overall GUI
|
3 |
+
from streamlit_calendar import calendar # to show calendar
|
4 |
+
from dateutil.relativedelta import relativedelta # for addition to dates
|
5 |
import datetime
|
6 |
+
import os # for extracting environment variable
|
|
|
|
|
|
|
7 |
|
8 |
+
import json # for parsing data from FMP API
|
9 |
+
import urllib.request # for getting data from FMP API
|
10 |
|
11 |
# For parsing data from API from JSON to a Python Dictionary
|
12 |
def get_jsonparsed_data(url):
|
|
|
13 |
headers = {'User-Agent': 'Mozilla/5.0'}
|
14 |
request = urllib.request.Request(url, headers=headers)
|
15 |
response = urllib.request.urlopen(request)
|
16 |
data = response.read().decode("utf-8")
|
17 |
return json.loads(data)
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
# Get FMP API stored as environment variable
|
20 |
+
apiKey = os.getenv('FMP_API_KEY')
|
|
|
21 |
if not apiKey:
|
22 |
st.error("API Key not found. Please set the FMP_API_KEY environment variable.")
|
23 |
st.stop()
|
|
|
25 |
# Financialmodelingprep (FMP) api base url
|
26 |
base_url = "https://financialmodelingprep.com/api/v3/"
|
27 |
|
|
|
28 |
# Get today's date and add 3 months to it
|
29 |
# Convert both today's date and the 3 months later date to strings (for input into API endpoint URL later)
|
30 |
# This is the date range within which we want to get our earnings dates
|
|
|
31 |
today = datetime.datetime.today()
|
32 |
today_string = today.strftime('%Y-%m-%d')
|
33 |
future_string = (today + relativedelta(months=3)).strftime('%Y-%m-%d')
|
34 |
|
35 |
+
# This is the full API endpoint to get the earnings dates from today to 3 months after
|
36 |
url = f"{base_url}earning_calendar?from={today_string}&to={future_string}&apikey={apiKey}"
|
37 |
|
38 |
# This decorator ensures that the call to the FMP API will only run once at the start of this app
|
|
|
42 |
def get_earnings_dates(url):
|
43 |
events = get_jsonparsed_data(url)
|
44 |
return events
|
|
|
|
|
45 |
|
46 |
+
events = get_earnings_dates(url)
|
47 |
|
48 |
with st.sidebar:
|
49 |
st.title("미국 주식 어닝 달력")
|
50 |
st.header("알고싶은 미국주식 티커를 입력해주세요~")
|
|
|
|
|
|
|
51 |
tickers_string = st.text_area('달력에 포함될 티커를 ,로 구분해서 입력해주세요~ \
|
52 |
예 "NVDA, META, AMZN," 입력후 컨트롤 + 엔터 또는 "적용" 버튼 클릭 ',
|
53 |
+
value='TSM,NVDA,AMD,INTC,MU,AAPL,GOOGL,FSLR,ARM,QCOM,MSFT,AMAT,LRCX,SNPS,CDNS,TSLA,ETN,FLNC,ANET,NXT,GEV,CRM,').upper()
|
54 |
st.button("적용")
|
55 |
st.write("기본세팅: ")
|
56 |
+
st.write("TSMC, Nvidia, AMD, Intel, Micron, Apple, 알파벳, FirstSolar, ARM, Qualcomm, MS, Applied mat, Lam research, Synopsys, Cadence design, Tesla, Eaton, Fluence energy, Arista net, Nextracker, GE 베르노바, SalesForce")
|
57 |
st.write("참고: Earnings 날짜는 오늘부터 3개월 이내로 제한됨")
|
58 |
st.write('')
|
59 |
st.write("달력은 미국시간 기준입니다.")
|
|
|
62 |
st.write("[장후: 한국시간 익일 아침]")
|
63 |
st.write('')
|
64 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
tickers_string = tickers_string.replace(' ', '')
|
66 |
tickers = tickers_string.split(',')
|
67 |
|
|
|
71 |
if event['symbol'] in tickers:
|
72 |
calendar_event = {}
|
73 |
calendar_event['title'] = event['symbol']
|
74 |
+
if event['time'] == 'bmo': # before market opens, add sunrise symbol
|
75 |
calendar_event['title'] = '장전' + calendar_event['title']
|
76 |
+
elif event['time'] == 'amc': # after market closes, add sunset symbol
|
77 |
+
calendar_event['title'] = '장후' + calendar_event['title']
|
78 |
calendar_event['start'] = event['date']
|
79 |
calendar_events.append(calendar_event)
|
80 |
|
81 |
st.header("미국 주식 어닝 달력")
|
82 |
|
|
|
83 |
calendar_options = {
|
84 |
+
"editable": "true",
|
85 |
+
"navLinks": "true",
|
86 |
+
"headerToolbar": {
|
87 |
+
"left": "today prev,next",
|
88 |
+
"center": "title",
|
89 |
+
"right": "dayGridDay,dayGridWeek,dayGridMonth,listMonth",
|
90 |
+
},
|
91 |
+
"initialView": "dayGridMonth"
|
92 |
+
}
|
93 |
+
|
94 |
+
custom_css = """
|
|
|
|
|
95 |
.fc-event-past {
|
96 |
opacity: 0.8;
|
97 |
}
|
|
|
106 |
}
|
107 |
"""
|
108 |
|
|
|
109 |
calendar = calendar(events=calendar_events, options=calendar_options, custom_css=custom_css)
|