Exemplo n.º 1
0
def get_zh_info():
    ts.set_token(config.get('tushare', 'token'))
    pro = ts.pro_api()

    df = pro.stock_basic()
    df.rename(columns={'symbol': 'code'}, inplace=True)

    table = 'zh_stocks_info'
    columns = [
        'code', 'ts_code', 'name', 'industry', 'area', 'market', 'list_date'
    ]
    df = df[columns]
    df = df[df['code'].str.isdigit()]
    mydb.upsert_table(table, columns, df)

    df, msg = swindex.get_index_list()
    df1 = df.loc[df['section_name'] == "一级行业"]
    ind1_list = df1.index_code

    sw_columns = ['code', 'name', 'sw_ind1', 'sw_ind1_weight']
    j = 0
    for i in ind1_list:
        j = j + 1
        print('processing [' + str(j) + '/' + str(ind1_list.size) + '] : ' + i)
        stock_data, msg = swindex.get_index_cons(i)
        stock_data.rename(columns={
            'stock_code': 'code',
            'stock_name': 'name',
            'weight': 'sw_ind1_weight'
        },
                          inplace=True)
        stock_data.loc[:, 'sw_ind1'] = df1.query(
            'index_code == "{}"'.format(i)).index_name.values[0]
        stock_data = stock_data[sw_columns]
        upsert_table(table, sw_columns, stock_data)
Exemplo n.º 2
0
 def get_sw_weight(self, code):
     '''
     获取申万指数的权重
     :param code: sw code
     :return: df
     '''
     df, msg = swindex.get_index_cons(code)
Exemplo n.º 3
0
def dump_components(index_file, sleeptime=0.0, stop=100):
    '''
    dump all components of indices
    :param index_file:
    :param sleeptime:
    :param stop:
    :return:
    '''
    class1_list = get_class1_info(index_file)
    count = 0
    cnt_comp = 0
    for class1 in class1_list:
        a_item = class1

        code = a_item[1]
        df_comp, msg = swindex.get_index_cons(str(code))
        leng_df = len(df_comp.index)
        cnt_comp += leng_df
        print(
            f'current{code} has components of {leng_df}/{cnt_comp}(in total)')
        fn = str(a_item[1]) + f'_components.csv'
        df_comp.to_csv("data/comp/" + fn)
        count += 1
        if count > stop:
            break
        if sleeptime > 0.0:
            sleep(sleeptime)
Exemplo n.º 4
0
 def _sw_weight(self):
     res = pd.DataFrame()
     #   获取申万一级行业成份股
     for index_code, index_name in INDUSTRY_SW.items():
         temp = swindex.get_index_cons(index_code)[0]
         temp.rename(columns={'index_code': 'code', 'index_name': 'name'}, inplace=True)
         temp['index_code'] = index_code + '.SI'
         temp['index_name'] = index_name
         res = res.append(temp, ignore_index=True)
     res['code'] = [i + '.SH' if i[0] == '6' else i + '.SZ' for i in res['code']]
     res['update_time'] = self.today
     return res
Exemplo n.º 5
0
from opendatatools import swindex

if __name__ == '__main__':
    #df, msg = swindex.get_index_list()
    #print(df)

    #df, msg = swindex.get_index_cons('801030')
    #print(df)

    df, msg = swindex.get_index_daily('801003', '2000-01-01', '2018-11-02')
    df, msg = swindex.get_index_cons('801060')
    print(df)

# freq 'D', 'W', 'M'
#df, msg = swindex.get_index_dailyindicator('801040', '2000-01-01', '2018-11-02', 'W')
#print(df)
Exemplo n.º 6
0
def swindex_get_index_cons():
    df, msg = swindex.get_index_cons(index_code=request.form['index_code'])
    return df.to_json(orient='table')
Exemplo n.º 7
0
df.rename(columns={'symbol': 'code'}, inplace=True)

table = 'zh_stocks_info'
columns = [
    'code', 'ts_code', 'name', 'industry', 'area', 'market', 'list_date'
]
df = df[columns]
df = df[df['code'].str.isdigit()]
mydb.upsert_table(table, columns, df)

df, msg = swindex.get_index_list()
df1 = df.loc[df['section_name'] == "一级行业"]
ind1_list = df1.index_code

sw_columns = ['code', 'name', 'sw_ind1', 'sw_ind1_weight']
j = 0
for i in ind1_list:
    j = j + 1
    print('processing [' + str(j) + '/' + str(ind1_list.size) + '] : ' + i)
    stock_data, msg = swindex.get_index_cons(i)
    stock_data.rename(columns={
        'stock_code': 'code',
        'stock_name': 'name',
        'weight': 'sw_ind1_weight'
    },
                      inplace=True)
    stock_data.loc[:, 'sw_ind1'] = df1.query(
        'index_code == "{}"'.format(i)).index_name.values[0]
    stock_data = stock_data[sw_columns]
    upsert_table(table, sw_columns, stock_data)
Exemplo n.º 8
0
initial_guess = None


def to_date(date):
    return datetime.datetime.strptime(date, '%Y%m%d')


def to_time(date):
    return datetime.datetime.strftime(date, '%Y%m%d')


# -------------------------------------           ------------------------------------ #
# ------------------------------------- Constrain ------------------------------------ #
# -------------------------------------           ------------------------------------ #
category, category_msg = swindex.get_index_list()
content, content_msg = swindex.get_index_cons('801040')

# class TrackingErrorLimit:
#
# 	def __init__(self, upper_limit, hard):
# 		"""
# 		追踪误差约束。基准为优化时指定的基准。
#
# 		:param upper_limit: 追踪误差上界
# 		:param hard: 软约束/硬约束
# 		"""
# 		self.upper_limit = upper_limit
# 		self.hard = hard
# 		self.constraint = {'type': 'eq', 'fun': lambda x: sum(x) - 1}
#
# 	def goal(self, omega):