Created
September 13, 2021 00:51
-
-
Save marcosan93/c6b15a2a2619015dbe637d5bf1351878 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 vectBacktest(df, thres=.01, short=True): | |
| """ | |
| Accepts a dataframe of sentiment and log returns. Returns a series of the | |
| portfolio performance from the backtest. | |
| """ | |
| # Getting the initial positions | |
| df['positions'] = df['sentiment'].apply(lambda x: getPositions(x, | |
| thres=thres, | |
| short=short)) | |
| # Performing the backtest | |
| returns = df['positions'] * df['log returns'] | |
| # Inversing the log returns and getting daily portfolio balance | |
| performance = returns.cumsum().apply(np.exp) | |
| # Returning the daily returns and the portfolio performance | |
| return performance |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment