Exemplo n.º 1
0
def updateStockHolder(data):
    code = data['code']
    df1 = data['data']

    st = dbs.getStock(code)
    sql = "select max(report_date) from stock_holder where code=:code"
    resultProxy = db.session.execute(text(sql), {'code': code})
    s_date = resultProxy.scalar()

    if (s_date == None):
        s_date = st.launch_date  # 取上市日期

    def convertDate(x):
        return pd.to_datetime(x).date()

    df2 = df1[df1['report_date'].apply(convertDate) > s_date]

    if not df2.empty:
        df2.to_sql('stock_holder',
                   db.engine,
                   if_exists='append',
                   index=False,
                   chunksize=1000)
        #更新汇总信息
        #refreshStockHolderSum(df2,code)

    latest_report = df1['report_date'].max()
    #更新stock情况

    st.latest_report = latest_report
    st.holder_updated_time = datetime.now()
    db.session.flush()
Exemplo n.º 2
0
def get_basic():
    code = request.args.get('code')
    st = ds.getStock(code[2:])
    return jsonify(msg='true',stock={
        'desc':st.desc,
        'grow_type':st.grow_type
        })
Exemplo n.º 3
0
def updateFinanceData(item):
    code = item['code']
    d1,d2,d3,d4 = item['d0'], item['d1'], item['d2'], item['d3']
    try:
        st = dbs.getStock(code)
        # 更新网易来源数据
        ns.updateFinanceData(st, d1)
        # 更新雪球来源数据
        xues.updateAssetWebData(st, d2)
        xues.updateIncomeWebData(st, d3)
        xues.updateCashWebData(st, d4)
        # print 'stock %s finance update done' % code
        # app.logger.info('.')

        # 更新stock情况
        st.finance_updated_time = datetime.now()
        db.session.flush()

        return True
    except Exception, ex:
        msg = traceback.format_exc()
        eLog = ReqErrorLog("finance_update", code, msg[:1800])
        db.session.add(eLog)
        db.session.commit()
        app.logger.warn('stock %s finance update fail' % code)
        return False
Exemplo n.º 4
0
def updateFinanceData(item):
    code = item['code']
    d1, d2, d3, d4 = item['d0'], item['d1'], item['d2'], item['d3']
    try:
        st = dbs.getStock(code)
        # 更新网易来源数据
        ns.updateFinanceData(st, d1)
        # 更新雪球来源数据
        xues.updateAssetWebData(st, d2)
        xues.updateIncomeWebData(st, d3)
        xues.updateCashWebData(st, d4)
        # print 'stock %s finance update done' % code
        # app.logger.info('.')

        # 更新stock情况
        st.finance_updated_time = datetime.now()
        db.session.flush()

        return True
    except Exception, ex:
        msg = traceback.format_exc()
        eLog = ReqErrorLog("finance_update", code, msg[:1800])
        db.session.add(eLog)
        db.session.commit()
        app.logger.warn('stock %s finance update fail' % code)
        return False
Exemplo n.º 5
0
def updateStockHolder(data):
    code = data['code']
    df1 = data['data']

    st = dbs.getStock(code)
    sql = "select max(report_date) from stock_holder where code=:code";
    resultProxy = db.session.execute(text(sql), {'code': code})
    s_date = resultProxy.scalar()

    if (s_date == None):
        s_date = st.launch_date  # 取上市日期

    def convertDate(x):
        return pd.to_datetime(x).date()
    df2 = df1[df1['report_date'].apply(convertDate) > s_date]

    if not df2.empty:
        df2.to_sql('stock_holder', db.engine, if_exists='append', index=False, chunksize=1000)
        #更新汇总信息
        #refreshStockHolderSum(df2,code)

    latest_report = df1['report_date'].max()
    #更新stock情况

    st.latest_report = latest_report
    st.holder_updated_time = datetime.now()
    db.session.flush()
Exemplo n.º 6
0
def get_basic():
    code = request.args.get('code')
    st = ds.getStock(code[2:])
    return jsonify(msg='true',
                   stock={
                       'desc': st.desc,
                       'grow_type': st.grow_type
                   })
Exemplo n.º 7
0
def updateTradeData(code):
    try:
        st = dbs.getStock(code)
        ns.updateTradeData(st)
        # 更新stock情况
        st.trade_updated_time = datetime.now()
        db.session.flush()
        return True
    except Exception, ex:
        msg = traceback.format_exc()
        eLog = ReqErrorLog("trade_update", code, msg[:1800])
        db.session.add(eLog)
        db.session.commit()
        app.logger.warn('stock %s trade update fail' % code)
        return False
Exemplo n.º 8
0
def updateTradeData(code):
    try:
        st = dbs.getStock(code)
        ns.updateTradeData(st)
        # 更新stock情况
        st.trade_updated_time = datetime.now()
        db.session.flush()
        return True
    except Exception, ex:
        msg = traceback.format_exc()
        eLog = ReqErrorLog("trade_update", code, msg[:1800])
        db.session.add(eLog)
        db.session.commit()
        app.logger.warn('stock %s trade update fail' % code)
        return False
Exemplo n.º 9
0
def getStockHolder(code, report_date, direction):
    sql = "select max(report_date) from stock_holder where code=:code"
    resultProxy = db.session.execute(text(sql), {'code': code})
    _max_date = resultProxy.scalar()
    if report_date == '':
        if (_max_date == None):
            _max_date = dbs.getStock(code).launch_date  # 取上市日期
        _next_date = pd.to_datetime(_max_date)
    else:
        _next_date = pd.to_datetime(report_date)

    if direction == 'next':
        if (_next_date.date() - _max_date).days <= 0:
            _next_date = QuarterEnd().rollforward(_next_date +
                                                  DateOffset(days=1))
    elif direction == 'pre':
        _next_date = QuarterEnd().rollback(_next_date - DateOffset(days=1))

    submit_date = QuarterEnd().rollback(_next_date - DateOffset(days=1))

    app.logger.debug('query holder data from ' +
                     submit_date.strftime('%Y-%m-%d') + ' to ' +
                     _next_date.strftime('%Y-%m-%d'))

    hdf = pd.read_sql_query("select id, code,report_date,holder_type,holder_name,holder_code,rate,amount \
                                from stock_holder where code=%(name)s and report_date>=%(submit_date)s and report_date<=%(report_date)s \
                                order by report_date desc,rank asc"                                                                   , db.engine, \
                            params={'name': code, 'submit_date': submit_date, 'report_date': _next_date})

    def fixHolderName(x):
        d1 = hdf[hdf['id'] == x]
        v1 = d1.get('holder_code').item()
        v2 = d1.get('holder_name').item()
        if v1 == None:  # 空值判断
            return v2
        else:
            return v1

    hdf['holder_name_new'] = hdf['id'].apply(fixHolderName)

    t2_df = hdf[:10]
    t3_df = hdf[10:20]
    m1_df = pd.merge(t2_df, t3_df, how='outer', on='holder_name_new')

    def getReportDate(x, attri):
        d1 = m1_df[m1_df['holder_name_new'] == x]
        v1 = d1.get(attri + '_x')
        v2 = d1.get(attri + '_y')
        if v1 == None:  # 空值判断
            return v2
        else:
            return v1

    def getValue(x, attri):
        d1 = m1_df[m1_df['holder_name_new'] == x]
        v1 = d1.get(attri + '_x')
        v2 = d1.get(attri + '_y')
        if v1.item() != v1.item():  # 空值判断
            return v2.item()
        else:
            return v1.item()

    def countVar(x):
        d1 = m1_df[m1_df['holder_name_new'] == x]
        v1 = d1.get('rate_x')
        v2 = d1.get('rate_y')
        if v1.item() != v1.item():  # 空值判断
            return '减持'
        elif v2.item() != v2.item():
            return '新进'
        elif v1.item() == v2.item():
            return '不变'
        else:
            return format(v1.item() - v2.item(), ',')

    m2_df = pd.DataFrame({
        'name':
        m1_df['holder_name_new'].apply(getValue, args=('holder_name', )),
        'code':
        m1_df['holder_name_new'].apply(getValue, args=('holder_code', )),
        'report_date':
        m1_df['holder_name_new'].apply(getValue, args=('report_date', )),
        'amount':
        m1_df['holder_name_new'].apply(getValue, args=('amount', )),
        'rate':
        m1_df['holder_name_new'].apply(getValue, args=('rate', )),
        'var':
        m1_df['holder_name_new'].apply(countVar)
    })

    return (_next_date.strftime('%Y-%m-%d'), m2_df)
Exemplo n.º 10
0
def updateStockHolder(data):
    code = data['code']
    fd = data['data']
    fArray = []

    st = dbs.getStock(code)

    if fd['list'] is None:
        return

    for e in fd['list']:
        fArray = fArray + e['list']
    ndf = pd.DataFrame(fArray)

    df1 = pd.DataFrame({
        'code':
        code,
        'report_date':
        ndf['enddate'].map(lambda x: pd.to_datetime(x).strftime("%Y-%m-%d")),
        'rank':
        ndf['rank2'],
        'holder_name':
        ndf['shholdername'],
        'holder_code':
        ndf['shholdercode'],
        'amount':
        ndf['holderamt'],
        'rate':
        ndf['holderrto'].map(lambda x: round(float(x), 2)),
        'holder_nature':
        ndf['shholdertype'],
        'holder_type':
        ndf['shholdernature'],
        'holder_parent':
        ndf['shholdername'].map(lambda x: x.split('-')[0])
    })

    sql = "select max(report_date) from stock_holder where code=:code"
    resultProxy = db.session.execute(text(sql), {'code': code})
    s_date = resultProxy.scalar()

    if (s_date == None):
        s_date = st.launch_date  # 取上市日期

    def convertDate(x):
        return pd.to_datetime(x).date()

    df2 = df1[df1['report_date'].apply(convertDate) > s_date]

    if not df2.empty:
        df2.to_sql('stock_holder',
                   db.engine,
                   if_exists='append',
                   index=False,
                   chunksize=1000)
        #更新汇总信息
        #refreshStockHolderSum(df2,code)

    latest_report = df1['report_date'].max()
    #更新stock情况

    st.latest_report = latest_report
    st.holder_updated_time = datetime.now()
    db.session.flush()
Exemplo n.º 11
0
def getStockHolder(code,report_date,direction):
    sql = "select max(report_date) from stock_holder where code=:code";
    resultProxy = db.session.execute(text(sql), {'code': code})
    _max_date = resultProxy.scalar()
    if report_date == '':
        if (_max_date == None):
            _max_date = dbs.getStock(code).launch_date  # 取上市日期
        _next_date = pd.to_datetime(_max_date)
    else:
        _next_date = pd.to_datetime(report_date)

    if direction == 'next':
        if (_next_date.date()-_max_date).days <= 0:
            _next_date = QuarterEnd().rollforward(_next_date + DateOffset(days=1))
    elif direction == 'pre':
        _next_date = QuarterEnd().rollback(_next_date - DateOffset(days=1))

    submit_date = QuarterEnd().rollback(_next_date - DateOffset(days=1))

    app.logger.debug('query holder data from ' + submit_date.strftime('%Y-%m-%d') +' to '+ _next_date.strftime('%Y-%m-%d'))

    hdf = pd.read_sql_query("select id, code,report_date,holder_type,holder_name,holder_code,rate,amount \
                                from stock_holder where code=%(name)s and report_date>=%(submit_date)s and report_date<=%(report_date)s \
                                order by report_date desc,amount desc", db.engine, \
                            params={'name': code, 'submit_date': submit_date.strftime('%Y-%m-%d'), 'report_date': _next_date.strftime('%Y-%m-%d')})

    def fixHolderName(x):
        d1 = hdf[hdf['id'] == x]
        v1 = d1.get('holder_code').item()
        v2 = d1.get('holder_name').item()
        if v1 == None:  # 空值判断
            return v2
        else:
            return v1

    hdf['holder_name_new'] = hdf['id'].apply(fixHolderName)

    t2_df = hdf[:10]
    t3_df = hdf[10:20]
    m1_df = pd.merge(t2_df, t3_df, how='outer', on='holder_name_new')

    def getReportDate(x, attri):
        d1 = m1_df[m1_df['holder_name_new'] == x]
        v1 = d1.get(attri + '_x')
        v2 = d1.get(attri + '_y')
        if v1 == None:  # 空值判断
            return v2
        else:
            return v1

    def getValue(x, attri):
        d1 = m1_df[m1_df['holder_name_new'] == x]
        v1 = d1.get(attri + '_x')
        v2 = d1.get(attri + '_y')
        if v1.item() != v1.item():  # 空值判断
            return v2.item()
        else:
            return v1.item()

    def countVar(x):
        d1 = m1_df[m1_df['holder_name_new'] == x]
        v1 = d1.get('rate_x')
        v2 = d1.get('rate_y')
        if v1.item() != v1.item():  # 空值判断
            return '减持'
        elif v2.item() != v2.item():
            return '新进'
        elif v1.item() == v2.item():
            return '不变'
        else:
            return format(v1.item() - v2.item(), ',')

    m2_df = pd.DataFrame({
        'name': m1_df['holder_name_new'].apply(getValue, args=('holder_name',)),
        'code': m1_df['holder_name_new'].apply(getValue, args=('holder_code',)),
        'report_date': m1_df['holder_name_new'].apply(getValue, args=('report_date',)),
        'amount': m1_df['holder_name_new'].apply(getValue, args=('amount',)),
        'rate': m1_df['holder_name_new'].apply(getValue, args=('rate',)),
        'var': m1_df['holder_name_new'].apply(countVar)
    })

    return (_next_date.strftime('%Y-%m-%d'),m2_df)