-
Notifications
You must be signed in to change notification settings - Fork 10
/
oi_mark_funding.py
307 lines (198 loc) · 12.2 KB
/
oi_mark_funding.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
###########################
# Author: slurpxbt
###########################
# this script pulls oi,funding and mark price data from bitmex, bybit, Okex, Binance for BTC perpetual swap contracts
# and aggreates data from whole market into files
#TODO: change data_file_path to your own in rows 42 and 174
# ---------------------------
# requirements:
# pip install bybit
# pip install APScheduler
# pip install bitmex
# if any other package is missing just pip install it
# ---------------------------
# imports --------------------------------------------------
import numpy as np
# bitmex imports
import bitmex
import datetime
# bybit imports
import bybit
# binance imports
from binance_f import RequestClient
# okex imports
from okex import swap_api as swap
from okex import futures_api as future
import json
# -----------------------------------------------------------
def get_and_store_btc_data():
data_file_path = "YOUR PATH"
client = bitmex.bitmex(test=False)
instrument_data = client.Instrument.Instrument_get(symbol='XBTUSD').result()
mex_mark = round(instrument_data[0][0]["markPrice"], 1) # [USD]
mex_oi = round(instrument_data[0][0]["openInterest"] / 10 ** 6, 3) # [mil USD]
mex_funding = round(instrument_data[0][0]["fundingRate"] * 100, 3) # [%]
# -----------------------------------------------------------
# get data from bybit
client = bybit.bybit(test=False, api_key="", api_secret="")
info = client.Market.Market_symbolInfo(symbol="BTCUSD").result()
info_dict = info[0]["result"][0]
bybit_mark = round(float(info_dict["mark_price"]), 1) # [USD]
bybit_oi = round(int(info_dict["open_interest"]) / 10 ** 6, 3) # [mil USD]
bybit_funding = round(float(info_dict["funding_rate"]) * 100, 3) # [%]
# -----------------------------------------------------------
# get data from binance
request_client = RequestClient(api_key="None", secret_key="None", url="https://fapi.binance.com")
binance_oi_api = request_client.get_open_interest(symbol="BTCUSDT")
binance_mark_api = request_client.get_mark_price(symbol="BTCUSDT")
binance_mark = round(binance_mark_api.markPrice , 1) # [USD]
binance_funding = round(binance_mark_api.lastFundingRate * 100, 3) # [mil USD]
binance_oi = round(binance_oi_api.openInterest * binance_mark / 10 ** 6, 3) # [%]
# -----------------------------------------------------------
# get data from okex
api_key = ""
secret_key = ""
passphrase = ""
swap_contract = "BTC-USD-SWAP"
swapAPI = swap.SwapAPI(api_key, secret_key, passphrase)
mark_price_api = swapAPI.get_mark_price(swap_contract)
okex_mark = round(float(mark_price_api["mark_price"]), 1) # [USD]
funding_api = swapAPI.get_funding_time(swap_contract)
okex_funding = round(float(funding_api["funding_rate"]) * 100, 3) # [%]
oi = swapAPI.get_holds(swap_contract)
okex_oi = round(int(oi["amount"]) * 100 / 10 ** 6, 3) # [mil USD]
# -----------------------------------------------------------
# time
time = datetime.datetime.now().strftime("%Y-%d-%m %H:%M") # year-day-month hours-minutes-seconds
# -----------------------------------------------------------
# print outs
print("-" * 200)
print(f"{mex_mark}[USD] - {mex_funding}[%] - {mex_oi}[mil USD] => Bitmex")
print(f"{bybit_mark}[USD] - {bybit_funding}[%] - {bybit_oi}[mil USD] => Bybit")
print(f"{binance_mark}[USD] - {binance_funding}[%] - {binance_oi}[mil USD] => Binance")
print(f"{okex_mark}[USD] - {okex_funding}[%] - {okex_oi}[mil USD] => Okex")
print("-" * 200)
# data storage
# all data
all_swap_data = [mex_mark, mex_funding, mex_oi, bybit_mark, bybit_funding, bybit_oi, binance_mark, binance_funding, binance_oi, okex_mark, okex_funding, okex_oi, time]
all_swap_data_txt = f"{mex_mark};{mex_funding};{mex_oi};{bybit_mark};{bybit_funding};{bybit_oi};{binance_mark};{binance_funding};{binance_oi};{okex_mark};{okex_funding};{okex_oi};{time}\n"
with open(f"{data_file_path}/data storage/all_swap_data_storage.txt", "a") as store_swap_data:
store_swap_data.write(all_swap_data_txt)
store_swap_data.close()
# -----------------------------------------------------------
# avg mark, cum OI, oi weighted funding
avg_mark = round(np.average([mex_mark, bybit_mark, binance_mark, okex_mark]), 3) # [USD]
cum_OI = round(np.sum([mex_oi, bybit_oi, binance_oi, okex_oi]), 3) # [mil USD 1000mil => 1bil]
oi_w_funding = round((mex_oi*mex_funding + bybit_oi*bybit_funding + binance_oi*binance_funding + okex_oi*okex_funding)/(mex_oi + bybit_oi + binance_oi + okex_oi), 3) # [%] => (-) bears are paying, (+) bulls are paying
avgMark_cumOI_oiWfunding = [avg_mark, cum_OI, oi_w_funding, time]
avgMark_cumOI_oiWfunding_txt = f"{avg_mark};{cum_OI};{oi_w_funding};{time}\n" # [USD] - [mil USD] - [%]
with open(f"{data_file_path}/data storage/avgMark_cumOI_oiWfunding_storage.txt", "a") as store_avgM_cumOi_oiWfund:
store_avgM_cumOi_oiWfund.write(avgMark_cumOI_oiWfunding_txt)
store_avgM_cumOi_oiWfund.close()
# -----------------------------------------------------------
# mark prices
mark_prices = [mex_mark, bybit_mark, binance_mark, okex_mark, time]
mark_prices_txt = f"{mex_mark};{bybit_mark};{binance_mark};{okex_mark};{time}\n" # [USD]
with open(f"{data_file_path}/data storage/mark_prices_storage.txt", "a") as store_mark_prices:
store_mark_prices.write(mark_prices_txt)
store_mark_prices.close()
# -----------------------------------------------------------
# fundings
fundings = [mex_funding, bybit_funding, binance_funding, okex_funding, time]
fundings_txt = f"{mex_funding};{bybit_funding};{binance_funding};{okex_funding};{time}\n" # [%]
with open(f"{data_file_path}/data storage/fundings_storage.txt", "a") as store_fundings:
store_fundings.write(fundings_txt)
store_fundings.close()
# -----------------------------------------------------------
# open interests
open_interests = [mex_oi, bybit_oi, binance_oi, okex_oi, time]
open_interests_txt = f"{mex_oi};{bybit_oi};{binance_oi};{okex_oi};{time}\n" # [mil USD]
with open(f"{data_file_path}/data storage/open_interests_storage.txt", "a") as store_open_interests:
store_open_interests.write(open_interests_txt)
store_open_interests.close()
# -----------------------------------------------------------
def get_and_store_eth_data():
data_file_path = "YOUR PATH"
client = bitmex.bitmex(test=False)
instrument_data = client.Instrument.Instrument_get(symbol='ETHUSD').result()
mex_mark = round(instrument_data[0][0]["markPrice"], 1) # [USD]
mex_oi = round(instrument_data[0][0]["openInterest"] / 10 ** 6, 3) # [mil USD]
mex_funding = round(instrument_data[0][0]["fundingRate"] * 100, 3) # [%]
# -----------------------------------------------------------
# get data from bybit
client = bybit.bybit(test=False, api_key="", api_secret="")
info = client.Market.Market_symbolInfo(symbol="ETHUSD").result()
info_dict = info[0]["result"][0]
bybit_mark = round(float(info_dict["mark_price"]), 1) # [USD]
bybit_oi = round(int(info_dict["open_interest"]) / 10 ** 6, 3) # [mil USD]
bybit_funding = round(float(info_dict["funding_rate"]) * 100, 3) # [%]
# -----------------------------------------------------------
# get data from binance
request_client = RequestClient(api_key="None", secret_key="None", url="https://fapi.binance.com")
binance_oi_api = request_client.get_open_interest(symbol="ETHUSDT")
binance_mark_api = request_client.get_mark_price(symbol="ETHUSDT")
binance_mark = round(binance_mark_api.markPrice , 1) # [USD]
binance_funding = round(binance_mark_api.lastFundingRate * 100, 3) # [mil USD]
binance_oi = round(binance_oi_api.openInterest * binance_mark / 10 ** 6, 3) # [%]
# -----------------------------------------------------------
# get data from okex
api_key = ""
secret_key = ""
passphrase = ""
swap_contract = "ETH-USD-SWAP"
swapAPI = swap.SwapAPI(api_key, secret_key, passphrase)
mark_price_api = swapAPI.get_mark_price(swap_contract)
okex_mark = round(float(mark_price_api["mark_price"]), 1) # [USD]
funding_api = swapAPI.get_funding_time(swap_contract)
okex_funding = round(float(funding_api["funding_rate"]) * 100, 3) # [%]
oi = swapAPI.get_holds(swap_contract)
okex_oi = round(int(oi["amount"]) * 10 / 10 ** 6, 3) # [mil USD]
# -----------------------------------------------------------
# time
time = datetime.datetime.now().strftime("%Y-%d-%m %H:%M") # year-day-month hours-minutes-seconds
# -----------------------------------------------------------
# print outs
print("-" * 200)
print(f"{mex_mark}[USD] - {mex_funding}[%] - {mex_oi}[mil USD] => Bitmex")
print(f"{bybit_mark}[USD] - {bybit_funding}[%] - {bybit_oi}[mil USD] => Bybit")
print(f"{binance_mark}[USD] - {binance_funding}[%] - {binance_oi}[mil USD] => Binance")
print(f"{okex_mark}[USD] - {okex_funding}[%] - {okex_oi}[mil USD] => Okex")
print("-" * 200)
# data storage
# all data
all_swap_data = [mex_mark, mex_funding, mex_oi, bybit_mark, bybit_funding, bybit_oi, binance_mark, binance_funding, binance_oi, okex_mark, okex_funding, okex_oi, time]
all_swap_data_txt = f"{mex_mark};{mex_funding};{mex_oi};{bybit_mark};{bybit_funding};{bybit_oi};{binance_mark};{binance_funding};{binance_oi};{okex_mark};{okex_funding};{okex_oi};{time}\n"
with open(f"{data_file_path}/data storage/all_swap_data_storage_eth.txt", "a") as store_swap_data:
store_swap_data.write(all_swap_data_txt)
store_swap_data.close()
# -----------------------------------------------------------
# avg mark, cum OI, oi weighted funding
avg_mark = round(np.average([mex_mark, bybit_mark, binance_mark, okex_mark]), 3) # [USD]
cum_OI = round(np.sum([mex_oi, bybit_oi, binance_oi, okex_oi]), 3) # [mil USD 1000mil => 1bil]
oi_w_funding = round((mex_oi*mex_funding + bybit_oi*bybit_funding + binance_oi*binance_funding + okex_oi*okex_funding)/(mex_oi + bybit_oi + binance_oi + okex_oi), 3) # [%] => (-) bears are paying, (+) bulls are paying
avgMark_cumOI_oiWfunding = [avg_mark, cum_OI, oi_w_funding, time]
avgMark_cumOI_oiWfunding_txt = f"{avg_mark};{cum_OI};{oi_w_funding};{time}\n" # [USD] - [mil USD] - [%]
with open(f"{data_file_path}/data storage/avgMark_cumOI_oiWfunding_storage_eth.txt", "a") as store_avgM_cumOi_oiWfund:
store_avgM_cumOi_oiWfund.write(avgMark_cumOI_oiWfunding_txt)
store_avgM_cumOi_oiWfund.close()
# -----------------------------------------------------------
# mark prices
mark_prices = [mex_mark, bybit_mark, binance_mark, okex_mark, time]
mark_prices_txt = f"{mex_mark};{bybit_mark};{binance_mark};{okex_mark};{time}\n" # [USD]
with open(f"{data_file_path}/data storage/mark_prices_storage_eth.txt", "a") as store_mark_prices:
store_mark_prices.write(mark_prices_txt)
store_mark_prices.close()
# -----------------------------------------------------------
# fundings
fundings = [mex_funding, bybit_funding, binance_funding, okex_funding, time]
fundings_txt = f"{mex_funding};{bybit_funding};{binance_funding};{okex_funding};{time}\n" # [%]
with open(f"{data_file_path}/data storage/fundings_storage_eth.txt", "a") as store_fundings:
store_fundings.write(fundings_txt)
store_fundings.close()
# -----------------------------------------------------------
# open interests
open_interests = [mex_oi, bybit_oi, binance_oi, okex_oi, time]
open_interests_txt = f"{mex_oi};{bybit_oi};{binance_oi};{okex_oi};{time}\n" # [mil USD]
with open(f"{data_file_path}/data storage/open_interests_storage_eth.txt", "a") as store_open_interests:
store_open_interests.write(open_interests_txt)
store_open_interests.close()