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
def save_session(session): db = Connection() table = db.get_table("Session") table.put_item(Item=session)
async def import_regime_model(tab_name): with open(output_path + "%s.json" % tab_name, 'r') as load_f: items = json.load(load_f) db = Connection.getInstance() for index, obj in enumerate(items): history = obj["history"] history = json.dumps(history) res = await findRegimeModel(obj["model_name"]) if res: model = await RegimeModel.find(res.id) historys = await update_regime_model(db, obj["model_name"], obj["factor_code"], obj["ngegimes"]) historys = [{ "Regime": history["Regime"], "Date": float(history["Date"]) } for history in historys] model.factor_code = obj["factor_code"] model.ngegimes = int(obj["ngegimes"]) model.history = json.dumps(historys) await model.update() else: factor = RegimeModel(factor_code=obj["factor_code"], model_name=obj["model_name"], ngegimes=obj["ngegimes"], history=history) await factor.save()
def __init__(self, folio, userid=None, model_ticker=None,regime_model_name=None): self.db = Connection.getInstance() self.folio = folio self.Name = folio.Name self.user_id = userid self.model_ticker = model_ticker self.regime_model_name = regime_model_name
async def toFolioPerformance(user_id, folio_id, folio, timestamp, allocations): port = port_folio({"folio_id": folio.id, "Allocations": allocations, "Name": folio.name}) modify_at = toPreTimestamp(folio.created_at, day_num=365) db = Connection.getInstance() effective_date = datetime.datetime.utcfromtimestamp(int(folio.created_at)) effective_date = str(effective_date).split(" ")[0] + " 00:00:00" pre_at = int(time.mktime(time.strptime(effective_date, '%Y-%m-%d %H:%M:%S'))) proforma = await port.calc_proforma(db, modify_at, pre_at, folioPerformance=False, modify_num=365) if len(proforma): dates = proforma.index rets = proforma.values performance = {int(d.timestamp()):float(str(p)) for d, p in zip(dates, rets)} await updateFolioPerformance(folio_id, user_id, proforma=performance, pre=1, modify_at=timestamp)
async def get_benchmarks(db=None, modify_time=time.time()): """ returns list of benchmarks """ if not db: db = Connection.getInstance() benchdata = await report_dao.findReferenceData("DefaultBenchmarks") if benchdata: data_dict = {} for t in benchdata.data.decode().split(','): data_dict[t.split("^")[-1]] = await db.get_risk_factor( t.strip(), modify_time) return data_dict else: return {}
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]) if float(obj_dict[f.key]) * 100 < 1000 else ">1000%" ] if f.key == "ann_return" else [f.label, f.format(obj_dict[f.key])] for f in fields] def init_help(self): fields = [self.help_dict[f] for f in self.field_list] return [[f.label, "0"] 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.getInstance() coin = db.get_coin(coinid) stats = BasicStats(coin.Performance) res = helper.help(stats)
def get_session(sessionid): db = Connection() table = db.get_table("Session") res = table.get_item(Key={"HashKey":sessionid}) return res['Item'] if res_ok(res) else def_sess(sessionid)
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)
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Mon Oct 31 13:11:48 2016 @author: coin-ai """ from datalib.datalib import Connection from datalib.coin import Coin from datetime import datetime from analytics.time_series import get_frequency db = Connection() COINS = db.get_all_coins() table = db.get_table("COIN") for COIN in COINS: # print(COIN.Name) ts = COIN.Performance T = len(ts) mx = int(max(ts.index).timestamp()) mn = int(min(ts.index).timestamp()) item = COIN.to_db_item() item['MaxDate'] = mx item['MinDate'] = mn item['Frequency'] = get_frequency(ts) item['T'] = T table.put_item(Item=item) # print("COIN updated: %s" % COIN.Name)
def __init__(self, item_id, tickers, isPortfolio=True, started_at=""): self.db = Connection.getInstance() self.item_id = item_id self.tickers = tickers self.started_at = started_at self.isPortfolio = isPortfolio
def __init__(self, item_id, start_at=None, isPortFolio=True): self.db = Connection.getInstance() self.item_id = item_id self.start_at = start_at self.isPortFolio = isPortFolio
def __init__(self, item, bench_ticker=None, modify_at=None): self.item = item self.db = Connection.getInstance() self.bench_ticker = bench_ticker self.modify_at = modify_at
async def portfolio_ratios(port_folio=None, folio_id="", db=None, lang="en"): try: if not db: db = Connection.getInstance() if not port_folio: if folio_id: port_folio = await folios_dao.findFolio(folio_id) if not port_folio: return None else: return None helper = StatsHelper(lang=lang) proforma = await folios_dao.GetFolioPerformance(port_folio.id) if proforma: fts = proforma["pre_data"]["data"] now_data = [nowData["data"] for nowData in proforma["now_data"]] now_fts = [] for d in now_data: fts.extend(d) now_fts.extend(d) prof_fts = [perf["NetReturn"] for perf in now_fts] dates = [ datetime.fromtimestamp(p['EffectiveDate']) for p in now_fts ] fts_data = calculate_fts(prof_fts, dates) dates = [datetime.fromtimestamp(p['EffectiveDate']) for p in fts] prof_array = [perf["NetReturn"] for perf in fts] fts = Series(data=prof_array, index=dates, name=port_folio.name) T = len(fts) referenceData = await report_dao.findReferenceData('Risk Free Rate' ) if referenceData: rf = float(referenceData.data) * 0.01 else: rf = 0 stats_all = BasicStats(fts, risk_free=rf, folio_name=port_folio.name) roll_windows = windows[stats_all.freq[0]] roll_results = [] ratio_items = { "Cumulative Return": [], "Period volatility": { "volatility": "", "day_7_volatility": "" }, "Max Drawdown": { "max_drawdown": "", "day_7_drawdown": "" }, "entire_history": "", "day_7_history": "" } resultsAll = helper.help(stats_all) result_dict = dict(resultsAll) ratio_items["entire_history"] = fts_data[ "entire_history"] if fts_data else 0.0 ratio_items["day_7_history"] = fts_data[ "day_7_history"] if fts_data else 0.0 ratio_items["Period volatility"]["volatility"] = fts_data[ "total_volatility"] if fts_data else 0.0 ratio_items["Max Drawdown"]["max_drawdown"] = fts_data[ "total_max_drawdown"] if fts_data else 0.0 ratio_items["Cumulative Return"].append( result_dict.get(_("Cumulative Return", lang), "")) for index, w in enumerate(roll_windows): if T >= w: roll_fts = fts[-1 * w:] ratio_data = helper.help(BasicStats(roll_fts, risk_free=rf)) ratio_dict = dict(ratio_data) val = ratio_dict.get(_("Cumulative Return", lang), "") ratio_items["Cumulative Return"].append(val) if index == 0: ratio_items["Max Drawdown"][ "day_7_drawdown"] = fts_data[ "week_max_drawdown"] if fts_data else 0.0 ratio_items["Period volatility"][ "day_7_volatility"] = fts_data[ "week_volatility"] if fts_data else 0.0 roll_results.append([w, ratio_data]) await folios_dao.updateFolioHistory(port_folio.id, ratio_items) reportData = { "ResultsAll": resultsAll, "RollResults": roll_results, "Coin": { "Name": port_folio.name } } params = "ratios_%s" % lang await db.set_cache(port_folio.id, "portfolio", params, data=reportData, modify_at=port_folio.modify_at) else: roll_results = [] ratio_items = { "Cumulative Return": [], "Period volatility": { "volatility": "" }, "Max Drawdown": { "max_drawdown": "", "day_7_drawdown": "" } } resultsAll = helper.init_help() result_dict = dict(resultsAll) ratio_items["Period volatility"]["volatility"] = result_dict.get( "% Volatility", "") ratio_items["Max Drawdown"]["max_drawdown"] = result_dict.get( "Max Drawdown", "") ratio_items["Cumulative Return"].append( result_dict.get("Cumulative Return", "")) await folios_dao.updateFolioHistory(port_folio.id, ratio_items) reportData = { "ResultsAll": resultsAll, "RollResults": roll_results, "Coin": { "Name": port_folio.name } } except Exception as e: errMsg = getErrorMsg() error("portfolio_ratios exception is: %s, errMsg: %s" % (str(e), str(errMsg))) reportData = {} return reportData