Beispiel #1
0
def get_last_day_market_val(target, tdate):
    """
    获取最近一日市值
    :param target:
    :param tdate:
        '20190908'
    :return:
    """
    if target == 'cash':
        return datetime.now().date(), 1
    if any([target.endswith('.SZ'), target.endswith('.SH')]):
        tname = target
    else:
        tname = series.loc[target]
    mongo = PyMongoWrapper()
    db_name = 'finance_n'
    tb_name = 'stocks_daily'
    table = mongo.getCollection(db_name, tb_name)
    r = list(
        mongo.findAll(table, {
            'ts_code': tname,
            'trade_date': {
                '$lte': datetime.strptime(tdate, '%Y%m%d')
            }
        },
                      fieldlist=['trade_date', 'close', 'pct_chg'],
                      sort=[('trade_date', -1)],
                      limit=1))[0]
    return r['trade_date'].date(), r['close']
Beispiel #2
0
    def get_stocks_dict_top_n(cls, n, t_date):
        mongo = PyMongoWrapper()
        table = mongo.getCollection('finance_n', 'stock_daily_basic')
        df = mongo.findAll(table, {'trade_date': pd.to_datetime(t_date)}, fieldlist=['ts_code'],
                           sort=[('rank', 1)], limit=n, returnFmt='df')
        stock_reverse_dict = cls.get_stocks_dict()[1]

        return df.ts_code.map(lambda c: stock_reverse_dict[c]).tolist()
Beispiel #3
0
    def get_stocks_dict(cls):
        mongo = PyMongoWrapper()
        table = mongo.getCollection('finance_n', 'stocks_info')
        df = mongo.findAll(table, fieldlist=['ts_code', 'name'], returnFmt='df')
        stock_names = df.name.tolist()
        stock_ts_codes = df.ts_code.tolist()

        return dict(zip(stock_names, stock_ts_codes)), dict(zip(stock_ts_codes, stock_names))
def r(o, s):
    if o == 'cash':
        return CE().date(), 1
    if Cu([o.endswith('.SZ'), o.endswith('.SH')]):
        b = o
    else:
        b = M.loc[o]
    W = PyMongoWrapper()
    m = W.getCollection('finance', b)
    r = Cw(
        W.findAll(m, {'trade_date': {
            '$lte': Cn(s, '%Y%m%d')
        }},
                  fieldlist=['trade_date', 'close', 'pct_chg'],
                  sort=[('trade_date', -1)],
                  limit=1))[0]
    return r['trade_date'].date(), r['close']
def get_last_day_market_val(T, D):
    if T == 'cash':
        return e().date(), 1
    if mF([T.endswith('.SZ'), T.endswith('.SH')]):
        s = T
    else:
        s = i.loc[T]
    K = PyMongoWrapper()
    v = K.getCollection('finance', s)
    r = X(
        K.findAll(v, {'trade_date': {
            '$lte': c(D, '%Y%m%d')
        }},
                  fieldlist=['trade_date', 'close', 'pct_chg'],
                  sort=[('trade_date', -1)],
                  limit=1))[0]
    return r['trade_date'].date(), r['close']
Beispiel #6
0
 def get_hist(cls, name=None, ts_code=None, count=365):
     if not ts_code:
         stocks_dict = cls.get_stocks_dict()[0]
         ts_code = stocks_dict[name]
     # start_date = datetime.now() - timedelta(days=count)
     mongo = PyMongoWrapper()
     table = mongo.getCollection('finance_n', 'stocks_daily')
     # df = mongo.findAll(table, {'trade_date': {'$gte': start_date}},
     #                    fieldlist=['trade_date', 'pct_chg'], returnFmt='df')
     df = mongo.findAll(table, {'ts_code': ts_code}, fieldlist=['trade_date', 'pct_chg'],
                        sort=[('trade_date', -1)], limit=count, returnFmt='df')
     df.set_index('trade_date', inplace=True)
     df.sort_index(inplace=True)
     df['net'] = (1 + df['pct_chg'] / 100).cumprod()
     del df['pct_chg']
     # print(df.head())
     # print(df.tail())
     return df
Beispiel #7
0
PROJ_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

stock = '山鹰纸业'

stock_series = pd.read_pickle(
    os.path.join(PROJ_DIR, 'finance/data/stock_dict.pkl'))
fund_otc_series = pd.read_pickle(
    os.path.join(PROJ_DIR, 'finance/data/fund_otc_series.pkl'))
mongo = PyMongoWrapper()
time_range = 'near_1_year'
start_date = datetime.now() - timedelta(days=365 * 5)

# 基本面 area、industry、sw_industry、市值、市值排名
table = mongo.getCollection('finance', 'stock_basic')
df_base = mongo.findAll(table, {'ts_code': stock_series.loc[stock]},
                        fieldlist=['name', 'area', 'industry', 'market'],
                        returnFmt='df')
# print(df)
table = mongo.getCollection('finance', 'swclass')
dft = mongo.findAll(
    table,
    {'股票代码': stock_series.loc[stock].map(lambda s: s.split('.')[0]).tolist()},
    fieldlist=['股票名称', '行业名称'],
    returnFmt='df')
dft.columns = ['name', 'swclass']
df_base = pd.merge(df_base, dft, on='name')

table = mongo.getCollection('finance', 'stock_daily_basic1')
dft = mongo.findAll(table, {
    '$and': [{
        'trade_date': '20190926'
Beispiel #8
0
    s = df_ac.groupby('account')['market_val'].sum()
    # s = df_ac.groupby('type')['market_val'].sum()
    s.loc['TOTAL'] = s.sum()
    dfr = pd.DataFrame(s).T
    dfr.index.name = 'date'
    dfr.index = pd.to_datetime([tdate])
    return dfr


if __name__ == '__main__':
    # add_transactions()
    transaction_list = []
    mongo = PyMongoWrapper()
    table = mongo.getCollection('finance', 'transactions')
    cols = 'trade_date account target price num fee_rate'.split()
    rs = mongo.findAll(table, fieldlist=cols, sort=[('trade_date', 1)])

    trans_df = pd.DataFrame(rs)
    trans_df.set_index('trade_date', inplace=True)

    trans_df['cost'] = trans_df['price'] * trans_df['num'] * (
        1 + trans_df.fee_rate)
    dfc = pd.DataFrame()
    dates = pd.date_range('20190921', '20190926', freq='B')
    dfc = get_account_val(trans_df, '20190930')
    # for d in dates:
    #     d = d.strftime('%Y%m%d')
    #     if dfc.empty:
    #         dfc = get_account_val(trans_df, d)
    #     else:
    #         dfc = pd.concat([dfc, get_account_val(trans_df, d)])
Beispiel #9
0
 '五粮液',
 '顺鑫农业',
 '洋河股份',
 '青青稞酒',
 '伊力特',
 '金种子酒',
 '贵州茅台',
 '老白干酒',
 '舍得酒业',
 '水井坊',
 '山西汾酒',
 '迎驾贡酒',
 '今世缘',
 '口子窖',
 '金徽酒']
STOCK_NAMES = [
               '东方航空',
               '南方航空',
               '中国核电',
               '中国平安',
               '北大荒',
               '中国石油',
               '山鹰纸业',
               ] # + BANKS + BROKERS + LIQUORS + GOLDS
table = mongo.getCollection('finance', 'stock_basic')
df = mongo.findAll(table, fieldlist=['name'], returnFmt='df')
STOCK_NAMES = df.name.tolist()[::-1]



    Y['market_val'] = Y.num * Y.price
    Y['profit'] = Y['market_val'] - Y['cost']
    s = Y.groupby('account')['market_val'].sum()
    s.loc['TOTAL'] = s.sum()
    l = B(s).T
    l.index.name = 'date'
    l.index = CI([s])
    return l


if __name__ == '__main__':
    q = []
    W = PyMongoWrapper()
    m = W.getCollection('finance', 'transactions')
    A = 'trade_date account target price num fee_rate'.split()
    rs = W.findAll(m, fieldlist=A, sort=[('trade_date', 1)])
    J = B(rs)
    J.set_index('trade_date', inplace=Cy)
    J['cost'] = J['price'] * J['num'] * (1 + J.fee_rate)
    e = B()
    N = Co('20190921', '20190926', freq='B')
    for d in N:
        d = d.strftime('%Y%m%d')
        if e.empty:
            e = H(J, d)
        else:
            e = CS([e, H(J, d)])
    CT(e)
    e.plot(subplots=Cy, figsize=(8, 14))
# Created by pyminifier (https://github.com/liftoff/pyminifier)
Beispiel #11
0
def get_stocks_to_mongo(stocks, mode='stock'):
    import pandas as pd
    stock_series = pd.read_pickle(
        os.path.join(PROJ_DIR, 'finance/data/stock_dict.pkl'))
    mongo = PyMongoWrapper()
    db = mongo.getDb('finance')
    tables = db.list_collection_names()
    start_date = '20010101'
    end_date = datetime.now().strftime('%Y%m%d')
    for name in stocks:
        if mode == 'stock':
            ts_code = stock_series.loc[name]

        table = mongo.getCollection('finance', ts_code)
        if ts_code not in tables:
            logger.info(f'{ts_code}表在数据库中不存在,新建表插入!')

            if mode in ['stock', 'index', 'fund', 'otc_fund']:
                mongo.setUniqueIndex('finance', ts_code, ['trade_date'])
        else:
            logger.info(f'{ts_code}表在数据库中存在,update表数据!')
            if mode in ['stock', 'index', 'fund']:
                r = mongo.findAll(table,
                                  fieldlist=['trade_date'],
                                  sort=[('trade_date', -1)],
                                  limit=1)
                r = list(r)
                if len(r) > 0:
                    d = r[0]['trade_date']
                    start_date = (d + timedelta(days=1)).strftime('%Y%m%d')
        try:
            if mode in ['stock', 'index', 'fund']:
                df = daily(ts_code,
                           start_date=start_date,
                           end_date=end_date,
                           mode=mode)
            elif mode in ['otc_fund']:
                import requests
                import json
                import pandas as pd
                from pyquery import PyQuery as pq
                headers = {
                    'user-agent':
                    'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36 Maxthon/5.2.1.6000',
                }
                dd = dict()
                r = requests.get(f'http://fund.eastmoney.com/{ts_code}.html')
                r.encoding = 'utf8'
                root = pq(r.text)
                d1 = root(
                    '#body > div:nth-child(12) > div > div > div.fundDetail-main > div.fundInfoItem > div.dataOfFund > dl.dataItem02 > dd.dataNums > span.ui-font-middle.ui-num'
                )
                dd['pct_chg'] = float(d1.text()[:-1])
                d1 = root(
                    '#body > div:nth-child(12) > div > div > div.fundDetail-main > div.fundInfoItem > div.dataOfFund > dl.dataItem02 > dd.dataNums > span.ui-font-large.ui-num'
                )
                dd['close'] = float(d1.text())
                d1 = root(
                    '#body > div:nth-child(12) > div > div > div.fundDetail-main > div.fundInfoItem > div.dataOfFund > dl.dataItem02 > dt > p'
                )
                dd['trade_date'] = datetime.strptime(d1.text()[-11:-1],
                                                     '%Y-%m-%d')
                df = pd.DataFrame(columns=['trade_date', 'close', 'pct_chg'])
                df = df.append(dd, ignore_index=True)
                df.set_index('trade_date', inplace=True)
                print(df)
                # print(d, dd, df)

            logger.info(f'共请求到{df.shape[0]}条数据!')
            if df.shape[0] > 0:
                mongo.insertDataframe(df,
                                      'finance',
                                      ts_code,
                                      df_index='trade_date')
            else:
                logger.warning('请求数据为空!')
        except Exception as e:
            logger.error(str(e))
        mongo.close()
    Q['market_val'] = Q.num * Q.price
    Q['profit'] = Q['market_val'] - Q['cost']
    s = Q.groupby('account')['market_val'].sum()
    s.loc['TOTAL'] = s.sum()
    B = w(s).T
    B.index.name = 'date'
    B.index = R([D])
    return B


if __name__ == '__main__':
    f = []
    K = PyMongoWrapper()
    v = K.getCollection('finance', 'transactions')
    n = 'trade_date account target price num fee_rate'.split()
    rs = K.findAll(v, fieldlist=n, sort=[('trade_date', 1)])
    J = w(rs)
    J.set_index('trade_date', inplace=mV)
    J['cost'] = J['price'] * J['num'] * (1 + J.fee_rate)
    C = w()
    P = N('20190921', '20190926', freq='B')
    for d in P:
        d = d.strftime('%Y%m%d')
        if C.empty:
            C = get_account_val(J, d)
        else:
            C = r([C, get_account_val(J, d)])
    mE(C)
    C.plot(subplots=mV, figsize=(8, 14))
# Created by pyminifier (https://github.com/liftoff/pyminifier)