本記事では、下図のようなスーパートレンドのIndicatorを作成する雛形コードを載せました。株の売買判定をする指標のひとつであるATR(Average True Range)を計算して、チャートに図示します。

必要なのは、株価に関する時系列データで日付と高値、安値、終値です(下図)。

ちなみに、上記のようなチャート情報は、証券会社に口座開設すれば、比較的容易に入手可能です。例えば、楽天証券の場合は下記リンク先を参考に入手できます。
hk29.hatenablog.jp
本コードを実行すると、スーパートレンドとそれを図示するための計算を行い、下図のように列に追記します。

■本プログラム
import os
import pandas as pd
import numpy as np
import datetime
now = datetime.datetime.now()
file_path = '8306_三菱UFJフィナンシャルG.csv'
file_name, _ = os.path.splitext(os.path.basename(file_path))
df1 = pd.read_csv(file_path)
print(file_name)
df1
df2 = df1.drop(['時刻'], axis=1)
df2
target_column_list = ['日付', '始値', '高値', '安値', '終値', '出来高']
rename_column_list = ['Time', 'Open', 'High', 'Low', 'Close', 'Trading_Volume']
df3 = df2.rename(columns=dict(zip(target_column_list, rename_column_list)))
df3
df3['Time'] = pd.to_datetime(df3['Time'])
df3
from dateutil import relativedelta
my_time = now + relativedelta.relativedelta(years = -3)
print(my_time.year, my_time.month, my_time.day)
DF = df3.copy()
DF = DF[DF['Time'] > datetime.datetime(my_time.year, my_time.month, my_time.day)]
DF.reset_index(drop = True, inplace = True)
DF
DF.set_index('Time', inplace=True)
DF
atr_period = 10
atr_multiplier = 2
high = DF['High']
low = DF['Low']
close = DF['Close']
price_diffs = [high - low,
high - close.shift(),
close.shift() - low]
true_range = pd.concat(price_diffs, axis=1)
true_range = true_range.abs().max(axis=1)
atr = true_range.ewm(alpha = 1 / atr_period, min_periods = atr_period).mean()
final_upperband = upperband = (high + low) / 2 + (atr_multiplier * atr)
final_lowerband = lowerband = (high + low) / 2 - (atr_multiplier * atr)
print(final_upperband, final_lowerband)
supertrend = [True] * len(DF)
for i in range(1, len(DF.index)):
curr, prev = i, i-1
if close[curr] > final_upperband[prev]:
supertrend[curr] = True
elif close[curr] < final_lowerband[prev]:
supertrend[curr] = False
else:
supertrend[curr] = supertrend[prev]
if supertrend[curr] == True and final_lowerband[curr] < final_lowerband[prev]:
final_lowerband[curr] = final_lowerband[prev]
if supertrend[curr] == False and final_upperband[curr] > final_upperband[prev]:
final_upperband[curr] = final_upperband[prev]
if supertrend[curr] == True:
final_upperband[curr] = np.nan
else:
final_lowerband[curr] = np.nan
df_buf = pd.DataFrame({
'Supertrend': supertrend,
'Final Lowerband': final_lowerband,
'Final Upperband': final_upperband
}, index=DF.index)
DF = DF.join(df_buf)
DF
import matplotlib.pyplot as plt
from matplotlib import dates as mdates
import japanize_matplotlib
plt.rcParams['font.size'] = 16
fig = plt.figure(figsize = (10, 6))
ax = fig.add_subplot(111)
plt.plot(DF.index, DF['Close'], c = 'k', label='Close Price')
plt.plot(DF.index, DF['Final Lowerband'], c = 'lime', label = 'BUY')
plt.plot(DF.index, DF['Final Upperband'], c = 'red', label = 'SELL')
ax.fill_between(DF.index, DF['Close'], DF['Final Lowerband'], facecolor='lime', alpha=0.3)
ax.fill_between(DF.index, DF['Close'], DF['Final Upperband'], facecolor='red', alpha=0.3)
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%b-%Y'))
plt.gca().xaxis.set_minor_locator(mdates.MonthLocator(interval = 1))
plt.gca().xaxis.set_major_locator(mdates.MonthLocator(interval = 3))
plt.gcf().autofmt_xdate()
plt.ylabel('株価 [¥]')
plt.title(file_name)
plt.legend(bbox_to_anchor = (1.28, 0.85))
plt.grid()
plt.show()
以上
<広告>
リンク
リンク