def bankrisk(innercode, checkdate):
        innercodeCalc, result = ca.reshape_innercode(innercode=innercode, checkdate=checkdate, cache=ca.bankRiskCache)
        #innercodeCalc=innercode
        result = df(result,columns=('innercode', 'es'))

        if len(innercodeCalc) != 0:
            query="""SELECT
                        financedata.bank_fin_prd.INNER_CODE,
                        financeprd.bank_risk.VAR
                    FROM
                        financedata.bank_fin_prd,
                        financeprd.bank_risk
                    WHERE
                        financedata.bank_fin_prd.INNER_CODE IN ( %s )
                    AND
                        financeprd.bank_risk.BANK_ID = financedata.bank_fin_prd.BANK_ID """ % ','.join(innercodeCalc)
            result_new = GetDataFromDB(config, query)
            result_new.columns = ['innercode', 'es']
            innercode_empty=df(columns=['innercode', 'es'])
            innercode_empty['innercode']=list(set(innercodeCalc)-set(result_new['innercode'].astype(str)))
            innercode_empty['es']=''
            innercode_empty.columns=['innercode','es']
            result_new=result_new.append(innercode_empty,ignore_index=1)
            #save cache for new data
            ca.set_cache(result_new=result_new, checkdate=checkdate,cache=ca.bankRiskCache)

            result=result.append(result_new,ignore_index=1)
            result['checkdate'] = checkdate
        else:
            result['checkdate'] = checkdate
        return (result.to_json(orient='index'))
Exemple #2
0
    def bankrisk(innercode, checkdate):
        innercodeCalc, result = ca.reshape_innercode(innercode=innercode, checkdate=checkdate, cache=ca.bankRiskCache)
        if len(innercodeCalc) != 0:
            ro.r.assign("innercode", innercode)
            ro.r("innercode=as.character(innercode)")

            ro.r("res=bankRisk(innercode)")

            innercode = list(ro.r("as.character(res$innercode)"))
            es = list(ro.r("as.character(res$es)"))
            # result = [innercode, es]
            ca.set_cache(innercode=innercode, checkdate=checkdate, es=es, cache=ca.bankRiskCache)

            result[0] = result[0] + innercode
            result[1] = result[1] + es
        else:
            pass

        return result
    def fundrisk(innercode, checkdate):
        """
        innercode from url:param innercode:
        checkdate from url:param checkdate:
        reult fundrisk with json type:return:
        """
        innercodeCalc, result = ca.reshape_innercode(innercode=innercode, checkdate=checkdate, cache=ca.fundRiskCache)
        """
        check the cache for the expected short fall already existed
        """
        #innercodeCalc=innercode
        result = df(result,columns=('innercode', 'es'))
        if len(innercodeCalc) != 0:
            startdate = datetime.strptime(checkdate, '%Y-%m-%d') - timedelta(days=730)
            query = """select
              financedata.ana_fnd_nav_calc.TRADEDATE,
              financedata.ana_fnd_nav_calc.fac_unit_net,
              financedata.ana_fnd_nav_calc.INNER_CODE
              from
              financedata.ana_fnd_nav_calc
              where
              financedata.ana_fnd_nav_calc.inner_code in ( %s )
              and
              financedata.ana_fnd_nav_calc.tradedate<= '%s'
              and
              financedata.ana_fnd_nav_calc.tradedate>= '%s'
              """ % (','.join(innercodeCalc), checkdate, startdate.strftime('%Y-%m-%d'))
            """
            asseemble the query
            """

            indexPrice = GetDataFromDB(config, query)
            indexPrice.columns = ['tradedate', 'close', 'innercode']
            """
            get date from db
            """
            grouped = indexPrice.groupby('innercode')
            result_new = df(columns=('innercode', 'es'))


            for i in innercodeCalc:
                try:
                    dataForCalc = grouped.get_group(int(i))
                    es = cr.es(dataForCalc['tradedate'], dataForCalc['close'])
                    result_new.loc[len(result_new.index)]=[i,es]
                except KeyError:
                    es = ''
                    result_new.loc[len(result_new.index)]=[i,es]

            """
            constract the result maxtrix which innercode cannot be found in database
            with empty es
            """


            #save cache for new data
            ca.set_cache(result_new=result_new, checkdate=checkdate,cache=ca.fundRiskCache)
            """
            save the result in cache
            """
            result = result.append(result_new,ignore_index=1)
            result['checkdate'] = checkdate
        else:
            result['checkdate'] = checkdate

        return (result.to_json(orient='index'))