def init(): ''' Populates investment portfolio with universe, benchmark(s), and current portfolio(s). Also populates variables needed for the drop-downs like what attributes to consider. ''' #configure instrument universe - the total set of instruments to be considered in the analysis. instrumentUniverse,iu_holdings = initialize.universe_from_csv() res = investmentportfolio.Create_Portfolio(instrumentUniverse) #add initial set of holdings to instrument universe res = investmentportfolio.Create_Portfolio_Holdings('instrument_universe',iu_holdings) #configure benchmark portfolios benchmarks = initialize.benchmarks_from_csv() for b in benchmarks: res = investmentportfolio.Create_Portfolio(b[0]) #add initial set of holdings to benchmark res = investmentportfolio.Create_Portfolio_Holdings(b[0]['name'],b[1]) #configure initial portfolio my_portfolio,mp_holdings = initialize.portfolio_from_csv() res = investmentportfolio.Create_Portfolio(my_portfolio) #add initial set of holdings to my portfolio res = investmentportfolio.Create_Portfolio_Holdings(my_portfolio['name'],mp_holdings) return Response(json.dumps(res), mimetype='application/json')
def portfolio_from_csv(): """ Loads a portfolio in Algo Risk Service (ARS) format into the Investment Portfolio service. """ holdings = { 'timestamp':'{:%Y-%m-%dT%H:%M:%S.%fZ}'.format(datetime.datetime.now()), 'holdings':[] } data = json.loads(request.data) data = [row.split(',') for row in data] headers = data[0] #Loop through and segregate each portfolio by its identifier (there may be multiple in the file) #Column 1 (not 0) is the ID column. Column 5 is the PORTFOLIO column... portfolios = {} unique_id_col = headers.index("UNIQUE ID") id_type_col = headers.index("ID TYPE") name_col = headers.index("NAME") pos_units_col = headers.index("POSITION UNITS") portfolio_col = headers.index("PORTFOLIO") price_col = headers.index("PRICE") currency_col = headers.index("CURRENCY") #for d in data... for d in data[1:]: hldg = { "name":d[name_col], "instrumentId":d[unique_id_col], "quantity":d[pos_units_col] } if len(headers)>5: for meta in headers[6:]: hldg[meta.replace('\r','')] = d[headers.index(meta)].replace('\r','') if d[portfolio_col] not in portfolios: portfolios[d[portfolio_col]] = [hldg] else: portfolios[d[5]].append(hldg) #Send each portfolio and its holdings to the investment portfolio service for key, value in portfolios.items(): my_portfolio = { "timestamp": '{:%Y-%m-%dT%H:%M:%S.%fZ}'.format(datetime.datetime.now()) , 'closed':False, 'data':{'type':'unit test portfolio'}, 'name':key } #create portfolio try: req = investmentportfolio.Create_Portfolio(my_portfolio) except: print("Unable to create portfolio for " + str(key) + ".") try: for h in range(0,len(value),500): hldgs = value[h:h+500] req = investmentportfolio.Create_Portfolio_Holdings(str(key),hldgs) except: print("Unable to create portfolio holdings for " + str(key) + ".") return req