Exemple #1
0
def generate_comment_footer_en(f,
                               ts=None,
                               dotSign='.',
                               prcn=0,
                               usdSign='$',
                               udfLst=None,
                               lang="en",
                               mp3YN=False):
    """ macro market comment
		required 
		required ts fields: currency,indicator,rate,mkt1,mkt2
	"""
    if ts is None:
        return None
    for (ky, va) in f.iteritems():
        exec("{}=va".format(ky))
    mkt1NTrendWd = udfStr(mkt1NChg,
                          ["an increase", "a decrease", "no change in"],
                          0.0001)
    rateNTrendWd = udfStr(rate1NChg, ["a rise", "a fall", "no change in"],
                          0.0001)
    rhoWd = udfStr(rho, ["positively", "negatively", "almost not"], 0.05)
    rateIfWd = udfStr(rate1NChg, ["goes up", "goes down", "stays flat"],
                      0.0001)
    mkt1IfWd = udfStr(rho * rate1NChg, ["rise", "fall", "stay flat"], 0.000001)
    if rho * rate1NChg * mkt1NChg < 0:
        macro1Adv = 'against our original forecast'
    else:
        macro1Adv = 'even more'
    if mkt1IfWd == "stay flat":
        macro1Adv = ''
    dux = locals()
    #ret=ts.format(**dux)
    ret = jj_fmt(ts, dux)
    return (ret)
Exemple #2
0
def generate_comment_footer_cn(f,
                               ts=None,
                               dotSign='.',
                               prcn=0,
                               usdSign='$',
                               udfLst=None,
                               lang="cn",
                               mp3YN=False):
    """ macro market comment
		required 
		required ts fields: currency,indicator,rate,mkt1,mkt2
	"""
    if ts is None:
        return None
    for (ky, va) in f.iteritems():
        exec("{}=va".format(ky))

    mkt1NTrendWd = udfStr(mkt1NChg, udf=udfLst, zs=0.0001, lang=lang)
    rateNTrendWd = udfStr(rate1NChg, udf=udfLst, zs=0.0001, lang=lang)
    rhoWd = udfStr(rho, ["正", "負", "獨立不存在"], 0.05, lang=lang)
    rateIfWd = udfStr(rate1NChg, udf=udfLst, zs=0.0001, lang=lang)
    mkt1IfWd = udfStr(rho * rate1NChg, udf=udfLst, zs=0.000001, lang=lang)
    if rho * rate1NChg * mkt1NChg < 0:
        macro1Adv = ",可能不如原始預期,必須持續觀察利率走向。"
    else:
        macro1Adv = ",更勝於原有預期。"
    if abs(rho * rate1NChg) <= 0.000001:
        macro1Adv = ''
    dux = locals()
    #ret=ts.format(**dux)
    ret = jj_fmt(ts, dux)
    return (ret)
Exemple #3
0
def generate_comment_header_cn(f,
                               ts=None,
                               dotSign='.',
                               prcn=0,
                               usdSign='$',
                               udfLst=None,
                               lang="cn",
                               mp3YN=False):
    """ macro1 market comment
		required ts fields: currency1,indicator1,rate1,mkt1,mkt2
	"""
    if ts is None:
        return None
    for (ky, va) in f.iteritems():
        exec("{}=va".format(ky))
    if 'macro1XChg' not in f:
        macro1XChg = 0
        macro1Vntdate = 20180101
    #xsign = 1 if currency1Ticker[-2:] == "US" else -1
    xsign = -1  # cn version always use EURO direction
    currency1XTrendWd = udfStr(xsign * currency1XChg, ["貶值", "升值", "持平"],
                               0.0001,
                               lang=lang)
    indicator1XTrendWd = udfStr(indicator1XChg,
                                udf=udfLst,
                                zs=0.0001,
                                lang=lang)
    rate1XTrendWd = udfStr(rate1XChg, udf=udfLst, zs=0.0001, lang=lang)
    commodity1XTrendWd = udfStr(commodity1XChg,
                                udf=udfLst,
                                zs=0.0001,
                                lang=lang)
    currency2XTrendWd = udfStr(xsign * currency2XChg, ["貶值", "升值", "持平"],
                               0.0001,
                               lang=lang)
    indicator2XTrendWd = udfStr(indicator2XChg,
                                udf=udfLst,
                                zs=0.0001,
                                lang=lang)
    rate2XTrendWd = udfStr(rate2XChg, udf=udfLst, zs=0.0001, lang=lang)
    commodity2XTrendWd = udfStr(commodity2XChg,
                                udf=udfLst,
                                zs=0.0001,
                                lang=lang)
    mkt1XTrendWd = udfStr(mkt1XChg, udf=udfLst, zs=0.0001, lang=lang)
    mkt2XTrendWd = udfStr(mkt2XChg, udf=udfLst, zs=0.0001, lang=lang)
    vntDateWd = ymd2md(str(macro1Vntdate), ym="%B %d",
                       lang=lang) if 'macro1Vntdate' in f else ""
    unitStr = fq2unit_str(macro1Mfreq, lang) if 'macro1Mfreq' in f else "月"
    pastTrendWd = udfStr(macro1XChg, udfLst, 0.0001, lang=lang)
    mkt0Label = mkt0Label[:mkt0Label.find('指數基金')]

    (currency1Adv,
     currency2Adv) = ('同時',
                      '也在') if currency1XChg * currency2XChg >= 0 else ('然而',
                                                                        '卻在')
    (indicator1Adv, indicator2Adv, indicator12Adv) = (
        '並且', '也在',
        '正相關') if indicator1XChg * indicator2XChg > 0 else ('然而', '卻在', '負相關')
    (rate1Adv, rate2Adv) = ('', '也在') if rate1XChg * rate2XChg >= 0 else ('可是',
                                                                          '卻在')
    (commodity1Adv, commodity2Adv, commodity12Adv) = (
        '並且', '也在',
        '正相關') if commodity1XChg * commodity2XChg >= 0 else ('然而', '卻在', '負相關')
    (mkt1Adv, mkt2Adv) = ('', '也正') if mkt1XChg * mkt2XChg >= 0 else ('但是',
                                                                      '卻正')
    (macro1Adv,
     macro11Adv) = ('', '也正') if mkt1XChg * mkt2XChg >= 0 else ('但是', '卻正')
    dux = locals()
    #ret=ts.format(**dux)
    ret = jj_fmt(ts, dux)
    return (ret)
Exemple #4
0
def generate_comment_header_en(f,
                               ts=None,
                               dotSign='.',
                               prcn=0,
                               usdSign='$',
                               udfLst=None,
                               lang="en",
                               mp3YN=False):
    """ macro1 market comment
		required ts fields: currency1,indicator1,rate1,mkt1,mkt2
	"""
    if ts is None:
        return None
    for (ky, va) in f.iteritems():
        exec("{}=va".format(ky))
    if 'macro1XChg' not in f:
        macro1XChg = 0
        macro1Vntdate = 20180101
    #xsign = 1 if currency1Ticker[-2:] == "US" else -1
    xsign = -1  # cn version always use EURO direction
    currency1XTrendWd = udfStr(xsign * currency1XChg,
                               ["depreciated", "appreciated", "stayed flat"],
                               0.0001,
                               lang=lang)
    indicator1XTrendWd = udfStr(indicator1XChg,
                                udf=udfLst,
                                zs=0.0001,
                                lang=lang)
    rate1XTrendWd = udfStr(rate1XChg, udf=udfLst, zs=0.0001, lang=lang)
    commodity1XTrendWd = udfStr(commodity1XChg,
                                udf=udfLst,
                                zs=0.0001,
                                lang=lang)
    currency2XTrendWd = udfStr(xsign * currency2XChg,
                               ["depreciated", "appreciated", "stayed flat"],
                               0.0001,
                               lang=lang)
    indicator2XTrendWd = udfStr(indicator2XChg,
                                udf=udfLst,
                                zs=0.0001,
                                lang=lang)
    rate2XTrendWd = udfStr(rate2XChg, udf=udfLst, zs=0.0001, lang=lang)
    commodity2XTrendWd = udfStr(commodity2XChg,
                                udf=udfLst,
                                zs=0.0001,
                                lang=lang)
    mkt1XTrendWd = udfStr(mkt1XChg, udf=udfLst, zs=0.0001, lang=lang)
    mkt2XTrendWd = udfStr(mkt2XChg, udf=udfLst, zs=0.0001, lang=lang)
    vntDateWd = ymd2md(str(macro1Vntdate), ym="%B %d",
                       lang=lang) if 'macro1Vntdate' in f else ""
    unitStr = fq2unit_str(macro1Mfreq, lang) if 'macro1Mfreq' in f else "month"
    pastTrendWd = udfStr(macro1XChg, udfLst, 0.0001, lang=lang)
    mkt0Label = mkt0Label[:mkt0Label.find('Index')]

    (currency1Adv, currency2Adv) = (
        'and', 'also') if currency1XChg * currency2XChg >= 0 else ('', 'yet')
    (indicator1Adv, indicator2Adv, indicator12Adv) = (
        'and', 'also',
        'positively correlated') if indicator1XChg * indicator2XChg > 0 else (
            '', 'however', 'negatively correlated')
    (rate1Adv,
     rate2Adv) = ('', 'also') if rate1XChg * rate2XChg >= 0 else ('', 'yet')
    (commodity1Adv, commodity2Adv, commodity12Adv) = (
        'and', 'also',
        'positively correlated') if commodity1XChg * commodity2XChg >= 0 else (
            '', 'however', 'negatively correlated')
    (mkt1Adv, mkt2Adv) = ('', 'also') if mkt1XChg * mkt2XChg >= 0 else ('',
                                                                        'yet')
    (macro1Adv,
     macro11Adv) = ('', 'also') if mkt1XChg * mkt2XChg >= 0 else ('but', '')
    dux = locals()
    #ret=ts.format(**dux)
    ret = jj_fmt(ts, dux)
    return (ret)
Exemple #5
0
                              date=date,
                              debugTF=False,
                              scroll=scroll)
        #df, dg, dh = mainTest(ticker=ticker, date='1y', debugTF=False, scroll=scroll)
    except Exception, e:
        print >> sys.stderr, "**ERROR:{}".format(str(e))
        exit(1)

    lang = 'cn'
    intra1_id = 1 if hh['sign'][1] != hh['sign'][2] else 2
    inix = hh['actual'][0]
    hh['iptchg'] = map(lambda x: x - inix, hh['actual'])
    hh['iptsign'] = map(lambda x: 1 if x > 0 else -1
                        if x < 0 else 0, hh['iptchg'])
    #print hh.to_dict(orient="records")
    intra1_udf = udfStr(hh['iptchg'][intra1_id], lang=lang)
    intra2_id = intra1_id + 1
    intra2_udf = udfStr(hh['iptchg'][intra2_id], lang=lang)
    noon_id = [j for j, x in enumerate(hh.iptday) if x > 150][0]
    noon_udf = udfStr(np.diff(hh['actual'][[0, noon_id]]), lang=lang)
    noon_str = hh.index[noon_id].strftime("%H點%M分")
    end2_udf = udfStr(hh['iptchg'][-2], lang=lang)
    end1_udf = udfStr(hh['iptchg'][-1], lang=lang)
    end1_sign = hh['iptchg'][-1] * hh['iptchg'][-2]
    """
	inix = zh['actual'][0]
	zh['iptchg'] =  map(lambda x:x-inix,zh['actual'])
	inix = xh['actual'][0]
	xh['iptchg'] =  map(lambda x:x-inix,xh['actual'])

	index_trend = udfStr(zh['iptchg'][-1],lang=lang)
Exemple #6
0
def headline_calc(tkLead='^GSPC',
                  idxLst=None,
                  eqtLst=None,
                  np=3,
                  xCol='changePercent',
                  colLst=[],
                  thd=0.05):
    '''
	return object of {'topLst', 'indexOrder', 'topIndex', 'indexLst', 'topUpDn'}
	Where
	  topIndex: ticker name of lead index defined as 'tkLead'='^GSPC'
	  topUpDn: sign in string UP/FLAT/DOWN within the range of 'thd'=[0.05,-0.05]
	  allUpDn: 1,0,-1 indecis all up/TBD/down
	  topLst: selected 'eqtLst' stock quote info ranked via 'changePercent'
	      w.r.t. the 'sign'/'topUpDn' of 'topIndex'
	  bttmLst: selected 'eqtLst' stock quote info oppsite to topLst
	  indexLst: 'idxLst' stock  quote info listed in the order of 'indexOrder'

	  Note aht topIndex quote info should be in the 'indexLst'
	'''
    from _alan_str import udfStr, find_mdb
    if eqtLst is None or len(eqtLst) < 1:
        eqtLst = get_eqtLst()
    if idxLst is None or len(idxLst) < 1:
        idxLst = ['^GSPC', '^DJI', '^IXIC']  #,'^SOX']
    if colLst is None or len(colLst) < 1:
        #colLst=['open','high','low','close','volume','ticker','change','changePercent','pbdate']
        colLst = [
            'close', 'volume', 'ticker', 'change', 'changePercent', 'pbdate',
            'pbdt'
        ]
    #xqTmp="SELECT * from yh_quote_curr WHERE ticker in ('{}')"

    # get selected equities quote performance
    #tkStr = "','".join(eqtLst)
    #eqtRtn = sqlQuery(xqTmp.format(tkStr))[colLst]
    jobj = dict(ticker={'$in': eqtLst})
    eqtRtn = find_mdb(jobj, dbname='ara', tablename='yh_quote_curr',
                      dfTF=True)[0][colLst]

    # get indices quote performance
    #tkStr = "','".join(idxLst)
    #idxRtn = sqlQuery(xqTmp.format(tkStr))[colLst]
    jobj = dict(ticker={'$in': idxLst})
    idxRtn = find_mdb(jobj, dbname='ara', tablename='yh_quote_curr',
                      dfTF=True)[0][colLst]

    # calc 'topLst' w.r.t. the 'sign'/'topUpDn' of 'topIndex'
    pbdate = idxRtn.query("ticker=='{}'".format(tkLead))['pbdate'].iloc[0]
    chgPct = idxRtn.query("ticker=='{}'".format(tkLead))[xCol].iloc[0]
    topUpDn = udfStr(chgPct, ['UP', 'DOWN', 'FLAT'], thd)
    topSign = udfStr(chgPct, [1, 0, -1], thd)
    sign = False if chgPct >= 0 else True
    xd = eqtRtn.sort_values(by=[xCol], ascending=sign)
    leadLst = xd.iloc[:np]
    if (xd['changePercent'].iloc[0] * xd['changePercent'].iloc[-1]) < 0:
        bttmLst = xd.iloc[-1:]
    else:
        bttmLst = []

    # update my lead index in the top level
    dd = dict(topIndex=tkLead)
    dd.update(topUpDn=topUpDn)

    # add  all indices info to idxLst
    dd.update(indexLst=idxRtn[colLst].to_dict(orient='records'))
    indexOrder = [x['ticker'] for x in dd['indexLst']]
    dd.update(indexOrder=indexOrder)

    # determine if indices are all Up/Undetermined/Down
    if all([x['changePercent'] < 0 for x in dd['indexLst']]):
        allUpDn = -1
    elif all([x['changePercent'] > 0 for x in dd['indexLst']]):
        allUpDn = 1
    else:
        allUpDn = 0
    dd.update(allUpDn=allUpDn)

    # add  topLst
    if len(leadLst) > 0:
        dd.update(topLst=leadLst[colLst].to_dict(orient='records'))
    else:
        dd.update(topLst=[])
    if len(bttmLst) > 0:
        dd.update(bttmLst=subDict(bttmLst, colLst).to_dict(orient='records'))
    else:
        dd.update(bttmLst=[])

    # get hiloRecord (based on past 1-year daily change since end date)
    hiloRecord = find_hiloRecord(ticker=tkLead, end=pbdate, days=366)
    dd.update(hiloRecord=hiloRecord)
    dd.update(start=pbdate)
    dd.update(mp3YN=False)

    return dd