python积累1

2018-12-08  本文已影响0人  黄yy家的jby

摘要

1.读文件,设定轴

df_pb = pd.read_excel('指数行业估值跟踪.xlsx',sheetname='pb',skiprows= 3)
#从第三行开始读
df_pb.set_index(['Date'],inplace=True)

2. 时间索引

time_list = df_pb.index
df_pb.loc[time_list[0],'position']
df_pb.loc[pd.to_datetime('20100101'),'position']

3.更改调仓时间

由于设定的调仓有可能不是交易日,需要进行实际调整

reallocation_time = []
a = [b for b in time_list if b.month in [5,9,11]]
for i in range(1,len(a)):
    if a[i].month != a[i-1].month:
        reallocation_time.append(a[I])

4. 寻找离得最近的rollingtime

temp = [a for a in reallocation_time if a<= time_list[i]][-1]

5. 高于更高的加仓,低于更低的减仓

max_n = min_n = 0
for i in range(len(timelist)):
    n = int((np.average(df_index.loc[time_list[i]])/np.average(df_index.loc[temp])-1)/0.1) 
    #n表示变化是10%的多少倍
    if n > max_n:
        max_n = n
        result.loc[time_list[i],'signal'] = 1
    if n < min_n:
        min_n = n
        result.loc[time_list[i],'signal'] = -1

6.写入

with open('guoguo.jpg',encoding='utf-8') as f:
        f.write(content)
        f.close()

7.dataframe 是否存在多值索引

df2[df2['E'].isin(['two', 'four'])]
多值索引
上一篇下一篇

猜你喜欢

热点阅读