Win11 WSL2 Ubuntu Python yfinance Ⅱ
yfinance 相関係数豪ドル/円4時間足予測ファイル
ここまでで、dsc-4h_3.py からの変更点について説明しました。
引き続き、これらの変更点を加味して
yfinance 相関係数豪ドル/円4時間足予測ファイル
yfin_audjp_sou_4h.py
をまとめます。
ファイル内容は以下のようになります。
# ① #!/home/yamada/miniconda3/bin/python3 #coding: utf-8 import pandas as pd import numpy as np import matplotlib.pyplot as plt import datetime from mplfinance.original_flavor import candlestick_ohlc # ② def addBusinessDays_4h(current_date, business_days_to_add_h): # current_date : 処理する初日の日時 例) 2022-02-03 00:00:00 # business_days_to_add_h : 4時間ずつ延長する回数 cur_date8 = [] # 21/03/30 12~ cur_date4 = [] # 3/30 cur_date14 = [] # 30 12: man_f = 1 # 月曜フラグ 0=count / 1=nocount doyo_f = 0 # 土曜フラグ 0 or 1 current_date0 = current_date weekday0 = current_date0.weekday() if(weekday0 == 0): #月 man_f = 0 while business_days_to_add_h > 0: current_date += datetime.timedelta(hours=4) weekday = current_date.weekday() hour_doyo = current_date.hour if weekday == 6: # sunday = 6 continue if((weekday == 5) and (hour_doyo == 8)): #土 doyo_f = 1 if((weekday == 5) and (doyo_f == 1)): #土 continue if((weekday == 0) and (man_f == 1)): #月 man_f = 0 continue # 日はスキップ # 月は一回のみスキップ # 年/月/日 時刻として文字列にする # 例 21/03/30 12~ e22, cur_date8 e11 = current_date.strftime("%y/%m/%d %H") e12 = e11[6:]# 21/03 削除 e13 = e12.replace("/0", "") e14 = e13.replace("/", "")+ ":" e22 = e11.replace("/0", "/") e22 = e22 + "~" e33 = e22[3:] # 21/ 削除 cur_date8.append(e22) cur_date4.append(e33) business_days_to_add_h -= 1 return cur_date8, cur_date4, cur_date14 # ③ df = pd.read_csv( "/home/yamada/public_html/manep/yfin_audjp_4h.csv") # ④ Open = df["open"].values High = df["high"].values Low = df["low"].values Close = df["close"].values Date = df["Datetime"].values Idx = df.index Idx_30 = Idx[-30:] - Idx[-1] + 98 + 6 Idx_99 = Idx[-99:] - Idx[-99] Open_99 = Open[-99:] High_99 = High[-99:] Low_99 = Low[-99:] Close_99 = Close[-99:] Date_99 = Date[-99:] # ⑤ tstr = Date[-1][:19] # +00:00 削除 tdatetime = datetime.datetime.strptime(tstr, '%Y-%m-%d %H:%M:%S') lastday = addBusinessDays_4h(tdatetime, 6) xDate = [] xD = [] dayf = 0 for i, key in enumerate(Date_99): day00 = str(key)[11:19] if((dayf == 1) and (day00 == '00:00:00')): dayf = 0 else: # ⑥ if((dayf == 0) and (day00 == '00:00:00')): e4 = str(key)[4:10] e6 = e4.replace("-0", "/") e7 = e6.replace("-", "/") e8 = e7.lstrip("/") xDate.append(e8) xD.append(i) dayf = 1 # ラストの X 軸日付処理 # 実デ-タが存在し、かつ dayf == 0 の時は # ラストの日付には時刻を含めた X 軸日付を付与 lastday_l = lastday[2][5] print("lastdat_l", lastday_l) xDate.append(lastday_l) xD.append(i + 6) # dsc_4h-2.py より from sklearn import preprocessing n_30 = 30 sl_cl_t = [] for n in range(Idx[-29]): sl_cl_t = np.append(sl_cl_t, Close[n:n+n_30]).reshape(n+1,n_30) print("sl_cl_t") print(sl_cl_t) print(sl_cl_t.shape) sl_cl_24 = sl_cl_t[:,:24] print("sl_cl_24") print(sl_cl_24) kijyu = sl_cl_t[-1, 6:] print("基準値") print(kijyu) cor = np.corrcoef(kijyu, sl_cl_24) print("cor") print(cor) cor1 = cor[1:, 0] cor_max = np.amax(cor1) print('相関max=', cor_max) c_max_id = np.argmax(cor1) print('相関max_ID=', c_max_id) print("相関する30個のデータ") print(sl_cl_t[c_max_id]) # print("基準") # print(kijyu) # インスタンスの作成 sscaler = preprocessing.StandardScaler() x = sl_cl_t[c_max_id].reshape(-1,1) print(" 元データsl_cl_t[c_max_id]を1×30 次元に変換") print(x) y = sscaler.fit_transform(x) print("sl_cl_t[c_max_id]標準偏差値") print(y) # 基準 kijyu の標準偏差 s と平均値 m s = np.std(kijyu) print("標準偏差 s = ", s) m = np.mean(kijyu) print("平均値 m = ", m) w = y * s + m print("予測値 w") print(w) ohlc = zip( Idx_99, Open_99, High_99, Low_99, Close_99) fig = plt.figure( figsize=(8.34, 5.56)) ax = fig.add_subplot(1,1,1) ax.grid() candlestick_ohlc( ax, ohlc, width=0.5, alpha = 1, colorup='r', colordown='g') plt.xticks(xD, xDate) plt.title('AUS$ / JPY Correlation 4Hs Chart') plt.xlabel('Date') plt.ylabel('Yen') plt.plot(Idx_30,w) # 相関する30個 # plt.show() plt.savefig( '/home/yamada/public_html/manep-img/yfin_sou_4h.png') # データ保存 w_6 = np.round(w[-6:], 3) # 3桁まで表示 # print(w_6) cor_max100 = np.round(cor_max * 100, 3) w_6 = np.append(w_6, cor_max100) # print(w_6) w_6 = pd.DataFrame(w_6) print(w_6) lastday_me = np.append(lastday[0], '信頼度(%)') lastday_pd = pd.DataFrame(lastday_me) print(lastday_pd) df_concat = pd.concat([lastday_pd, w_6], axis = 1) print(df_concat) # ⑦ df_concat.to_csv( '/home/yamada/public_html/manep-img/yfin_sou_4h.csv', header=False, index=False) # yamada@yama:~$ conda activate # (base) yamada@yama:~$ python3 public_html/yfin_audjp_sou_4h.py
これで相関係数豪ドル/円4時間足予測ファイルⅡができました。
引き続きこのファイルを保存します。