Комментарии пользователя mscinsider
# Calculate daily returns
df['Daily Returns'] = df['price'].pct_change()
# Shift the daily returns by one to apply them to the next day
df['Shifted Daily Returns'] = df['Daily Returns'].shift(1)
# Strategy returns based on the signal and the shifted daily returns
# When Signal is 1 (buy)
# When Signal is -1 (sell)
# Neutral days have no impact (returns are 0).
df['Strategy Returns'] = np.where(df['Signal'] == 1, df['Shifted Daily Returns'],
np.where(df['Signal'] == -1, -df['Shifted Daily Returns'], 0))
# Calculate cumulative returns by compounding the strategy returns
df['Cumulative Returns'] = (1 + df['Strategy Returns']).cumprod() — 1
glazaolega, нет, это просто пример ) ,- здесь более 6 разных бэктестов, читайте — smart-lab.ru/blog/1003409.php
Могу сделать на любой инструмент вас интересующий, показать код который я использовал и рассказать какие данные.
# Calculate daily returns
df['Daily Returns'] = df['price'].pct_change()
# Shift the daily returns by one to apply them to the next day
df['Shifted Daily Returns'] = df['Daily Returns'].shift(1)
# Strategy returns based on the signal and the shifted daily returns
# When Signal is 1 (buy)
# When Signal is -1 (sell)
# Neutral days have no impact (returns are 0).
df['Strategy Returns'] = np.where(df['Signal'] == 1, df['Shifted Daily Returns'],
np.where(df['Signal'] == -1, -df['Shifted Daily Returns'], 0))
# Calculate cumulative returns by compounding the strategy returns
df['Cumulative Returns'] = (1 + df['Strategy Returns']).cumprod() — 1