安装 cufflinks
bash
pip install cufflinks
绘制K线图
python
import yfinance as yf
import cufflinks as cf
cf.set_config_file(offline=True, world_readable=True)
data = yf.download('QQQ', '2022-01-01', '2023-05-06')
qf = cf.QuantFig(data, title='QQQ', legend='top', name='QQQ')
qf.add_volume()
qf.add_ema(periods=20, column='Close', color='gray')
qf.add_trendline('2023-01-06','2023-03-13',on='low')
qf.add_resistance('2022-08-16',on='high',mode='toend')
qf.add_support('2022-10-13',on='low',mode='toend')
qf.iplot(up_color='green',down_color='red')
剔除周六周日
python
fig = qf.figure(up_color='green',down_color='red')
fig.update_xaxes(rangebreaks=[dict(bounds=["sat", "mon"])])
fig.show()
剔除所有非交易日
python
import datetime
def date_range(begin, end):
range_list = []
d = begin
delta = datetime.timedelta(days=1)
while d <= end:
range_list.append(d)
d += delta
return range_list
begin = datetime.date(2022,1,1)
end = datetime.datetime.now().date()
# 获取所有日期
dr = date_range(begin, end)
dr_str = list(map(lambda x: x.strftime("%Y-%m-%d"), dr))
# 获取所有交易日
data_str = list(map(lambda x: x.strftime("%Y-%m-%d"), data.index.tolist()))
# 获取所有非交易日
s = list(filter(lambda x: x not in data_str,dr_str))
fig = qf.figure(up_color='green',down_color='red')
fig.update_xaxes(
rangebreaks=[
dict(values=s) # 剔除所有非交易日
]
)
fig.show()