Skip to content

Instantly share code, notes, and snippets.

@MarkRBest
Created August 18, 2025 07:19
Show Gist options
  • Select an option

  • Save MarkRBest/a8f6060c7876728b2dd2d496f382a765 to your computer and use it in GitHub Desktop.

Select an option

Save MarkRBest/a8f6060c7876728b2dd2d496f382a765 to your computer and use it in GitHub Desktop.
ETH SOCP
# %%
import ccxt
import pandas as pd
import numpy as np
import cvxpy as cvx
import matplotlib.pyplot as plt
from tqdm import tqdm
# %%
# -------------------
# Step 1: Download data with ccxt
# -------------------
exchange = ccxt.binance({
"enableRateLimit": True,
"options": {"defaultType": "future"} # <-- ensures perpetual futures
})
symbol = "ETH/USDT"
timeframe = "1m"
limit = 1000 # max candles per call
# fetch recent 1000 minutes (about 16 hours)
ohlcv = exchange.fetch_ohlcv(symbol, timeframe=timeframe, limit=limit)
# -------------------
# Step 2: Convert to DataFrame
# -------------------
df = pd.DataFrame(
ohlcv,
columns=["timestamp", "open", "high", "low", "close", "volume"]
)
df["timestamp"] = pd.to_datetime(df["timestamp"], unit="ms")
df.set_index("timestamp", inplace=True)
prices = df["close"]
# %%
# -------------------
# Step 3: Apply CVXPY smoothing
# -------------------
data = prices.values
noisy_data = data.copy() # here we just smooth real data (no artificial noise)
c = 100
y = cvx.Variable(len(data))
diff = y[1:] - y[:-1]
tv = cvx.norm1(diff)
obj = cvx.Minimize(cvx.sum_squares(noisy_data - y) + c * tv)
prob = cvx.Problem(obj)
solution = prob.solve(solver="ECOS")
smoothed = np.array(y.value).flatten()
# -------------------
# Step 4: Plot
# -------------------
plt.figure(figsize=(12,6))
plt.plot(df.index, noisy_data, label="ETHUSDT Close (raw)", alpha=0.6)
plt.plot(df.index, smoothed, label="Smoothed", linewidth=2)
plt.legend()
plt.title("ETH/USDT Perp 1m Close - TV Smoothing with CVXPY")
plt.show()
# %%
walk_forward_smoothed = []
for t in tqdm(range(c, len(data))):
y = cvx.Variable(t)
diff = y[1:] - y[:-1]
tv = cvx.norm1(diff)
obj = cvx.Minimize(cvx.sum_squares(data[:t] - y) + c * tv)
prob = cvx.Problem(obj)
prob.solve(solver="ECOS") # or "SCS" if ECOS unavailable
smoothed_value = y.value[-1] # take the most recent smoothed value
walk_forward_smoothed.append(smoothed_value)
# align with original time axis
walk_forward_series = np.full(len(data), np.nan)
walk_forward_series[c:] = walk_forward_smoothed
# Plot
plt.figure(figsize=(12,6))
plt.plot(data, label="Raw data", alpha=0.6)
plt.plot(walk_forward_series, label="Walk-forward smoothed", linewidth=2)
plt.legend()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment