Skip to content

Instantly share code, notes, and snippets.

@marcosan93
Created September 13, 2021 00:51
Show Gist options
  • Select an option

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

Select an option

Save marcosan93/c6b15a2a2619015dbe637d5bf1351878 to your computer and use it in GitHub Desktop.
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