def create_theme_media(dbname="ara",
                       tablename="topic_theme_media",
                       debugTF=False,
                       **optx):
    ''' create MDB::'topic_theme_media' table based on 'madmoney_hist' info
	arguments:
	'end' default to 2-weeks of today in YYYYMMDD 
	'callLst' default to ["4","5"] types
	'''
    zpk = getKeyVal(optx, 'zpk', {'ticker', 'pbdate', 'start', 'end'})
    start = getKeyVal(optx, 'start', 0)
    end = getKeyVal(optx, 'end', None)
    callLst = getKeyVal(optx, 'callLst', ["4", "5"])
    if end is None:
        end = int(dt2ymd(next_date(weeks=-2)))
    xqTmp = "SELECT pbdate,adjusted as price FROM prc_hist where name='{}' and pbdate>{} and pbdate <={} ORDER BY pbdate"
    jobj = {"Call": {"$in": callLst}, "pbdate": {"$lte": end, "$gte": start}}
    df, mdb, emsg = find_mdb(jobj,
                             sortLst=["pbdate"],
                             dbname="ara",
                             tablename="madmoney_hist",
                             dfTF=True)

    dd = []
    for j in df.index:
        dx = df.iloc[j].to_dict()
        dx['pbdate'] = int(dx['pbdate'])
        ticker = dx['ticker']
        pbdate = period1 = dx['pbdate']
        period2 = int(dt2ymd(next_date(period1, months=1)))
        xqr = xqTmp.format(ticker, period1, period2)
        dp = sqlQuery(xqr)
        vprc = dp['price'].values
        if len(vprc) < 1:
            continue
        vdate = [int(x) for x in dp['pbdate'].values]
        sPrice, ePrice = vprc[0], vprc[-1]
        period1, period2 = vdate[0], vdate[-1]
        rrt = vprc[-1] / vprc[0] * 100. - 100
        day_diff = ymd_diff(period1, period2)
        dx.update(rrt=rrt, day_diff=day_diff, start=period1, end=period2)
        dx.update(sPrice=sPrice, ePrice=ePrice)
        sys.stderr.write(
            "{j:03d}|{ticker:5s}|{Call}|{rrt:8.2f}|{day_diff:3d}|{start:8d}|{end:8d}|{pbdate:8d}|{Company}|\n"
            .format(j=j, **dx))
        dd.append(dx)
        dy, _, emsg = insert_mdb([dx],
                                 tablename=tablename,
                                 dbname=dbname,
                                 zpk=zpk)

    if start > 12345678:
        jobj = {"day_diff": {"$lt": 20}, "pbdate": {"$lte": start}}
        mdb[dbname][tablename].delete_many(jobj)
    df = pd.DataFrame(dd)
    return df
Exemple #2
0
def get_exch_time(start=None,end=None,isoCode='XNYS',tz='America/New_York',dbname='ara',tablename='calendar_trading',wmode=None,debugTF=True):
	cal = get_calendar(isoCode)
	open_time = cal._opens.tz_convert(tz).to_pydatetime()[start:end]
	close_time = cal._closes.tz_convert(tz).to_pydatetime()[start:end]
	pbdate=[ int(dt2ymd(x)) for x in close_time]
	df = pd.DataFrame(data=np.array([open_time,close_time,pbdate]).T,columns=['open_time','close_time','pbdate'])
	df['iso_code'] = isoCode
	if wmode:
		save2pgdb(df,db=dbname,tablename=tablename,wmode=wmode)
		pname = "pickle/{tablename}.{isoCode}.pickle".format(**locals())
		fp = open(pname,"wb")
		pickle.dump(df,fp)
		fp.close()
		if debugTF:
			sys.stderr.write("Calendar Saved to Table:{tablename}: Pickle:{pname}\n".format(**locals()))
	return df
Exemple #3
0
def gen_chartpath(chartname='',
                  chartdir='US/mp3_hourly',
                  chartformat='svg',
                  epoch=None,
                  **optx):
    """
	Generate 'chartpath', 'chartname' as
	  {chartdir}/{templateName}_{ticker}_{epoch}.{chartformat},
	  {templateName}_{ticker}_{epoch}.{chartformat}
	where
	  'chartdir','chartformat' and 'epoch' are optional
	Example,
	  gen_chartpath(templateName='stock_performance',ticker='AAPL')
	Note,
	  'ticker' with '^' or '=' characters will be removed
	"""
    chartname = '' if not isinstance(chartname, str) else chartname
    if len(chartname) > 0:
        if len(chartdir) > 0:
            chartpath = "{}/{}".format(chartdir, chartname)
        else:
            chartpath = chartname
        return chartpath, chartname
    if epoch is None:
        epoch = dt2ymd(next_date(), "%s")
    chartname = chartpath = ''
    ky = ['templateName', 'ticker']
    kLst = getKeyVal(optx, ky, [], extendTF=True)
    kLst.extend([epoch, chartformat])
    if not any([x is None for x in kLst]):
        chartname = "{}_{}_{}.{}".format(*kLst)
        chartname = re.sub('[\^=]', '', chartname)
    if len(chartdir) > 0 and len(chartname) > 0:
        chartpath = "{}/{}".format(chartdir, chartname)
    else:
        chartpath = chartname
    return chartpath, chartname
Exemple #4
0
def run_rmc(jobj):
    """
	run Remote-Market-Commentary based on input dict: [jobj]
	with keys: tmplstr,argstr,ticker,tmplrpt,username,category,tmplname,action
	return string for Market Commentary
	"""
    global username, ticker, lang, dirname
    if 'username' in jobj:
        username = jobj['username']
    if 'ticker' in jobj:
        ticker = jobj['ticker']
    if 'lang' in jobj:
        lang = jobj['lang']
    dirname = 'templates/'
    jobj.update(dirname=dirname)
    jobj.update(mp3YN=False)
    jobj.update(start=dt2ymd(next_date()))
    # run rmc based on tmplstr
    if str(jobj['tmpltype']) == 'pppscf' and str(jobj['action']) == 'run':
        jobj['tmplrpt'] = batch_pppscf(
            [jobj['ticker']],
            optx={
                "j2ts": jobj['tmplstr'],
                "lang": jobj['lang'],
                "debug": True,
                "saveDB": False,
                "category": jobj['category']
            })
        # run_alan_pppscf.batch_pppscf() 1st then import gData in batch_pppscf()
        from _alan_pppscf import gData
        jobj["retcode"] = "run successfully"
    elif str(jobj['tmpltype']) == 'fcs' and str(jobj['action']) == 'run':
        jobj['tmplrpt'] = batch_ohlc_fcs(
            [jobj['ticker']],
            optx={
                "j2ts": jobj['tmplstr'],
                "lang": jobj['lang'],
                "debug": True,
                "saveDB": False,
                "category": jobj['category']
            })
        from _alan_ohlc_fcs import gData
        jobj["retcode"] = "run successfully"
    elif str(jobj['tmpltype']) == 'eps' and str(jobj['action']) == 'run':
        from fcs_eps_price import run_eps_fcs
        gData = run_eps_fcs([jobj['ticker']])[0]
        jobj['tmplrpt'] = argstr_tmplrpt(jobj['tmplstr'], gData,
                                         jobj['argstr'].replace("\n", ''))
        jobj["retcode"] = "run successfully"
    elif str(jobj['tmpltype']) == 'mkt' and str(jobj['action']) == 'run':
        # Same as command line:
        # headline_writer.py --lang=cn --extra_xs='onTheFly=True;j2ts="{% include \"daily_briefing_cn.j2\" %}";dirname="templates"' --start=20190705 2>/dev/null

        from headline_writer import generate_headline
        optx = dict(j2ts=jobj['tmplstr'], onTheFly=True)
        optx.update(jobj)
        if jobj['argsflg'] == '1' and len(jobj['argstr']) > 0:
            gdd = {}
            exec(jobj['argstr'], globals(), gdd)
            optx.update(gdd)
        try:
            ret = generate_headline(**optx)
        except Exception as e:
            err_msg = "**ERROR:{} @ {} ".format(str(e), "generate_headline")
            pqint(err_msg, file=sys.stderr)
            ret = err_msg
        jobj['tmplrpt'] = ret
    elif str(jobj['tmpltype']) == 'test' and str(jobj['action']) == 'run':
        optx = {'j2ts_header': jobj['tmplstr']}
        optx.update(jobj)
        try:
            # use 'jobj' as internal object and update jobj's key/val into **optx
            pqint("===username: {}, argflg: {},argstr: {}".format(
                username, jobj['argsflg'], jobj['argstr']),
                  file=sys.stderr)
            if jobj['argsflg'] == '1' and len(jobj['argstr']) > 0:
                gdd = {}
                xstr = jobj['argstr']
                exec(xstr, globals(), gdd)
                if 'jobj' in gdd:
                    optx.update(gdd['jobj'])
                    pqint("Add additional:{}".format(gdd['jobj']),
                          file=sys.stderr)
                else:
                    optx.update(gdd)
                    pqint(gdd, file=sys.stderr)
        except Exception as e:
            pqint("**ERROR:{} @ {} ".format(str(e), "exec argstr"),
                  file=sys.stderr)
        pqint(optx)
        ret = jj_fmt(jobj['tmplstr'], **optx)
        jobj['tmplrpt'] = ret
    # TBD: testing 'get' instead of lsi2nlg_calc()
    elif str(jobj['action']) == 'get':
        mobj = find_lsi2nlg_info(jobj=jobj)
        jobj.update(mobj)
        jobj['argsflg'] = 1
    # TBD: testing 'save' instead of lsi2nlg_calc()
    #elif str(jobj['action']) == 'save':
    #	mobj = write_lsi2nlg_info(jobj)
    #	jobj['argsflg']=1
    else:
        jobj['argstr'] = jobj['argstr'].replace("\n", '')
        jobj = lsi2nlg_calc(jobj)
        jobj['argsflg'] = 1
    # run rmc with additional argstr command
    if str(jobj['tmpltype']) in ['eps', 'fcs', 'pppscf'] and str(
            jobj['action']) == 'run':
        if 'itnstr' in jobj:
            try:
                jobj['itnstr'] = argstr_set(gData, ['fp'])
            except Exception as e:
                pqint("**ERROR:{} @ {} ".format(str(e),
                                                "Assign Internal Variables"),
                      file=sys.stderr)
        try:
            if 'argsflg' in jobj and int(jobj['argsflg']) == 1:
                jobj['tmplrpt'] = argstr_tmplrpt(
                    jobj['tmplstr'], gData, jobj['itnstr'],
                    jobj['argstr'].replace("\n", ''))
        except Exception as e:
            pqint("**ERROR:{} @ {} ".format(str(e),
                                            "Apply Additional Variables"),
                  file=sys.stderr)
            jobj["retcode"] = str(e)
    jobj["lastmod"] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    jstr = json.dumps(jobj)
    return jstr
             shortName='Company',
             fiftyTwoWeekRange='Range52Week',
             changePercent='dayChg%',
             change='Chg',
             pbdt='pubDate'))
    df = mediaD.merge(quoD,
                      on='ticker')  #- remove no-quote rows # ,how='left')

    df.dropna(subset=['marketCap'], inplace=True)
    df['buyChg%'] = (df['closePrice'] / df['buyPrice'].astype(float) - 1) * 100
    colX = [
        'ticker', 'buyCount', 'buyDate', 'marketCap', 'buyPrice', 'closePrice',
        'buyChg%', 'dayChg%', 'EPS', 'Company', 'Range52Week', 'pbdate',
        'pubDate', 'sector', 'industry'
    ]
    #df=subDict(df,colX)
    print(" --media DF:\n{}".format(df), file=sys.stderr)
    zpk = optx.pop('zpk', {'ticker'})
    upsert_mdb(df, dbname=dbname, tablename=tablename, zpk=zpk)
    sys.stderr.write(" --DF:\n{}\n".format(df.head().to_string(index=False)))
    return df


if __name__ == '__main__':
    #start=int(dt2ymd(next_date(weeks=-6)))
    #df = create_theme_media(debugTF=True,start=start)
    start = int(dt2ymd(next_date(months=-7, day=31)))
    df = create_topic_theme_media(debugTF=True, start=start)
    colx = ['ticker', 'rrt', 'end', 'Company', 'pbdate', 'buyCount']
    #print(subDict(df,colx).sort_values(['buyCount'],ascending=False),file=sys.stderr)