Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import datetime | |
| import requests | |
| import pandas as pd | |
| from streamlit_calendar import calendar | |
| import os | |
| API_KEY = os.getenv("FMP_API_KEY") | |
| def fetch_ipo_confirmed(from_date, to_date): | |
| url = ( | |
| f"https://financialmodelingprep.com/api/v4/ipo-calendar-confirmed" | |
| f"?from={from_date}&to={to_date}&apikey={API_KEY}" | |
| ) | |
| resp = requests.get(url) | |
| if resp.status_code == 200: | |
| return resp.json() | |
| return [] | |
| def fetch_ipo_prospectus(from_date, to_date): | |
| url = ( | |
| f"https://financialmodelingprep.com/api/v4/ipo-calendar-prospectus" | |
| f"?from={from_date}&to={to_date}&apikey={API_KEY}" | |
| ) | |
| resp = requests.get(url) | |
| if resp.status_code == 200: | |
| return resp.json() | |
| return [] | |
| def fetch_ipo_calendar(from_date, to_date): | |
| url = ( | |
| f"https://financialmodelingprep.com/api/v3/ipo_calendar" | |
| f"?from={from_date}&to={to_date}&apikey={API_KEY}" | |
| ) | |
| resp = requests.get(url) | |
| if resp.status_code == 200: | |
| return resp.json() | |
| return [] | |
| def main(): | |
| st.set_page_config(page_title="IPO Calendar", layout="wide") | |
| # Keep session data | |
| if "general_data" not in st.session_state: | |
| st.session_state["general_data"] = [] | |
| st.title("IPO Calendar") | |
| st.write("This displays three types of IPO events: Confirmed, Prospectus, and Calendar. Set parameters and click run.") | |
| st.sidebar.title("Input Parameters") | |
| with st.sidebar.expander("How to Use", expanded=False): | |
| st.write( | |
| """ | |
| 1. Check the event types you want to see. | |
| 2. Select the date range. | |
| 3. Click the button to retrieve the data. | |
| 4. View the calendar and the table below. | |
| """ | |
| ) | |
| with st.sidebar.expander("Event Type", expanded=True): | |
| include_confirmed = st.checkbox("Include IPO Confirmed", value=True, help="Include IPOs that are confirmed and have a scheduled date for going public.") | |
| include_prospectus = st.checkbox("Include IPO Prospectus", value=True, help="Include IPO prospectuses with detailed company and securities information.") | |
| include_calendar = st.checkbox("Include IPO Calendar", value=True, help="Include a list of upcoming IPOs with expected dates and price ranges.") | |
| with st.sidebar.expander("Parameters", expanded=True): | |
| today = datetime.date.today() | |
| one_month_later = today + datetime.timedelta(days=30) | |
| from_date = st.date_input("From Date", value=today, help="Select the start date for the IPO data.") | |
| to_date = st.date_input("To Date", value=one_month_later, help="Select the end date for the IPO data.") | |
| if st.sidebar.button("Retrieve Calendar"): | |
| all_events = [] | |
| if include_confirmed: | |
| confirmed_data = fetch_ipo_confirmed(from_date, to_date) | |
| for item in confirmed_data: | |
| # Choose effectivenessDate if present, else use filingDate | |
| date_str = item.get("effectivenessDate") or item.get("filingDate") or "" | |
| if date_str: | |
| start_dt = f"{date_str}T00:00:00" | |
| end_dt = f"{date_str}T23:59:59" | |
| sym = item.get("symbol", "") | |
| event_title = f"[IPO Confirmed] {sym}" | |
| event_entry = { | |
| "start": start_dt, | |
| "end": end_dt, | |
| "title": event_title, | |
| "color": "#3D9DF3", | |
| "eventType": "IPO Confirmed" | |
| } | |
| event_entry.update(item) | |
| all_events.append(event_entry) | |
| if include_prospectus: | |
| prospectus_data = fetch_ipo_prospectus(from_date, to_date) | |
| for item in prospectus_data: | |
| # Use filingDate because ipoDate might not always be a standard format | |
| date_str = item.get("filingDate", "") | |
| if date_str: | |
| start_dt = f"{date_str}T00:00:00" | |
| end_dt = f"{date_str}T23:59:59" | |
| sym = item.get("symbol", "") | |
| event_title = f"[IPO Prospectus] {sym}" | |
| event_entry = { | |
| "start": start_dt, | |
| "end": end_dt, | |
| "title": event_title, | |
| "color": "#80C080", | |
| "eventType": "IPO Prospectus" | |
| } | |
| event_entry.update(item) | |
| all_events.append(event_entry) | |
| if include_calendar: | |
| calendar_data = fetch_ipo_calendar(from_date, to_date) | |
| for item in calendar_data: | |
| date_str = item.get("date", "") | |
| if date_str: | |
| start_dt = f"{date_str}T00:00:00" | |
| end_dt = f"{date_str}T23:59:59" | |
| sym = item.get("symbol", "") | |
| event_title = f"[IPO Calendar] {sym}" | |
| event_entry = { | |
| "start": start_dt, | |
| "end": end_dt, | |
| "title": event_title, | |
| "color": "#FFC870", | |
| "eventType": "IPO Calendar" | |
| } | |
| event_entry.update(item) | |
| all_events.append(event_entry) | |
| st.session_state["general_data"] = all_events | |
| #st.subheader("Calendar Results") | |
| data_general = st.session_state["general_data"] | |
| if data_general: | |
| # Prepare data for display | |
| calendar_events = [] | |
| for ev in data_general: | |
| calendar_events.append({ | |
| "title": ev["title"], | |
| "start": ev["start"], | |
| "end": ev["end"], | |
| "color": ev["color"], | |
| }) | |
| cal_options = { | |
| "initialView": "dayGridMonth", | |
| "headerToolbar": { | |
| "left": "today prev,next", | |
| "center": "title", | |
| "right": "dayGridDay,dayGridWeek,dayGridMonth", | |
| }, | |
| "navLinks": True, | |
| } | |
| calendar(events=calendar_events, options=cal_options, key="ipo_cal") | |
| st.write("Data Table") | |
| df_g = pd.DataFrame(data_general) | |
| st.dataframe(df_g, use_container_width=True) | |
| else: | |
| st.write(" ") | |
| #st.write("No data retrieved. Check your selections and press the button.") | |
| if __name__ == "__main__": | |
| main() | |
| hide_streamlit_style = """ | |
| <style> | |
| #MainMenu {visibility: hidden;} | |
| footer {visibility: hidden;} | |
| </style> | |
| """ | |
| st.markdown(hide_streamlit_style, unsafe_allow_html=True) | |