[PYTHON] Try to model the cumulative return of rollovers in futures trading

Modeling futures trading

It is basically a continuation of last time and previous.

Today, the standard "[[] in Futures Trading Fri](http://www.comtex.co.jp/www/?module=Default&action=meigara&page=sheet1&article=T-kin) ”actually builds continuous returns from rollovers, helping to predict and model It is a story to use for.

Rollover is simply a shift from a contract with an imminent deadline for futures trading to a contract that is soon or long ahead. That is. In order to model the continuous transition of the transaction balance, various conditions of the market and the transaction must be taken into consideration, but here we will consider a simple model that transitions linearly.

Brand selection

Refer to Gold Futures Rollover Dates 2013 and select an appropriate stock. At the time of writing, it is 3/13, so let's aim at GCJ14 and GCM14, which will soon reach the contract expiration date (Last Trade).

Random walk

[Random Walk](http://ja.wikipedia.org/wiki/%E3%83%A9%E3%83%B3%E3%83%80%E3%83%A0%E3%83%BB%E3% Simulate futures contracts with 82% A6% E3% 82% A9% E3% 83% BC% E3% 82% AF% E7% 90% 86% E8% AB% 96).

def random_walk(px, n, f):
    np.random.seed(34567)
    N = 200
    _walk = (np.random.randint(0, 200, size=N) - 100) * 0.25
    perturb = (np.random.randint(0, 20, size=N) - 10) * 0.25
    walk = _walk.cumsum()
    rng = pd.date_range(px.index[0], periods=len(px) + N, freq='B')
    near = np.concatenate([px.values, px.values[-1] + walk])
    far = np.concatenate([px.values, px.values[-1] + walk + perturb])
    prices = pd.DataFrame({n: near, f: far}, index=rng)
    return prices

You now have a random walk function. Let's pass Yahoo! Finance data to this.

#SPY investment trust base price S&Used as an approximate price for the P 500 index
px = web.get_data_yahoo('SPY')['Adj Close'] * 10
#Specify the brand and contract expiration date
expiries = {
    'GCJ14': datetime(2014,4,28),
    'GCM14': datetime(2014,6,26)
}
expiry = pd.Series(expiries).order()

print( px.tail(5) )
# => 
# Date
# 2014-03-06    1881.8
# 2014-03-07    1882.6
# 2014-03-10    1881.6
# 2014-03-11    1872.3
# 2014-03-12    1872.8

print( expiry )
# =>
# GCJ14   2014-04-28
# GCM14   2014-06-26

prices = random_walk(px, 'GCJ14', 'GCM14')
print( prices.tail(5) )
# =>
#               GCJ14    GCM14
# 2014-10-17  1618.80  1621.05
# 2014-10-20  1634.80  1632.55
# 2014-10-21  1622.55  1623.80
# 2014-10-22  1638.55  1639.30
# 2014-10-23  1637.55  1638.30

Weighting

Next, build a model that calculates the weights. The code is the same as the O'Reilly book.

def get_roll_weights(start, expiry, items, roll_periods=5):
    dates = pd.date_range(start, expiry[-1], freq='B')
    weights = pd.DataFrame(np.zeros((len(dates), len(items))),
                        index=dates, columns=items)
    prev_date = weights.index[0]
    for i, (item, ex_date) in enumerate( expiry.iteritems() ):
        if i < len(expiry) - 1:
            weights.ix[prev_date:ex_date - pd.offsets.BDay(), item] = 1
            roll_rng = pd.date_range(end=ex_date - pd.offsets.BDay(),
                                     periods=roll_periods + 1, freq='B')
            decay_weights = np.linspace(0, 1, roll_periods + 1)
            weights.ix[roll_rng, item] = 1 - decay_weights
            weights.ix[roll_rng, expiry.index[i + 1]] = decay_weights
        else:
            weights.ix[prev_date:, item] = 1
        prev_date = ex_date
    return weights

Calculation of continuous returns

Finally, use these functions to find the continuous return index.

weights = get_roll_weights('3/11/2014', expiry, prices.columns)

sample_prices  = prices.ix['2014-04-17' : '2014-04-28']
sample_weights = weights.ix['2014-04-17' : '2014-04-28']
sample = sample_prices * sample_weights

#Build a new data frame
result = pd.DataFrame({'GCJ14': sample['GCJ14'], 'GCM14': sample['GCM14'], 'GOLD': sample['GCJ14'] + sample['GCM14'] })

#Get a return on rolled up futures contracts
print( result )
# =>
#               GCJ14    GCM14     GOLD
# 2014-04-17  1663.30     0.00  1663.30
# 2014-04-18  1687.55     0.00  1687.55
# 2014-04-21  1348.24   337.16  1685.40
# 2014-04-22  1017.78   678.42  1696.20
# 2014-04-23   676.62  1015.53  1692.15
# 2014-04-24   338.01  1351.84  1689.85
# 2014-04-25     0.00  1705.55  1705.55
# 2014-04-28     0.00  1723.05  1723.05

image.png

That's about it for analyzing stock portfolios, and from the next time onward, we will apply it to other fields.

reference

Introduction to data analysis with Python-Data processing using NumPy and pandas http://www.oreilly.co.jp/books/9784873116556/

Recommended Posts

Try to model the cumulative return of rollovers in futures trading
Try to evaluate the performance of machine learning / regression model
Try to evaluate the performance of machine learning / classification model
Cython to try in the shortest
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Try to simulate the movement of the solar system
Try to display the railway data of national land numerical information in 3D
Specify the lighting Model of SCN Material in Pythonista
Count the number of parameters in the deep learning model
Try to solve the problems / problems of "Matrix Programmer" (Chapter 1)
How to visualize the decision tree model of scikit-learn
[Django] Let's try to clarify the part of Django that was somehow through in the test
Try to estimate the number of likes on Twitter
Try to get the contents of Word with Golang
How to use the model learned in Lobe in Python
Try to decipher the login data stored in Firefox
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Try scraping the data of COVID-19 in Tokyo with Python
How to find the optimal number of clusters in k-means
Try to get the function list of Python> os package
[Memo] The mystery of cumulative assignment statements in Python functions
I made a function to check the model of DCGAN
Try to improve the accuracy of Twitter like number estimation
Try to solve the problems / problems of "Matrix Programmer" (Chapter 0 Functions)
Try to automate the operation of network devices with Python
Try to extract the keywords that are popular in COTOHA
Try to model a multimodal distribution using the EM algorithm
Try to extract the features of the sensor data with CNN
Summary of character string format in Python3 Whether to live with the old model or the new model
Try to calculate the position of the transmitter from the radio wave propagation model with python [Wi-Fi, Beacon]
How to return the data contained in django model in json format and map it on leaflet
How to handle multiple versions of CUDA in the same environment
I want to manually assign the training parameters of the [Pytorch] model
How to determine the existence of a selenium element in Python
[Note] Let's try to predict the amount of electricity used! (Part 1)
How to know the internal structure of an object in Python
Try to get a list of breaking news threads in Python.
[Python] PCA scratch in the example of "Introduction to multivariate analysis"
How to change the color of just the button pressed in Tkinter
[Python] Try to graph from the image of Ring Fit [OCR]
Try transcribing the probability mass function of the binomial distribution in Python
How to check the memory size of a variable in Python
First python ② Try to write code while examining the features of python
Try to solve the N Queens problem with SA of PyQUBO
Output the contents of ~ .xlsx in the folder to HTML with Python
Feel free to change the label of the legend in Seaborn in python
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
Try to edit a new image using the trained StyleGAN2 model
Use PyCaret to predict the price of pre-owned apartments in Tokyo!
I wrote the code to write the code of Brainf * ck in python
How to check the memory size of a dictionary in Python
How to get the vertex coordinates of a feature in ArcPy
[Introduction to SIR model] Consider the fitting result of Diamond Princess ♬
Create a function to get the contents of the database in Go
I want to know the population of each country in the world.
Try to predict the triplet of boat race by ranking learning
[Completed version] Try to find out the number of residents in the town from the address list with Python
Programming to fight in the world ~ 5-5,5-6
Programming to fight in the world 5-3
The story of participating in AtCoder
Programming to fight in the world-Chapter 4