-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcnoresearchupdate.py
245 lines (213 loc) · 9.14 KB
/
cnoresearchupdate.py
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import streamlit as st
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import matplotlib as mpl
import ccxt
from numba import njit
from numpy.typing import NDArray
# -----------------------------
# 1. Numba-accelerated EMA function
# -----------------------------
@njit(cache=True)
def ema(arr_in: NDArray, window: int) -> NDArray:
alpha = 3 / float(window + 1)
n = arr_in.size
ewma = np.empty(n, dtype=np.float64)
ewma[0] = arr_in[0]
for i in range(1, n):
ewma[i] = (arr_in[i] * alpha) + (ewma[i - 1] * (1 - alpha))
return ewma
# -----------------------------
# 2. Fetch trades from Kraken via CCXT
# -----------------------------
def fetch_trades_kraken(symbol="BTC/USD", lookback_minutes=1440, limit=43200):
exchange = ccxt.kraken()
now_ms = exchange.milliseconds()
cutoff_ts = now_ms - lookback_minutes * 60 * 1000
since = cutoff_ts
all_trades = []
while True:
trades = exchange.fetch_trades(symbol, since=since, limit=limit)
if not trades:
break
all_trades += trades
since = trades[-1]['timestamp'] + 1
if trades[-1]['timestamp'] >= now_ms or len(trades) < limit:
break
# Filter trades to our lookback window
all_trades = [t for t in all_trades if t['timestamp'] >= cutoff_ts]
if not all_trades:
raise ValueError(f"No trades returned in the last {lookback_minutes} minutes.")
df_tr = pd.DataFrame(all_trades)
df_tr['stamp'] = pd.to_datetime(df_tr['timestamp'], unit='ms')
df_tr['vol'] = df_tr['amount']
df_tr.sort_values('stamp', inplace=True)
df_tr.reset_index(drop=True, inplace=True)
return df_tr
# -----------------------------
# 3. Fetch OHLC data from Kraken via CCXT
# -----------------------------
def fetch_ohlc(symbol="BTC/USD", timeframe="1m", lookback_minutes=1440):
exchange = ccxt.kraken()
now_ms = exchange.milliseconds()
cutoff_ts = now_ms - lookback_minutes * 60 * 1000
all_ohlcv = []
since = cutoff_ts
max_limit = 1440
while True:
ohlcv = exchange.fetch_ohlcv(symbol, timeframe, since=since, limit=max_limit)
if not ohlcv:
break
all_ohlcv += ohlcv
last_timestamp = ohlcv[-1][0]
if last_timestamp <= cutoff_ts or len(ohlcv) < max_limit:
break
since = last_timestamp + 1
if not all_ohlcv:
raise ValueError("No OHLC data returned.")
df = pd.DataFrame(all_ohlcv, columns=["timestamp", "open", "high", "low", "close", "volume"])
df["stamp"] = pd.to_datetime(df["timestamp"], unit="ms")
df.sort_values("stamp", inplace=True)
return df
# -----------------------------
# 4. Tick-Test Classification (Proxy for Trade Direction)
# -----------------------------
def tick_test_classify(trades_df):
"""
Classify each trade based on its price relative to the previous trade.
If current price > previous price -> "buy"; if lower -> "sell";
if unchanged, carry forward previous classification.
"""
trades_df = trades_df.sort_values('stamp').reset_index(drop=True)
lr_side = []
prev_side = 'buy'
prev_price = trades_df['price'].iloc[0]
lr_side.append('buy') # arbitrarily label first trade as "buy"
for i in range(1, len(trades_df)):
current_price = trades_df['price'].iloc[i]
if current_price > prev_price:
lr_side.append('buy')
prev_side = 'buy'
elif current_price < prev_price:
lr_side.append('sell')
prev_side = 'sell'
else:
lr_side.append(prev_side)
prev_price = current_price
trades_df['lr_side'] = lr_side
return trades_df
# -----------------------------
# 5. Compute "BVC Hawkes" using Exponential Decay Weighting
# -----------------------------
def compute_bvc_hawkes(lr_trades_df, window_seconds=60, decay_rate=0.1):
"""
For each resampled time window, weight trade volumes exponentially based on recency.
Compute weighted_buy_vol and weighted_total_vol, then:
bvc_hawkes = weighted_buy_vol / weighted_total_vol
This mimics a Hawkes-inspired volume classification.
"""
df = lr_trades_df.copy()
df = df.sort_values('stamp').reset_index(drop=True)
df = df.set_index('stamp')
# Resample trades into windows of window_seconds
groups = df.resample(f'{window_seconds}s')
records = []
for time, group in groups:
if group.empty:
continue
# Assume window end is the resample label "time"
window_end = time
# Compute time differences (in seconds) from each trade to the window end
time_diffs = (window_end - group.index).total_seconds()
weights = np.exp(-decay_rate * time_diffs)
# Boolean mask: 1 for buy trades, 0 for sell
is_buy = (group['lr_side'] == 'buy').astype(float)
weighted_buy_vol = (group['vol'] * weights * is_buy).sum()
weighted_total_vol = (group['vol'] * weights).sum()
bvc_hawkes = weighted_buy_vol / (weighted_total_vol + 1e-8)
records.append({
'stamp': time,
'weighted_buy_vol': weighted_buy_vol,
'weighted_total_vol': weighted_total_vol,
'bvc_hawkes': bvc_hawkes
})
return pd.DataFrame(records)
# -----------------------------
# 6. Streamlit App UI and Plotting
# -----------------------------
st.title("Price Chart Color-Coded by BVC (Hawkes-Inspired Volume Classification)")
# Sidebar parameters
symbol = st.sidebar.text_input("Symbol", value="BTC/USD")
lookback_minutes = st.sidebar.number_input("Lookback (minutes)", min_value=60, max_value=10080, value=1440)
timeframe = st.sidebar.selectbox("Timeframe", ["1m", "5m", "15m", "1h"])
window_seconds = st.sidebar.number_input("Resample Window (sec)", min_value=10, max_value=3600, value=60)
decay_rate = st.sidebar.number_input("Decay Rate", min_value=0.001, max_value=1.0, value=0.1, step=0.001)
if st.button("Run Analysis"):
st.write(f"**Symbol:** {symbol}, **Lookback:** {lookback_minutes} minutes, **Timeframe:** {timeframe}")
st.write("Fetching trades...")
try:
trades_df = fetch_trades_kraken(symbol, lookback_minutes=lookback_minutes)
st.write(f"Fetched {len(trades_df)} trades.")
except Exception as e:
st.error(f"Error fetching trades: {e}")
st.stop()
st.write("Fetching OHLC data...")
try:
ohlc_df = fetch_ohlc(symbol, timeframe=timeframe, lookback_minutes=lookback_minutes)
st.write(f"Fetched {len(ohlc_df)} OHLC bars.")
except Exception as e:
st.error(f"Error fetching OHLC: {e}")
st.stop()
st.write("Classifying trades (tick-test)...")
classified_df = tick_test_classify(trades_df)
st.write("Computing BVC (Hawkes-Inspired) with exponential decay...")
bvc_hawkes_df = compute_bvc_hawkes(classified_df, window_seconds=window_seconds, decay_rate=decay_rate)
# Prepare OHLC for plotting: scaled price and EMA
if len(ohlc_df) < 1:
st.error("No OHLC data available. Please adjust lookback/timeframe.")
st.stop()
first_close = ohlc_df['close'].iloc[0]
ohlc_df['ScaledPrice'] = np.log(ohlc_df['close'] / first_close) * 1e4
window_ema = 10
ohlc_df['ScaledPrice_EMA'] = ema(ohlc_df['ScaledPrice'].values, window=window_ema)
ohlc_df.set_index('stamp', inplace=True)
# Merge OHLC and BVC (Hawkes) data using an as-of merge
merged_df = pd.merge_asof(
ohlc_df.reset_index().sort_values('stamp'),
bvc_hawkes_df.sort_values('stamp'),
on='stamp',
direction='backward'
).sort_values('stamp').reset_index(drop=True)
st.write("Plotting chart...")
# Plot: color segments based on bvc_hawkes
# bvc_hawkes ranges from 0 to 1 (0: all sell, 1: all buy)
fig, ax = plt.subplots(figsize=(10, 4), dpi=120)
norm_bvc = mpl.colors.Normalize(vmin=0, vmax=1)
cmap = plt.cm.bwr # Blue for low values, red for high
for i in range(len(merged_df) - 1):
xvals = merged_df['stamp'].iloc[i:i+2]
yvals = merged_df['ScaledPrice'].iloc[i:i+2]
bvc_value = merged_df['bvc_hawkes'].iloc[i]
if np.isnan(bvc_value):
bvc_value = 0.5 # neutral fallback
color = cmap(norm_bvc(bvc_value))
ax.plot(xvals, yvals, color=color, linewidth=1)
# Overlay EMA
ax.plot(merged_df['stamp'], merged_df['ScaledPrice_EMA'], color='black', linewidth=0.7, label=f"EMA({window_ema})")
ax.set_title("Price Chart with BVC (Hawkes-Inspired) Color-Coding")
ax.set_xlabel("Time")
ax.set_ylabel("Scaled Price")
ax.legend()
ax.xaxis.set_major_locator(mdates.AutoDateLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d %H:%M'))
plt.setp(ax.get_xticklabels(), rotation=30, ha='right')
# Add a colorbar to show BVC mapping
sm = mpl.cm.ScalarMappable(norm=norm_bvc, cmap=cmap)
sm.set_array([])
cbar = plt.colorbar(sm, ax=ax)
cbar.set_label('BVC (Hawkes-Inspired) [0: Sell, 1: Buy]', rotation=270, labelpad=15)
st.pyplot(fig)
if st.checkbox("Show Merged DataFrame"):
st.write(merged_df[['stamp', 'close', 'bvc_hawkes']].head(50))