Pythonで読み込んだFXデータのタイムフレームを変えたりする

とりあえず、EUR/USDの1分足データを読み込むところは、前回の記事と一緒です。

In [1]:
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

dataM1 = pd.read_csv('C:\Python\DAT_ASCII_EURUSD_M1_2015.csv', sep=';',
                     names=('Time','Open','High','Low','Close', ''),
                     index_col='Time', parse_dates=True)
dataM1
Out[1]:
Open High Low Close
Time
2015-01-01 13:00:00 1.20965 1.20977 1.20962 1.20962 0
2015-01-01 13:01:00 1.20963 1.20968 1.20962 1.20962 0
2015-01-01 13:02:00 1.20965 1.20970 1.20961 1.20961 0
2015-01-01 13:04:00 1.20959 1.21008 1.20959 1.20983 0
2015-01-01 13:05:00 1.20988 1.20988 1.20988 1.20988 0
2015-01-01 13:06:00 1.20984 1.20984 1.20982 1.20982 0
2015-01-01 13:07:00 1.20986 1.20999 1.20986 1.20987 0
2015-01-01 13:08:00 1.20998 1.21022 1.20987 1.21008 0
2015-01-01 13:09:00 1.20996 1.20996 1.20996 1.20996 0
2015-01-01 13:10:00 1.21013 1.21019 1.20967 1.20977 0
2015-01-01 13:11:00 1.20976 1.20999 1.20976 1.20988 0
2015-01-01 13:12:00 1.20987 1.20987 1.20987 1.20987 0
2015-01-01 13:13:00 1.20997 1.21006 1.20993 1.21006 0
2015-01-01 13:14:00 1.21007 1.21007 1.21006 1.21006 0
2015-01-01 13:16:00 1.21004 1.21004 1.21004 1.21004 0
2015-01-01 13:18:00 1.21003 1.21004 1.20979 1.20979 0
2015-01-01 13:27:00 1.21003 1.21003 1.20979 1.21003 0
2015-01-01 13:28:00 1.20979 1.21003 1.20979 1.21003 0
2015-01-01 13:29:00 1.21001 1.21003 1.20999 1.21003 0
2015-01-01 13:31:00 1.21033 1.21041 1.21033 1.21033 0
2015-01-01 13:32:00 1.21028 1.21035 1.21006 1.21035 0
2015-01-01 13:35:00 1.21005 1.21006 1.21005 1.21005 0
2015-01-01 13:36:00 1.21004 1.21006 1.21004 1.21006 0
2015-01-01 13:37:00 1.20991 1.21006 1.20991 1.21004 0
2015-01-01 13:38:00 1.21003 1.21004 1.20982 1.21004 0
2015-01-01 13:39:00 1.21019 1.21034 1.21019 1.21034 0
2015-01-01 13:40:00 1.21030 1.21034 1.21030 1.21032 0
2015-01-01 13:41:00 1.21006 1.21007 1.21006 1.21007 0
2015-01-01 13:43:00 1.21003 1.21006 1.21003 1.21006 0
2015-01-01 13:44:00 1.21003 1.21007 1.21003 1.21007 0
... ... ... ... ... ...
2015-12-31 16:29:00 1.08644 1.08647 1.08644 1.08646 0
2015-12-31 16:30:00 1.08646 1.08651 1.08645 1.08651 0
2015-12-31 16:31:00 1.08650 1.08667 1.08650 1.08666 0
2015-12-31 16:32:00 1.08666 1.08680 1.08666 1.08676 0
2015-12-31 16:33:00 1.08676 1.08676 1.08675 1.08676 0
2015-12-31 16:34:00 1.08675 1.08676 1.08675 1.08676 0
2015-12-31 16:35:00 1.08674 1.08674 1.08673 1.08674 0
2015-12-31 16:36:00 1.08675 1.08676 1.08675 1.08676 0
2015-12-31 16:37:00 1.08681 1.08689 1.08681 1.08687 0
2015-12-31 16:38:00 1.08688 1.08688 1.08685 1.08687 0
2015-12-31 16:39:00 1.08688 1.08694 1.08687 1.08691 0
2015-12-31 16:40:00 1.08690 1.08690 1.08689 1.08690 0
2015-12-31 16:41:00 1.08689 1.08692 1.08686 1.08692 0
2015-12-31 16:42:00 1.08689 1.08691 1.08687 1.08691 0
2015-12-31 16:43:00 1.08691 1.08697 1.08683 1.08696 0
2015-12-31 16:44:00 1.08697 1.08697 1.08693 1.08696 0
2015-12-31 16:45:00 1.08684 1.08693 1.08629 1.08651 0
2015-12-31 16:46:00 1.08652 1.08653 1.08617 1.08640 0
2015-12-31 16:47:00 1.08635 1.08642 1.08609 1.08620 0
2015-12-31 16:48:00 1.08621 1.08621 1.08571 1.08578 0
2015-12-31 16:49:00 1.08562 1.08571 1.08539 1.08559 0
2015-12-31 16:50:00 1.08558 1.08568 1.08543 1.08554 0
2015-12-31 16:51:00 1.08554 1.08570 1.08537 1.08538 0
2015-12-31 16:52:00 1.08534 1.08558 1.08534 1.08558 0
2015-12-31 16:53:00 1.08564 1.08565 1.08527 1.08538 0
2015-12-31 16:54:00 1.08538 1.08550 1.08533 1.08541 0
2015-12-31 16:55:00 1.08542 1.08542 1.08530 1.08531 0
2015-12-31 16:56:00 1.08531 1.08550 1.08531 1.08533 0
2015-12-31 16:57:00 1.08533 1.08547 1.08528 1.08538 0
2015-12-31 16:58:00 1.08540 1.08549 1.08536 1.08545 0

372210 rows × 5 columns

今回は、このデータを元に、色々なタイムフレームのデータを作ってみます。

タイムフレームの種類って、MT4だと、1分、5分、15分、30分、1時間、4時間、1日、1週間、1ヵ月くらいですが、MT5だと、2分、3分、4分、6分、12分とかもあります。まあ、タイムフレームは自由に作れる方が便利です。

pandasのライブラリには適当なタイムフレームで4本値を作る関数が用意されています。詳しいことはよくわかりませんが、resample().ohlc()という関数を使えばいいらしいです。例えば、1時間足の4本値データを作りたい場合、

In [2]:
dataH1 = dataM1.resample('H').ohlc()

と書きます。実行結果は次のようになります。

In [3]:
dataH1
Out[3]:
Open High Low Close
open high low close open high low close open high low close open high low close open high low close
Time
2015-01-01 13:00:00 1.20965 1.21033 1.20959 1.20983 1.20977 1.21041 1.20968 1.21002 1.20962 1.21033 1.20959 1.20980 1.20962 1.21035 1.20961 1.21002 0.0 0.0 0.0 0.0
2015-01-01 14:00:00 1.21000 1.21000 1.21000 1.21000 1.21002 1.21002 1.21002 1.21002 1.20983 1.20983 1.20983 1.20983 1.21002 1.21002 1.21002 1.21002 0.0 0.0 0.0 0.0
2015-01-01 15:00:00 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-01-01 16:00:00 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-01-01 17:00:00 1.21037 1.21044 1.21001 1.21019 1.21037 1.21072 1.21003 1.21019 1.21036 1.21042 1.21001 1.21015 1.21036 1.21045 1.21002 1.21016 0.0 0.0 0.0 0.0
2015-01-01 18:00:00 1.21016 1.21016 1.20795 1.20901 1.21017 1.21017 1.20805 1.20904 1.21005 1.21010 1.20783 1.20865 1.21006 1.21010 1.20795 1.20866 0.0 0.0 0.0 0.0
2015-01-01 19:00:00 1.20868 1.20883 1.20672 1.20672 1.20877 1.20888 1.20673 1.20673 1.20868 1.20879 1.20668 1.20672 1.20873 1.20884 1.20672 1.20672 0.0 0.0 0.0 0.0
2015-01-01 20:00:00 1.20673 1.20717 1.20523 1.20680 1.20677 1.20721 1.20532 1.20680 1.20658 1.20710 1.20520 1.20669 1.20658 1.20715 1.20523 1.20671 0.0 0.0 0.0 0.0
2015-01-01 21:00:00 1.20670 1.20685 1.20504 1.20594 1.20681 1.20689 1.20525 1.20594 1.20668 1.20676 1.20482 1.20583 1.20676 1.20686 1.20506 1.20583 0.0 0.0 0.0 0.0
2015-01-01 22:00:00 1.20581 1.20625 1.20553 1.20575 1.20581 1.20626 1.20556 1.20576 1.20575 1.20620 1.20551 1.20573 1.20576 1.20624 1.20552 1.20575 0.0 0.0 0.0 0.0
2015-01-01 23:00:00 1.20574 1.20596 1.20498 1.20527 1.20575 1.20596 1.20509 1.20529 1.20551 1.20585 1.20491 1.20526 1.20555 1.20596 1.20498 1.20528 0.0 0.0 0.0 0.0
2015-01-02 00:00:00 1.20527 1.20554 1.20474 1.20520 1.20527 1.20556 1.20486 1.20527 1.20523 1.20551 1.20461 1.20517 1.20526 1.20556 1.20476 1.20517 0.0 0.0 0.0 0.0
2015-01-02 01:00:00 1.20517 1.20570 1.20487 1.20538 1.20518 1.20576 1.20490 1.20547 1.20496 1.20565 1.20486 1.20537 1.20504 1.20567 1.20488 1.20546 0.0 0.0 0.0 0.0
2015-01-02 02:00:00 1.20545 1.20565 1.20370 1.20561 1.20545 1.20582 1.20391 1.20577 1.20515 1.20560 1.20351 1.20549 1.20516 1.20565 1.20365 1.20560 0.0 0.0 0.0 0.0
2015-01-02 03:00:00 1.20563 1.20682 1.20458 1.20474 1.20595 1.20696 1.20465 1.20496 1.20561 1.20667 1.20448 1.20473 1.20581 1.20680 1.20460 1.20474 0.0 0.0 0.0 0.0
2015-01-02 04:00:00 1.20474 1.20589 1.20393 1.20572 1.20493 1.20606 1.20435 1.20581 1.20426 1.20580 1.20389 1.20566 1.20456 1.20589 1.20394 1.20581 0.0 0.0 0.0 0.0
2015-01-02 05:00:00 1.20579 1.20579 1.20425 1.20507 1.20579 1.20579 1.20430 1.20511 1.20557 1.20557 1.20423 1.20482 1.20558 1.20558 1.20425 1.20508 0.0 0.0 0.0 0.0
2015-01-02 06:00:00 1.20508 1.20570 1.20459 1.20524 1.20508 1.20583 1.20475 1.20543 1.20494 1.20567 1.20450 1.20524 1.20496 1.20570 1.20456 1.20542 0.0 0.0 0.0 0.0
2015-01-02 07:00:00 1.20540 1.20546 1.20361 1.20376 1.20549 1.20549 1.20364 1.20382 1.20524 1.20527 1.20352 1.20376 1.20547 1.20547 1.20360 1.20382 0.0 0.0 0.0 0.0
2015-01-02 08:00:00 1.20382 1.20434 1.20263 1.20322 1.20391 1.20442 1.20279 1.20322 1.20376 1.20426 1.20254 1.20303 1.20382 1.20436 1.20262 1.20310 0.0 0.0 0.0 0.0
2015-01-02 09:00:00 1.20310 1.20334 1.20104 1.20153 1.20327 1.20335 1.20115 1.20161 1.20302 1.20311 1.20085 1.20147 1.20312 1.20335 1.20103 1.20149 0.0 0.0 0.0 0.0
2015-01-02 10:00:00 1.20206 1.20300 1.20134 1.20285 1.20272 1.20322 1.20144 1.20316 1.20192 1.20296 1.20119 1.20276 1.20259 1.20313 1.20131 1.20313 0.0 0.0 0.0 0.0
2015-01-02 11:00:00 1.20315 1.20315 1.20078 1.20110 1.20317 1.20320 1.20088 1.20138 1.20294 1.20295 1.20074 1.20109 1.20306 1.20306 1.20078 1.20136 0.0 0.0 0.0 0.0
2015-01-02 12:00:00 1.20136 1.20157 1.20047 1.20070 1.20145 1.20158 1.20056 1.20103 1.20129 1.20140 1.20038 1.20069 1.20137 1.20158 1.20045 1.20096 0.0 0.0 0.0 0.0
2015-01-02 13:00:00 1.20098 1.20150 1.20086 1.20113 1.20105 1.20155 1.20098 1.20122 1.20086 1.20147 1.20080 1.20110 1.20089 1.20150 1.20086 1.20120 0.0 0.0 0.0 0.0
2015-01-02 14:00:00 1.20118 1.20118 1.20051 1.20059 1.20120 1.20120 1.20060 1.20060 1.20116 1.20116 1.20044 1.20050 1.20116 1.20116 1.20052 1.20056 0.0 0.0 0.0 0.0
2015-01-02 15:00:00 1.20055 1.20058 1.20016 1.20040 1.20056 1.20086 1.20017 1.20041 1.20053 1.20057 1.20014 1.20029 1.20056 1.20057 1.20015 1.20033 0.0 0.0 0.0 0.0
2015-01-02 16:00:00 1.20032 1.20059 1.20003 1.20003 1.20034 1.20060 1.20008 1.20014 1.20029 1.20054 1.20002 1.20002 1.20032 1.20059 1.20005 1.20009 0.0 0.0 0.0 0.0
2015-01-02 17:00:00 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
2015-01-02 18:00:00 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
2015-12-30 11:00:00 1.09140 1.09224 1.09103 1.09126 1.09146 1.09242 1.09117 1.09139 1.09123 1.09215 1.09054 1.09116 1.09144 1.09223 1.09102 1.09116 0.0 0.0 0.0 0.0
2015-12-30 12:00:00 1.09115 1.09148 1.09030 1.09106 1.09124 1.09155 1.09038 1.09116 1.09113 1.09127 1.09018 1.09097 1.09116 1.09146 1.09031 1.09102 0.0 0.0 0.0 0.0
2015-12-30 13:00:00 1.09103 1.09139 1.09043 1.09126 1.09127 1.09162 1.09069 1.09130 1.09096 1.09136 1.09030 1.09126 1.09127 1.09139 1.09042 1.09130 0.0 0.0 0.0 0.0
2015-12-30 14:00:00 1.09130 1.09219 1.09085 1.09219 1.09140 1.09247 1.09090 1.09247 1.09129 1.09219 1.09077 1.09219 1.09139 1.09243 1.09083 1.09243 0.0 0.0 0.0 0.0
2015-12-30 15:00:00 1.09242 1.09344 1.09212 1.09330 1.09254 1.09374 1.09228 1.09330 1.09201 1.09341 1.09201 1.09302 1.09212 1.09344 1.09212 1.09304 0.0 0.0 0.0 0.0
2015-12-30 16:00:00 1.09305 1.09333 1.09297 1.09309 1.09312 1.09334 1.09303 1.09317 1.09303 1.09333 1.09292 1.09302 1.09308 1.09333 1.09300 1.09312 0.0 0.0 0.0 0.0
2015-12-30 17:00:00 1.09309 1.09328 1.09225 1.09255 1.09315 1.09329 1.09230 1.09258 1.09301 1.09316 1.09224 1.09255 1.09315 1.09328 1.09226 1.09257 0.0 0.0 0.0 0.0
2015-12-30 18:00:00 1.09262 1.09336 1.09259 1.09333 1.09265 1.09352 1.09260 1.09352 1.09257 1.09334 1.09257 1.09326 1.09260 1.09352 1.09259 1.09352 0.0 0.0 0.0 0.0
2015-12-30 19:00:00 1.09353 1.09366 1.09331 1.09363 1.09354 1.09372 1.09334 1.09364 1.09352 1.09365 1.09330 1.09363 1.09353 1.09367 1.09332 1.09363 0.0 0.0 0.0 0.0
2015-12-30 20:00:00 1.09364 1.09364 1.09243 1.09243 1.09364 1.09368 1.09243 1.09243 1.09354 1.09364 1.09219 1.09236 1.09355 1.09364 1.09237 1.09237 0.0 0.0 0.0 0.0
2015-12-30 21:00:00 1.09237 1.09256 1.09196 1.09230 1.09237 1.09259 1.09205 1.09239 1.09227 1.09253 1.09187 1.09230 1.09232 1.09258 1.09196 1.09238 0.0 0.0 0.0 0.0
2015-12-30 22:00:00 1.09237 1.09313 1.09237 1.09312 1.09240 1.09320 1.09239 1.09312 1.09228 1.09312 1.09228 1.09309 1.09239 1.09314 1.09237 1.09309 0.0 0.0 0.0 0.0
2015-12-30 23:00:00 1.09305 1.09333 1.09289 1.09332 1.09320 1.09337 1.09289 1.09333 1.09300 1.09332 1.09287 1.09332 1.09318 1.09332 1.09289 1.09332 0.0 0.0 0.0 0.0
2015-12-31 00:00:00 1.09333 1.09348 1.09266 1.09267 1.09340 1.09351 1.09266 1.09267 1.09333 1.09340 1.09257 1.09257 1.09339 1.09342 1.09259 1.09259 0.0 0.0 0.0 0.0
2015-12-31 01:00:00 1.09258 1.09315 1.09256 1.09312 1.09262 1.09319 1.09257 1.09319 1.09258 1.09312 1.09255 1.09311 1.09259 1.09318 1.09255 1.09318 0.0 0.0 0.0 0.0
2015-12-31 02:00:00 1.09317 1.09319 1.09247 1.09269 1.09319 1.09325 1.09249 1.09270 1.09313 1.09318 1.09229 1.09268 1.09317 1.09319 1.09248 1.09269 0.0 0.0 0.0 0.0
2015-12-31 03:00:00 1.09271 1.09318 1.09152 1.09233 1.09317 1.09335 1.09154 1.09233 1.09271 1.09313 1.09144 1.09226 1.09298 1.09318 1.09151 1.09226 0.0 0.0 0.0 0.0
2015-12-31 04:00:00 1.09225 1.09312 1.09221 1.09239 1.09232 1.09323 1.09227 1.09242 1.09224 1.09302 1.09217 1.09221 1.09229 1.09313 1.09223 1.09232 0.0 0.0 0.0 0.0
2015-12-31 05:00:00 1.09233 1.09244 1.09004 1.09110 1.09233 1.09248 1.09016 1.09114 1.09229 1.09239 1.08999 1.09099 1.09231 1.09243 1.09004 1.09099 0.0 0.0 0.0 0.0
2015-12-31 06:00:00 1.09099 1.09162 1.08913 1.08913 1.09124 1.09181 1.08923 1.08923 1.09092 1.09150 1.08880 1.08910 1.09121 1.09163 1.08914 1.08915 0.0 0.0 0.0 0.0
2015-12-31 07:00:00 1.08914 1.08973 1.08828 1.08833 1.08939 1.08975 1.08836 1.08836 1.08903 1.08963 1.08821 1.08828 1.08933 1.08966 1.08830 1.08833 0.0 0.0 0.0 0.0
2015-12-31 08:00:00 1.08832 1.08945 1.08799 1.08908 1.08834 1.08949 1.08806 1.08909 1.08810 1.08936 1.08789 1.08873 1.08827 1.08945 1.08800 1.08877 0.0 0.0 0.0 0.0
2015-12-31 09:00:00 1.08876 1.08887 1.08764 1.08829 1.08892 1.08892 1.08788 1.08833 1.08876 1.08884 1.08752 1.08807 1.08885 1.08887 1.08763 1.08827 0.0 0.0 0.0 0.0
2015-12-31 10:00:00 1.08827 1.08995 1.08637 1.08637 1.08837 1.09006 1.08669 1.08669 1.08814 1.08988 1.08599 1.08599 1.08830 1.08998 1.08636 1.08668 0.0 0.0 0.0 0.0
2015-12-31 11:00:00 1.08667 1.08687 1.08552 1.08582 1.08675 1.08693 1.08580 1.08607 1.08581 1.08680 1.08534 1.08578 1.08583 1.08689 1.08551 1.08581 0.0 0.0 0.0 0.0
2015-12-31 12:00:00 1.08579 1.08643 1.08534 1.08641 1.08588 1.08673 1.08557 1.08673 1.08568 1.08633 1.08529 1.08619 1.08570 1.08656 1.08533 1.08656 0.0 0.0 0.0 0.0
2015-12-31 13:00:00 1.08653 1.08804 1.08616 1.08703 1.08656 1.08804 1.08621 1.08703 1.08612 1.08790 1.08608 1.08692 1.08615 1.08800 1.08615 1.08695 0.0 0.0 0.0 0.0
2015-12-31 14:00:00 1.08696 1.08739 1.08578 1.08578 1.08713 1.08744 1.08581 1.08581 1.08695 1.08729 1.08567 1.08569 1.08710 1.08739 1.08573 1.08573 0.0 0.0 0.0 0.0
2015-12-31 15:00:00 1.08579 1.08761 1.08579 1.08674 1.08622 1.08771 1.08622 1.08684 1.08572 1.08750 1.08572 1.08661 1.08618 1.08761 1.08614 1.08661 0.0 0.0 0.0 0.0
2015-12-31 16:00:00 1.08656 1.08697 1.08531 1.08540 1.08666 1.08697 1.08542 1.08549 1.08655 1.08693 1.08527 1.08536 1.08658 1.08696 1.08531 1.08545 0.0 0.0 0.0 0.0

8740 rows × 20 columns

確かに'Time'の列が1時間間隔になっており、1時間足のデータができていることはわかりますが、'Open''High''Low''Close'のなかに、さらに'open''high''low''close'の列ができており、全部で16個もあります。実は、resample().ohlc()を使うと、それぞれの系列に対してOHLCの4本値を算出してしまうのでした。

結局、必要なのは、'Open'のなかの'open''High'のなかの'high''Low'のなかの'low''Close'のなかの'close'なので、次のように各列を抽出して4本値データを作ってみます。

In [4]:
ohlcH1 = pd.DataFrame({'Open': dataH1['Open']['open'],
                       'High': dataH1['High']['high'],
                       'Low': dataH1['Low']['low'],
                       'Close': dataH1['Close']['close']},
                       columns=['Open','High','Low','Close'])
ohlcH1
Out[4]:
Open High Low Close
Time
2015-01-01 13:00:00 1.20965 1.21041 1.20959 1.21002
2015-01-01 14:00:00 1.21000 1.21002 1.20983 1.21002
2015-01-01 15:00:00 NaN NaN NaN NaN
2015-01-01 16:00:00 NaN NaN NaN NaN
2015-01-01 17:00:00 1.21037 1.21072 1.21001 1.21016
2015-01-01 18:00:00 1.21016 1.21017 1.20783 1.20866
2015-01-01 19:00:00 1.20868 1.20888 1.20668 1.20672
2015-01-01 20:00:00 1.20673 1.20721 1.20520 1.20671
2015-01-01 21:00:00 1.20670 1.20689 1.20482 1.20583
2015-01-01 22:00:00 1.20581 1.20626 1.20551 1.20575
2015-01-01 23:00:00 1.20574 1.20596 1.20491 1.20528
2015-01-02 00:00:00 1.20527 1.20556 1.20461 1.20517
2015-01-02 01:00:00 1.20517 1.20576 1.20486 1.20546
2015-01-02 02:00:00 1.20545 1.20582 1.20351 1.20560
2015-01-02 03:00:00 1.20563 1.20696 1.20448 1.20474
2015-01-02 04:00:00 1.20474 1.20606 1.20389 1.20581
2015-01-02 05:00:00 1.20579 1.20579 1.20423 1.20508
2015-01-02 06:00:00 1.20508 1.20583 1.20450 1.20542
2015-01-02 07:00:00 1.20540 1.20549 1.20352 1.20382
2015-01-02 08:00:00 1.20382 1.20442 1.20254 1.20310
2015-01-02 09:00:00 1.20310 1.20335 1.20085 1.20149
2015-01-02 10:00:00 1.20206 1.20322 1.20119 1.20313
2015-01-02 11:00:00 1.20315 1.20320 1.20074 1.20136
2015-01-02 12:00:00 1.20136 1.20158 1.20038 1.20096
2015-01-02 13:00:00 1.20098 1.20155 1.20080 1.20120
2015-01-02 14:00:00 1.20118 1.20120 1.20044 1.20056
2015-01-02 15:00:00 1.20055 1.20086 1.20014 1.20033
2015-01-02 16:00:00 1.20032 1.20060 1.20002 1.20009
2015-01-02 17:00:00 NaN NaN NaN NaN
2015-01-02 18:00:00 NaN NaN NaN NaN
... ... ... ... ...
2015-12-30 11:00:00 1.09140 1.09242 1.09054 1.09116
2015-12-30 12:00:00 1.09115 1.09155 1.09018 1.09102
2015-12-30 13:00:00 1.09103 1.09162 1.09030 1.09130
2015-12-30 14:00:00 1.09130 1.09247 1.09077 1.09243
2015-12-30 15:00:00 1.09242 1.09374 1.09201 1.09304
2015-12-30 16:00:00 1.09305 1.09334 1.09292 1.09312
2015-12-30 17:00:00 1.09309 1.09329 1.09224 1.09257
2015-12-30 18:00:00 1.09262 1.09352 1.09257 1.09352
2015-12-30 19:00:00 1.09353 1.09372 1.09330 1.09363
2015-12-30 20:00:00 1.09364 1.09368 1.09219 1.09237
2015-12-30 21:00:00 1.09237 1.09259 1.09187 1.09238
2015-12-30 22:00:00 1.09237 1.09320 1.09228 1.09309
2015-12-30 23:00:00 1.09305 1.09337 1.09287 1.09332
2015-12-31 00:00:00 1.09333 1.09351 1.09257 1.09259
2015-12-31 01:00:00 1.09258 1.09319 1.09255 1.09318
2015-12-31 02:00:00 1.09317 1.09325 1.09229 1.09269
2015-12-31 03:00:00 1.09271 1.09335 1.09144 1.09226
2015-12-31 04:00:00 1.09225 1.09323 1.09217 1.09232
2015-12-31 05:00:00 1.09233 1.09248 1.08999 1.09099
2015-12-31 06:00:00 1.09099 1.09181 1.08880 1.08915
2015-12-31 07:00:00 1.08914 1.08975 1.08821 1.08833
2015-12-31 08:00:00 1.08832 1.08949 1.08789 1.08877
2015-12-31 09:00:00 1.08876 1.08892 1.08752 1.08827
2015-12-31 10:00:00 1.08827 1.09006 1.08599 1.08668
2015-12-31 11:00:00 1.08667 1.08693 1.08534 1.08581
2015-12-31 12:00:00 1.08579 1.08673 1.08529 1.08656
2015-12-31 13:00:00 1.08653 1.08804 1.08608 1.08695
2015-12-31 14:00:00 1.08696 1.08744 1.08567 1.08573
2015-12-31 15:00:00 1.08579 1.08771 1.08572 1.08661
2015-12-31 16:00:00 1.08656 1.08697 1.08527 1.08545

8740 rows × 4 columns

これで1時間足の4本値データができました。 他のタイムフレームも、resample()の引数を変えることで簡単に作ることができます。例えば、5分の場合、'5Min'、4時間の場合、'4H'、1日の場合、'D'を指定します。ただ、毎回、上の二つの行を実行するのは面倒なので、関数にしておきましょう。
In [5]:
def TF_ohlc(df, tf):
    x = df.resample(tf).ohlc()
    ret = pd.DataFrame({'Open': x['Open']['open'],
                       'High': x['High']['high'],
                       'Low': x['Low']['low'],
                       'Close': x['Close']['close']},
                       columns=['Open','High','Low','Close'])
    return ret

では、この関数を使って日足データを作ってみましょう。

In [6]:
ohlcD1 = TF_ohlc(dataM1, 'D')
ohlcD1
Out[6]:
Open High Low Close
Time
2015-01-01 1.20965 1.21072 1.20482 1.20528
2015-01-02 1.20527 1.20696 1.20002 1.20009
2015-01-03 NaN NaN NaN NaN
2015-01-04 1.19427 1.19759 1.18664 1.19410
2015-01-05 1.19410 1.19763 1.18871 1.19488
2015-01-06 1.19488 1.19687 1.18408 1.18755
2015-01-07 1.18758 1.18965 1.18021 1.18225
2015-01-08 1.18226 1.18266 1.17541 1.18057
2015-01-09 1.18057 1.18459 1.17626 1.18407
2015-01-10 NaN NaN NaN NaN
2015-01-11 1.18499 1.18700 1.18451 1.18530
2015-01-12 1.18530 1.18709 1.17862 1.18363
2015-01-13 1.18367 1.18522 1.17534 1.17857
2015-01-14 1.17858 1.18462 1.17272 1.17744
2015-01-15 1.17743 1.17862 1.15678 1.16311
2015-01-16 1.16311 1.16487 1.14600 1.15620
2015-01-17 NaN NaN NaN NaN
2015-01-18 1.15516 1.15741 1.15516 1.15569
2015-01-19 1.15568 1.16389 1.15513 1.15772
2015-01-20 1.15771 1.16148 1.15405 1.15656
2015-01-21 1.15658 1.16802 1.15470 1.15987
2015-01-22 1.15986 1.16509 1.13159 1.13423
2015-01-23 1.13427 1.13561 1.11144 1.12024
2015-01-24 NaN NaN NaN NaN
2015-01-25 1.11511 1.12019 1.10978 1.11726
2015-01-26 1.11727 1.12953 1.11723 1.12451
2015-01-27 1.12452 1.14225 1.12236 1.13370
2015-01-28 1.13368 1.13824 1.12650 1.12688
2015-01-29 1.12687 1.13678 1.12617 1.13263
2015-01-30 1.13265 1.13636 1.12786 1.12839
... ... ... ... ...
2015-12-02 1.06138 1.06321 1.05508 1.05923
2015-12-03 1.05923 1.09811 1.05223 1.09397
2015-12-04 1.09398 1.09505 1.08359 1.08832
2015-12-05 NaN NaN NaN NaN
2015-12-06 1.08725 1.08876 1.08540 1.08627
2015-12-07 1.08627 1.08725 1.07960 1.08454
2015-12-08 1.08451 1.09141 1.08404 1.09114
2015-12-09 1.09119 1.10425 1.09063 1.10057
2015-12-10 1.10057 1.10076 1.09251 1.09363
2015-12-11 1.09362 1.10308 1.09344 1.09886
2015-12-12 NaN NaN NaN NaN
2015-12-13 1.09823 1.09931 1.09541 1.09606
2015-12-14 1.09607 1.10486 1.09455 1.10225
2015-12-15 1.10224 1.10594 1.09045 1.09359
2015-12-16 1.09359 1.10110 1.08319 1.08502
2015-12-17 1.08503 1.08780 1.08027 1.08467
2015-12-18 1.08464 1.08744 1.08050 1.08652
2015-12-19 NaN NaN NaN NaN
2015-12-20 1.08531 1.08828 1.08470 1.08699
2015-12-21 1.08698 1.09389 1.08478 1.09206
2015-12-22 1.09205 1.09841 1.09021 1.09378
2015-12-23 1.09377 1.09421 1.08701 1.09332
2015-12-24 1.09332 1.09676 1.09291 1.09630
2015-12-25 NaN NaN NaN NaN
2015-12-26 NaN NaN NaN NaN
2015-12-27 1.09676 1.09786 1.09547 1.09750
2015-12-28 1.09750 1.09924 1.09557 1.09731
2015-12-29 1.09729 1.09915 1.08992 1.09309
2015-12-30 1.09311 1.09436 1.09018 1.09332
2015-12-31 1.09333 1.09351 1.08527 1.08545

365 rows × 4 columns

これで365日分の4本値が出ましたが、よく見ると、ところどころに'NaN'というデータが入っています。これは、FXのデータがないところです。FXは1日24時間取引が行われるとはいえ、土日は休場です。なので、本来なら1週間のうち5日間分だけデータがあればよいのですが、上のデータだと、サーバー時間の関係で週明けのチャートが日曜日から始まっているようです。

実際、元のデータ見てみると、下のように17時から始まっていることがわかります。

20150102 165500;1.200230;1.200300;1.200170;1.200190;0
20150102 165600;1.200190;1.200200;1.200120;1.200120;0
20150102 165700;1.200120;1.200150;1.200030;1.200070;0
20150102 165800;1.200070;1.200100;1.200070;1.200070;0
20150102 165900;1.200030;1.200140;1.200020;1.200090;0
20150104 170000;1.194270;1.194910;1.193760;1.194100;0
20150104 170100;1.194080;1.194080;1.190380;1.191300;0
20150104 170200;1.190980;1.191120;1.186640;1.188360;0
20150104 170300;1.188610;1.191330;1.187320;1.191330;0
20150104 170400;1.191080;1.192610;1.190470;1.192570;0

この時刻を7時間だけ遅らせれば、月曜日の0時から始まり、日曜日の4本値データがなくなるはずです。そのために、時刻を表すインデックスに一律7時間を加えます。その方法も色々とあるみたいですが、以下はその一例です。

In [7]:
import pandas.tseries.offsets as offsets
dataM1.index += offsets.Hour(7)
dataM1
Out[7]:
Open High Low Close
Time
2015-01-01 20:00:00 1.20965 1.20977 1.20962 1.20962 0
2015-01-01 20:01:00 1.20963 1.20968 1.20962 1.20962 0
2015-01-01 20:02:00 1.20965 1.20970 1.20961 1.20961 0
2015-01-01 20:04:00 1.20959 1.21008 1.20959 1.20983 0
2015-01-01 20:05:00 1.20988 1.20988 1.20988 1.20988 0
2015-01-01 20:06:00 1.20984 1.20984 1.20982 1.20982 0
2015-01-01 20:07:00 1.20986 1.20999 1.20986 1.20987 0
2015-01-01 20:08:00 1.20998 1.21022 1.20987 1.21008 0
2015-01-01 20:09:00 1.20996 1.20996 1.20996 1.20996 0
2015-01-01 20:10:00 1.21013 1.21019 1.20967 1.20977 0
2015-01-01 20:11:00 1.20976 1.20999 1.20976 1.20988 0
2015-01-01 20:12:00 1.20987 1.20987 1.20987 1.20987 0
2015-01-01 20:13:00 1.20997 1.21006 1.20993 1.21006 0
2015-01-01 20:14:00 1.21007 1.21007 1.21006 1.21006 0
2015-01-01 20:16:00 1.21004 1.21004 1.21004 1.21004 0
2015-01-01 20:18:00 1.21003 1.21004 1.20979 1.20979 0
2015-01-01 20:27:00 1.21003 1.21003 1.20979 1.21003 0
2015-01-01 20:28:00 1.20979 1.21003 1.20979 1.21003 0
2015-01-01 20:29:00 1.21001 1.21003 1.20999 1.21003 0
2015-01-01 20:31:00 1.21033 1.21041 1.21033 1.21033 0
2015-01-01 20:32:00 1.21028 1.21035 1.21006 1.21035 0
2015-01-01 20:35:00 1.21005 1.21006 1.21005 1.21005 0
2015-01-01 20:36:00 1.21004 1.21006 1.21004 1.21006 0
2015-01-01 20:37:00 1.20991 1.21006 1.20991 1.21004 0
2015-01-01 20:38:00 1.21003 1.21004 1.20982 1.21004 0
2015-01-01 20:39:00 1.21019 1.21034 1.21019 1.21034 0
2015-01-01 20:40:00 1.21030 1.21034 1.21030 1.21032 0
2015-01-01 20:41:00 1.21006 1.21007 1.21006 1.21007 0
2015-01-01 20:43:00 1.21003 1.21006 1.21003 1.21006 0
2015-01-01 20:44:00 1.21003 1.21007 1.21003 1.21007 0
... ... ... ... ... ...
2015-12-31 23:29:00 1.08644 1.08647 1.08644 1.08646 0
2015-12-31 23:30:00 1.08646 1.08651 1.08645 1.08651 0
2015-12-31 23:31:00 1.08650 1.08667 1.08650 1.08666 0
2015-12-31 23:32:00 1.08666 1.08680 1.08666 1.08676 0
2015-12-31 23:33:00 1.08676 1.08676 1.08675 1.08676 0
2015-12-31 23:34:00 1.08675 1.08676 1.08675 1.08676 0
2015-12-31 23:35:00 1.08674 1.08674 1.08673 1.08674 0
2015-12-31 23:36:00 1.08675 1.08676 1.08675 1.08676 0
2015-12-31 23:37:00 1.08681 1.08689 1.08681 1.08687 0
2015-12-31 23:38:00 1.08688 1.08688 1.08685 1.08687 0
2015-12-31 23:39:00 1.08688 1.08694 1.08687 1.08691 0
2015-12-31 23:40:00 1.08690 1.08690 1.08689 1.08690 0
2015-12-31 23:41:00 1.08689 1.08692 1.08686 1.08692 0
2015-12-31 23:42:00 1.08689 1.08691 1.08687 1.08691 0
2015-12-31 23:43:00 1.08691 1.08697 1.08683 1.08696 0
2015-12-31 23:44:00 1.08697 1.08697 1.08693 1.08696 0
2015-12-31 23:45:00 1.08684 1.08693 1.08629 1.08651 0
2015-12-31 23:46:00 1.08652 1.08653 1.08617 1.08640 0
2015-12-31 23:47:00 1.08635 1.08642 1.08609 1.08620 0
2015-12-31 23:48:00 1.08621 1.08621 1.08571 1.08578 0
2015-12-31 23:49:00 1.08562 1.08571 1.08539 1.08559 0
2015-12-31 23:50:00 1.08558 1.08568 1.08543 1.08554 0
2015-12-31 23:51:00 1.08554 1.08570 1.08537 1.08538 0
2015-12-31 23:52:00 1.08534 1.08558 1.08534 1.08558 0
2015-12-31 23:53:00 1.08564 1.08565 1.08527 1.08538 0
2015-12-31 23:54:00 1.08538 1.08550 1.08533 1.08541 0
2015-12-31 23:55:00 1.08542 1.08542 1.08530 1.08531 0
2015-12-31 23:56:00 1.08531 1.08550 1.08531 1.08533 0
2015-12-31 23:57:00 1.08533 1.08547 1.08528 1.08538 0
2015-12-31 23:58:00 1.08540 1.08549 1.08536 1.08545 0

372210 rows × 5 columns

これで7時間ずれていることがわかります。このデータから日足データを作ると次のようになります。

In [8]:
ohlcD1 = TF_ohlc(dataM1, 'D')
ohlcD1
Out[8]:
Open High Low Close
Time
2015-01-01 1.20965 1.21041 1.20959 1.21002
2015-01-02 1.21037 1.21072 1.20002 1.20009
2015-01-03 NaN NaN NaN NaN
2015-01-04 NaN NaN NaN NaN
2015-01-05 1.19427 1.19763 1.18664 1.19327
2015-01-06 1.19329 1.19687 1.18841 1.18893
2015-01-07 1.18894 1.18965 1.18021 1.18392
2015-01-08 1.18377 1.18474 1.17541 1.17916
2015-01-09 1.17930 1.18459 1.17626 1.18407
2015-01-10 NaN NaN NaN NaN
2015-01-11 NaN NaN NaN NaN
2015-01-12 1.18499 1.18709 1.17862 1.18334
2015-01-13 1.18331 1.18595 1.17534 1.17722
2015-01-14 1.17722 1.18462 1.17272 1.17887
2015-01-15 1.17888 1.17924 1.15678 1.16299
2015-01-16 1.16304 1.16487 1.14600 1.15620
2015-01-17 NaN NaN NaN NaN
2015-01-18 NaN NaN NaN NaN
2015-01-19 1.15516 1.16389 1.15513 1.16019
2015-01-20 1.16068 1.16148 1.15405 1.15485
2015-01-21 1.15483 1.16802 1.15414 1.16092
2015-01-22 1.16095 1.16509 1.13159 1.13647
2015-01-23 1.13649 1.13738 1.11144 1.12024
2015-01-24 NaN NaN NaN NaN
2015-01-25 NaN NaN NaN NaN
2015-01-26 1.11511 1.12953 1.10978 1.12386
2015-01-27 1.12377 1.14225 1.12236 1.13805
2015-01-28 1.13761 1.13829 1.12760 1.12863
2015-01-29 1.12862 1.13678 1.12617 1.13192
2015-01-30 1.13183 1.13636 1.12786 1.12839
... ... ... ... ...
2015-12-02 1.06312 1.06356 1.05508 1.06127
2015-12-03 1.06129 1.09811 1.05223 1.09376
2015-12-04 1.09376 1.09562 1.08359 1.08832
2015-12-05 NaN NaN NaN NaN
2015-12-06 NaN NaN NaN NaN
2015-12-07 1.08725 1.08876 1.07960 1.08369
2015-12-08 1.08362 1.09021 1.08300 1.08917
2015-12-09 1.08909 1.10425 1.08791 1.10240
2015-12-10 1.10226 1.10245 1.09251 1.09401
2015-12-11 1.09392 1.10308 1.09266 1.09886
2015-12-12 NaN NaN NaN NaN
2015-12-13 NaN NaN NaN NaN
2015-12-14 1.09823 1.10486 1.09455 1.09909
2015-12-15 1.09905 1.10594 1.09045 1.09280
2015-12-16 1.09284 1.10110 1.08879 1.09115
2015-12-17 1.09105 1.09138 1.08027 1.08251
2015-12-18 1.08238 1.08744 1.08050 1.08652
2015-12-19 NaN NaN NaN NaN
2015-12-20 NaN NaN NaN NaN
2015-12-21 1.08531 1.09389 1.08470 1.09145
2015-12-22 1.09137 1.09841 1.09021 1.09555
2015-12-23 1.09550 1.09565 1.08701 1.09093
2015-12-24 1.09093 1.09676 1.09028 1.09630
2015-12-25 NaN NaN NaN NaN
2015-12-26 NaN NaN NaN NaN
2015-12-27 NaN NaN NaN NaN
2015-12-28 1.09676 1.09924 1.09547 1.09652
2015-12-29 1.09650 1.09915 1.08992 1.09193
2015-12-30 1.09172 1.09436 1.09018 1.09312
2015-12-31 1.09309 1.09372 1.08527 1.08545

365 rows × 4 columns

これで1週間5本の日足データができました。ただ、NaNのデータが残っているとデータ処理の際に都合が悪いので、その行を削除することもできます。dropna()という関数を使います。

In [9]:
ohlcD1 = ohlcD1.dropna()
ohlcD1
Out[9]:
Open High Low Close
Time
2015-01-01 1.20965 1.21041 1.20959 1.21002
2015-01-02 1.21037 1.21072 1.20002 1.20009
2015-01-05 1.19427 1.19763 1.18664 1.19327
2015-01-06 1.19329 1.19687 1.18841 1.18893
2015-01-07 1.18894 1.18965 1.18021 1.18392
2015-01-08 1.18377 1.18474 1.17541 1.17916
2015-01-09 1.17930 1.18459 1.17626 1.18407
2015-01-12 1.18499 1.18709 1.17862 1.18334
2015-01-13 1.18331 1.18595 1.17534 1.17722
2015-01-14 1.17722 1.18462 1.17272 1.17887
2015-01-15 1.17888 1.17924 1.15678 1.16299
2015-01-16 1.16304 1.16487 1.14600 1.15620
2015-01-19 1.15516 1.16389 1.15513 1.16019
2015-01-20 1.16068 1.16148 1.15405 1.15485
2015-01-21 1.15483 1.16802 1.15414 1.16092
2015-01-22 1.16095 1.16509 1.13159 1.13647
2015-01-23 1.13649 1.13738 1.11144 1.12024
2015-01-26 1.11511 1.12953 1.10978 1.12386
2015-01-27 1.12377 1.14225 1.12236 1.13805
2015-01-28 1.13761 1.13829 1.12760 1.12863
2015-01-29 1.12862 1.13678 1.12617 1.13192
2015-01-30 1.13183 1.13636 1.12786 1.12839
2015-02-02 1.13105 1.13620 1.12922 1.13411
2015-02-03 1.13407 1.15340 1.13123 1.14785
2015-02-04 1.14788 1.14847 1.13155 1.13433
2015-02-05 1.13433 1.14987 1.13038 1.14768
2015-02-06 1.14763 1.14856 1.13117 1.13154
2015-02-09 1.12964 1.13592 1.12702 1.13231
2015-02-10 1.13229 1.13454 1.12730 1.13202
2015-02-11 1.13200 1.13472 1.12799 1.13354
... ... ... ... ...
2015-11-19 1.06572 1.07630 1.06552 1.07346
2015-11-20 1.07323 1.07384 1.06397 1.06436
2015-11-23 1.06377 1.06568 1.05927 1.06356
2015-11-24 1.06354 1.06731 1.06195 1.06428
2015-11-25 1.06419 1.06892 1.05660 1.06236
2015-11-26 1.06237 1.06273 1.05998 1.06093
2015-11-27 1.06095 1.06380 1.05685 1.05900
2015-11-30 1.05881 1.05949 1.05578 1.05631
2015-12-01 1.05629 1.06370 1.05623 1.06313
2015-12-02 1.06312 1.06356 1.05508 1.06127
2015-12-03 1.06129 1.09811 1.05223 1.09376
2015-12-04 1.09376 1.09562 1.08359 1.08832
2015-12-07 1.08725 1.08876 1.07960 1.08369
2015-12-08 1.08362 1.09021 1.08300 1.08917
2015-12-09 1.08909 1.10425 1.08791 1.10240
2015-12-10 1.10226 1.10245 1.09251 1.09401
2015-12-11 1.09392 1.10308 1.09266 1.09886
2015-12-14 1.09823 1.10486 1.09455 1.09909
2015-12-15 1.09905 1.10594 1.09045 1.09280
2015-12-16 1.09284 1.10110 1.08879 1.09115
2015-12-17 1.09105 1.09138 1.08027 1.08251
2015-12-18 1.08238 1.08744 1.08050 1.08652
2015-12-21 1.08531 1.09389 1.08470 1.09145
2015-12-22 1.09137 1.09841 1.09021 1.09555
2015-12-23 1.09550 1.09565 1.08701 1.09093
2015-12-24 1.09093 1.09676 1.09028 1.09630
2015-12-28 1.09676 1.09924 1.09547 1.09652
2015-12-29 1.09650 1.09915 1.08992 1.09193
2015-12-30 1.09172 1.09436 1.09018 1.09312
2015-12-31 1.09309 1.09372 1.08527 1.08545

260 rows × 4 columns

日足データの場合、resample('B')で平日だけ取り出すこともできるようですが、FXでも平日で休場となる場合もありますし、日中足の場合には使えないので、ここではdropna()で、データがない行を削除しました。

次回は4本値データをグラフに表示させる方法の予定です。