-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstd.py
More file actions
80 lines (75 loc) · 3.16 KB
/
std.py
File metadata and controls
80 lines (75 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import requests
import pandas as pd
import numpy as np
import streamlit as st
from datetime import datetime, timedelta
import altair as alt
def get_data(currency="BTC"):
chart_res = requests.get(
f"https://asia.deribit.com/api/v2/public/get_tradingview_chart_data?end_timestamp={int(datetime.now().timestamp() * 1000)}&instrument_name={currency}-PERPETUAL&resolution=1D&start_timestamp={int((datetime.now() - timedelta(days=1000)).timestamp() * 1000)}"
)
df = pd.DataFrame(chart_res.json()["result"])
df = df.astype({"ticks": "datetime64[ms]"})
df.set_index("ticks", inplace=True)
df["change"] = df["close"].pct_change()
df["log_diff"] = np.log(df.close) - np.log(df.close.shift(1))
df["change_squared"] = df["change"] * df["change"]
df["rolling30_std"] = df["log_diff"].rolling(30).std()
df["rolling30_volatility"] = df["rolling30_std"] * np.sqrt(365)
df["rolling365_std"] = df["log_diff"].rolling(365).std()
df["rolling365_volatility"] = df["rolling365_std"] * np.sqrt(365)
df["rolling365_std_ma"] = df["rolling365_std"].rolling(30).mean()
df["close_365ma"] = df["close"].rolling(365).mean()
df["close_30ma"] = df["close"].rolling(30).mean()
return df
st.set_page_config(layout="wide")
option = st.sidebar.selectbox(
"Currencies:", ["BTC", "ETH"], on_change=get_data, key="currency"
)
df = get_data(st.session_state.currency)
output_df = pd.DataFrame()
output_df["rolling15_std"] = df["log_diff"].rolling(15).std()
output_df["rolling30_std"] = df["rolling30_std"].copy()
output_df["rolling365_std"] = df["rolling365_std"].copy()
output_df = output_df.dropna(how="all")
c = st.container()
c.header("Rolling std(log)")
c.line_chart(output_df)
base = alt.Chart(df.dropna())
bar = base.mark_bar().encode(
alt.X("log_diff:Q", bin=alt.Bin(base=20, maxbins=30), type="ordinal"), y="count()"
)
std_col1, std_col2, std_col3 = c.columns(3)
std_col1.metric(
label="15D",
value="{value:.4%}".format(value=output_df["rolling15_std"].iloc[-1]),
delta="{delta:.4%}".format(
delta=output_df["rolling15_std"].iloc[-1] - output_df["rolling15_std"].iloc[-2]
),
)
std_col2.metric(
label="30D",
value="{value:.4%}".format(value=output_df["rolling30_std"].iloc[-1]),
delta="{delta:.4%}".format(
delta=output_df["rolling30_std"].iloc[-1] - output_df["rolling30_std"].iloc[-2]
),
)
std_col3.metric(
label="365D",
value="{value:.4%}".format(value=output_df["rolling365_std"].iloc[-1]),
delta="{delta:.4%}".format(
delta=output_df["rolling365_std"].iloc[-1]
- output_df["rolling365_std"].iloc[-2]
),
)
# mean_line = base.mark_rule(color='red').encode(x="mean(change):Q", size=alt.value(3))
c2 = st.container()
c2.header("Histogram of daily log changes")
c2.altair_chart(bar.interactive(), width="stretch")
col1, col2, col3, col4 = c2.columns(4)
col1.metric(label="Mean", value="{mean:.4%}".format(mean=df["log_diff"].mean()))
col2.metric(label="Median", value="{median:.4%}".format(median=df["log_diff"].median()))
col3.metric(label="Skew", value="{skew:.4f}".format(skew=df["log_diff"].skew()))
col4.metric(
label="Kurtosis", value="{kurtosis:.4f}".format(kurtosis=df["log_diff"].kurt())
)