def upload_portfolio_allocations(user_id, portfolio_id, filename, name): df = pd.read_excel(filename) db = Connection() table = db.get_table("Portfolio") info = lambda f: { "HashKey": f["CoinID"], "Name": f["Name"], "Allocation": Decimal(str(f["Allocation"])) } allocs = [info(df.iloc[i]) for i in range(len(df))] for a in allocs: coin = db.get_coin(a['HashKey'], get_ts=True) if len(coin.Performance) < 6: a['Allocation'] = Decimal(0) updates = {"Allocations": {"Value": allocs, "Action": "PUT"}} key = {"CompanyID": user_id, "HashKey": portfolio_id} table.update_item(Key=key, AttributeUpdates=updates) port = Portfolio({ "HashKey": portfolio_id, "Allocations": allocs, "Name": name }) proforma = port.calc_proforma(db) recalculate_portfolio_coin(portfolio_id, user_id, proforma, name, db)
def create_allocation_template(userid, portfolio_id, target_file): """ Creates excel file with current allocations as well as all Coins that can be included in portfolios """ try: db = Connection() watched_coins = sorted(db.get_watched_coins(userid, get_ts=True), key=alloc_sort) port = db.get_portfolio(userid, portfolio_id) allocs = port.Allocations alloc_ids = [a['HashKey'] for a in allocs] wb = xlwt.Workbook() wsh = wb.add_sheet('Allocations') wsh.write(0, 0, "CoinID") wsh.write(0, 1, "Name") wsh.write(0, 2, "Allocation") wsh.write(0, 3, "T") wsh.write(0, 4, "Start Date") wsh.write(0, 5, "End Date") wsh.write(0, 6, "Frequency") def write_coin_stats(coin, i): wsh.write(i + 1, 3, len(coin.Performance)) if (len(coin.Performance)) > 0: wsh.write( i + 1, 4, min(coin.Performance.index).to_datetime().strftime( "%m/%d/%Y")) wsh.write( i + 1, 5, max(coin.Performance.index).to_datetime().strftime( "%m/%d/%Y")) wsh.write(i + 1, 6, get_frequency(coin.Performance)) for i, alloc in enumerate(allocs): hashkey = alloc['HashKey'] name = alloc['Name'] coin = db.get_coin(hashkey) w = alloc['Allocation'] wsh.write(i + 1, 0, hashkey) wsh.write(i + 1, 1, name) wsh.write(i + 1, 2, w) write_coin_stats(coin, i) i = len(allocs) for coin in watched_coins: if coin.HashKey not in alloc_ids: wsh.write(i + 1, 0, coin.HashKey) wsh.write(i + 1, 1, coin.Name) wsh.write(i + 1, 2, 0) write_coin_stats(coin, i) i = i + 1 wb.save(target_file) except Exception as ex: return False return True
for field_info in fields: field_name = field_info['Key'] field_label = field_info['Label'] if 'Format' in field_info: fmt = field_info['Format'] else: fmt = "{0}" field = FieldInfo(field_name, field_label, fmt) self.field_list.append(field_name) self.help_dict[field_name] = field def help(self, obj): obj_dict = obj.__dict__ fields = [self.help_dict[f] for f in self.field_list if f in obj_dict] return [(f.label, f.format(obj_dict[f.key])) for f in fields] if __name__ == '__main__': helper = StatsHelper() from analytics.basicstats import BasicStats from datalib.datalib import Connection coinid = 'e042001c-9a43-11e6-bfb2-14109fdf0df7' db = Connection() coin = db.get_coin(coinid) stats = BasicStats(coin.Performance) res = helper.help(stats)