def get_index_members_akshare(exchange: str, index_code: str, pname: str):
    cur_index_members_df = ak.index_stock_cons(index=index_code)
    cimd = cur_index_members_df[['品种代码', '纳入日期']]
    cimd.columns = ['stock_code', 'in_date']
    cimd['out_date'] = 'nan'

    index_member_hist_df = ak.index_stock_hist(index=exchange + index_code)
    index_members = pd.concat([cimd, index_member_hist_df], axis=0)
    index_members = index_members.reset_index(drop=True)

    all_data = []
    for _, row in index_members.iterrows():
        if row['stock_code'][0] == '6':
            cur_symbol = row['stock_code'] + '.SH'
        else:
            cur_symbol = row['stock_code'] + '.SZ'

        in_date = pd.to_datetime(row['in_date'])
        cur_in_date = int(in_date.strftime('%Y%m%d'))

        if row['out_date'] == 'nan':
            cur_out_date = 0
        else:
            out_date = pd.to_datetime(row['out_date'])
            cur_out_date = int(out_date.strftime('%Y%m%d'))

        all_data.append([cur_symbol, cur_in_date, cur_out_date])
    index_members2 = pd.DataFrame(all_data, columns=index_members.columns)
    index_members2 = index_members2.drop_duplicates(
        ['stock_code', 'in_date',
         'out_date']).sort_values(['stock_code', 'in_date',
                                   'out_date']).reset_index(drop=True)
    index_members2.to_pickle(pname)
Beispiel #2
0
def BenchmarkComponentWeightLimit(lower_limit, hard=True, benchmark='000300'):
    """
	成分股权重约束,即要求优化结果中,基准成分股的权重之和的下限。基准成分股权重限制。基准为优化时指定的基准。
	:param benchmark: 格式参照‘000300’
	:param lower_limit: 基准成分股权重之和下界
	:param hard: 软约束/硬约束
	"""

    # start = '2019-01-09'
    try:
        index_df = ak.index_stock_cons(index=benchmark)
    except ValueError:
        print('The symbol is invalid.')
        exit(1)
    else:
        lst = index_df['品种代码']

        judge = []

        for k in range(len(returns.columns)):
            if returns.columns[k][:6] in lst:
                judge.append(1)
            else:
                judge.append(0)

        def fun(omega):
            return omega.dot(judge) - lower_limit

        constraints.append({'type': 'eq', 'fun': fun})
        constraints_hard.append(hard)
Beispiel #3
0
def get_hs300_list1():
    index_stock_cons_df = ak.index_stock_cons(index="000300")
    print(index_stock_cons_df)

    print(index_stock_cons_df.shape[0])
    #print(index_stock_cons_df['品种代码'].values.tolist()[0:-1])
    hslist = sorted(index_stock_cons_df['品种代码'].values.tolist())
    #[print(index) for index in hslist]
    hsdict = Counter(hslist)
    [print(k, hsdict[k]) for k in hsdict.keys()]
Beispiel #4
0
def indexStocksCN(idx):
    print(idx)
    index_stock_cons_df = ak.index_stock_cons(index=idx[2:])
    # print(index_stock_cons_df)
    index_stock_cons_df.drop_duplicates(subset='品种代码',
                                        keep='first',
                                        inplace=True)
    index_stock_cons_df.set_index('品种代码', inplace=True)
    # 今日全场股票概况(价格、市值等)
    df = ak.stock_zh_a_spot()
    df.set_index('symbol', inplace=True)
    # 按总市值排顺序,只保留指数成份股
    df = df.loc[df.index.isin(
        [dealSymbol(s) for s in index_stock_cons_df.index])].copy()
    df.sort_values(by='mktcap', ascending=True, inplace=True)
    df = df.iloc[:int(len(df) / 2) - 1]
    # 计算因子值
    days = 30
    sortedDf = pd.DataFrame()
    tqdmRange = tqdm(range(len(df)))
    tqdmRange = tqdm([-3, -2, -1])
    for i in tqdmRange:
        symbol = dealSymbol(df.index[i])
        tqdmRange.set_description("cauculating factor value for %s" % symbol)
        factorVaule_1 = []
        factorVaule_2 = []
        stockK = ak.stock_zh_index_daily_tx(symbol=symbol)
        stockK['symbol'] = symbol
        for j in range(0, len(stockK)):
            if j < days:
                factorVaule_1.append(None)
                factorVaule_2.append(None)
                continue
            factorVaule_1.append(factor_1(stockK.iloc[j - days:j]))
            factorVaule_2.append(factor_1(stockK.iloc[j - days:j]))
        stockK['factor_1'] = factorVaule_1
        stockK['factor_2'] = factorVaule_2
        stockK.to_csv('Quotation/' + symbol + '.csv')
        stockK = stockK[['symbol', 'factor_1', 'factor_2']].copy()
        stockK.reset_index(inplace=True)
        sortedDf = sortedDf.append(stockK)
    sortedDf.sort_values(by=['date', 'factor_1'], ascending=True, inplace=True)
    sortedDf.dropna(subset=['factor_1'], inplace=True)
    sortedDf.to_csv(idx + 'factor.csv')
    sortedDf['date'] = pd.to_datetime(sortedDf['date']).date
    return sortedDf
Beispiel #5
0
def get_laststock_set(hs300, datadir):
    allset = set()
    if os.path.exists(hs300):
        input = open(hs300, 'r')
        allset = set([stock.rstrip() for stock in input.readlines()])
    else:
        index_stock_cons_df = ak.index_stock_cons(index="000300")  #沪深300
        allset = set(index_stock_cons_df['品种代码'].values.tolist()[0::])

    print('沪深300个数', len(allset))

    existset = set()
    if os.path.exists(datadir):
        filelist = os.listdir(datadir)
        existset = set([stock.split('_')[0] for stock in filelist])

    lastset = allset - existset

    if len(lastset) > 0:
        for stock in lastset:
            bget, mises_stock_df, latestmv = calc_stock_finmv_df(stock)
            if bget is False: print("get empty DataFrame:%s" % stock)

    return allset, lastset
Beispiel #6
0
    fund_etf_hist_df.iloc[0, 4] = 0  # 第一行的处理为0

    fund_etf_hist_df['Additional'] = fund_etf_hist_df['Daily_Increase'].map(
        addinput)

    fund_etf_hist_df['share'] = (
        fund_etf_hist_df['Fixed_Input'] +
        fund_etf_hist_df['Additional']) / fund_etf_hist_df['close'].shift(-1)

    fund_etf_hist_df.iloc[-1, 6] = 0  # 最后一天份数设为0
    fund_etf_hist_df.iloc[-1, 3] = 0  # 最后一天定投金额设为0

    return fund_etf_hist_df


index_stock_cons_df = ak.index_stock_cons(index=TARGET_INDEX)
index_stock_cons_df.to_csv(
    '/data/jupyter/root/TARGET_INDEX_STOCK_LIST_GBK.csv', encoding="gbk")
index_stock_cons_df.to_csv(
    '/data/jupyter/root/TARGET_INDEX_STOCK_LIST_UTF8.csv')

temp1 = ak.stock_financial_abstract(stock=300782).head(5)
temp1['股票编码'] = 300782
temp2 = ak.stock_financial_abstract(stock=300661).head(5)
temp2['股票编码'] = 300661

temp1 = temp1.append(temp2).reindex()
temp1

temp1.append(temp2).to_csv('/data//jupyter/root/temp.csv')
Beispiel #7
0
def get_index_cons_list(index: str):
    index_stock_cons_df = ak.index_stock_cons(index)
    stk_id_list = index_stock_cons_df['品种代码'].tolist()
    return stk_id_list
Beispiel #8
0
def query_index_content(code='sh000001'):
    code = code[2:]
    print(code)
    index_stock_cons_df = ak.index_stock_cons(index=code)
    index_stock_cons_df.rename(columns={'品种代码': 'code'}, inplace=True)
    return index_stock_cons_df