Skip to content

Instantly share code, notes, and snippets.

@va2577
Created September 17, 2018 23:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save va2577/66ca39f2a10caccd46a43a52f125f0a9 to your computer and use it in GitHub Desktop.
Save va2577/66ca39f2a10caccd46a43a52f125f0a9 to your computer and use it in GitHub Desktop.
Hammer (candlestick pattern)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Hammer (candlestick pattern)\n",
"\n",
"> The candle looks like a hammer, as it has a long lower wick and a short body at the top of the candlestick with little or no upper wick.\n",
">\n",
"> <cite>[Hammer (candlestick pattern) - Wikipedia](https://en.wikipedia.org/wiki/Hammer_(candlestick_pattern))</cite>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import pandas as pd"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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",
" </tr>\n",
" <tr>\n",
" <th>time</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2003-05-05</th>\n",
" <td>118.940</td>\n",
" <td>119.046</td>\n",
" <td>118.461</td>\n",
" <td>118.603</td>\n",
" <td>592866.9</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2003-05-06</th>\n",
" <td>118.591</td>\n",
" <td>118.751</td>\n",
" <td>117.290</td>\n",
" <td>117.500</td>\n",
" <td>581707.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2003-05-07</th>\n",
" <td>117.456</td>\n",
" <td>117.830</td>\n",
" <td>116.052</td>\n",
" <td>116.303</td>\n",
" <td>584496.2</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2003-05-08</th>\n",
" <td>116.311</td>\n",
" <td>116.969</td>\n",
" <td>115.940</td>\n",
" <td>116.823</td>\n",
" <td>588236.7</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2003-05-09</th>\n",
" <td>116.835</td>\n",
" <td>117.612</td>\n",
" <td>116.794</td>\n",
" <td>117.151</td>\n",
" <td>583132.9</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" open high low close volume\n",
"time \n",
"2003-05-05 118.940 119.046 118.461 118.603 592866.9\n",
"2003-05-06 118.591 118.751 117.290 117.500 581707.0\n",
"2003-05-07 117.456 117.830 116.052 116.303 584496.2\n",
"2003-05-08 116.311 116.969 115.940 116.823 588236.7\n",
"2003-05-09 116.835 117.612 116.794 117.151 583132.9"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dtype = { 'time': str, 'open': float, 'high': float, 'low': float, 'close': float, 'volume': float }\n",
"names = ['time', 'open', 'high', 'low', 'close', 'volume']\n",
"df = pd.read_csv('~/Documents/data/d/USDJPY_D.csv', dtype=dtype, header=0, index_col='time', names=names, parse_dates=['time'])\n",
"df.head()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"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",
" </tr>\n",
" <tr>\n",
" <th>time</th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>2003-05-12</th>\n",
" <td>117.286</td>\n",
" <td>117.286</td>\n",
" <td>116.304</td>\n",
" <td>117.025</td>\n",
" <td>586660.3</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2003-05-15</th>\n",
" <td>116.173</td>\n",
" <td>116.525</td>\n",
" <td>115.282</td>\n",
" <td>116.515</td>\n",
" <td>585585.1</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2006-01-12</th>\n",
" <td>114.140</td>\n",
" <td>114.430</td>\n",
" <td>113.410</td>\n",
" <td>114.390</td>\n",
" <td>4741040.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2006-07-04</th>\n",
" <td>114.665</td>\n",
" <td>114.790</td>\n",
" <td>114.370</td>\n",
" <td>114.780</td>\n",
" <td>3387976.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2006-07-13</th>\n",
" <td>115.500</td>\n",
" <td>115.520</td>\n",
" <td>114.985</td>\n",
" <td>115.385</td>\n",
" <td>3752077.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" open high low close volume\n",
"time \n",
"2003-05-12 117.286 117.286 116.304 117.025 586660.3\n",
"2003-05-15 116.173 116.525 115.282 116.515 585585.1\n",
"2006-01-12 114.140 114.430 113.410 114.390 4741040.0\n",
"2006-07-04 114.665 114.790 114.370 114.780 3387976.0\n",
"2006-07-13 115.500 115.520 114.985 115.385 3752077.0"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def hammer(df):\n",
" min_of_open_or_close = df.loc[:, ['open', 'close']].min(axis=1)\n",
" max_of_open_or_close = df.loc[:, ['open', 'close']].max(axis=1)\n",
" lower_wick = min_of_open_or_close - df['low']\n",
" upper_wick = df['high'] - max_of_open_or_close\n",
" body = max_of_open_or_close - min_of_open_or_close\n",
" trading_range = df['high'] - df['low']\n",
" return (lower_wick / body > 2.0) & (upper_wick / trading_range < 0.05)\n",
"\n",
"df[hammer(df)].head()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ハンマーの足の数: 31\n",
"全体の足の数: 3827\n",
"ハンマーの足の割合: 0.00810033969166449\n"
]
}
],
"source": [
"s = hammer(df)\n",
"print('ハンマーの足の数:', s[s == True].count())\n",
"print('全体の足の数:', s.count())\n",
"print('ハンマーの足の割合:', s[s == True].count() / s.count())"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.41935483870967744"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s21 = df['close'] - df['open']\n",
"s22 = s21[s.shift(1).fillna(False)]\n",
"s22[s22 > 0].count() / s22.count()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.018419354838708695"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s22.mean()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1.4646128939359697"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"s22[s22 > 0].mean() / s22[s22 < 0].abs().mean()"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 4.44 s, sys: 1.3 s, total: 5.73 s\n",
"Wall time: 6.62 s\n"
]
}
],
"source": [
"# 21通貨ペア\n",
"\n",
"data = [\n",
" 'AUDJPY',\n",
" 'AUDNZD',\n",
" 'AUDUSD',\n",
" 'CADJPY',\n",
" 'CHFJPY',\n",
" 'EURAUD',\n",
" 'EURGBP',\n",
" 'EURJPY',\n",
" 'EURUSD',\n",
" 'GBPAUD',\n",
" 'GBPJPY',\n",
" 'GBPUSD',\n",
" 'HKDJPY',\n",
" 'NZDJPY',\n",
" 'NZDUSD',\n",
" 'SGDJPY',\n",
" 'USDCAD',\n",
" 'USDCHF',\n",
" 'USDHKD',\n",
" 'USDJPY',\n",
" 'USDSGD'\n",
"]\n",
"\n",
"def read(filepath):\n",
" dtype = { 'time': str, 'open': float, 'high': float, 'low': float, 'close': float, 'volume': float }\n",
" names = ['time', 'open', 'high', 'low', 'close', 'volume']\n",
" df = pd.read_csv(filepath, dtype=dtype, header=0, index_col='time', names=names, parse_dates=['time'])\n",
" return df\n",
"\n",
"%time d = { i: read(f'~/Documents/data/h/{i}_H.csv') for i in data }"
]
},
{
"cell_type": "code",
"execution_count": 9,
"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>ハンマーの足の数</th>\n",
" <th>全体の足の数</th>\n",
" <th>陽線の割合</th>\n",
" <th>始値から終値の平均</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>AUDJPY</th>\n",
" <td>1084.0</td>\n",
" <td>88226.0</td>\n",
" <td>0.483395</td>\n",
" <td>-0.123985</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AUDNZD</th>\n",
" <td>622.0</td>\n",
" <td>69240.0</td>\n",
" <td>0.405145</td>\n",
" <td>-1.956270</td>\n",
" </tr>\n",
" <tr>\n",
" <th>AUDUSD</th>\n",
" <td>1039.0</td>\n",
" <td>90266.0</td>\n",
" <td>0.484119</td>\n",
" <td>-0.724254</td>\n",
" </tr>\n",
" <tr>\n",
" <th>CADJPY</th>\n",
" <td>1343.0</td>\n",
" <td>82583.0</td>\n",
" <td>0.494415</td>\n",
" <td>-1.555547</td>\n",
" </tr>\n",
" <tr>\n",
" <th>CHFJPY</th>\n",
" <td>1042.0</td>\n",
" <td>90266.0</td>\n",
" <td>0.455854</td>\n",
" <td>-1.583877</td>\n",
" </tr>\n",
" <tr>\n",
" <th>EURAUD</th>\n",
" <td>909.0</td>\n",
" <td>76602.0</td>\n",
" <td>0.415842</td>\n",
" <td>-2.425743</td>\n",
" </tr>\n",
" <tr>\n",
" <th>EURGBP</th>\n",
" <td>1027.0</td>\n",
" <td>90266.0</td>\n",
" <td>0.440117</td>\n",
" <td>-0.766796</td>\n",
" </tr>\n",
" <tr>\n",
" <th>EURJPY</th>\n",
" <td>1238.0</td>\n",
" <td>90266.0</td>\n",
" <td>0.453150</td>\n",
" <td>-1.047981</td>\n",
" </tr>\n",
" <tr>\n",
" <th>EURUSD</th>\n",
" <td>1033.0</td>\n",
" <td>91826.0</td>\n",
" <td>0.465634</td>\n",
" <td>-0.520232</td>\n",
" </tr>\n",
" <tr>\n",
" <th>GBPAUD</th>\n",
" <td>1108.0</td>\n",
" <td>73776.0</td>\n",
" <td>0.443141</td>\n",
" <td>-2.696209</td>\n",
" </tr>\n",
" <tr>\n",
" <th>GBPJPY</th>\n",
" <td>1233.0</td>\n",
" <td>90266.0</td>\n",
" <td>0.443633</td>\n",
" <td>-2.535685</td>\n",
" </tr>\n",
" <tr>\n",
" <th>GBPUSD</th>\n",
" <td>1346.0</td>\n",
" <td>91826.0</td>\n",
" <td>0.461367</td>\n",
" <td>-1.678306</td>\n",
" </tr>\n",
" <tr>\n",
" <th>HKDJPY</th>\n",
" <td>1620.0</td>\n",
" <td>67675.0</td>\n",
" <td>0.385185</td>\n",
" <td>-0.226235</td>\n",
" </tr>\n",
" <tr>\n",
" <th>NZDJPY</th>\n",
" <td>1283.0</td>\n",
" <td>75122.0</td>\n",
" <td>0.481684</td>\n",
" <td>-0.353235</td>\n",
" </tr>\n",
" <tr>\n",
" <th>NZDUSD</th>\n",
" <td>1279.0</td>\n",
" <td>90266.0</td>\n",
" <td>0.447224</td>\n",
" <td>-0.981470</td>\n",
" </tr>\n",
" <tr>\n",
" <th>SGDJPY</th>\n",
" <td>1127.0</td>\n",
" <td>67675.0</td>\n",
" <td>0.469388</td>\n",
" <td>-0.996983</td>\n",
" </tr>\n",
" <tr>\n",
" <th>USDCAD</th>\n",
" <td>1499.0</td>\n",
" <td>90266.0</td>\n",
" <td>0.442962</td>\n",
" <td>-0.873516</td>\n",
" </tr>\n",
" <tr>\n",
" <th>USDCHF</th>\n",
" <td>1423.0</td>\n",
" <td>91826.0</td>\n",
" <td>0.432888</td>\n",
" <td>-1.977512</td>\n",
" </tr>\n",
" <tr>\n",
" <th>USDHKD</th>\n",
" <td>2566.0</td>\n",
" <td>67675.0</td>\n",
" <td>0.388932</td>\n",
" <td>-0.534684</td>\n",
" </tr>\n",
" <tr>\n",
" <th>USDJPY</th>\n",
" <td>1429.0</td>\n",
" <td>91826.0</td>\n",
" <td>0.456963</td>\n",
" <td>-0.568369</td>\n",
" </tr>\n",
" <tr>\n",
" <th>USDSGD</th>\n",
" <td>1365.0</td>\n",
" <td>82182.0</td>\n",
" <td>0.419048</td>\n",
" <td>-1.299267</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" ハンマーの足の数 全体の足の数 陽線の割合 始値から終値の平均\n",
"AUDJPY 1084.0 88226.0 0.483395 -0.123985\n",
"AUDNZD 622.0 69240.0 0.405145 -1.956270\n",
"AUDUSD 1039.0 90266.0 0.484119 -0.724254\n",
"CADJPY 1343.0 82583.0 0.494415 -1.555547\n",
"CHFJPY 1042.0 90266.0 0.455854 -1.583877\n",
"EURAUD 909.0 76602.0 0.415842 -2.425743\n",
"EURGBP 1027.0 90266.0 0.440117 -0.766796\n",
"EURJPY 1238.0 90266.0 0.453150 -1.047981\n",
"EURUSD 1033.0 91826.0 0.465634 -0.520232\n",
"GBPAUD 1108.0 73776.0 0.443141 -2.696209\n",
"GBPJPY 1233.0 90266.0 0.443633 -2.535685\n",
"GBPUSD 1346.0 91826.0 0.461367 -1.678306\n",
"HKDJPY 1620.0 67675.0 0.385185 -0.226235\n",
"NZDJPY 1283.0 75122.0 0.481684 -0.353235\n",
"NZDUSD 1279.0 90266.0 0.447224 -0.981470\n",
"SGDJPY 1127.0 67675.0 0.469388 -0.996983\n",
"USDCAD 1499.0 90266.0 0.442962 -0.873516\n",
"USDCHF 1423.0 91826.0 0.432888 -1.977512\n",
"USDHKD 2566.0 67675.0 0.388932 -0.534684\n",
"USDJPY 1429.0 91826.0 0.456963 -0.568369\n",
"USDSGD 1365.0 82182.0 0.419048 -1.299267"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def test(df, pair):\n",
" s = hammer(df)\n",
" s2 = (df['close'] - df['open']) / (0.01 if 'JPY' in pair else 0.0001)\n",
" s3 = s2[s.shift(1).fillna(False)]\n",
" return (\n",
" s[s == True].count(),\n",
" s.count(),\n",
" s3[s3 > 0].count() / s3.count(),\n",
" s3.mean()\n",
" )\n",
"\n",
"columns = [\n",
" 'ハンマーの足の数',\n",
" '全体の足の数',\n",
" '陽線の割合',\n",
" '始値から終値の平均'\n",
"]\n",
"pd.DataFrame({ i: test(d[i], i) for i in data }, index=columns).T"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ハンマーの足の数 1267.380952\n",
"全体の足の数 83329.619048\n",
"陽線の割合 0.446195\n",
"始値から終値の平均 -1.210769\n",
"dtype: float64"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame({ i: test(d[i], i) for i in data }, index=columns).T.mean()"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ハンマーの足の数 1267.380952\n",
"全体の足の数 83329.619048\n",
"陽線の割合 0.479826\n",
"始値から終値の平均 -1.032897\n",
"dtype: float64"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# n本先の足の終値-ハンマーの次の足の始値\n",
"def test2(df, pair, periods=0):\n",
" s = hammer(df)\n",
" df2 = df.shift(periods * -1)\n",
" s2 = (df2['close'] - df['open']) / (0.01 if 'JPY' in pair else 0.0001)\n",
" s3 = s2[s.shift(1).fillna(False)]\n",
" return (\n",
" s[s == True].count(),\n",
" s.count(),\n",
" s3[s3 > 0].count() / s3.count(),\n",
" s3.mean()\n",
" )\n",
"\n",
"pd.DataFrame({ i: test2(d[i], i, periods=5) for i in data }, index=columns).T.mean()"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ハンマーの足の数 1267.380952\n",
"全体の足の数 83329.619048\n",
"陽線の割合 0.486039\n",
"始値から終値の平均 -1.123826\n",
"dtype: float64"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame({ i: test2(d[i], i, periods=10) for i in data }, index=columns).T.mean()"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ハンマーの足の数 1267.380952\n",
"全体の足の数 83329.619048\n",
"陽線の割合 0.498421\n",
"始値から終値の平均 -0.808150\n",
"dtype: float64"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame({ i: test2(d[i], i, periods=20) for i in data }, index=columns).T.mean()"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ハンマーの足の数 1267.380952\n",
"全体の足の数 83329.619048\n",
"陽線の割合 0.504215\n",
"始値から終値の平均 -0.067837\n",
"dtype: float64"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame({ i: test2(d[i], i, periods=50) for i in data }, index=columns).T.mean()"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ハンマーの足の数 1267.380952\n",
"全体の足の数 83329.619048\n",
"陽線の割合 0.504479\n",
"始値から終値の平均 -1.044284\n",
"dtype: float64"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"pd.DataFrame({ i: test2(d[i], i, periods=100) for i in data }, index=columns).T.mean()"
]
}
],
"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.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment