-
-
Save gaburipeach/cafee3baf52d43b67ea66b810f975b2b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
import csv | |
import json | |
import requests | |
import pandas as pd | |
import numpy as np | |
import os | |
import glob | |
import nest_asyncio | |
import gzip | |
from datetime import datetime | |
import matplotlib.pyplot as plt | |
import matplotlib.ticker as mtick | |
from matplotlib.ticker import FormatStrFormatter | |
from tardis_client import TardisClient, Channel | |
import ccxt | |
# BYBIT | |
todate = 1676694984#int(datetime.utcnow().timestamp()) | |
oldtodate = todate-10000 | |
bybit_oi_dfs = [] | |
while True: | |
print(todate, oldtodate) | |
bybit_oi = requests.get("https://api2.bybit.com/v2/public/open-interest?symbol=GMTUSDT&period=5min&limit=200&to={todate}".format(todate=todate)).json() | |
data = pd.DataFrame(bybit_oi["result"]) | |
print(data.shape) | |
todate = int(data.timestamp.max()) | |
print("todate: ", todate, oldtodate) | |
if todate > oldtodate: | |
bybit_oi_dfs.append(data) | |
else: | |
print(todate, oldtodate) | |
break | |
oldtodate=todate | |
todate += 200*5*60 | |
todate = 1676694984#int(datetime.utcnow().timestamp()) | |
oldtodate = todate-10000 | |
bybit_price_dfs = [] | |
while True: | |
print(todate, oldtodate) | |
bybit_price = requests.get("https://api2.bybit.com/v2/public/open-interest?symbol=GMTUSDT&period=5min&limit=200&to={todate}".format(todate=todate)).json() | |
data = pd.DataFrame(bybit_price["result"]) | |
print(data.shape) | |
todate = int(data.timestamp.max()) | |
print("todate: ", todate, oldtodate) | |
if todate > oldtodate: | |
bybit_price_dfs.append(data) | |
else: | |
print(todate, oldtodate) | |
break | |
oldtodate=todate | |
todate += 200*5*60 | |
boidf = pd.concat(bybit_oi_dfs) | |
boidf["date"] = pd.to_datetime(boidf.timestamp, unit='s') | |
exch = ccxt.bybit() | |
symbol = "STXUSDT" | |
timer = "1m" | |
beginning=1676694984000 | |
# UTC now - 1 minute | |
now = int((datetime.utcnow() - datetime(1970,1,1)).total_seconds()-60)*1000 | |
dfs = [] | |
import time | |
while beginning < now: | |
data = exch.fetchOHLCV( | |
f"{symbol}", timer, since=int(beginning) | |
) | |
df = pd.DataFrame( | |
data, columns=["time", "open", "high", "low", "close", "volume"] | |
) | |
print(df.shape) | |
dfs.append(df) | |
print('max = ', df.time.max()) | |
beginning = df.time.max() | |
# time.sleep(5) | |
df = pd.concat(dfs, ignore_index=True) | |
df["time_utc"] = pd.to_datetime(df.time, unit="ms").astype(str) | |
df["symbol"] = symbol | |
df["logret"] = np.log(df.close/df.close.shift(1)) | |
df["ret"] = df.close/df.close.shift(1)-1 | |
df["date"] = pd.to_datetime(df.time, unit='ms') | |
# ax = plt.figure().gca() | |
# df.set_index("date")[["ret", "logret"]].cumsum().plot(grid=True, ax=ax) | |
# plt.title(symbol + " Returns") | |
# ax.yaxis.set_major_formatter(mtick.PercentFormatter(1.0)) | |
df["wap"] = (df.close*df.volume).rolling(5).sum() | |
df["roll_volume"] = df.volume.rolling(5).sum() | |
df["px"] = df.wap/df.roll_volume | |
ff = pd.merge(df, boidf, on="date") | |
ff["oi_delta"] = ff.open_interest-ff.open_interest.shift(1) | |
pos = ff.loc[ff.oi_delta>0].set_index("date") | |
times = ["1h", "4h", "8h", "24h", "48h", "96h"] | |
for time in times: | |
pos["oi_wap_{}".format(time)] = (pos.oi_delta*pos.px).rolling(time).sum() | |
pos["oi_sum_{}".format(time)] = (pos.oi_delta).rolling(time).sum() | |
pos["oi_px_{}".format(time)] = pos["oi_wap_{}".format(time)]/pos["oi_sum_{}".format(time)] | |
plt.figure() | |
pos.px.plot(color='black', alpha=0.6, label="Current Price") | |
plt.grid(True) | |
for time in times: | |
pos["oi_px_{}".format(time)].plot(label=time, alpha=0.5, linestyle='dashed') | |
plt.legend() | |
plt.grid(True) | |
plt.title("$STX Bybit Time Lagged OI weighted avg price \n (periods with open OI only)") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment