Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -54,7 +54,7 @@ def plot_distributions(bootstrap_simulations, data, thresholds, bootstrap_probab
|
|
| 54 |
fig.add_vline(x=latest_price, line=dict(color='green', dash='dash'), name=f'Latest Price: {latest_price:.2f}')
|
| 55 |
fig.add_vrect(x0=ci_68_bootstrap[0], x1=ci_68_bootstrap[1], fillcolor='yellow', opacity=0.2, layer="below", line_width=0, annotation_text="68% CI", annotation_position="top left")
|
| 56 |
fig.add_vrect(x0=ci_95_bootstrap[0], x1=ci_95_bootstrap[1], fillcolor='grey', opacity=0.2, layer="below", line_width=0, annotation_text="95% CI", annotation_position="top left")
|
| 57 |
-
|
| 58 |
max_freq = np.histogram(final_bootstrap_prices, bins=50)[0].max()
|
| 59 |
|
| 60 |
# Calculate positions based on a fraction of the max frequency
|
|
@@ -110,20 +110,44 @@ def plot_price_with_cones(data, bootstrap_percentiles, days, thresholds, bootstr
|
|
| 110 |
|
| 111 |
fig.update_layout(title='Bootstrapping Simulation Cone', xaxis_title='Date', yaxis_title='Price', showlegend=True)
|
| 112 |
fig.update_xaxes(type='date')
|
| 113 |
-
|
| 114 |
return fig
|
| 115 |
|
| 116 |
# Streamlit app
|
| 117 |
st.set_page_config(layout="wide")
|
| 118 |
-
st.title('Future Stock Price Bootstrap Simulation')
|
| 119 |
|
| 120 |
st.sidebar.header('Input Parameters')
|
| 121 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
st.write("""
|
| 123 |
### Description
|
| 124 |
-
This application simulates future stock prices using bootstrapping simulation methods.
|
| 125 |
-
You can specify the stock ticker, the date range, the number of simulation days, the number of simulations, and price thresholds.
|
| 126 |
-
The simulation results will show the probability of the
|
| 127 |
|
| 128 |
**Background and Concept**
|
| 129 |
|
|
@@ -131,7 +155,7 @@ The concept of bootstrapping was introduced by Bradley Efron in 1979. The primar
|
|
| 131 |
|
| 132 |
**Steps in Bootstrapping:**
|
| 133 |
|
| 134 |
-
Given a dataset \( X = \{x_1, x_2, ..., x_n\} \), we aim to estimate the statistic \( \theta \) (e.g., the mean return of a stock).
|
| 135 |
|
| 136 |
1. **Resampling**: Create a resample \( X^* \) by drawing \( n \) observations from \( X \) with replacement. This means that each data point can be selected multiple times in a single resample:
|
| 137 |
""")
|
|
@@ -147,27 +171,12 @@ st.latex(r'\{\theta_1^*, \theta_2^*, ..., \theta_B^*\}')
|
|
| 147 |
st.write("""
|
| 148 |
4. **Estimate**: Use the bootstrap statistics to estimate the mean, standard error, and confidence intervals of \( \theta \).
|
| 149 |
|
| 150 |
-
**How to use:**
|
| 151 |
-
1. Enter the stock ticker, start date, and end date.
|
| 152 |
-
2. Set the number of days for the simulation and the number of iterations.
|
| 153 |
-
3. Enter the price thresholds.
|
| 154 |
-
4. Click 'Run Simulation' to start the bootstrapping simulation.
|
| 155 |
-
|
| 156 |
**Results:**
|
| 157 |
The app will display two charts:
|
| 158 |
1. The distribution of the final simulated prices with key statistical measures.
|
| 159 |
-
2. The historical
|
| 160 |
""")
|
| 161 |
|
| 162 |
-
ticker = st.sidebar.text_input('Enter Stock Ticker', 'ASML.AS')
|
| 163 |
-
start_date = st.sidebar.date_input('Start Date', pd.to_datetime('2020-01-01'))
|
| 164 |
-
end_date = st.sidebar.date_input('End Date', pd.to_datetime('2025-01-01'))
|
| 165 |
-
days = st.sidebar.number_input('Number of Days for Simulation', min_value=1, max_value=365, value=30)
|
| 166 |
-
n_iterations = st.sidebar.number_input('Number of Simulations', min_value=100, max_value=100000, value=10000)
|
| 167 |
-
threshold1 = st.sidebar.number_input('Threshold 1', min_value=0, value=850)
|
| 168 |
-
threshold2 = st.sidebar.number_input('Threshold 2', min_value=0, value=1050)
|
| 169 |
-
thresholds = [threshold1, threshold2]
|
| 170 |
-
|
| 171 |
if st.sidebar.button('Run Simulation'):
|
| 172 |
data = get_stock_data(ticker, start_date, end_date)
|
| 173 |
|
|
@@ -199,11 +208,11 @@ if st.sidebar.button('Run Simulation'):
|
|
| 199 |
- **Bootstrap 95% CI:** The 95% confidence interval for the simulated future prices.
|
| 200 |
- **Threshold 1 and Threshold 2:** {threshold1:.2f}, {threshold2:.2f}
|
| 201 |
- **Probability Annotations:**
|
| 202 |
-
- The probability of the
|
| 203 |
-
- The probability of the
|
| 204 |
-
- The probability of the
|
| 205 |
|
| 206 |
-
These results help in understanding the potential future movements of the stock price based on historical data and bootstrapping simulation.
|
| 207 |
""")
|
| 208 |
|
| 209 |
hide_streamlit_style = """
|
|
@@ -212,4 +221,4 @@ hide_streamlit_style = """
|
|
| 212 |
footer {visibility: hidden;}
|
| 213 |
</style>
|
| 214 |
"""
|
| 215 |
-
st.markdown(hide_streamlit_style
|
|
|
|
| 54 |
fig.add_vline(x=latest_price, line=dict(color='green', dash='dash'), name=f'Latest Price: {latest_price:.2f}')
|
| 55 |
fig.add_vrect(x0=ci_68_bootstrap[0], x1=ci_68_bootstrap[1], fillcolor='yellow', opacity=0.2, layer="below", line_width=0, annotation_text="68% CI", annotation_position="top left")
|
| 56 |
fig.add_vrect(x0=ci_95_bootstrap[0], x1=ci_95_bootstrap[1], fillcolor='grey', opacity=0.2, layer="below", line_width=0, annotation_text="95% CI", annotation_position="top left")
|
| 57 |
+
|
| 58 |
max_freq = np.histogram(final_bootstrap_prices, bins=50)[0].max()
|
| 59 |
|
| 60 |
# Calculate positions based on a fraction of the max frequency
|
|
|
|
| 110 |
|
| 111 |
fig.update_layout(title='Bootstrapping Simulation Cone', xaxis_title='Date', yaxis_title='Price', showlegend=True)
|
| 112 |
fig.update_xaxes(type='date')
|
| 113 |
+
|
| 114 |
return fig
|
| 115 |
|
| 116 |
# Streamlit app
|
| 117 |
st.set_page_config(layout="wide")
|
| 118 |
+
st.title('Future Stock/Crypto Price Bootstrap Simulation')
|
| 119 |
|
| 120 |
st.sidebar.header('Input Parameters')
|
| 121 |
|
| 122 |
+
# How to use (in sidebar, closed by default)
|
| 123 |
+
with st.sidebar.expander("How to Use", expanded=False):
|
| 124 |
+
st.write("""
|
| 125 |
+
1. Enter the stock ticker or crypto pair (e.g., 'AAPL' or 'BTC-USD') in the 'Ticker' field.
|
| 126 |
+
2. Set the start and end dates for historical data.
|
| 127 |
+
3. Adjust the number of days for simulation and number of iterations if desired.
|
| 128 |
+
4. Set price thresholds for probability calculations.
|
| 129 |
+
5. Click 'Run Simulation' to start the bootstrapping simulation.
|
| 130 |
+
6. Analyze the resulting charts and statistics.
|
| 131 |
+
""")
|
| 132 |
+
|
| 133 |
+
# Ticker and dates in an expander (open by default)
|
| 134 |
+
with st.sidebar.expander("Ticker and Dates", expanded=True):
|
| 135 |
+
ticker = st.text_input('Enter Ticker', 'ASML.AS', help="Enter a stock ticker (e.g., AAPL) or a crypto pair (e.g., BTC-USD)")
|
| 136 |
+
start_date = st.date_input('Start Date', pd.to_datetime('2020-01-01'), help="Select the start date for historical data")
|
| 137 |
+
end_date = st.date_input('End Date', pd.to_datetime('2025-01-01'), help="Select the end date for historical data")
|
| 138 |
+
|
| 139 |
+
# Other parameters
|
| 140 |
+
days = st.sidebar.number_input('Number of Days for Simulation', min_value=1, max_value=365, value=30, help="Number of days to simulate into the future")
|
| 141 |
+
n_iterations = st.sidebar.number_input('Number of Simulations', min_value=100, max_value=100000, value=10000, help="Number of bootstrap iterations to run")
|
| 142 |
+
threshold1 = st.sidebar.number_input('Threshold 1', min_value=0, value=850, help="Lower price threshold for probability calculations")
|
| 143 |
+
threshold2 = st.sidebar.number_input('Threshold 2', min_value=0, value=1050, help="Upper price threshold for probability calculations")
|
| 144 |
+
thresholds = [threshold1, threshold2]
|
| 145 |
+
|
| 146 |
st.write("""
|
| 147 |
### Description
|
| 148 |
+
This application simulates future stock or cryptocurrency prices using bootstrapping simulation methods.
|
| 149 |
+
You can specify the stock ticker or crypto pair, the date range, the number of simulation days, the number of simulations, and price thresholds.
|
| 150 |
+
The simulation results will show the probability of the price falling below, between, or above the specified thresholds.
|
| 151 |
|
| 152 |
**Background and Concept**
|
| 153 |
|
|
|
|
| 155 |
|
| 156 |
**Steps in Bootstrapping:**
|
| 157 |
|
| 158 |
+
Given a dataset \( X = \{x_1, x_2, ..., x_n\} \), we aim to estimate the statistic \( \theta \) (e.g., the mean return of a stock or cryptocurrency).
|
| 159 |
|
| 160 |
1. **Resampling**: Create a resample \( X^* \) by drawing \( n \) observations from \( X \) with replacement. This means that each data point can be selected multiple times in a single resample:
|
| 161 |
""")
|
|
|
|
| 171 |
st.write("""
|
| 172 |
4. **Estimate**: Use the bootstrap statistics to estimate the mean, standard error, and confidence intervals of \( \theta \).
|
| 173 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 174 |
**Results:**
|
| 175 |
The app will display two charts:
|
| 176 |
1. The distribution of the final simulated prices with key statistical measures.
|
| 177 |
+
2. The historical prices with simulated future price cones and the specified thresholds.
|
| 178 |
""")
|
| 179 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
if st.sidebar.button('Run Simulation'):
|
| 181 |
data = get_stock_data(ticker, start_date, end_date)
|
| 182 |
|
|
|
|
| 208 |
- **Bootstrap 95% CI:** The 95% confidence interval for the simulated future prices.
|
| 209 |
- **Threshold 1 and Threshold 2:** {threshold1:.2f}, {threshold2:.2f}
|
| 210 |
- **Probability Annotations:**
|
| 211 |
+
- The probability of the price being below Threshold 1: {bootstrap_probabilities["below"]:.2%}
|
| 212 |
+
- The probability of the price being between Threshold 1 and Threshold 2: {bootstrap_probabilities["between"]:.2%}
|
| 213 |
+
- The probability of the price being above Threshold 2: {bootstrap_probabilities["above"]:.2%}
|
| 214 |
|
| 215 |
+
These results help in understanding the potential future movements of the stock or cryptocurrency price based on historical data and bootstrapping simulation.
|
| 216 |
""")
|
| 217 |
|
| 218 |
hide_streamlit_style = """
|
|
|
|
| 221 |
footer {visibility: hidden;}
|
| 222 |
</style>
|
| 223 |
"""
|
| 224 |
+
st.markdown(hide_streamlit_style
|