This code implements the following functionality: Get the yield of several industry indexes (index_list) for the past 20 trading days, and select the index with the highest yield. It mainly uses the functionality of the argmax function in numpy. All code is compiled and tested based on JoinQuant.
import numpy as np
index_list = ['000912.XSHG', #Consumption
'000913.XSHG', #Medical
'000908.XSHG',
'000914.XSHG',
'000915.XSHG',
'000922.XSHG']
return_index = []
for index in index_list:
return_index_his = attribute_history(index, 20, unit='1d',
fields=['close'],skip_paused=True,
df=True, fq='pre')
return_index_his=return_index_his['close'].values
return_index.append(return_index_his[-1] / return_index_his[0] - 1)
sector = index_list[np.argmax(return_index)]