Skip to content

Instantly share code, notes, and snippets.

@marcosan93
Created January 14, 2022 16:39
Show Gist options
  • Select an option

  • Save marcosan93/52042820a81f978e5a913cbf9aefa855 to your computer and use it in GitHub Desktop.

Select an option

Save marcosan93/52042820a81f978e5a913cbf9aefa855 to your computer and use it in GitHub Desktop.
def runningFBP(prices, forecast_hours, training_hours):
"""
Runs Facebook Prophet to get predictions over a set period
of time. Uses FBP to train and predict every hour and gets the
price forecasts in the next N hours.
"""
# DF for the predicted values
pred_df = pd.DataFrame()
# Running the model each day
for i in tqdm(range(training_hours, len(prices))):
# Training and Predicting the last hour on the forecast
forecast = fbpTrainPredict(
prices[i-training_hours:i],
forecast_hours
).tail(1)[[
'ds',
'yhat',
'yhat_lower',
'yhat_upper'
]]
# Adding the forecast predicted
pred_df = pred_df.append(forecast, ignore_index=True)
# Prepping for merge by converting date values to be the same type
pred_df['ds'] = pred_df['ds'].apply(str)
prices['ds'] = prices['ds'].apply(str)
# Shifting the forecasts back in order to compare it to the 'current' open values
pred_df[['yhat', 'yhat_lower', 'yhat_upper']] = pred_df[['yhat', 'yhat_lower', 'yhat_upper']].shift(-forecast_hours)
# Merging with the prices DF in order to compare values for positions later
merge_df = prices[['ds', 'open']].merge(
pred_df,
on='ds',
how='outer'
).dropna().set_index('ds')
return merge_df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment