def navchart(): data = generatenav() navchart = data[["NAV_fx"]] # dates need to be in Epoch time for Highcharts navchart.index = (navchart.index - datetime(1970, 1, 1)).total_seconds() navchart.index = navchart.index * 1000 navchart.index = navchart.index.astype(np.int64) navchart = navchart.to_dict() navchart = navchart["NAV_fx"] port_value_chart = data[[ "PORT_cash_value_fx", "PORT_fx_pos", "PORT_ac_CFs_fx" ]] port_value_chart["ac_pnl_fx"] = (port_value_chart["PORT_fx_pos"] - port_value_chart["PORT_ac_CFs_fx"]) # dates need to be in Epoch time for Highcharts port_value_chart.index = (port_value_chart.index - datetime(1970, 1, 1)).total_seconds() port_value_chart.index = port_value_chart.index * 1000 port_value_chart.index = port_value_chart.index.astype(np.int64) port_value_chart = port_value_chart.to_dict() return render_template("warden/warden_navchart.html", title="NAV Historical Chart", navchart=navchart, port_value_chart=port_value_chart, fx=FX, current_user=fx_rate(), donated=donate_check())
def navchartdatajson(): data = generatenav() # Generate data for NAV chart navchart = data[["NAV_fx"]] # dates need to be in Epoch time for Highcharts navchart.index = (navchart.index - datetime(1970, 1, 1)).total_seconds() navchart.index = navchart.index * 1000 navchart.index = navchart.index.astype(np.int64) navchart = navchart.to_dict() navchart = navchart["NAV_fx"] # Sort for HighCharts import collections navchart = collections.OrderedDict(sorted(navchart.items())) navchart = json.dumps(navchart) return navchart
def stackchartdatajson(): data = generatenav() # Generate data for Stack chart # Filter to Only BTC Positions try: data['BTC_cum'] = data['BTC_quant'].cumsum() stackchart = data[["BTC_cum"]] # dates need to be in Epoch time for Highcharts stackchart.index = (stackchart.index - datetime(1970, 1, 1)).total_seconds() stackchart.index = stackchart.index * 1000 stackchart.index = stackchart.index.astype(np.int64) stackchart = stackchart.to_dict() stackchart = stackchart["BTC_cum"] # Sort for HighCharts import collections stackchart = collections.OrderedDict(sorted(stackchart.items())) stackchart = json.dumps(stackchart) except Exception as e: return (json.dumps({"Error": str(e)})) return stackchart
def portstats(): meta = {} # Looking to generate the following data here and return as JSON # for AJAX query on front page: # Start date, End Date, Start NAV, End NAV, Returns (1d, 1wk, 1mo, 1yr, # YTD), average daily return. Best day, worse day. Std dev of daily ret, # Higher NAV, Lower NAV + dates. Higher Port Value (date). data = generatenav() meta["start_date"] = (data.index.min()).date().strftime("%B %d, %Y") meta["end_date"] = data.index.max().date().strftime("%B %d, %Y") meta["start_nav"] = data["NAV_fx"][0] meta["end_nav"] = data["NAV_fx"][-1].astype(float) meta["max_nav"] = data["NAV_fx"].max().astype(float) meta["max_nav_date"] = data[ data["NAV_fx"] == data["NAV_fx"].max()].index.strftime("%B %d, %Y")[0] meta["min_nav"] = data["NAV_fx"].min().astype(float) meta["min_nav_date"] = data[ data["NAV_fx"] == data["NAV_fx"].min()].index.strftime("%B %d, %Y")[0] meta["end_portvalue"] = data["PORT_fx_pos"][-1].astype(float) meta["end_portvalue_usd"] = meta["end_portvalue"] / fx_rate()['fx_rate'] meta["max_portvalue"] = data["PORT_fx_pos"].max().astype(float) meta["max_port_date"] = data[data["PORT_fx_pos"] == data["PORT_fx_pos"]. max()].index.strftime("%B %d, %Y")[0] meta["min_portvalue"] = round(data["PORT_fx_pos"].min(), 0) meta["min_port_date"] = data[data["PORT_fx_pos"] == data["PORT_fx_pos"]. min()].index.strftime("%B %d, %Y")[0] meta["return_SI"] = (meta["end_nav"] / meta["start_nav"]) - 1 # Temporary fix for an issue with portfolios that are just too new # Create a function to handle this try: meta["return_1d"] = (meta["end_nav"] / data["NAV_fx"][-2]) - 1 except IndexError: meta["return_1d"] = "-" try: meta["return_1wk"] = (meta["end_nav"] / data["NAV_fx"][-7]) - 1 except IndexError: meta["return_1wk"] = "-" try: meta["return_30d"] = (meta["end_nav"] / data["NAV_fx"][-30]) - 1 except IndexError: meta["return_30d"] = "-" try: meta["return_90d"] = (meta["end_nav"] / data["NAV_fx"][-90]) - 1 except IndexError: meta["return_90d"] = "-" try: meta["return_ATH"] = (meta["end_nav"] / meta["max_nav"]) - 1 except IndexError: meta["return_ATH"] = "-" try: yr_ago = pd.to_datetime(datetime.today() - relativedelta(years=1)) yr_ago_NAV = data.NAV_fx[data.index.get_loc(yr_ago, method="nearest")] meta["return_1yr"] = meta["end_nav"] / yr_ago_NAV - 1 except IndexError: meta["return_1yr"] = "-" # Create data for summa"age meta["fx"] = FX meta["daily"] = {} for days in range(1, 8): meta["daily"][days] = {} meta["daily"][days]["date"] = data.index[days * -1].date().strftime( "%A <br> %m/%d") meta["daily"][days]["nav"] = data["NAV_fx"][days * -1] meta["daily"][days]["nav_prev"] = data["NAV_fx"][(days + 1) * -1] meta["daily"][days]["perc_chg"] = (meta["daily"][days]["nav"] / meta["daily"][days]["nav_prev"]) - 1 meta["daily"][days]["port"] = data["PORT_fx_pos"][days * -1] meta["daily"][days]["port_prev"] = data["PORT_fx_pos"][(days + 1) * -1] meta["daily"][days]["port_chg"] = (meta["daily"][days]["port"] - meta["daily"][days]["port_prev"]) # Removes Numpy type from json - returns int instead def convert(o): if isinstance(o, np.int64): return int(o) else: return (o) # create chart data for a small NAV chart return simplejson.dumps(meta, ignore_nan=True, default=convert)