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.
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](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
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
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
That's about it for analyzing stock portfolios, and from the next time onward, we will apply it to other fields.
Introduction to data analysis with Python-Data processing using NumPy and pandas http://www.oreilly.co.jp/books/9784873116556/
Recommended Posts