Last active
September 12, 2024 10:45
-
-
Save addiversitas/591f382d7b2cef976f98f12bcc46c5d5 to your computer and use it in GitHub Desktop.
Perpetual Rollover for Futures contracts
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
import numpy as np | |
import pandas as pd | |
#load data from your preferred source | |
data = pd.read_csv("PATH/TO/FILE/futures_rolling.csv", index_col = [0], parse_dates=True) | |
june_21 = data["VX-2021M"] | |
july_21 = data["VX-2021N"] | |
def perpetual_rollover(previous_futures, next_futures, roll_date, number_of_rolls): | |
all_dates = next_futures.index #this assumes that both futures ts have the same indices, if not, get the unique dates here | |
index_roll_date = previous_futures.index.get_loc(roll_date) + 1 #adding 1 for easier indexing | |
roll_dates = previous_futures.index[(index_roll_date-number_of_rolls-1):index_roll_date] | |
roll_scalar = np.linspace(0, 1, number_of_rolls + 1) | |
roll_weights = pd.DataFrame(np.zeros((len(all_dates), 2)), index=all_dates) | |
for i in all_dates: | |
if i in roll_dates: | |
current_scalar = roll_scalar[np.where(roll_dates == i)][0] | |
elif i < roll_dates[0]: | |
current_scalar = 0 | |
else: | |
current_scalar = 1 | |
roll_weights.loc[i][0] = 1-current_scalar | |
roll_weights.loc[i][1] = current_scalar | |
adjusted_price = (roll_weights.iloc[:,0] * previous_futures).fillna(0) + roll_weights.iloc[:,1] * next_futures | |
return adjusted_price | |
adjusted_price = perpetual_rollover(june_21, july_21, "2021-06-16", 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment