# -*- coding: utf-8 -*-
from datetime import datetime
import numpy as np
import pandas as pd
from tools import Tools as util
periods = {"1h" : 1, "4h" : 4, "1d" : 24} # 取得したい時間足
ema_terms = [50, 100, 150, 200] # EMA期間リスト
sma_terms = [100, 200, 365] # SMA期間リスト
# 結果出力に必要な列リスト
columns = ["timestamp"]
for term in ema_terms:
columns.append("EMA" + str(term))
for term in sma_terms:
columns.append("SMA" + str(term))
dict_df_ohlcv = {}
for period in periods.keys():
# OHLCV取得
df = util.get_ohlcv_df(period=period, count=2200, reverse=False, partial=False)
# UnixTimeから日付変換してindexに設定
df["datetime"] = pd.to_datetime(df["timestamp"], unit="s")
df = df.set_index("datetime")
df.index = df.index.tz_localize("UTC")
df.index = df.index.tz_convert("Asia/Tokyo")
# EMA計算
for term in ema_terms:
df["EMA" + str(term)] = df["close"].ewm(span=term).mean()
# SMA計算
for term in sma_terms:
df["SMA" + str(term)] = df["close"].rolling(window=term, min_periods=1).mean()
# 必要な列だけにしてdict格納
dict_df_ohlcv[period] = df[columns]
# データ表示
display(period, dict_df_ohlcv[period].tail())
'1h'
timestamp | EMA50 | EMA100 | EMA150 | EMA200 | SMA100 | SMA200 | SMA365 | |
---|---|---|---|---|---|---|---|---|
datetime | ||||||||
2018-10-05 21:00:00+09:00 | 1538740800 | 6533.423291 | 6533.612891 | 6537.183763 | 6538.830897 | 6526.240 | 6555.8575 | 6564.472603 |
2018-10-05 22:00:00+09:00 | 1538744400 | 6533.798848 | 6533.798774 | 6537.260800 | 6538.872381 | 6525.645 | 6556.2000 | 6564.857534 |
2018-10-05 23:00:00+09:00 | 1538748000 | 6534.316540 | 6534.060185 | 6537.389796 | 6538.953253 | 6525.180 | 6556.6325 | 6565.300000 |
2018-10-06 00:00:00+09:00 | 1538751600 | 6535.049225 | 6534.435231 | 6537.596553 | 6539.093021 | 6525.060 | 6557.0575 | 6565.754795 |
2018-10-06 01:00:00+09:00 | 1538755200 | 6535.753177 | 6534.802850 | 6537.800573 | 6539.231399 | 6524.850 | 6557.5550 | 6566.154795 |
'4h'
timestamp | EMA50 | EMA100 | EMA150 | EMA200 | SMA100 | SMA200 | SMA365 | |
---|---|---|---|---|---|---|---|---|
datetime | ||||||||
2018-10-05 09:00:00+09:00 | 1538697600 | 6538.847681 | 6541.722802 | 6554.309922 | 6574.840917 | 6541.525 | 6570.9050 | 6577.291781 |
2018-10-05 13:00:00+09:00 | 1538712000 | 6538.873262 | 6541.678786 | 6554.113764 | 6574.489266 | 6544.120 | 6567.3150 | 6576.208219 |
2018-10-05 17:00:00+09:00 | 1538726400 | 6538.878232 | 6541.625741 | 6553.913582 | 6574.136139 | 6545.980 | 6564.0275 | 6574.886301 |
2018-10-05 21:00:00+09:00 | 1538740800 | 6539.059478 | 6541.662855 | 6553.775654 | 6573.831301 | 6548.375 | 6560.8000 | 6573.632877 |
2018-10-06 01:00:00+09:00 | 1538755200 | 6539.606165 | 6541.887353 | 6553.765380 | 6573.624025 | 6550.620 | 6557.4250 | 6572.372603 |
'1d'
timestamp | EMA50 | EMA100 | EMA150 | EMA200 | SMA100 | SMA200 | SMA365 | |
---|---|---|---|---|---|---|---|---|
datetime | ||||||||
2018-10-01 09:00:00+09:00 | 1538352000 | 6646.604410 | 6878.154839 | 7140.903685 | 7308.594834 | 6744.185 | 7341.1125 | 8608.277534 |
2018-10-02 09:00:00+09:00 | 1538438400 | 6643.639531 | 6872.072565 | 7133.355289 | 7301.255463 | 6748.245 | 7332.7150 | 8614.272603 |
2018-10-03 09:00:00+09:00 | 1538524800 | 6638.104647 | 6864.754296 | 7124.999586 | 7293.307520 | 6751.770 | 7323.9750 | 8620.065753 |
2018-10-04 09:00:00+09:00 | 1538611200 | 6631.316230 | 6856.838370 | 7116.257866 | 7285.065523 | 6753.905 | 7317.0550 | 8625.947123 |
2018-10-05 09:00:00+09:00 | 1538697600 | 6628.245005 | 6850.821768 | 7108.797494 | 7277.781174 | 6758.695 | 7308.8750 | 8632.357260 |
# -*- coding: utf-8 -*-
from datetime import datetime
import numpy as np
import pandas as pd
from tools import Tools as util
from collections import OrderedDict
periods = ["1h", "4h", "1d"] # 取得したい時間足
ema_terms = [50, 100, 150, 200] # EMA期間リスト
sma_terms = [100, 200, 365] # SMA期間リスト
lst_output = [] # 出力する結果格納リスト
for period in periods:
# OHLCV取得
df = util.get_ohlcv_df(period=period, count=2200, reverse=False, partial=False)
# EMA計算
for term in ema_terms:
ema = df["close"].ewm(span=term).mean()
lst_output.append([df.iloc[-1, 0], period, "EMA" + str(term), ema.iloc[-1]])
# SMA計算
for term in sma_terms:
sma = df["close"].rolling(window=term, min_periods=1).mean()
lst_output.append([df.iloc[-1, 0], period, "SMA" + str(term), sma.iloc[-1]])
# DataFrame化
df_output = pd.DataFrame(lst_output, columns=["timestamp", "period", "indicator", "price"])
# データ表示
display(df_output)
timestamp | period | indicator | price | |
---|---|---|---|---|
0 | 1538755200 | 1h | EMA50 | 6535.753177 |
1 | 1538755200 | 1h | EMA100 | 6534.802850 |
2 | 1538755200 | 1h | EMA150 | 6537.800573 |
3 | 1538755200 | 1h | EMA200 | 6539.231399 |
4 | 1538755200 | 1h | SMA100 | 6524.850000 |
5 | 1538755200 | 1h | SMA200 | 6557.555000 |
6 | 1538755200 | 1h | SMA365 | 6566.154795 |
7 | 1538755200 | 4h | EMA50 | 6539.606165 |
8 | 1538755200 | 4h | EMA100 | 6541.887353 |
9 | 1538755200 | 4h | EMA150 | 6553.765380 |
10 | 1538755200 | 4h | EMA200 | 6573.624025 |
11 | 1538755200 | 4h | SMA100 | 6550.620000 |
12 | 1538755200 | 4h | SMA200 | 6557.425000 |
13 | 1538755200 | 4h | SMA365 | 6572.372603 |
14 | 1538697600 | 1d | EMA50 | 6628.245005 |
15 | 1538697600 | 1d | EMA100 | 6850.821768 |
16 | 1538697600 | 1d | EMA150 | 7108.797494 |
17 | 1538697600 | 1d | EMA200 | 7277.781174 |
18 | 1538697600 | 1d | SMA100 | 6758.695000 |
19 | 1538697600 | 1d | SMA200 | 7308.875000 |
20 | 1538697600 | 1d | SMA365 | 8632.357260 |
# -*- coding: utf-8 -*-
from datetime import datetime
import numpy as np
import pandas as pd
from tools import Tools as util
from collections import OrderedDict
periods = ["1h", "4h", "1d"] # 取得したい時間足
ema_terms = [50, 100, 150, 200] # EMA期間リスト
sma_terms = [100, 200, 365] # SMA期間リスト
lst_output = [] # 出力する結果格納リスト
# 結果出力に必要な列リスト
columns = ["timestamp", "period"]
for term in ema_terms:
columns.append("EMA" + str(term))
for term in sma_terms:
columns.append("SMA" + str(term))
for period in periods:
# OHLCV取得
df = util.get_ohlcv_df(period=period, count=2200, reverse=False, partial=False)
lst = [df.iloc[-1, 0], period]
# EMA計算
for term in ema_terms:
ema = df["close"].ewm(span=term).mean()
lst.append(ema.iloc[-1])
# SMA計算
for term in sma_terms:
sma = df["close"].rolling(window=term, min_periods=1).mean()
lst.append(sma.iloc[-1])
lst_output.append(lst)
# DataFrame化
df_output = pd.DataFrame(lst_output, columns=columns)
# データ表示
display(df_output)
timestamp | period | EMA50 | EMA100 | EMA150 | EMA200 | SMA100 | SMA200 | SMA365 | |
---|---|---|---|---|---|---|---|---|---|
0 | 1538755200 | 1h | 6535.753177 | 6534.802850 | 6537.800573 | 6539.231399 | 6524.850 | 6557.555 | 6566.154795 |
1 | 1538755200 | 4h | 6539.606165 | 6541.887353 | 6553.765380 | 6573.624025 | 6550.620 | 6557.425 | 6572.372603 |
2 | 1538697600 | 1d | 6628.245005 | 6850.821768 | 7108.797494 | 7277.781174 | 6758.695 | 7308.875 | 8632.357260 |