Ejemplo n.º 1
0
def main_tst(description="Stock Update MP4 Creator",usage="Usage: %prog [option] daily_briefing|intraday_briefing [SYMBOL] [True|False]"):
	opts, args = parse_opt(sys.argv, description=description,usage=usage)
	ticker=getKeyVal(opts,'ticker','AAPL')
	txtOnly=getKeyVal(opts,'txtOnly',False)
	_pn_={}
	tmplname = 'daily_briefing' if len(args)<1 else args[0]
	ticker = ticker if len(args)<2 else args[1]
	txtOnly = True if len(args)>2 and args[2][:1].lower() in ['1','t'] else txtOnly
	sys.stderr.write("===RUN:{}:{}:{}\n".format(tmplname,ticker,txtOnly))
	dpn = tmpl_wrapper(tmplname,ticker=ticker,debugTF=True,txtOnly=txtOnly)
	return dpn
Ejemplo n.º 2
0
def generate_headline(opts={},**optx):
	if not opts:
		opts, args = parse_opt(sys.argv)
	if optx:
		opts.update(optx)
	debugTF = opts['debugTF'] if 'debugTF' in opts else False
	from headline_sts import headline_hist
	from record_hilo import record_hilo_tst
	if 'nlookback' not in opts:
		nlookback=1
	else:
		nlookback = max(1,int(opts['nlookback']))
	lang = opts['lang']
	ndays=nlookback+1
	if debugTF:
		sys.stderr.write("{}\n{}\n".format("===generate_headline():opts",opts))
	if 'j2ts' in opts and opts['j2ts'] is not None and len(opts['j2ts'])>0:
		ts=opts['j2ts']
		del opts['j2ts']
	elif 'j2name' in opts and len(opts['j2name'])>0:
		fj2name = opts['j2name'].replace('.j2','')+'.j2'
		ts='{} include "{}" {}'.format('{%',fj2name,'%}')
		del opts['j2name']
	else:
		ts="""
		{%- set xcase = 2 if chgLevel2>200 or chgLevel2<-200 else 1 -%}
		{%- set headline_file='daily_headline_{}_{}.j2'.format(xcase,lang) -%}
		{%- include headline_file -%}
		"""
	start = opts['start'] if 'end' in opts else None
	df = headline_hist(ndays=ndays,saveDB=False,end=start)
	jobj = record_hilo_tst(opts) # hilo report
	if df.shape[0]<1:
		return ''
	dd = df.iloc[0].to_dict() # headline data output
	ret = create_headline(dd,ts=ts,jobj=jobj,**opts)
	if debugTF:
		sys.stderr.write("{}\n{}\n{}\n{}\n".format("===generate_headline():Comment,HeadlineData,HiLoData",ret,dd,jobj))
	return ret
Ejemplo n.º 3
0
def headline_writer(opts={},**optx):
	if not opts:
		opts, args = parse_opt(sys.argv)
	if optx:
		opts.update(optx)
	lang = opts['lang']
	nlookback = int(opts['nlookback'])
	#xqr="SELECT * FROM headline_hist ORDER BY pbdate DESC limit {}".format(nlookback)
	#dj=sqlQuery(xqr).to_dict(orient='records')
	dj,_,_=find_mdb({},tablename="headline_hist",dbname="ara",sortLst=['pbdate'],limit=nlookback)
	ts="""
	{%- set xcase = 2 if chgLevel2>200 or chgLevel2<-200 else 1 -%}
	{%- set headline_file='daily_headline_{}_{}.j2'.format(xcase,lang) -%}
	{%- include headline_file -%}
	"""

	for dd in dj:
		try:
			ret = create_headline(dd,ts=ts,**opts)
			sys.stderr.write("{}\n".format(ret))
		except Exception as e:
			sys.stderr.write("{}:{}".format(dd['pbdate'],str(e)))
			continue
Ejemplo n.º 4
0
	''' 
	return yahoo screener DictList/DataFrame relavant info based on 'scrIds'
	'''
	try:
		jdTmp = yh_predefined(scrIds=scrIds)['finance']['result'][0]['quotes']
	except Exception as e:
			sys.stderr.write("**ERROR: {}: {}\n".format("yh_predefined_query", str(e)))
			return []
	dd=[]
	for jobj in jdTmp:
		try:
			if screenerTF:
				d = screener_output_1(jobj)
			else:
				d = jobj
			dd.append(d)
		except Exception as e:
			sys.stderr.write("**ERROR: {}: {}\n".format("yh_predefined_query", str(e)))
			continue
	if dfTF:
		dd = pd.DataFrame(dd)
	return dd

if __name__ == '__main__':
	description="Get predefined data via query2.finance.yahoo.com/v1/finance/screener"
	opts, args = parse_opt(sys.argv, description=description)
	if len(args)<1:
		args.append('most_actives')
	dd = bb_predefined(args[0],**opts)
	sys.stderr.write("\n{}\n".format(dd.to_string()))
Ejemplo n.º 5
0
		sys.stderr.write("Save to {}\n".format(tablename))
		#save2pgdb(df,db=pgDB,tablename=tablename)
		clientM=None
		zpk={"ticker","pbdate"}
		mobj,clientM,msg = write2mdb(df,clientM,tablename=tablename,zpk=zpk)

	if 'topDict' in optx and optx['topDict']==True:
		if 'topLst1' in df:
			df['topLst1']=[ ast.literal_eval(x) if hasattr(df['topLst1'], "__len__") else {} for x in df['topLst1'] ]
		if 'topLst2' in df:
			df['topLst2']=[ ast.literal_eval(x) if hasattr(df['topLst2'], "__len__") else {} for x in df['topLst2'] ]

	df['chg1']=df['chgLevel1']
	df['chg2']=df['chgLevel2']
	df['allUpDn']=0
	for j in range(df.shape[0]):
		if all([ x>0 for x in df[['chgPct1','chgPct2','chgPct3']].iloc[j] ]) :
			allUpDn = 1
		elif all([ x<0 for x in df[['chgPct1','chgPct2','chgPct3']].iloc[j] ]) :
			allUpDn = -1
		else:
			allUpDn = 0
		df.loc[df.index[j],'allUpDn']=allUpDn
	return df

if __name__ == '__main__':
	description="""Find leading stocks (up/down > 1%) w.r.t. SP500/DowJone's up/down movement"""
	(opts, args)=parse_opt(sys.argv,ndays=2,outTF=True,saveDB=False)
	df = headline_hist(topDict=True,**opts)
	print(df.to_dict(orient='records'))
Ejemplo n.º 6
0
		return ret
	tqLst=df['peers'].values
	dq = merge_ticker_cik(tqLst)
	if len(dq)>0:
		dq.rename(columns={'ticker':'peers'},inplace=True)
		df = df.merge(dq,on='peers',how='left')
	if peersLstTF:
		return peerLst
	if len(df)>0: 
		peerInfo=dict(peersList=','.join(df['peers'].values),peers=list(df['peers']))
		peerInfo.update(peers_cn=list(df['company_cn']))
	else :
		peerInfo=dict(peersList='',peers=[],peers_cn=[])
	return df,peerLst,peerInfo

def iex_peers(tkLst=[],debugTF=False,peersLstTF=False):
	""" Get peer group based on tickers: 'tkLst'
	passthru of yh_peers()
	help(yh_peers)
	"""
	return yh_peers(tkLst=tkLst,debugTF=debugTF,peersLstTF=peersLstTF)

if __name__ == '__main__':
	from _alan_optparse import parse_opt, subDict
	opts, args = parse_opt(sys.argv)
	df,peerLst,peerInfo = yh_peers(args,debugTF=opts['debugTF'])
	if opts['output'] in ['csv','html','dict']:
		sys.stdout.write("{}".format(df2out(df,output=opts['output'])))
	else:
		sys.stdout.write("{}".format(peerLst))
Ejemplo n.º 7
0
def record_hilo_tst(opts={}, **optx):
    ## ASSIGN local variables
    if len(opts) < 1:
        opts, args = parse_opt(sys.argv)
    opts.update(optx)
    ticker = args[0] if len(args[0]) > 0 else '^GSPC'
    ticker = getKeyVal(opts, 'ticker', ticker)
    debugTF = getKeyVal(opts, 'debugTF', False)
    start, end, days = getKeyVal(opts, ['start', 'end', 'days'],
                                 [None, None, 3600])
    tablename = getKeyVal(opts, 'tablename', None)
    funcName = getKeyVal(opts, 'funcName', 'recordHiLo2')
    nlookback = getKeyVal(opts, 'nlookback', 1)
    src = getKeyVal(opts, 'src', 'yh')
    searchDB = getKeyVal(opts, 'searchDB', True)
    colLst = getKeyVal(opts, 'colLst', ['close', 'pchg'])
    colx = getKeyVal(opts, 'colx', 'pchg')
    mthLst = getKeyVal(opts, 'mthLst', [])
    if debugTF == True:
        sys.stderr.write("OPTS:{}\n".format(opts))
    ## ARRANGE additional local variables
    nlookback = -int(nlookback)
    if funcName in globals():
        funcArg = globals()[funcName]
    else:
        funcArg = recordHiLo
    clientM = None
    ## GET DATA
    df = psd(ticker,
             start=start,
             end=end,
             days=days,
             src=src,
             searchDB=searchDB,
             debugTF=debugTF)
    if debugTF == True:
        sys.stderr.write("DF:\n{}\n".format(df.tail()))
    if colx == 'pchg' and colx not in df and 'close' in df:
        df['pchg'] = np.round(df['close'].pct_change(), 4)
    ## LOOPING funcArg for backtest
    for xd in df.index[nlookback:]:
        try:
            dx = df.loc[df.index <= xd]
            pbdate = int(xd.strftime('%Y%m%d'))
            jobj = funcArg(dx,
                           endDT=xd,
                           ticker=ticker,
                           colx=colx,
                           colLst=colLst,
                           mthLst=mthLst)
            if debugTF == True:
                sys.stderr.write("{}\n".format(dx.iloc[-1]))
            #if any([jobj['MTD'],jobj['YTD']]):
            if jobj['YTD']:
                sys.stderr.write("{}\n".format(jobj['YTD']))
                if tablename is not None:
                    zpk = {"ticker", "pbdate"}
                    mobj, clientM, msg = write2mdb(jobj,
                                                   clientM,
                                                   tablename=tablename,
                                                   zpk=zpk)
        except Exception as e:
            continue
    return jobj