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 dump_sw_class1_list(file):
    '''
    Get the list of SW class1 list and save to file
    :param file: the file containing indices that will be saved
    :return:
    '''
    df, msg = swindex.get_index_list()
    print(msg)
    df_class_one = df[df['section_name'] == u'一级行业']
    df_class_one.to_csv(file)
Exemplo n.º 3
0
def swindex_get_index_list():
    df, msg = swindex.get_index_list()
    return df.to_json(orient='table')
Exemplo n.º 4
0
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)
Exemplo n.º 5
0
def get_heatmap_data(is_save: bool = False):
    """
    热力图数据、散点图数据
    申万一级行业涨跌幅、换手率与交易量,
    数据格式:4or5 * n,n为申万一级行业个数,
    每个数组中[x轴(换手率),y轴(涨跌幅),面积(交易量),行业名称,(可选)]
    :param is_save:
    :return:
    """
    data = dict()
    #
    index_list, msg = swindex.get_index_list()
    days_delta = 30
    end_date = datetime.date.today()
    start_date = end_date - datetime.timedelta(days=days_delta)
    l1_industry = index_list[index_list['section_name'] == '一级行业'].set_index(
        keys='index_code')
    l1_codes = l1_industry.index.tolist()
    #
    data['trade_date'] = None
    data['industry'] = l1_industry['index_name'].tolist()
    data['heatmap'] = []
    data['scatter'] = []
    #
    for i in range(0, len(l1_codes)):
        kdaily, msg = swindex.get_index_daily(
            index_code=l1_codes[i],
            start_date=start_date.isoformat(),
            end_date=end_date.isoformat())
        daily_indi, msg = swindex.get_index_dailyindicator(
            index_code=l1_codes[i],
            start_date=start_date.isoformat(),
            end_date=end_date.isoformat(),
            freq='D')
        daily_data = pd.merge(
            left=kdaily,
            right=daily_indi.drop(
                ['index_code', 'index_name', 'close', 'chg_pct', 'volume'],
                axis=1),
            on='date',
            how='left')
        daily_data.sort_values(by='date', inplace=True)
        daily_data.reset_index(drop=True, inplace=True)
        # 热力图纵轴索引
        daily_data['ver_range'] = [i] * len(daily_data)
        # 热力图横轴索引
        daily_data['hor_range'] = range(0, len(daily_data))
        # 单日涨跌
        daily_data[['turn_rate', 'change_pct',
                    'vol']] = daily_data[['turn_rate', 'change_pct',
                                          'vol']].astype('float')
        # 累计涨跌
        daily_data['change_sum'] = round(daily_data['change_pct'].cumsum(), 2)
        if data['trade_date'] is None:
            data['trade_date'] = daily_data['date'].map(
                lambda x: to_date_str(x, only_date=True)).tolist()
        # data[l1_codes[i]] = dict()
        # data[l1_codes[i]]['index_name'] = l1_industry.loc[l1_codes[i], 'index_name']
        # 热力图数据
        heatmap_data = daily_data[[
            'hor_range', 'ver_range', 'change_pct', 'change_sum'
        ]].values.tolist()
        data['heatmap'].extend(heatmap_data)
        # 散点图
        scatter_data = [
            daily_data.loc[len(daily_data) - 1,
                           ['turn_rate', 'change_pct', 'vol', 'index_name']].
            values.tolist()
        ]
        data['scatter'].extend(scatter_data)
    if is_save:
        with open(PRO_PATH + '/data/heatmap.json', 'w') as f:
            json.dump(data, f, indent=1, ensure_ascii=False)
    return data
Exemplo n.º 6
0
bounds = []
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}
#