布林带
布林帶
中軌:移動平均線
上軌:中軌+2x5日收盤價標準差 (頂部的壓力)
下軌:中軌-2x5日收盤價標準差 (底部的支撐力)
布林帶收窄代表穩定的趨勢,布林帶張開代表有較大的波動空間的趨勢。
繪制5日均線的布林帶
?
weights = np.exp(np.linspace(-1, 0, 5)) weights /= weights.sum() em5 = np.convolve(closing_prices, weights[::-1], 'valid') stds = np.zeros(em5.size) for i in range(stds.size):stds[i] = closing_prices[i:i + 5].std() stds *= 2 lowers = medios - stds uppers = medios + stdsmp.plot(dates, closing_prices, c='lightgray', label='Closing Price') mp.plot(dates[4:], medios, c='dodgerblue', label='Medio') mp.plot(dates[4:], lowers, c='limegreen', label='Lower') mp.plot(dates[4:], uppers, c='orangered', label='Upper')?
# 繪制布林帶 import numpy as np import matplotlib.pyplot as mp import datetime as dt import matplotlib.dates as mddef dmy2ymd(dmy):"""把日月年轉年月日:param day::return:"""dmy = str(dmy, encoding='utf-8')t = dt.datetime.strptime(dmy, '%d-%m-%Y')s = t.date().strftime('%Y-%m-%d')return sdates, opening_prices, \ highest_prices, lowest_prices, \ closing_prices = \np.loadtxt('aapl.csv',delimiter=',',usecols=(1, 3, 4, 5, 6),unpack=True,dtype='M8[D],f8,f8,f8,f8',converters={1: dmy2ymd}) # 日月年轉年月日 print(dates) # 繪制收盤價的折現圖 mp.figure('APPL', facecolor='lightgray') mp.title('APPL', fontsize=18) mp.xlabel('Date', fontsize=14) mp.ylabel('Price', fontsize=14) mp.grid(linestyle=":")# 設置刻度定位器 # 每周一一個主刻度,一天一個次刻度 ax = mp.gca() ma_loc = md.WeekdayLocator(byweekday=md.MO) ax.xaxis.set_major_locator(ma_loc) ax.xaxis.set_major_formatter(md.DateFormatter('%Y-%m-%d')) ax.xaxis.set_minor_locator(md.DayLocator()) # 修改dates的dtype為md.datetime.datetiem dates = dates.astype(md.datetime.datetime) mp.plot(dates, closing_prices,color='dodgerblue',linewidth=2,linestyle='--',alpha=0.8,label='APPL Closing Price')#基礎卷積實現5日加權平均線 #尋找一組卷積核 kernel = np.exp(np.linspace(-1,0,5)) #卷積核中所有元素之和=1 kernel/=kernel.sum() print(kernel) ema53 = np.convolve(closing_prices,kernel[::-1],'valid') mp.plot(dates[4:],ema53,color='red',label='EMA-53')#最近5日標準差數組 stds = np.zeros(ema53.size) for i in range(stds.size):stds[i] = closing_prices[i:i+5].std() #計算上軌和下軌 upper = ema53 + 2*stds lower = ema53 - 2*stds mp.plot(dates[4:],upper,color='orangered',label='Upper') mp.plot(dates[4:],lower,color='orangered',label='Lower') #填充 mp.fill_between(dates[4:],upper,lower,upper>lower,color='orangered',alpha=0.1)mp.legend() mp.gcf().autofmt_xdate() mp.show()?
轉載于:https://www.cnblogs.com/maplethefox/p/11460480.html
總結
- 上一篇: KubeSphere DevOps流水线
- 下一篇: Centos7 二进制安装 Kubern