import os
import numpy as np
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
import seaborn as sns
from ipywidgets.widgets.widget_float import FloatProgress
from IPython.display import display
from IPython.core.display import display as dfplot
%matplotlib inline
def read_hst(hst_file_path):
if not os.path.exists(hst_file_path):
return None
with open(hst_file_path, 'rb') as f:
data = f.read()
ver = np.frombuffer(data[:4], dtype=np.int32)[0]
if ver==400:
dtype = np.dtype(zip(list('tolhcv'), 'i4 f8 f8 f8 f8 f8'.split()))
elif ver==401:
dtype = np.dtype(zip(list('tohlcvsr'), 'i8 f8 f8 f8 f8 i8 i4 i8'.split()))
df = DataFrame(np.frombuffer(data[148:], dtype=dtype))
df = df.set_index(pd.to_datetime(df['t'], unit='s')).drop('t', axis=1)
return df[list('ohlcv')]
def resample(df, tf='D'):
ml = 'first max min last sum last'.split()
kl = 'Open High Low Close Volume'.split()+['Adj Close']
f = lambda k: [k, k.upper(), k.lower(), k[:1], k[:1].lower()]
how = {key:m for k,m in zip(kl, ml) for key in f(k) if key in df.columns}
col_ord = [c for c in df.columns if c in how.keys()]
return df.resample(tf).agg(how).dropna()[col_ord]
# fxdd
df_dd = resample(read_hst('D:/USDJPY.hst'), 'H')
# gmo
df_gm = resample(read_hst('D:/gmo_USDJPY1.hst'), 'H')
# dukas
df_du = pd.read_csv('D:/dukas_usdjpy.csv', index_col=0, parse_dates=True)
df_du.columns = list('ohlcv')
df_du = resample(df_du, 'H')
df_du.index = df_du.index.shift(9, freq='H')
df_du.index.name = 't'
# ???
df_un = resample(read_hst('D:/USDJPY1.hst'), 'H')
dfplot(df_dd.head(3))
dfplot(df_gm.head(3))
dfplot(df_du.head(3))
dfplot(df_un.head(3))
o | h | l | c | v | |
---|---|---|---|---|---|
t | |||||
2005-01-10 02:00:00 | 104.79 | 104.8 | 104.64 | 104.65 | 183.0 |
2005-01-10 03:00:00 | 104.65 | 104.8 | 104.65 | 104.76 | 385.0 |
2005-01-10 04:00:00 | 104.75 | 104.8 | 104.62 | 104.64 | 272.0 |
o | h | l | c | v | |
---|---|---|---|---|---|
t | |||||
2007-01-02 07:00:00 | 119.01 | 119.03 | 118.91 | 118.96 | 240.0 |
2007-01-02 08:00:00 | 118.96 | 118.98 | 118.87 | 118.89 | 240.0 |
2007-01-02 09:00:00 | 118.89 | 118.92 | 118.54 | 118.58 | 240.0 |
o | h | l | c | v | |
---|---|---|---|---|---|
t | |||||
2003-05-05 06:00:00 | 118.940 | 119.016 | 118.893 | 118.976 | 12575.0 |
2003-05-05 07:00:00 | 118.993 | 119.023 | 118.953 | 118.995 | 12500.7 |
2003-05-05 08:00:00 | 118.991 | 119.046 | 118.973 | 119.033 | 11496.0 |
o | h | l | c | v | |
---|---|---|---|---|---|
t | |||||
1999-01-04 07:00:00 | 113.47 | 113.70 | 113.40 | 113.61 | 183.0 |
1999-01-04 08:00:00 | 113.65 | 113.85 | 113.40 | 113.75 | 516.0 |
1999-01-04 09:00:00 | 113.79 | 113.89 | 113.29 | 113.68 | 896.0 |
def plot_mean(df):
dfplot((df['h']-df['l']).groupby([df.index.year, df.index.hour]).mean().unstack(0).round(3)
.style.background_gradient('bwr', 0.5, 0.5, axis=0).set_properties(**{'font-size': '8pt'}))
plot_mean(df_gm)
plot_mean(df_du)
plot_mean(df_dd)
2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | |
---|---|---|---|---|---|---|---|---|---|---|
0 | 0.243 | 0.377 | 0.318 | 0.227 | 0.16 | 0.126 | 0.238 | 0.181 | 0.217 | 0.269 |
1 | 0.209 | 0.302 | 0.257 | 0.183 | 0.124 | 0.096 | 0.194 | 0.142 | 0.171 | 0.212 |
2 | 0.176 | 0.305 | 0.229 | 0.151 | 0.115 | 0.086 | 0.188 | 0.122 | 0.162 | 0.186 |
3 | 0.176 | 0.311 | 0.242 | 0.174 | 0.113 | 0.088 | 0.191 | 0.13 | 0.165 | 0.186 |
4 | 0.178 | 0.296 | 0.191 | 0.144 | 0.1 | 0.077 | 0.167 | 0.121 | 0.15 | 0.166 |
5 | 0.148 | 0.263 | 0.167 | 0.124 | 0.103 | 0.077 | 0.157 | 0.098 | 0.126 | 0.153 |
6 | 0.139 | 0.225 | 0.159 | 0.12 | 0.115 | 0.071 | 0.141 | 0.091 | 0.117 | 0.145 |
7 | 0.161 | 0.234 | 0.18 | 0.138 | 0.121 | 0.086 | 0.166 | 0.098 | 0.119 | 0.171 |
8 | 0.181 | 0.246 | 0.217 | 0.142 | 0.112 | 0.094 | 0.176 | 0.109 | 0.128 | 0.201 |
9 | 0.199 | 0.316 | 0.272 | 0.188 | 0.162 | 0.122 | 0.23 | 0.155 | 0.189 | 0.299 |
10 | 0.185 | 0.253 | 0.235 | 0.174 | 0.151 | 0.103 | 0.216 | 0.15 | 0.182 | 0.303 |
11 | 0.141 | 0.205 | 0.192 | 0.134 | 0.121 | 0.089 | 0.18 | 0.118 | 0.147 | 0.253 |
12 | 0.148 | 0.207 | 0.174 | 0.124 | 0.089 | 0.086 | 0.182 | 0.117 | 0.137 | 0.249 |
13 | 0.144 | 0.217 | 0.176 | 0.122 | 0.086 | 0.087 | 0.172 | 0.113 | 0.147 | 0.226 |
14 | 0.158 | 0.242 | 0.188 | 0.132 | 0.095 | 0.087 | 0.18 | 0.114 | 0.141 | 0.216 |
15 | 0.202 | 0.285 | 0.247 | 0.178 | 0.115 | 0.101 | 0.191 | 0.127 | 0.173 | 0.237 |
16 | 0.235 | 0.326 | 0.27 | 0.199 | 0.142 | 0.113 | 0.225 | 0.148 | 0.203 | 0.271 |
17 | 0.221 | 0.328 | 0.271 | 0.193 | 0.143 | 0.12 | 0.213 | 0.156 | 0.198 | 0.258 |
18 | 0.209 | 0.31 | 0.245 | 0.178 | 0.131 | 0.117 | 0.194 | 0.137 | 0.168 | 0.214 |
19 | 0.207 | 0.29 | 0.227 | 0.155 | 0.117 | 0.106 | 0.172 | 0.117 | 0.155 | 0.195 |
20 | 0.203 | 0.297 | 0.224 | 0.169 | 0.118 | 0.1 | 0.159 | 0.122 | 0.15 | 0.183 |
21 | 0.262 | 0.378 | 0.299 | 0.238 | 0.17 | 0.152 | 0.262 | 0.176 | 0.242 | 0.284 |
22 | 0.261 | 0.393 | 0.316 | 0.251 | 0.178 | 0.139 | 0.249 | 0.211 | 0.263 | 0.29 |
23 | 0.289 | 0.43 | 0.31 | 0.248 | 0.18 | 0.145 | 0.269 | 0.208 | 0.252 | 0.305 |
2003 | 2004 | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.303 | 0.291 | 0.265 | 0.266 | 0.248 | 0.378 | 0.319 | 0.227 | 0.16 | 0.127 | 0.237 | 0.18 | 0.217 | 0.27 |
1 | 0.254 | 0.237 | 0.232 | 0.203 | 0.219 | 0.303 | 0.258 | 0.183 | 0.125 | 0.099 | 0.194 | 0.141 | 0.17 | 0.213 |
2 | 0.224 | 0.208 | 0.188 | 0.172 | 0.191 | 0.308 | 0.237 | 0.152 | 0.116 | 0.091 | 0.188 | 0.121 | 0.161 | 0.186 |
3 | 0.202 | 0.225 | 0.184 | 0.171 | 0.191 | 0.314 | 0.245 | 0.176 | 0.114 | 0.092 | 0.191 | 0.129 | 0.164 | 0.189 |
4 | 0.186 | 0.192 | 0.172 | 0.141 | 0.194 | 0.301 | 0.193 | 0.145 | 0.103 | 0.08 | 0.166 | 0.12 | 0.149 | 0.17 |
5 | 0.165 | 0.165 | 0.146 | 0.127 | 0.17 | 0.267 | 0.169 | 0.125 | 0.106 | 0.08 | 0.157 | 0.098 | 0.124 | 0.155 |
6 | 0.177 | 0.178 | 0.162 | 0.155 | 0.162 | 0.234 | 0.165 | 0.132 | 0.125 | 0.081 | 0.15 | 0.091 | 0.113 | 0.143 |
7 | 0.161 | 0.179 | 0.168 | 0.159 | 0.175 | 0.24 | 0.183 | 0.144 | 0.128 | 0.093 | 0.169 | 0.097 | 0.117 | 0.171 |
8 | 0.174 | 0.22 | 0.188 | 0.182 | 0.192 | 0.247 | 0.22 | 0.145 | 0.117 | 0.1 | 0.176 | 0.108 | 0.128 | 0.203 |
9 | 0.207 | 0.257 | 0.225 | 0.208 | 0.21 | 0.317 | 0.274 | 0.189 | 0.163 | 0.123 | 0.23 | 0.155 | 0.188 | 0.298 |
10 | 0.214 | 0.244 | 0.218 | 0.194 | 0.197 | 0.254 | 0.236 | 0.174 | 0.152 | 0.106 | 0.215 | 0.15 | 0.181 | 0.308 |
11 | 0.188 | 0.196 | 0.168 | 0.144 | 0.159 | 0.206 | 0.194 | 0.135 | 0.124 | 0.092 | 0.18 | 0.118 | 0.146 | 0.256 |
12 | 0.163 | 0.171 | 0.158 | 0.133 | 0.169 | 0.209 | 0.175 | 0.125 | 0.095 | 0.091 | 0.181 | 0.117 | 0.136 | 0.258 |
13 | 0.16 | 0.195 | 0.153 | 0.141 | 0.161 | 0.217 | 0.177 | 0.124 | 0.092 | 0.091 | 0.172 | 0.113 | 0.146 | 0.229 |
14 | 0.175 | 0.194 | 0.159 | 0.15 | 0.176 | 0.243 | 0.189 | 0.134 | 0.099 | 0.091 | 0.18 | 0.114 | 0.141 | 0.22 |
15 | 0.219 | 0.238 | 0.203 | 0.191 | 0.217 | 0.286 | 0.248 | 0.18 | 0.117 | 0.103 | 0.191 | 0.126 | 0.173 | 0.24 |
16 | 0.254 | 0.267 | 0.214 | 0.228 | 0.246 | 0.326 | 0.272 | 0.199 | 0.142 | 0.115 | 0.225 | 0.147 | 0.202 | 0.275 |
17 | 0.237 | 0.257 | 0.237 | 0.215 | 0.233 | 0.328 | 0.274 | 0.193 | 0.144 | 0.121 | 0.212 | 0.154 | 0.197 | 0.265 |
18 | 0.241 | 0.258 | 0.216 | 0.206 | 0.224 | 0.31 | 0.247 | 0.179 | 0.132 | 0.119 | 0.194 | 0.137 | 0.167 | 0.22 |
19 | 0.228 | 0.241 | 0.202 | 0.191 | 0.223 | 0.291 | 0.229 | 0.155 | 0.119 | 0.108 | 0.172 | 0.116 | 0.154 | 0.196 |
20 | 0.239 | 0.244 | 0.207 | 0.178 | 0.221 | 0.296 | 0.225 | 0.169 | 0.119 | 0.101 | 0.159 | 0.121 | 0.149 | 0.185 |
21 | 0.266 | 0.324 | 0.247 | 0.266 | 0.273 | 0.376 | 0.299 | 0.239 | 0.17 | 0.154 | 0.261 | 0.175 | 0.24 | 0.282 |
22 | 0.289 | 0.301 | 0.268 | 0.292 | 0.268 | 0.394 | 0.317 | 0.251 | 0.179 | 0.141 | 0.248 | 0.209 | 0.26 | 0.294 |
23 | 0.293 | 0.31 | 0.266 | 0.275 | 0.292 | 0.429 | 0.313 | 0.249 | 0.18 | 0.147 | 0.269 | 0.207 | 0.251 | 0.292 |
2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.116 | 0.124 | 0.152 | 0.224 | 0.171 | 0.141 | 0.119 | 0.087 | 0.171 | 0.096 | 0.116 | 0.144 |
1 | 0.132 | 0.155 | 0.172 | 0.244 | 0.189 | 0.145 | 0.12 | 0.085 | 0.167 | 0.098 | 0.119 | 0.182 |
2 | 0.165 | 0.188 | 0.191 | 0.27 | 0.239 | 0.158 | 0.119 | 0.104 | 0.178 | 0.12 | 0.146 | 0.25 |
3 | 0.195 | 0.194 | 0.197 | 0.293 | 0.254 | 0.19 | 0.161 | 0.118 | 0.233 | 0.152 | 0.184 | 0.318 |
4 | 0.172 | 0.163 | 0.164 | 0.234 | 0.229 | 0.168 | 0.148 | 0.097 | 0.216 | 0.142 | 0.17 | 0.27 |
5 | 0.137 | 0.124 | 0.143 | 0.211 | 0.186 | 0.131 | 0.119 | 0.089 | 0.179 | 0.117 | 0.148 | 0.28 |
6 | 0.114 | 0.123 | 0.142 | 0.21 | 0.175 | 0.123 | 0.088 | 0.09 | 0.18 | 0.12 | 0.132 | 0.228 |
7 | 0.12 | 0.132 | 0.15 | 0.216 | 0.176 | 0.127 | 0.098 | 0.087 | 0.176 | 0.11 | 0.144 | 0.226 |
8 | 0.131 | 0.145 | 0.17 | 0.245 | 0.201 | 0.15 | 0.099 | 0.09 | 0.18 | 0.117 | 0.15 | 0.226 |
9 | 0.181 | 0.198 | 0.219 | 0.297 | 0.262 | 0.19 | 0.12 | 0.107 | 0.202 | 0.135 | 0.179 | 0.246 |
10 | 0.197 | 0.228 | 0.242 | 0.346 | 0.286 | 0.205 | 0.142 | 0.121 | 0.231 | 0.15 | 0.219 | 0.301 |
11 | 0.198 | 0.206 | 0.214 | 0.321 | 0.255 | 0.194 | 0.144 | 0.118 | 0.21 | 0.151 | 0.186 | 0.229 |
12 | 0.179 | 0.184 | 0.199 | 0.298 | 0.237 | 0.169 | 0.13 | 0.115 | 0.192 | 0.128 | 0.158 | 0.203 |
13 | 0.174 | 0.169 | 0.207 | 0.282 | 0.215 | 0.159 | 0.116 | 0.103 | 0.17 | 0.119 | 0.153 | 0.192 |
14 | 0.185 | 0.166 | 0.214 | 0.305 | 0.23 | 0.172 | 0.118 | 0.106 | 0.164 | 0.12 | 0.153 | 0.203 |
15 | 0.232 | 0.309 | 0.28 | 0.417 | 0.338 | 0.282 | 0.185 | 0.164 | 0.28 | 0.198 | 0.288 | 0.315 |
16 | 0.25 | 0.26 | 0.26 | 0.382 | 0.303 | 0.234 | 0.175 | 0.138 | 0.239 | 0.208 | 0.248 | 0.279 |
17 | 0.259 | 0.301 | 0.287 | 0.449 | 0.343 | 0.264 | 0.182 | 0.15 | 0.282 | 0.213 | 0.259 | 0.326 |
18 | 0.227 | 0.224 | 0.225 | 0.343 | 0.296 | 0.219 | 0.153 | 0.113 | 0.225 | 0.167 | 0.192 | 0.241 |
19 | 0.188 | 0.172 | 0.191 | 0.291 | 0.229 | 0.161 | 0.119 | 0.09 | 0.192 | 0.128 | 0.16 | 0.189 |
20 | 0.151 | 0.144 | 0.168 | 0.299 | 0.233 | 0.157 | 0.114 | 0.085 | 0.184 | 0.124 | 0.153 | 0.197 |
21 | 0.154 | 0.166 | 0.177 | 0.318 | 0.237 | 0.174 | 0.113 | 0.085 | 0.196 | 0.133 | 0.175 | 0.192 |
22 | 0.127 | 0.111 | 0.173 | 0.3 | 0.176 | 0.135 | 0.097 | 0.075 | 0.173 | 0.113 | 0.141 | 0.158 |
23 | 0.105 | 0.112 | 0.131 | 0.2 | 0.126 | 0.099 | 0.085 | 0.057 | 0.149 | 0.095 | 0.108 | 0.118 |
plot_mean(df_un)
1999 | 2000 | 2001 | 2002 | 2003 | 2004 | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 0.195 | 0.161 | 0.202 | 0.186 | 0.174 | 0.173 | 0.136 | 0.157 | 0.169 | 0.23 | 0.179 | 0.135 | 0.117 | 0.08 | 0.154 | 0.095 | 0.113 | 0.148 |
1 | 0.32 | 0.229 | 0.245 | 0.252 | 0.22 | 0.22 | 0.175 | 0.186 | 0.189 | 0.279 | 0.242 | 0.156 | 0.128 | 0.085 | 0.165 | 0.1 | 0.123 | 0.174 |
2 | 0.385 | 0.252 | 0.297 | 0.289 | 0.236 | 0.24 | 0.192 | 0.195 | 0.197 | 0.283 | 0.244 | 0.18 | 0.122 | 0.103 | 0.185 | 0.132 | 0.149 | 0.248 |
3 | 0.287 | 0.219 | 0.274 | 0.265 | 0.213 | 0.216 | 0.164 | 0.165 | 0.169 | 0.228 | 0.222 | 0.162 | 0.149 | 0.114 | 0.23 | 0.152 | 0.182 | 0.309 |
4 | 0.255 | 0.172 | 0.215 | 0.211 | 0.187 | 0.17 | 0.138 | 0.125 | 0.145 | 0.215 | 0.182 | 0.126 | 0.128 | 0.094 | 0.201 | 0.13 | 0.167 | 0.268 |
5 | 0.245 | 0.155 | 0.218 | 0.2 | 0.172 | 0.151 | 0.112 | 0.124 | 0.142 | 0.206 | 0.171 | 0.118 | 0.102 | 0.088 | 0.178 | 0.121 | 0.151 | 0.266 |
6 | 0.264 | 0.171 | 0.229 | 0.216 | 0.179 | 0.184 | 0.121 | 0.133 | 0.151 | 0.227 | 0.179 | 0.124 | 0.084 | 0.087 | 0.177 | 0.118 | 0.134 | 0.234 |
7 | 0.31 | 0.22 | 0.265 | 0.223 | 0.202 | 0.182 | 0.137 | 0.146 | 0.17 | 0.239 | 0.201 | 0.147 | 0.097 | 0.089 | 0.176 | 0.111 | 0.141 | 0.218 |
8 | 0.352 | 0.266 | 0.315 | 0.289 | 0.261 | 0.241 | 0.192 | 0.198 | 0.221 | 0.303 | 0.264 | 0.189 | 0.119 | 0.092 | 0.177 | 0.117 | 0.153 | 0.223 |
9 | 0.376 | 0.287 | 0.32 | 0.306 | 0.266 | 0.245 | 0.199 | 0.23 | 0.244 | 0.349 | 0.284 | 0.2 | 0.132 | 0.108 | 0.21 | 0.144 | 0.182 | 0.251 |
10 | 0.356 | 0.272 | 0.304 | 0.284 | 0.26 | 0.237 | 0.202 | 0.205 | 0.217 | 0.32 | 0.249 | 0.19 | 0.14 | 0.125 | 0.232 | 0.156 | 0.219 | 0.292 |
11 | 0.329 | 0.266 | 0.296 | 0.288 | 0.236 | 0.228 | 0.181 | 0.186 | 0.201 | 0.302 | 0.236 | 0.163 | 0.133 | 0.116 | 0.206 | 0.148 | 0.186 | 0.227 |
12 | 0.309 | 0.275 | 0.295 | 0.269 | 0.238 | 0.214 | 0.171 | 0.169 | 0.208 | 0.281 | 0.22 | 0.156 | 0.126 | 0.115 | 0.185 | 0.124 | 0.158 | 0.198 |
13 | 0.319 | 0.282 | 0.299 | 0.27 | 0.249 | 0.232 | 0.18 | 0.168 | 0.212 | 0.315 | 0.229 | 0.174 | 0.125 | 0.106 | 0.169 | 0.12 | 0.152 | 0.192 |
14 | 0.375 | 0.335 | 0.346 | 0.334 | 0.307 | 0.331 | 0.263 | 0.305 | 0.283 | 0.418 | 0.331 | 0.274 | 0.148 | 0.104 | 0.167 | 0.124 | 0.161 | 0.209 |
15 | 0.371 | 0.303 | 0.328 | 0.326 | 0.298 | 0.28 | 0.238 | 0.258 | 0.256 | 0.385 | 0.307 | 0.226 | 0.181 | 0.166 | 0.293 | 0.227 | 0.288 | 0.305 |
16 | 0.38 | 0.301 | 0.339 | 0.362 | 0.346 | 0.315 | 0.266 | 0.293 | 0.288 | 0.445 | 0.335 | 0.257 | 0.171 | 0.136 | 0.233 | 0.199 | 0.245 | 0.284 |
17 | 0.34 | 0.276 | 0.312 | 0.299 | 0.292 | 0.26 | 0.221 | 0.23 | 0.23 | 0.334 | 0.288 | 0.214 | 0.173 | 0.151 | 0.287 | 0.211 | 0.261 | 0.321 |
18 | 0.298 | 0.232 | 0.268 | 0.248 | 0.272 | 0.219 | 0.182 | 0.174 | 0.192 | 0.296 | 0.227 | 0.155 | 0.135 | 0.112 | 0.215 | 0.159 | 0.189 | 0.235 |
19 | 0.237 | 0.194 | 0.22 | 0.205 | 0.233 | 0.19 | 0.141 | 0.144 | 0.17 | 0.307 | 0.236 | 0.156 | 0.114 | 0.094 | 0.183 | 0.125 | 0.164 | 0.187 |
20 | 0.221 | 0.183 | 0.199 | 0.205 | 0.214 | 0.203 | 0.156 | 0.164 | 0.178 | 0.315 | 0.224 | 0.162 | 0.113 | 0.085 | 0.189 | 0.125 | 0.162 | 0.2 |
21 | 0.182 | 0.155 | 0.18 | 0.185 | 0.182 | 0.154 | 0.121 | 0.115 | 0.173 | 0.291 | 0.172 | 0.131 | 0.099 | 0.087 | 0.186 | 0.13 | 0.165 | 0.185 |
22 | 0.167 | 0.155 | 0.173 | 0.168 | 0.188 | 0.145 | 0.104 | 0.116 | 0.13 | 0.227 | 0.154 | 0.12 | 0.099 | 0.074 | 0.167 | 0.108 | 0.144 | 0.157 |
23 | 0.147 | 0.145 | 0.175 | 0.177 | 0.166 | 0.154 | 0.122 | 0.13 | 0.152 | 0.214 | 0.15 | 0.118 | 0.107 | 0.075 | 0.146 | 0.092 | 0.111 | 0.131 |
# あとこういうのはseaborn.heatmap()で表示するのもなかなかいい感じ
plt.figure(figsize=(15,8))
sns.heatmap(
(df_gm['c'].pct_change().abs()*100).groupby([df_gm.index.year, df_gm.index.hour]).mean().unstack(0),
cmap='Greens', annot=True, annot_kws={'size':12}, fmt='.3f'
);
# こんな感じだと普通の表みたい
from matplotlib.colors import ListedColormap
arr = np.random.randn(10,5)*100
plt.figure(figsize=(8,6))
sns.heatmap(
arr, # データ
cmap=ListedColormap(['k']), # 色 http://matplotlib.org/examples/color/colormaps_reference.html
annot=True, # 数値を表示する
annot_kws={'size':14, 'color':'gold', 'style':'italic'}, # 数値のパラメータ
fmt='.3f', # 数値のフォーマット
cbar=False, # カラーバーを表示するか
linewidths=1, # 境界線の太さ
linecolor='crimson', # 境界線の色
xticklabels=list('ABCDE'), # x軸のラベル
yticklabels=list('abcdefghij') # y軸のラベル
)
plt.title('test', size=24)
plt.xlabel('(:3)', rotation=270, size=24)
plt.ylabel('(^q^)'+' '*10, rotation=0, size=24);
def test(df, period1=10, period2=20, spread=1.0, point=0.01):
"""
簡単なテストを行います
dataframeと2本のMAのパラメータを受け取って
MAクロスのトレードのバックテストしてトレードの履歴の
dataframeを返す
"""
df = df.copy()
df.columns = [c[:1].lower() for c in df.columns]
df['ma1'] = df['c'].rolling(period1).mean()
df['ma2'] = df['c'].rolling(period2).mean()
df['ma1_shift'] = df['ma1'].shift()
df['ma2_shift'] = df['ma2'].shift()
df.dropna(inplace=True)
ma1 = df['ma1'].values
ma2 = df['ma2'].values
ma1s = df['ma1_shift'].values
ma2s = df['ma2_shift'].values
buyopen = (ma1>ma2) & (ma1s<ma2s)
sellopen = (ma1<ma2) & (ma1s>ma2s)
buyclose = sellopen
sellclose = buyopen
# シグナルがある足の次の足の始値でトレードしたことを想定したいので
# 使用する価格をシグナルと同じインデックスで使えるようにしておこうと思います
trade_price = np.append(df['o'][1:], df['c'][-1])
trade_time = np.append(df.index.values[1:], df.index.values[-1])
class ORDER_TYPE(object):
NONE = None
BUY = 'buy'
SELL = 'sell'
pos = ORDER_TYPE.NONE
openprice = None
opentime = None
records = [] # opentime type openprice closeprice closetime pips
for i in xrange(len(df)):
if pos:
if pos is ORDER_TYPE.BUY and buyclose[i]:
records.append([opentime, pos, openprice, trade_price[i],
trade_time[i], trade_price[i]-openprice])
pos = ORDER_TYPE.NONE
if pos is ORDER_TYPE.SELL and sellclose[i]:
records.append([opentime, pos, openprice, trade_price[i],
trade_time[i], openprice-trade_price[i]])
pos = ORDER_TYPE.NONE
if not pos:
if buyopen[i]:
pos, openprice, opentime = (ORDER_TYPE.BUY, trade_price[i], trade_time[i])
if sellopen[i]:
pos, openprice, opentime = (ORDER_TYPE.SELL, trade_price[i], trade_time[i])
if pos is not ORDER_TYPE.NONE:
pips = trade_price[-1]-openprice if pos==ORDER_TYPE.BUY else openprice-trade_price[-1]
records.append([opentime, pos, openprice, trade_price[-1], trade_time[-1], pips])
result = DataFrame(records, columns='opentime type openprice closeprice closetime pips'.split())
result.set_index('opentime', inplace=True)
result['pips'] /= point # adj pips
result['pips'] -= spread # spread cost
return result
def gen_int_param(start, end, n):
d = (float(end)/start)**(1./(n-1))
return [int(round(start*d**i)) for i in range(n)]
def hm(df, fmt='.2f', figsize=None):
if not figsize:
figsize = (15, 3)
plt.figure(figsize=figsize)
sns.heatmap(df[::-1], cmap='RdYlGn', annot=True, fmt=fmt, center=0)
import pandas_datareader.data as web
# test_df = web.get_data_yahoo('^N225', '1984-01-01', '2016-08-31')
test_df = web.get_data_yahoo('JPY=X', '2000-01-01')
print 'test_df.shape', test_df.shape
dfplot(test_df.head(3))
print 'back test !'
%time res = test(test_df, 50, 85, spread=1, point=0.01)
print 'res.shape', res.shape
dfplot(res.head(3))
res['pips'].cumsum().plot(figsize=(15,3), marker='o');
test_df.shape (4336, 6)
Open | High | Low | Close | Volume | Adj Close | |
---|---|---|---|---|---|---|
Date | ||||||
2000-01-03 | 102.07 | 103.33 | 101.31 | 101.69 | 0 | 101.69 |
2000-01-04 | 101.64 | 103.32 | 101.47 | 103.14 | 0 | 103.14 |
2000-01-05 | 103.13 | 104.48 | 102.75 | 104.09 | 0 | 104.09 |
back test ! Wall time: 15 ms res.shape (60, 5)
type | openprice | closeprice | closetime | pips | |
---|---|---|---|---|---|
opentime | |||||
2000-06-19 | buy | 106.32 | 108.67 | 2000-08-03 | 234.0 |
2000-08-03 | sell | 108.67 | 106.57 | 2000-08-29 | 209.0 |
2000-08-29 | buy | 106.57 | 107.72 | 2000-10-19 | 114.0 |
%%time
df = df_dd['20060101': '20151231'].copy()
# df = df_dd['20150101': '20151231'].copy()
# df = df_dd['20160101': '20160831'].copy()
# df = df_un.copy()
# df = fxdata.data_fxcm('USDJPY', 60, 0)
# df = df['20060101': '20151231'].copy()
# MAのパラメータのリスト
param_1 = gen_int_param(5, 200, 20)
param_2 = gen_int_param(10, 400, 20)
trade_count_df = DataFrame(np.zeros((len(param_1), len(param_2))), index=param_1, columns=param_2)
pips_df = trade_count_df.copy()
score_df = trade_count_df.copy()
expected_df = trade_count_df.copy()
print 'testcount: {}'.format(trade_count_df.size)
print 'ma_param_1: {}'.format(param_1)
print 'ma_param_2: {}'.format(param_2)
progressbar = True
if progressbar:
# プログレスバー
# 私はプログレスバーをぼーっと眺める病患者ですのでたのしい(´・ω・`)
progress = FloatProgress(min=0, max=trade_count_df.size)
display(progress)
for f in trade_count_df.index:
for s in trade_count_df.columns:
bt = test(df, f, s)
trade_count_df.loc[f, s] = len(bt)
pips_df.loc[f, s] = bt['pips'].sum()
expected_df.loc[f, s] = bt['pips'].mean()
score_df.loc[f, s] = bt['pips'].mean()/bt['pips'].std()
if progressbar:
progress.value +=1
hm(trade_count_df, '.0f'); plt.title('trades')
hm(pips_df, '.0f'); plt.title('sum pips')
hm(expected_df, '.1f'); plt.title('expected')
hm(score_df); plt.title('mean pips / std')
testcount: 400 ma_param_1: [5, 6, 7, 9, 11, 13, 16, 19, 24, 29, 35, 42, 51, 62, 76, 92, 112, 136, 165, 200] ma_param_2: [10, 12, 15, 18, 22, 26, 32, 39, 47, 57, 70, 85, 103, 125, 152, 184, 223, 271, 329, 400] Wall time: 20.3 s
df = df_dd['20060101': '20151231'].copy()
%time res = test(df, 13, 26)
print res['pips'].sum()
dfplot(res.head(3))
mn = resample(df, 'M')
fig = plt.figure(figsize=(15,3))
ax1 = fig.add_subplot(111)
ax1.plot(res['pips'].cumsum())
ax1.legend(['pips'], loc='upper left')
ax2 = ax1.twinx()
ax2.plot(mn['c'], color='r', marker='.', ms=4, lw=.2, ls='-')
ax2.grid(False)
ax2.legend(['USDJPY'], loc='upper right')
# 曜日別, 時間別
plt.figure(figsize=(15,2))
ax = plt.subplot(121)
res['pips'].groupby(res.index.weekday).sum().plot(kind='bar', ax=ax)
ax.set_xticklabels('Mon Tue Wed Thurs Fri Sat'.split(), rotation=0)
ax = plt.subplot(122)
res['pips'].groupby(res.index.hour).sum().plot(kind='bar', ax=ax)
ax.set_xticklabels(range(24), rotation=0)
# それぞれの年の時間別
hm(res['pips'].groupby([res.index.year, res.index.hour]).sum().unstack(0), '.0f', figsize=(12, 4));
Wall time: 47 ms 12096.0
type | openprice | closeprice | closetime | pips | |
---|---|---|---|---|---|
opentime | |||||
2006-01-04 21:00:00 | buy | 116.01 | 115.87 | 2006-01-05 11:00:00 | -15.0 |
2006-01-05 11:00:00 | sell | 115.87 | 116.15 | 2006-01-05 12:00:00 | -29.0 |
2006-01-05 12:00:00 | buy | 116.15 | 116.13 | 2006-01-05 16:00:00 | -3.0 |