Streams as instruments -- FIRST SEEN dummies

In [49]:
from simple_back_test import *
from plotnine import *
In [26]:
pe = general_stats_first_seen()
In [27]:
len(pe)
Out[27]:
366
In [28]:
pe.describe()
Out[28]:
index predicted_streams actual_streams open_price close_price ret
count 366.000000 366.000000 366.000000 366.000000 3.660000e+02 366.000000
mean 1673.292350 14.576078 14.729638 17082.010766 7.485393e+04 3.201099
std 1512.050872 1.068588 1.625493 27420.843146 2.957263e+05 25.998816
min 2.000000 13.137789 11.313132 2031.090264 4.094500e+02 -0.916837
25% 414.500000 13.688634 13.503302 3523.368340 3.659226e+03 -0.260957
50% 894.500000 14.390992 14.509169 7112.025573 1.000547e+04 0.448554
75% 3198.750000 15.202668 15.600113 16013.820541 2.979125e+04 1.498147
max 4810.000000 17.604891 20.423193 176915.945206 3.703820e+06 436.692092
In [29]:
pe.open_price.sum()
Out[29]:
6252015.9403058095
In [30]:
pe.close_price.sum()
Out[30]:
27396537.459999997
In [37]:
pd.qcut(pe['open_price'],3)
Out[37]:
2          (2031.089, 4409.006]
435        (2031.089, 4409.006]
422        (2031.089, 4409.006]
571        (2031.089, 4409.006]
798        (2031.089, 4409.006]
526        (2031.089, 4409.006]
193        (2031.089, 4409.006]
36         (2031.089, 4409.006]
38         (2031.089, 4409.006]
194        (2031.089, 4409.006]
185        (2031.089, 4409.006]
539        (2031.089, 4409.006]
802        (2031.089, 4409.006]
1242       (2031.089, 4409.006]
1348       (2031.089, 4409.006]
1345       (2031.089, 4409.006]
1347       (2031.089, 4409.006]
1423       (2031.089, 4409.006]
174        (2031.089, 4409.006]
300        (2031.089, 4409.006]
246        (2031.089, 4409.006]
261        (2031.089, 4409.006]
25         (2031.089, 4409.006]
351        (2031.089, 4409.006]
413        (2031.089, 4409.006]
1138       (2031.089, 4409.006]
52         (2031.089, 4409.006]
706        (2031.089, 4409.006]
230        (2031.089, 4409.006]
239        (2031.089, 4409.006]
                 ...           
114     (11699.525, 176915.945]
1118    (11699.525, 176915.945]
277     (11699.525, 176915.945]
404     (11699.525, 176915.945]
831     (11699.525, 176915.945]
1126    (11699.525, 176915.945]
1120    (11699.525, 176915.945]
352     (11699.525, 176915.945]
279     (11699.525, 176915.945]
145     (11699.525, 176915.945]
54      (11699.525, 176915.945]
63      (11699.525, 176915.945]
212     (11699.525, 176915.945]
168     (11699.525, 176915.945]
564     (11699.525, 176915.945]
405     (11699.525, 176915.945]
172     (11699.525, 176915.945]
175     (11699.525, 176915.945]
472     (11699.525, 176915.945]
61      (11699.525, 176915.945]
213     (11699.525, 176915.945]
166     (11699.525, 176915.945]
521     (11699.525, 176915.945]
163     (11699.525, 176915.945]
169     (11699.525, 176915.945]
195     (11699.525, 176915.945]
170     (11699.525, 176915.945]
198     (11699.525, 176915.945]
215     (11699.525, 176915.945]
214     (11699.525, 176915.945]
Name: open_price, Length: 366, dtype: category
Categories (3, interval[float64]): [(2031.089, 4409.006] < (4409.006, 11699.525] < (11699.525, 176915.945]]
In [48]:
pe['open_price_bin'] = pd.qcut(pe['open_price'],3, labels=['<4,409', '<11,700', '<110,000'])

return_stats = pe.groupby('open_price_bin')['ret'].agg([min, max, np.mean, np.median, np.std, "count"])

capital = pe.groupby('open_price_bin')['open_price'].agg([sum])

return_stats.join(capital)
Out[48]:
min max mean median std count sum
open_price_bin
<4,409 -0.877874 9.395892 0.882663 0.250870 1.994756 122 3.683373e+05
<11,700 -0.820495 436.692092 5.113487 0.280281 40.364775 122 9.000389e+05
<110,000 -0.916837 218.506047 3.607147 0.798074 19.910312 122 4.983640e+06
In [46]:
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[46]:
<ggplot: (-9223363265192046925)>
In [47]:
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[47]:
<ggplot: (8771662978140)>

With slippage

In [55]:
pe = general_stats_first_seen(slippage=0.8)
pe['open_price_bin'] = pd.qcut(pe['open_price'],3)

return_stats = pe.groupby('open_price_bin')['ret'].agg([min, max, np.mean, np.median, np.std, "count"])

capital = pe.groupby('open_price_bin')['open_price'].agg([sum])

return_stats.join(capital)
Out[55]:
min max mean median std count sum
open_price_bin
(2022.53, 4511.512] -0.877874 9.395892 0.790443 0.226009 1.821447 108 3.309290e+05
(4511.512, 11507.086] -0.916837 436.692092 5.772132 0.368967 42.878417 108 7.562700e+05
(11507.086, 141532.756] -0.843068 218.506047 3.980469 0.809486 21.139043 108 3.839108e+06
In [57]:
pe = general_stats_first_seen(slippage=0.5)
pe['open_price_bin'] = pd.qcut(pe['open_price'],3)

return_stats = pe.groupby('open_price_bin')['ret'].agg([min, max, np.mean, np.median, np.std, "count"])

capital = pe.groupby('open_price_bin')['open_price'].agg([sum])

return_stats.join(capital)
Out[57]:
min max mean median std count sum
open_price_bin
(2001.752, 3876.501] -0.808948 436.692092 6.749661 0.090700 48.045299 86 2.450293e+05
(3876.501, 8648.544] -0.916837 218.506047 3.446082 0.357560 23.677308 85 4.953874e+05
(8648.544, 88457.973] -0.843068 21.637598 2.315469 0.968619 3.861602 86 2.228496e+06