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