Skip to content

Instantly share code, notes, and snippets.

@marcosan93
Created September 23, 2021 20:11
Show Gist options
  • Select an option

  • Save marcosan93/2d6ccdbaee4e5be9a228346b278d2aac to your computer and use it in GitHub Desktop.

Select an option

Save marcosan93/2d6ccdbaee4e5be9a228346b278d2aac to your computer and use it in GitHub Desktop.
def fbpBacktest(df, short=True):
"""
Performs the final backtest using log returns and the positions function.
Returns the performance.
"""
# Getting positions
df['positions'] = df.apply(lambda x: get_prophet_positions(x, short=short), axis=1)
# Compensating for lookahead bias
df['positions'] = df['positions'].shift(1)
# Getting log returns
df['log_returns'] = df['Open'].apply(np.log).diff()
# Dropping any Nans
df.dropna(inplace=True)
# Performing the backtest
returns = df['positions'] * df['log_returns']
# Inversing the log returns and getting daily portfolio balance
performance = returns.cumsum().apply(np.exp)
return performance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment