Created
September 23, 2021 20:11
-
-
Save marcosan93/2d6ccdbaee4e5be9a228346b278d2aac to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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