def heatmapbenchmark_json(): # Get portfolio data first heatmap_gen, heatmap_stats, years, cols = heatmap_generator() # Now get the ticker information and run comparison if request.method == "GET": ticker = request.args.get("ticker") # Defaults to king BTC if not ticker: ticker = "BTC" # Gather the first trade date in portfolio and store # used to match the matrixes later # Panda dataframe with transactions df = transactions_fx() # Filter the df acccoring to filter passed as arguments df["trade_date"] = pd.to_datetime(df["trade_date"]) start_date = df["trade_date"].min() start_date -= timedelta(days=1) # start on t-1 of first trade # Generate price Table now for the ticker and trim to match portfolio fx = current_app.settings['PORTFOLIO']['base_fx'] data = historical_prices(ticker, fx) mask = data.index >= start_date data = data.loc[mask] # If notification is an error, skip this ticker if data is None: messages = data.errors return jsonify(messages) data = data.rename(columns={'close_converted': ticker + '_price'}) data = data[[ticker + '_price']] data.sort_index(ascending=True, inplace=True) data["pchange"] = (data / data.shift(1)) - 1 # Run the mrh function to generate heapmap table heatmap = mrh.get(data["pchange"], eoy=True) heatmap_stats = heatmap cols = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "eoy", ] cols_months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ] years = heatmap.index.tolist() # Create summary stats for the Ticker heatmap_stats["MAX"] = heatmap_stats[heatmap_stats[cols_months] != 0].max( axis=1) heatmap_stats["MIN"] = heatmap_stats[heatmap_stats[cols_months] != 0].min( axis=1) heatmap_stats["POSITIVES"] = heatmap_stats[ heatmap_stats[cols_months] > 0].count(axis=1) heatmap_stats["NEGATIVES"] = heatmap_stats[ heatmap_stats[cols_months] < 0].count(axis=1) heatmap_stats["POS_MEAN"] = heatmap_stats[ heatmap_stats[cols_months] > 0].mean(axis=1) heatmap_stats["NEG_MEAN"] = heatmap_stats[ heatmap_stats[cols_months] < 0].mean(axis=1) heatmap_stats["MEAN"] = heatmap_stats[ heatmap_stats[cols_months] != 0].mean(axis=1) # Create the difference between the 2 df - Pandas is cool! heatmap_difference = heatmap_gen - heatmap # return (heatmap, heatmap_stats, years, cols, ticker, heatmap_diff) return simplejson.dumps( { "heatmap": heatmap.to_dict(), "heatmap_stats": heatmap_stats.to_dict(), "cols": cols, "years": years, "ticker": ticker, "heatmap_diff": heatmap_difference.to_dict(), }, ignore_nan=True, default=datetime.isoformat, )
def heatmap_generator(): # If no Transactions for this user, return empty.html from warden_modules import transactions_fx, generatenav transactions = transactions_fx() if transactions.empty: return None, None, None, None # Generate NAV Table first data = generatenav() data["navpchange"] = (data["NAV_fx"] / data["NAV_fx"].shift(1)) - 1 returns = data["navpchange"] # Run the mrh function to generate heapmap table heatmap = mrh.get(returns, eoy=True) heatmap_stats = heatmap cols = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "eoy", ] cols_months = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec", ] years = (heatmap.index.tolist()) heatmap_stats["MAX"] = heatmap_stats[heatmap_stats[cols_months] != 0].max( axis=1) heatmap_stats["MIN"] = heatmap_stats[heatmap_stats[cols_months] != 0].min( axis=1) heatmap_stats["POSITIVES"] = heatmap_stats[ heatmap_stats[cols_months] > 0].count(axis=1) heatmap_stats["NEGATIVES"] = heatmap_stats[ heatmap_stats[cols_months] < 0].count(axis=1) heatmap_stats["POS_MEAN"] = heatmap_stats[ heatmap_stats[cols_months] > 0].mean(axis=1) heatmap_stats["NEG_MEAN"] = heatmap_stats[ heatmap_stats[cols_months] < 0].mean(axis=1) heatmap_stats["MEAN"] = heatmap_stats[ heatmap_stats[cols_months] != 0].mean(axis=1) return (heatmap, heatmap_stats, years, cols)