Streams as instruments

In [3]:
from simple_back_test import *
from plotnine import *
In [4]:
pe = general_stats_base()
In [5]:
pe.head()
Out[5]:
spyid streams pop_5 streams_100_date pop_5_date predicted_streams open_price close_price open_date close_date duration ret
11 6BuARH7nrDZXH9MytcSQiq 918335 51 2018-06-06 2018-03-04 514120.57638 2056.482306 4591.675 2018-03-04 2018-06-06 94.0 1.232781
167 51GEVPGtTFN5rpihE5OwoC 2514000 51 2018-05-21 2018-02-14 514120.57638 2056.482306 12570.000 2018-02-14 2018-05-21 96.0 5.112379
199 6LxeQpoUx2WnI4euArRgk0 1881000 51 2018-05-21 2018-02-14 514120.57638 2056.482306 9405.000 2018-02-14 2018-05-21 96.0 3.573344
1385 2axmxXWKBu2zEa1d8hrQVD 153023 51 2018-05-23 2018-05-23 514120.57638 2056.482306 765.115 2018-05-23 2018-05-23 0.0 -0.627950
248 6pu3UrqCDnRPzWXNLGqVDk 384809 51 2018-05-21 2018-02-14 514120.57638 2056.482306 1924.045 2018-02-14 2018-05-21 96.0 -0.064400
In [6]:
pe.describe()
Out[6]:
streams pop_5 predicted_streams open_price close_price duration ret
count 3.420000e+02 342.000000 3.420000e+02 342.000000 3.420000e+02 342.000000 342.000000
mean 1.425047e+07 60.263158 2.570947e+06 10283.789419 7.125237e+04 46.511696 7.366937
std 5.471450e+07 7.770973 3.659211e+06 14636.843183 2.735725e+05 51.629218 39.889201
min 3.469000e+03 51.000000 5.141206e+05 2056.482306 1.734500e+01 -3.000000 -0.996693
25% 5.080018e+05 54.000000 7.304034e+05 2921.613655 2.540009e+03 -1.000000 -0.574105
50% 1.917000e+06 58.500000 1.238947e+06 4955.788871 9.585000e+03 0.000000 0.602282
75% 6.441000e+06 63.000000 2.094384e+06 8377.534239 3.220500e+04 96.000000 4.047267
max 7.407640e+08 85.000000 2.750258e+07 110010.331810 3.703820e+06 197.000000 627.103870
In [7]:
pe.open_price.sum()
Out[7]:
3517055.9814293105
In [8]:
pe.close_price.sum()
Out[8]:
24368309.060000002
In [14]:
pe['open_price_bin'] = pd.qcut(pe['open_price'],3, labels=['<3700', '<6600', '<110k'])
In [31]:
return_stats = pe.groupby('open_price_bin')['ret'].agg([min, max, np.mean, np.median, np.std, "count"])
In [33]:
capital = pe.groupby('open_price_bin')['open_price'].agg([sum])
In [34]:
return_stats.join(capital)
Out[34]:
min max mean median std count sum
open_price_bin
<3700 -0.866732 210.786338 5.954202 0.864885 24.092440 132 3.669222e+05
<6600 -0.996693 627.103870 12.507707 0.831038 66.933627 102 5.466312e+05
<110k -0.698859 59.064542 4.238443 -0.181741 9.684720 108 2.603503e+06
In [38]:
pe['log_ret_p1'] = np.log(pe['ret'] + 1)
ggplot(pe, aes('open_price_bin', 'log_ret_p1')) + geom_violin()
/home/paperspace/anaconda3/envs/whitelist/lib/python3.6/site-packages/plotnine/utils.py:281: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  ndistinct = ids.apply(len_unique, axis=0).as_matrix()
/home/paperspace/anaconda3/envs/whitelist/lib/python3.6/site-packages/pandas/core/generic.py:4388: FutureWarning: Attribute 'is_copy' is deprecated and will be removed in a future version.
  object.__getattribute__(self, name)
/home/paperspace/anaconda3/envs/whitelist/lib/python3.6/site-packages/pandas/core/generic.py:4389: FutureWarning: Attribute 'is_copy' is deprecated and will be removed in a future version.
  return object.__setattr__(self, name, value)
/home/paperspace/anaconda3/envs/whitelist/lib/python3.6/site-packages/plotnine/positions/position.py:188: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  intervals = data[xminmax].drop_duplicates().as_matrix().flatten()
Out[38]:
<ggplot: (8734780861935)>
In [39]:
ggplot(pe, aes('open_price_bin', 'log_ret_p1')) + geom_boxplot()
/home/paperspace/anaconda3/envs/whitelist/lib/python3.6/site-packages/plotnine/utils.py:281: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  ndistinct = ids.apply(len_unique, axis=0).as_matrix()
/home/paperspace/anaconda3/envs/whitelist/lib/python3.6/site-packages/pandas/core/generic.py:4388: FutureWarning: Attribute 'is_copy' is deprecated and will be removed in a future version.
  object.__getattribute__(self, name)
/home/paperspace/anaconda3/envs/whitelist/lib/python3.6/site-packages/pandas/core/generic.py:4389: FutureWarning: Attribute 'is_copy' is deprecated and will be removed in a future version.
  return object.__setattr__(self, name, value)
/home/paperspace/anaconda3/envs/whitelist/lib/python3.6/site-packages/plotnine/positions/position.py:188: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
  intervals = data[xminmax].drop_duplicates().as_matrix().flatten()
Out[39]:
<ggplot: (8734780835336)>