def intraday(): try: todatestring = str(datetime.datetime.now().date()) fromdatestring = todatestring[:-2] + '01' tickerstring = request.args.get('tickerstring', default='MSFT,AAPL', type=str) fromdatestring = request.args.get('fromdate', default=fromdatestring, type=str) todatestring = request.args.get('todate', default=todatestring, type=str) days = request.args.get('days', default=1, type=int) import pullstackedreturns as psp symbols = tickerstring.split(',') fromdate, todate = fromdatestring, todatestring df_final = pd.DataFrame() for symbol in symbols: df = pid.intradaystockprices(ticker=symbol, period=60, days=days) if len(df_final) == 0: df_final = df else: df_final = df_final.append(df, ignore_index=True) df_final.index = df_final.index.map(str) list_of_dicts = df_final.T.to_dict() ret = jsonify({'intraday': list_of_dicts}) except Exception as inst: ret = 'Main.py My error was caught: ' + str(inst) return ret
def stockhistoryasdataframeindividual(symbols, fromdate, todate): import pullintradayprices as pid import pandas as pd from datetime import datetime p = stockhistory(symbols, fromdate, todate) list_of_dicts = [] list_of_missing = [] try: for d, item in p.swapaxes(0, 1).iteritems(): for t, x in item.iteritems(): if np.isnan(x['Adj Close']) == True: list_of_missing.append({ 'Date': d, 'Ticker': t, 'Adj Close': x['Adj Close'], 'Close': x['Close'] }) else: list_of_dicts.append({ 'Date': d, 'Ticker': t, 'Adj Close': x['Adj Close'], 'Close': x['Close'] }) sysdate = str(datetime.now()).split()[0] if todate >= sysdate: for s in symbols: df = pid.intradaystockprices(ticker=s, period=60, days=1) datex = df.tail(1).index[0] myvalue = df.tail(1)['Close'][0] list_of_dicts.append({ 'Date': datex, 'Ticker': s, 'Adj Close': myvalue, 'Close': myvalue }) except Exception as e: print(e) df_good = pd.DataFrame(list_of_dicts) #print 'check',isinstance(df_good.index, pd.DatetimeIndex) #datetime_object = datetime.strptime(sysdate, '%Y-%m-%d') #longdate = sysdate + ' 00:00:00' #df_good.drop(longdate, inplace=True) df_missing = pd.DataFrame(list_of_missing) return df_good, df_missing
def stockcurrentasdataframe(symbols, ): import pullintradayprices as pid list_of_dicts = [] for s in symbols: df = pid.intradaystockprices(ticker=s, period=60, days=1) d = df.tail(1).index[0] myvalue = df.tail(1)['Close'][0] list_of_dicts.append({ 'Date': d, 'Ticker': s, 'Adj Close': myvalue, 'Close': myvalue }) print list_of_dicts return df
def stockhistoryasdataframeindividual(symbols, fromdate, todate): #import numpy as np import pullintradayprices as pid import pandas as pd p = stockhistory(symbols, fromdate, todate) list_of_dicts = [] list_of_missing = [] try: for d, item in p.swapaxes(0, 1).iteritems(): for t, x in item.iteritems(): if np.isnan(x['Adj Close']) == True: list_of_missing.append({ 'Date': d, 'Ticker': t, 'Adj Close': x['Adj Close'], 'Close': x['Close'] }) else: list_of_dicts.append({ 'Date': d, 'Ticker': t, 'Adj Close': x['Adj Close'], 'Close': x['Close'] }) # Append intraday price #list_of_dicts = [] for s in symbols: df = pid.intradaystockprices(ticker=s, period=60, days=1) d = df.tail(1).index[0] myvalue = df.tail(1)['Close'][0] list_of_dicts.append({ 'Date': d, 'Ticker': s, 'Adj Close': myvalue, 'Close': myvalue }) except Exception as e: print(e) df_good = pd.DataFrame(list_of_dicts) df_missing = pd.DataFrame(list_of_missing) return df_good, df_missing
def getfile(): try: if request.method == 'POST': clearcontentsofcache() print 'yyyyy' date14 = str(datetime.datetime.now().strftime("%Y%m%d%H%M%S")) feature = request.form['feature'] tickerstring = request.form['tickerstring'] fromdate = request.form['fromdate'] todate = request.form['todate'] tickerstring = "".join(tickerstring.split()) symbols = tickerstring.split(',') feature = feature[:3].lower() print 'check', tickerstring, feature print symbols if feature == 'cov': myfilename = covariancematrix(symbols, fromdate, todate) elif feature == 'cor': myfilename = correlationmatrix(symbols, fromdate, todate) elif feature == 'pri': myfilename = 'prices_' + date14 + '.csv' cachedfilepathname = os.path.join(app.config['UPLOAD_FOLDER'], myfilename) df = psp.stockpricesstacked(symbols, fromdate, todate) df.to_csv(cachedfilepathname, columns=(list(df.columns.values))) time.sleep(1.5) elif feature == 'adj': myfilename = 'adjclose_' + date14 + '.csv' cachedfilepathname = os.path.join(app.config['UPLOAD_FOLDER'], myfilename) df = psp.stockpricesstacked(symbols, fromdate, todate, pricechangeortotal='total') df.to_csv(cachedfilepathname, columns=(list(df.columns.values))) time.sleep(1.5) elif feature == 'ret': myfilename = 'returns_' + date14 + '.csv' cachedfilepathname = os.path.join(app.config['UPLOAD_FOLDER'], myfilename) df = psr.stockreturnsstacked(symbols, fromdate, todate) df.to_csv(cachedfilepathname, columns=(list(df.columns.values))) time.sleep(1.5) elif feature == 'int': print 'got here xxx' myfilename = 'intraday_' + date14 + '.csv' cachedfilepathname = os.path.join(app.config['UPLOAD_FOLDER'], myfilename) days = request.args.get('days', default=1, type=int) df = pd.DataFrame() for symbol in symbols: print 'xx', symbol df_x = pid.intradaystockprices(ticker=symbol, period=60, days=days) if len(df) == 0: df = df_x else: df = df.append(df_x) df.to_csv(cachedfilepathname, columns=(list(df.columns.values))) time.sleep(1.5) ret = 'Successfully executed.' return send_file('cache/' + myfilename, mimetype='text/csv', attachment_filename=myfilename, as_attachment=True) else: #print 'xxx' return initial_html except Exception as inst: ret = 'getfile My error was caught: ' + str(inst) return initial_html