Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dashboard settings #16

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
266 changes: 134 additions & 132 deletions dashboard/modules/base_goerli/perp_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

## data
@st.cache_data(ttl=1)
def fetch_data(account_id="NULL"):
def fetch_data(account_id=""):
# initialize connection
db = get_connection()

Expand All @@ -22,44 +22,50 @@ def fetch_data(account_id="NULL"):
)
df_order_expired = pd.read_sql_query(
f"""
SELECT * FROM base_goerli.perp_previous_order_expired
WHERE account_id = {account_id}
SELECT
cast(account_id as text) as clean_account_id,
*
FROM base_goerli.perp_previous_order_expired
WHERE account_id = {account_id if account_id else 'NULL'}
""",
db,
)
df_trade = pd.read_sql_query(
f"""
SELECT * FROM base_goerli.fct_perp_trades
WHERE account_id = {account_id}
WHERE account_id = '{account_id}'
""",
db,
)
df_transfer = pd.read_sql_query(
f"""
SELECT * FROM base_goerli.perp_collateral_modified
WHERE account_id = {account_id}
SELECT
cast(account_id as text) as clean_account_id,
*
FROM base_goerli.perp_collateral_modified
WHERE account_id = {account_id if account_id else 'NULL'}
""",
db,
)
df_account_liq = pd.read_sql_query(
f"""
SELECT * FROM base_goerli.fct_perp_liq_account
WHERE account_id = {account_id}
WHERE account_id = '{account_id}'
""",
db,
)
df_position_liq = pd.read_sql_query(
f"""
SELECT * FROM base_goerli.fct_perp_liq_position
WHERE account_id = {account_id}
WHERE account_id = '{account_id}'
""",
db,
)

df_hourly = pd.read_sql_query(
f"""
SELECT * FROM base_goerli.fct_perp_account_stats_hourly
WHERE account_id = {account_id}
WHERE account_id = '{account_id}'
""",
db,
)
Expand Down Expand Up @@ -110,158 +116,154 @@ def main():
if len(account_numbers) > 0:
st.dataframe(account_numbers, hide_index=True)

filt_col1, filt_col2 = st.columns(2)
with filt_col1:
start_date = st.date_input(
"Start", datetime.today().date() - timedelta(days=30)
)

with filt_col2:
end_date = st.date_input("End", datetime.today().date())

accounts = data["accounts"]["id"].unique()
accounts = sorted(list([int(_) for _ in accounts]))
account = st.selectbox("Select account", accounts, index=0)

data = fetch_data(account_id=account)
if account:
data = fetch_data(account_id=account)

## do some lighter transforms
df_open_positions = (
data["trade"]
.sort_values("ts")
.groupby(["account_id", "market_id"])
.last()
.reset_index()
)
df_open_positions = df_open_positions[df_open_positions["position_size"].abs() > 0]
## do some lighter transforms
df_open_positions = (
data["trade"]
.sort_values("ts")
.groupby(["account_id", "market_id"])
.last()
.reset_index()
)
df_open_positions = df_open_positions[
df_open_positions["position_size"].abs() > 0
]

## make the charts
charts = make_charts(data)
## make the charts
charts = make_charts(data)

## display
# Open positions
df_open_account = df_open_positions[df_open_positions["account_id"] == account]
## display
# Open positions
df_open_account = df_open_positions[df_open_positions["account_id"] == account]

last_liq = (
data["account_liq"]
.loc[data["account_liq"]["account_id"] == account, "ts"]
.max()
)
last_liq = (
data["account_liq"]
.loc[data["account_liq"]["account_id"] == account, "ts"]
.max()
)

# this is a hack to handle the case where there are no liquidations
last_liq = last_liq if pd.isna(last_liq) == False else "2023-01-01 00:00:00+00:00"
# this is a hack to handle the case where there are no liquidations
last_liq = (
last_liq if pd.isna(last_liq) == False else "2023-01-01 00:00:00+00:00"
)

df_open_account = df_open_account.loc[
df_open_account["ts"] > last_liq,
["account_id", "market_symbol", "position_size", "notional_position_size"],
]
df_open_account = df_open_account.loc[
df_open_account["ts"] > last_liq,
["account_id", "market_symbol", "position_size", "notional_position_size"],
]

st.markdown(
"""
### Open Positions
"""
)
if len(df_open_account) > 0:
df_open_account
else:
st.markdown(
"""
No open positions
### Open Positions
"""
)
if len(df_open_account) > 0:
df_open_account
else:
st.markdown(
"""
No open positions
"""
)

col1, col2 = st.columns(2)
col1, col2 = st.columns(2)

with col1:
st.plotly_chart(charts["cumulative_volume"], use_container_width=True)
pass
with col1:
st.plotly_chart(charts["cumulative_volume"], use_container_width=True)
pass

with col2:
st.plotly_chart(charts["cumulative_fees"], use_container_width=True)
pass
with col2:
st.plotly_chart(charts["cumulative_fees"], use_container_width=True)
pass

# Recent trades
st.markdown(
# Recent trades
st.markdown(
"""
### Recent Trades
"""
### Recent Trades
"""
)
)

st.dataframe(
data["trade"][
[
"ts",
"account_id",
"market_symbol",
"position_size",
"trade_size",
"notional_trade_size",
"fill_price",
"total_fees",
"accrued_funding",
"tracking_code",
st.dataframe(
data["trade"][
[
"ts",
"account_id",
"market_symbol",
"position_size",
"trade_size",
"notional_trade_size",
"fill_price",
"total_fees",
"accrued_funding",
"tracking_code",
]
]
]
.sort_values("ts", ascending=False)
.head(50),
use_container_width=True,
hide_index=True,
)
.sort_values("ts", ascending=False)
.head(50),
use_container_width=True,
hide_index=True,
)

# Recent transfers
st.markdown(
# Recent transfers
st.markdown(
"""
### Recent Transfers
"""
### Recent Transfers
"""
)
)

st.dataframe(
data["transfer"][
[
"block_timestamp",
"account_id",
"synth_market_id",
"amount_delta",
st.dataframe(
data["transfer"][
[
"block_timestamp",
"clean_account_id",
"synth_market_id",
"amount_delta",
]
]
]
.sort_values("block_timestamp", ascending=False)
.head(50),
use_container_width=True,
hide_index=True,
)
.sort_values("block_timestamp", ascending=False)
.head(50),
use_container_width=True,
hide_index=True,
)

# Account liquidations table
st.markdown(
# Account liquidations table
st.markdown(
"""
### Liquidations
"""
### Liquidations
"""
)
)

st.dataframe(
data["account_liq"][["ts", "account_id", "total_reward"]]
.sort_values("ts", ascending=False)
.head(25),
use_container_width=True,
hide_index=True,
)
st.dataframe(
data["account_liq"][["ts", "account_id", "total_reward"]]
.sort_values("ts", ascending=False)
.head(25),
use_container_width=True,
hide_index=True,
)

# Expired orders table
st.markdown(
# Expired orders table
st.markdown(
"""
### Expired Orders
"""
### Expired Orders
"""
)
)

st.dataframe(
data["order_expired"][
[
"block_timestamp",
"account_id",
"market_id",
"acceptable_price",
"commitment_time",
]
],
use_container_width=True,
hide_index=True,
)
st.dataframe(
data["order_expired"][
[
"block_timestamp",
"clean_account_id",
"market_id",
"acceptable_price",
"commitment_time",
]
],
use_container_width=True,
hide_index=True,
)
Loading