How to retrieve dates from user input of a future date

The following code allows me to select dates to visualise and predict stock prices in a defined date range

start = '2010-01-01'
end = '2021-11-20'

st.title('Stock Prediction')

ticker_input = st.sidebar.text_input('Enter Stock Ticker', 'AAPL')
df = data.DataReader(ticker_input, 'yahoo', start, end)
st.subheader(ticker_input)

The code works as intended when I change the end variable to a future date within the IDE by changing end to '2022-01-01' and then run it in streamlit. My prediction chart would also change to reflect the end date. How can I change the end variable so the user can select dates in the future themselves? The tutorial I followed doesn’t show this and I’ve tried to look at examples where datetime lets users select dates in the future and they all seem to just go up to present day.

start = st.date_input('Start', value = pd.to_datetime('2010-01-01'))
end = st.date_input('End', value = pd.to_datetime('2024-01-01'))

I tried using pd.to_datetime and st.date_input like this to see if the user can change it from the dropdown calendar but it doesn’t work.

You could try the following:

import streamlit as st from datetime import datetime as dt

start = dt.strptime(‘2010-01-01’, ‘%Y-%m-%d’) # set default value
end = dt.strptime(‘2021-11-20’, ‘%Y-%m-%d’) # set default value

st.title(‘Stock Prediction’)

ticker_input = st.sidebar.text_input(‘Enter Stock Ticker’, ‘AAPL’)
start = st.date_input(“Enter Start Date”, value = start)
end = st.date_input(“Enter Start Date”, value = end)

add code to check if end date is >= start date + any other processing code.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.