生活随笔
收集整理的這篇文章主要介紹了
python matplotlib 绘制布林带
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
python matplotlib 繪制布林帶
csv文件下載地址:
鏈接:https://pan.baidu.com/s/1sDe3h0jBliT5QAWGwNK8kg
提取碼:tsdt
"""繪制布林帶
"""import numpy
as np
import datetime
as dt
import matplotlib
.pyplot
as mp
import matplotlib
.dates
as md
def dmy2ymd(dmy
):dmy
= str(dmy
, encoding
='utf-8')time
= dt
.datetime
.strptime
(dmy
, '%d-%m-%Y').date
()t
= time
.strftime
('%Y-%m-%d')return t
dates
, open_price
, max_price
, min_price
, close_prices
, volumes
= np
.loadtxt
(r".\aapl.csv",delimiter
=",",usecols
=(1, 3, 4, 5, 6, 7),unpack
=True,dtype
="M8[D], f8, f8, f8, f8, f8",converters
={1: dmy2ymd
})print(open_price
)
mp
.figure
("Apple K Line", facecolor
="lightgray")
mp
.title
("Apple K Line", fontsize
=16)
mp
.xlabel
("Data", fontsize
=14)
mp
.ylabel
("Price", fontsize
=14)
ax
= mp
.gca
()
ax
.xaxis
.set_major_locator
(md
.WeekdayLocator
(byweekday
=md
.MO
))
ax
.xaxis
.set_major_formatter
(md
.DateFormatter
('%Y-%m-%d'))
ax
.xaxis
.set_minor_locator
(md
.DayLocator
())mp
.tick_params
(labelsize
=8)
mp
.grid
(linestyle
=":")
dates
= dates
.astype
(md
.datetime
.datetime
)
mp
.plot
(dates
, close_prices
, color
="r", linestyle
="--", linewidth
=2, label
="close",alpha
=0.3)
x
= np
.linspace
(-1, 0, 5)
weights
= np
.exp
(x
)[::-1]
kernel
= weights
/ weights
.sum()
ma_53
= np
.convolve
(close_prices
, kernel
, "valid")
mp
.plot
(dates
[4:], ma_53
, color
="orangered", linewidth
=2, label
="MA-53")
stds
= np
.zeros_like
(ma_53
)
for i
in range(stds
.size
):stds
[i
] = close_prices
[i
:i
+ 5].std
()
stds
*= 2
lowers
= ma_53
- stds
uppers
= ma_53
+ stds
mp
.plot
(dates
[4:], uppers
, color
="red", linewidth
=2, label
="Upper")
mp
.plot
(dates
[4:], lowers
, color
="green", linewidth
=2, label
="Lower")mp
.fill_between
(dates
[4:], lowers
, uppers
, lowers
< uppers
, color
="lightgray", alpha
=0.3)mp
.legend
()
mp
.gcf
().autofmt_xdate
()
mp
.show
()
總結
以上是生活随笔為你收集整理的python matplotlib 绘制布林带的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。