一、环境
pip install pandas
pip install mootdx
二、代码
#行业业绩轮动公众号首发
from mootdx.quotes import Quotes
import pandas as pd
client = Quotes.factory(market='std')
# 获取实时行情
df = client.bars(symbol='600519', frequency=9, offset=15) #获取最近15日茅台k线
df['收盘涨幅'] = 0
df['最高涨幅'] = 0
df['最低涨幅'] = 0
print(df)
# 计算简单移动平均线
# data['MA5'] = data['close'].rolling(window=5).mean()
# data['MA10'] = data['close'].rolling(window=10).mean()
# print(data[['close', 'MA5', 'MA10']])
last_close = 0
def cal_zhangfu(close1=1, close2=1):
zhangfu = float(close1)/float(close2)-1
zhangfu = zhangfu*100
zhangfu = round(zhangfu,2)
zhangfu = str(zhangfu) + '%'
#print("-cal-",close1,close2,zhangfu)
return zhangfu
for id in range(len(df)):
if 0 == last_close:
last_close = df.iloc[id]['close'] #第1天获取收盘价跳过
continue
else:
copy = df.iloc[id]
copy['收盘涨幅'] = cal_zhangfu(df.iloc[id]['close'],last_close)
copy['最高涨幅'] = cal_zhangfu(df.iloc[id]['high'],last_close)
copy['最低涨幅'] = cal_zhangfu(df.iloc[id]['low'],last_close)
#print(df.iloc[id]['datetime'], df.iloc[id]['收盘涨幅'], df.iloc[id]['最高涨幅'], df.iloc[id]['最低涨幅'])
df.iloc[id] = copy
last_close = df.iloc[id]['close']
print(df)
三、运行运行结果 9.26 茅台大涨9%+
标签:last,为例,python,df,zhangfu,iloc,close,涨跌幅,id From: https://blog.csdn.net/weixin_42661541/article/details/142875520