forked from robcarver17/systematictradingexamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarry.py
68 lines (44 loc) · 1.59 KB
/
carry.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
This file shows how to calculate the Carry trading rule for crude oil futures
As in chapter 7 / appendix B of "Systematic Trading" by Robert Carver (www.systematictrading.org)
Required: pandas, matplotlib
USE AT YOUR OWN RISK! No warranty is provided or implied.
Handling of NAN's and Inf's isn't done here (except within pandas),
And there is no error handling!
"""
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from common import cap_series, pd_readcsv, find_datediff, get_price_for_instrument, get_carry_data, ROOT_DAYS_IN_YEAR
## This is the stitched price series
## We can't use the price of the contract we're trading, or the volatility will be jumpy
code="US10"
price=get_price_for_instrument(code)
"""
Formulation here will work whether we are trading the nearest contract or not
For other asset classes you will have to work out nerpu (net expected return in price units) yourself
"""
data=get_carry_data(code)
data[['TRADED','NEARER']].plot()
plt.show()
#d1=pd.datetime(2007,1,1)
#d2=pd.datetime(2009,12,31)
nerpu=data.apply(find_datediff, axis=1)
nerpu.plot()
plt.title("Nerpu")
plt.show()
## Shouldn't need changing
vol_lookback=25
stdev_returns=pd.ewmstd(price - price.shift(1), span=vol_lookback)
ann_stdev=stdev_returns*ROOT_DAYS_IN_YEAR
raw_carry=(nerpu/ann_stdev.transpose()).transpose()
f_scalar=30.0
raw_carry.plot()
plt.title("Raw carry")
plt.show()
forecast=raw_carry*f_scalar
c_forecast=cap_series(forecast).to_frame()
data_to_plot=pd.concat([forecast,c_forecast], axis=1)
data_to_plot.columns=['Forecast','Capped forecast']
data_to_plot.plot()
plt.show()