#๐ debuging my trading bot (python/streamlit)
5 messages ยท Page 1 of 1 (latest)
@tender estuary
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
Closes after a period of inactivity, or when you send !close.
# Forecasting Module
df_model = df.dropna().copy()
df_model["days_since_start"] = (df_model.index - df_model.index.min()).days
daily_avg_close = df_model[["days_since_start", "Close"]].rename(columns={"Close": "close"})
X = daily_avg_close["days_since_start"].values.reshape(-1, 1)
y = daily_avg_close["close"].values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=10)
poly = PolynomialFeatures(degree=2)
X_poly_train = poly.fit_transform(X_train)
model = LinearRegression()
model.fit(X_poly_train, y_train)
# Calculating future dates in terms of days since the start
last_day = df_model["days_since_start"].max()
future_days_count = 365 # Predict one year into the future
future_days_since_start = np.arange(last_day + 1, last_day + 1 + future_days_count).reshape(-1, 1)
# Predicting future prices
X_future_poly = poly.transform(future_days_since_start)
y_future_pred = model.predict(X_future_poly)
# Creating the corresponding date range for the future predictions
start_date_model = df_model.index.min()
future_dates = [start_date_model + datetime.timedelta(days=int(day)) for day in future_days_since_start]
# Plotting the forecast
fig_forecast = go.Figure()
# fig_forecast.add_trace(go.Scatter(x=df_model["days_since_start"], y=df_model["Close"],
# name="Historical Close Price", line=dict(color="green")))
fig_forecast.add_trace(go.Scatter(x=future_days_since_start.flatten(), y=y_future_pred,
name="Predicted Close Price", line=dict(color="blue", dash="dash"),
mode='lines')) # Explicitly set mode to 'lines'
fig_forecast.update_layout(title="Historical and Predicted Stock Prices",
xaxis_title="Days Since Start",
yaxis_title="Closing Price",
width=1100, height=600, hovermode="x unified")
st.subheader(":package: Forecast & Backtesting View")
st.plotly_chart(fig_forecast, use_container_width=False)
@tender estuary
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.