Skip to content

Commit

Permalink
Adding ohlcv and fixed point plots
Browse files Browse the repository at this point in the history
  • Loading branch information
slundqui committed Aug 22, 2023
1 parent bd7b650 commit fdc975c
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 45 deletions.
23 changes: 16 additions & 7 deletions lib/chainsync/bin/run_hyperdrive_dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@

import mplfinance as mpf
import streamlit as st
from chainsync.dashboard import build_leaderboard, build_ticker, get_user_lookup, plot_fixed_rate, plot_ohlcv
from chainsync.dashboard import (
build_fixed_rate,
build_leaderboard,
build_ohlcv,
build_ticker,
get_user_lookup,
plot_fixed_rate,
plot_ohlcv,
)
from chainsync.db.base import get_user_map, initialize_session
from chainsync.db.hyperdrive import get_all_traders, get_pool_analysis, get_pool_config, get_ticker, get_wallet_pnl
from ethpy import build_eth_config
Expand Down Expand Up @@ -49,19 +57,20 @@
# Adds user lookup to the ticker
display_ticker = build_ticker(ticker, user_lookup)

# TODO get wallet pnl and calculate leaderboard
# get wallet pnl and calculate leaderboard
wallet_pnl = get_wallet_pnl(session, start_block=-max_live_blocks, coerce_float=False)
# Get the latest updated block
latest_wallet_pnl = wallet_pnl[wallet_pnl["blockNumber"] == wallet_pnl["blockNumber"].max()]

comb_rank, ind_rank = build_leaderboard(latest_wallet_pnl, user_lookup)

# TODO calculate ohlcv and volume
# ohlcv = calc_ohlcv(combined_data, config_data, freq="5T")
# build ohlcv and volume
ohlcv = build_ohlcv(pool_analysis, freq="5T")
# build fixed rate
fixed_rate = build_fixed_rate(pool_analysis)

with ticker_placeholder.container():
st.header("Ticker")
st.dataframe(ticker, height=200, use_container_width=True)
st.dataframe(display_ticker, height=200, use_container_width=True)
st.header("Total Leaderboard")
st.dataframe(comb_rank, height=500, use_container_width=True)
st.header("Wallet Leaderboard")
Expand All @@ -74,7 +83,7 @@
ax_fixed_rate.clear()

plot_ohlcv(ohlcv, ax_ohlcv, ax_vol)
plot_fixed_rate(fixed_rate_x, fixed_rate_y, ax_fixed_rate)
plot_fixed_rate(fixed_rate, ax_fixed_rate)

ax_ohlcv.tick_params(axis="both", which="both")
ax_vol.tick_params(axis="both", which="both")
Expand Down
4 changes: 3 additions & 1 deletion lib/chainsync/chainsync/dashboard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
"""Dashboard utilities"""

from .build_fixed_rate import build_fixed_rate
from .build_leaderboard import build_leaderboard
from .build_ohlcv import build_ohlcv
from .build_ticker import build_ticker
from .extract_data_logs import get_combined_data, read_json_to_pd
from .plot_fixed_rate import plot_fixed_rate
from .plot_ohlcv import calc_ohlcv, plot_ohlcv
from .plot_ohlcv import plot_ohlcv
from .usernames import address_to_username, combine_usernames, get_user_lookup
3 changes: 3 additions & 0 deletions lib/chainsync/chainsync/dashboard/build_fixed_rate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
def build_fixed_rate(pool_analysis):
fixed_rate = pool_analysis[["timestamp", "fixed_rate"]]
return fixed_rate
27 changes: 27 additions & 0 deletions lib/chainsync/chainsync/dashboard/build_ohlcv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from decimal import Decimal

import pandas as pd


def build_ohlcv(pool_analysis, freq="D"):
"""
freq var: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases
"""
# TODO get volume from pool_analysis
# fixme hacking it in for now
pool_analysis["trade_volume"] = Decimal(1)

spot_prices = pool_analysis[["timestamp", "spot_price", "trade_volume"]].copy()
spot_prices = spot_prices.set_index("timestamp")

# TODO this is filling groups without data with nans, is this desired?
ohlcv = spot_prices.groupby([pd.Grouper(freq=freq)]).agg(
{"spot_price": ["first", "last", "max", "min"], "trade_volume": "sum"}
)

ohlcv.columns = ["Open", "Close", "High", "Low", "Volume"]
ohlcv.index.name = "Date"
# ohlcv must be floats
ohlcv = ohlcv.astype(float)

return ohlcv
4 changes: 2 additions & 2 deletions lib/chainsync/chainsync/dashboard/plot_fixed_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from matplotlib import ticker as mpl_ticker


def plot_fixed_rate(x_data, y_data, axes):
def plot_fixed_rate(data, axes):
"""Returns the fixed rate plot"""
axes.plot(x_data, y_data)
axes.plot(data["timestamp"], data["fixed_rate"])
# change y-axis unit format to 0.1%
axes.yaxis.set_major_formatter(mpl_ticker.FuncFormatter(lambda x, p: format(x, "0.3%")))
axes.yaxis.set_label_position("right")
Expand Down
34 changes: 0 additions & 34 deletions lib/chainsync/chainsync/dashboard/plot_ohlcv.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from __future__ import annotations

import mplfinance as mpf
import pandas as pd
from chainsync.analysis.calc_spot_price import calc_spot_price


def plot_ohlcv(ohlcv, ohlcv_ax, vol_ax):
Expand All @@ -22,36 +20,4 @@ def plot_ohlcv(ohlcv, ohlcv_ax, vol_ax):
vol_ax.yaxis.set_label_position("right")
vol_ax.yaxis.tick_right()


def calc_ohlcv(trade_data, config_data, freq="D"):
"""
freq var: https://pandas.pydata.org/pandas-docs/stable/user_guide/timeseries.html#offset-aliases
"""
spot_prices = (
calc_spot_price(
trade_data["share_reserves"],
trade_data["bond_reserves"],
config_data["initialSharePrice"],
config_data["invTimeStretch"],
)
.to_frame()
.astype(float)
)

spot_prices.columns = ["spot_price"]
value = trade_data["value"]

spot_prices["value"] = value
spot_prices["timestamp"] = trade_data["timestamp"]
spot_prices = spot_prices.set_index("timestamp")

ohlcv = spot_prices.groupby([pd.Grouper(freq=freq)]).agg(
{"spot_price": ["first", "last", "max", "min"], "value": "sum"}
)

ohlcv.columns = ["Open", "Close", "High", "Low", "Volume"]
ohlcv.index.name = "Date"

return ohlcv

# format x-axis as time
6 changes: 5 additions & 1 deletion lib/chainsync/chainsync/db/hyperdrive/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,7 +683,10 @@ def get_pool_analysis(
DataFrame
A DataFrame that consists of the queried pool info data
"""
query = session.query(PoolAnalysis)
if return_timestamp:
query = session.query(PoolInfo.timestamp, PoolAnalysis)
else:
query = session.query(PoolAnalysis)

# Support for negative indices
if (start_block is not None) and (start_block < 0):
Expand All @@ -698,6 +701,7 @@ def get_pool_analysis(

if return_timestamp:
# TODO query from PoolInfo the timestamp
query = query.join(PoolInfo, PoolAnalysis.blockNumber == PoolInfo.blockNumber)
pass

# Always sort by block in order
Expand Down

0 comments on commit fdc975c

Please sign in to comment.