Created
September 18, 2018 08:30
-
-
Save nagishin/bf299b89196edd3b54c34a2a20bb12a2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"【line_profilerをインストール】 \n", | |
"`pip install line_profiler` \n", | |
"<br>\n", | |
"\n", | |
"【line_profilerのマジックコードを入力】 \n", | |
"`%load_ext line_profiler` \n", | |
"<br>\n", | |
"\n", | |
"【測定したい部分を関数化】 \n", | |
"<br>\n", | |
"\n", | |
"【計測実行】 \n", | |
"`%lprun -f 関数名 関数(引数)` \n", | |
" * -f : 関数名\n", | |
" * -u : Timer Unit \n", | |
"<br>\n", | |
"\n", | |
"【計測結果】 \n", | |
" * Line# : 計測行\n", | |
" * Hit : その行を通った回数\n", | |
" * Time : その行にかかった時間(単位は上部のTimer unit: 1e-06 s→マイクロ秒)\n", | |
" * Per Hit : その行を1回通るのにかかった時間\n", | |
" * %Time : 全体の中でその行が占める時間割合\n", | |
" * Line Contents : その行のコード\n", | |
" \n", | |
"|Line #|Hit|Time|Per Hit|%Time|Line Contents|\n", | |
"|---:|---:|---:|---:|---:|:---|\n", | |
"|10|11|9.0|0.8|0.6|for m in range(1, 11):|\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 27, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The line_profiler extension is already loaded. To reload it, use:\n", | |
" %reload_ext line_profiler\n" | |
] | |
} | |
], | |
"source": [ | |
"# -*- coding: utf-8 -*-\n", | |
"import math\n", | |
"%load_ext line_profiler\n", | |
"\n", | |
"def calc(count):\n", | |
" ret = []\n", | |
" for i in range(count):\n", | |
" ans = math.sqrt(i)\n", | |
" ret.append(ans)\n", | |
" return ret\n", | |
"\n", | |
"count = 1000000\n", | |
"%lprun -f calc calc(count)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 38, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The line_profiler extension is already loaded. To reload it, use:\n", | |
" %reload_ext line_profiler\n" | |
] | |
} | |
], | |
"source": [ | |
"# -*- coding: utf-8 -*-\n", | |
"import math\n", | |
"%load_ext line_profiler\n", | |
"\n", | |
"def calc2(count):\n", | |
" ret = [math.sqrt(i) for i in range(count)]\n", | |
" return ret\n", | |
"\n", | |
"count = 1000000\n", | |
"%lprun -f calc2 calc2(count)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 39, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"The line_profiler extension is already loaded. To reload it, use:\n", | |
" %reload_ext line_profiler\n" | |
] | |
} | |
], | |
"source": [ | |
"#pip install line_profiler\n", | |
"# -*- coding: utf-8 -*-\n", | |
"import time, requests\n", | |
"import numpy as np\n", | |
"import pandas as pd\n", | |
"from collections import OrderedDict\n", | |
"%load_ext line_profiler\n", | |
"\n", | |
"count = 1000\n", | |
"# 現在時刻(UTC)と取得開始時刻\n", | |
"to_time = int(time.time())\n", | |
"from_time = to_time - count * 60\n", | |
"# OHLCVデータ取得\n", | |
"param = {\"period\": 1, \"from\": from_time, \"to\": to_time}\n", | |
"url = \"https://www.bitmex.com/api/udf/history?symbol=XBTUSD&resolution={period}&from={from}&to={to}\".format(**param)\n", | |
"res = requests.get(url)\n", | |
"data = res.json()\n", | |
"ohlcv = OrderedDict({\"open\":data[\"o\"], \"high\":data[\"h\"], \"low\":data[\"l\"], \"close\":data[\"c\"], \"volume\":data[\"v\"],})\n", | |
"df = pd.DataFrame(ohlcv)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 43, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>open</th>\n", | |
" <th>high</th>\n", | |
" <th>low</th>\n", | |
" <th>close</th>\n", | |
" <th>volume</th>\n", | |
" <th>hopen</th>\n", | |
" <th>hhigh</th>\n", | |
" <th>hlow</th>\n", | |
" <th>hclose</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>996</th>\n", | |
" <td>6263.5</td>\n", | |
" <td>6264.0</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>1136551</td>\n", | |
" <td>6266.26</td>\n", | |
" <td>6266.26</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.38</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>997</th>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>511296</td>\n", | |
" <td>6264.82</td>\n", | |
" <td>6264.82</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.12</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>998</th>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>491341</td>\n", | |
" <td>6263.97</td>\n", | |
" <td>6263.97</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.25</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>999</th>\n", | |
" <td>6263.5</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>520483</td>\n", | |
" <td>6263.61</td>\n", | |
" <td>6263.61</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.38</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1000</th>\n", | |
" <td>6263.5</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>6262.0</td>\n", | |
" <td>6262.0</td>\n", | |
" <td>602794</td>\n", | |
" <td>6263.49</td>\n", | |
" <td>6263.50</td>\n", | |
" <td>6262.0</td>\n", | |
" <td>6262.75</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" open high low close volume hopen hhigh hlow \\\n", | |
"996 6263.5 6264.0 6263.0 6263.0 1136551 6266.26 6266.26 6263.0 \n", | |
"997 6263.0 6263.5 6263.0 6263.0 511296 6264.82 6264.82 6263.0 \n", | |
"998 6263.0 6263.5 6263.0 6263.5 491341 6263.97 6263.97 6263.0 \n", | |
"999 6263.5 6263.5 6263.0 6263.5 520483 6263.61 6263.61 6263.0 \n", | |
"1000 6263.5 6263.5 6262.0 6262.0 602794 6263.49 6263.50 6262.0 \n", | |
"\n", | |
" hclose \n", | |
"996 6263.38 \n", | |
"997 6263.12 \n", | |
"998 6263.25 \n", | |
"999 6263.38 \n", | |
"1000 6262.75 " | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"def analyze_heikinashi(df):\n", | |
" # 始値 = (一本前の始値(平均足) + 一本前の終値(平均足)) / 2\n", | |
" # 終値 = (始値(ローソク足) + 高値(ローソク足) + 安値(ローソク足) + 終値(ローソク足)) / 4\n", | |
" o,h,l,c,v,ho,hh,hl,hc = range(9)\n", | |
" df[\"hopen\"] = float(0.0)\n", | |
" df[\"hhigh\"] = float(0.0)\n", | |
" df[\"hlow\"] = float(0.0)\n", | |
" df[\"hclose\"] = float(0.0)\n", | |
"\n", | |
" df.iloc[0, ho] = df.iloc[0, o]\n", | |
" df.iloc[0, hh] = df.iloc[0, h]\n", | |
" df.iloc[0, hl] = df.iloc[0, l]\n", | |
" df.iloc[0, hc] = df.iloc[0, c]\n", | |
"\n", | |
" df.iloc[1, ho] = df.iloc[0, o:v].mean()\n", | |
" df.iloc[1, hc] = df.iloc[1, o:v].mean()\n", | |
" df.iloc[1, hh] = df.iloc[1, h]\n", | |
" df.iloc[1, hl] = df.iloc[1, l]\n", | |
"\n", | |
" for i, ohlcv in df.iloc[2:].iterrows():\n", | |
" df.iloc[i, ho] = (df.iloc[i-1, ho] + df.iloc[i-1, hc]) / 2.0\n", | |
" df.iloc[i, hc] = ohlcv[o:v].mean()\n", | |
" high_low = [df.iloc[i, ho], df.iloc[i, h], df.iloc[i, l], df.iloc[i, hc]]\n", | |
" df.iloc[i, hh] = max(high_low)\n", | |
" df.iloc[i, hl] = min(high_low)\n", | |
" df.iloc[:, ho:] = df.iloc[:, ho:].round(2)\n", | |
"\n", | |
"%lprun -f analyze_heikinashi analyze_heikinashi(df)\n", | |
"display(df.tail())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 44, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/html": [ | |
"<div>\n", | |
"<style scoped>\n", | |
" .dataframe tbody tr th:only-of-type {\n", | |
" vertical-align: middle;\n", | |
" }\n", | |
"\n", | |
" .dataframe tbody tr th {\n", | |
" vertical-align: top;\n", | |
" }\n", | |
"\n", | |
" .dataframe thead th {\n", | |
" text-align: right;\n", | |
" }\n", | |
"</style>\n", | |
"<table border=\"1\" class=\"dataframe\">\n", | |
" <thead>\n", | |
" <tr style=\"text-align: right;\">\n", | |
" <th></th>\n", | |
" <th>open</th>\n", | |
" <th>high</th>\n", | |
" <th>low</th>\n", | |
" <th>close</th>\n", | |
" <th>volume</th>\n", | |
" <th>hopen</th>\n", | |
" <th>hhigh</th>\n", | |
" <th>hlow</th>\n", | |
" <th>hclose</th>\n", | |
" </tr>\n", | |
" </thead>\n", | |
" <tbody>\n", | |
" <tr>\n", | |
" <th>996</th>\n", | |
" <td>6263.5</td>\n", | |
" <td>6264.0</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>1136551</td>\n", | |
" <td>6266.26</td>\n", | |
" <td>6266.26</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.38</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>997</th>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>511296</td>\n", | |
" <td>6264.82</td>\n", | |
" <td>6264.82</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.12</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>998</th>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>491341</td>\n", | |
" <td>6263.97</td>\n", | |
" <td>6263.97</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.25</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>999</th>\n", | |
" <td>6263.5</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>520483</td>\n", | |
" <td>6263.61</td>\n", | |
" <td>6263.61</td>\n", | |
" <td>6263.0</td>\n", | |
" <td>6263.38</td>\n", | |
" </tr>\n", | |
" <tr>\n", | |
" <th>1000</th>\n", | |
" <td>6263.5</td>\n", | |
" <td>6263.5</td>\n", | |
" <td>6262.0</td>\n", | |
" <td>6262.0</td>\n", | |
" <td>602794</td>\n", | |
" <td>6263.49</td>\n", | |
" <td>6263.50</td>\n", | |
" <td>6262.0</td>\n", | |
" <td>6262.75</td>\n", | |
" </tr>\n", | |
" </tbody>\n", | |
"</table>\n", | |
"</div>" | |
], | |
"text/plain": [ | |
" open high low close volume hopen hhigh hlow \\\n", | |
"996 6263.5 6264.0 6263.0 6263.0 1136551 6266.26 6266.26 6263.0 \n", | |
"997 6263.0 6263.5 6263.0 6263.0 511296 6264.82 6264.82 6263.0 \n", | |
"998 6263.0 6263.5 6263.0 6263.5 491341 6263.97 6263.97 6263.0 \n", | |
"999 6263.5 6263.5 6263.0 6263.5 520483 6263.61 6263.61 6263.0 \n", | |
"1000 6263.5 6263.5 6262.0 6262.0 602794 6263.49 6263.50 6262.0 \n", | |
"\n", | |
" hclose \n", | |
"996 6263.38 \n", | |
"997 6263.12 \n", | |
"998 6263.25 \n", | |
"999 6263.38 \n", | |
"1000 6262.75 " | |
] | |
}, | |
"metadata": {}, | |
"output_type": "display_data" | |
} | |
], | |
"source": [ | |
"def analyze_heikinashi2(df):\n", | |
" o,h,l,c,v,ho,hh,hl,hc = range(9)\n", | |
" np_o = df.open.values\n", | |
" np_h = df.high.values\n", | |
" np_l = df.low.values\n", | |
" np_c = df.close.values\n", | |
" np_ho = np.zeros(df.shape[0], dtype=float)\n", | |
" np_hc = np.zeros(df.shape[0], dtype=float)\n", | |
"\n", | |
" np_ho[0] = df.iloc[0, o]\n", | |
" np_hc[0] = df.iloc[0, c]\n", | |
" np_ho[1] = (np_o[0] + np_h[0] + np_l[0] + np_c[0]) / 4.0\n", | |
" np_hc[1] = (np_o[1] + np_h[1] + np_l[1] + np_c[1]) / 4.0\n", | |
"\n", | |
" for i in range(2, df.shape[0]):\n", | |
" np_ho[i] = (np_ho[i-1] + np_hc[i-1]) / 2.0\n", | |
" np_hc[i] = (np_o[i] + np_h[i] + np_l[i] + np_c[i]) / 4.0\n", | |
" np_ho = np.round(np_ho, 2)\n", | |
" np_hc = np.round(np_hc, 2)\n", | |
" df[\"hopen\"] = pd.Series(np_ho)\n", | |
" df[\"hhigh\"] = pd.Series(max(ohlc) for ohlc in zip(np_ho, np_h, np_hc))\n", | |
" df[\"hlow\"] = pd.Series(min(ohlc) for ohlc in zip(np_ho, np_l, np_hc))\n", | |
" df[\"hclose\"] = pd.Series(np_hc)\n", | |
" return df\n", | |
"\n", | |
"%lprun -f analyze_heikinashi2 analyze_heikinashi2(df)\n", | |
"display(df.tail())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.6.2" | |
}, | |
"toc": { | |
"nav_menu": {}, | |
"number_sections": true, | |
"sideBar": true, | |
"skip_h1_title": false, | |
"title_cell": "Table of Contents", | |
"title_sidebar": "Contents", | |
"toc_cell": false, | |
"toc_position": {}, | |
"toc_section_display": true, | |
"toc_window_display": false | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment