forked from Kucoin/kucoin-universal-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_arbitrage_strategy.py
More file actions
456 lines (389 loc) · 22.1 KB
/
example_arbitrage_strategy.py
File metadata and controls
456 lines (389 loc) · 22.1 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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
"""
DISCLAIMER:
This strategy is provided for educational and illustrative purposes only. It is not intended to be used as financial
or investment advice. Trading cryptocurrencies involves significant risk, and you should carefully consider your
investment objectives, level of experience, and risk appetite. Past performance of any trading strategy is not
indicative of future results.
The authors and contributors of this example are not responsible for any financial losses or damages that may occur
from using this code. Use it at your own discretion and consult with a professional financial advisor if necessary.
"""
import logging
import math
import os
import time
import uuid
from datetime import datetime, timedelta
from enum import Enum
from kucoin_universal_sdk.api.client import DefaultClient
from kucoin_universal_sdk.generate.account.account.model_get_cross_margin_account_req import \
GetCrossMarginAccountReqBuilder, GetCrossMarginAccountReq
from kucoin_universal_sdk.generate.account.account.model_get_futures_account_req import GetFuturesAccountReqBuilder
from kucoin_universal_sdk.generate.account.account.model_get_spot_account_list_req import GetSpotAccountListReqBuilder, \
GetSpotAccountListReq
from kucoin_universal_sdk.generate.futures.fundingfees.model_get_current_funding_rate_req import \
GetCurrentFundingRateReqBuilder
from kucoin_universal_sdk.generate.futures.market.model_get_symbol_req import GetSymbolReqBuilder
from kucoin_universal_sdk.generate.futures.order.model_add_order_req import AddOrderReqBuilder, AddOrderReq
from kucoin_universal_sdk.generate.futures.order.model_cancel_order_by_id_req import CancelOrderByIdReqBuilder
from kucoin_universal_sdk.generate.futures.order.model_get_order_by_order_id_req import \
GetOrderByOrderIdReqBuilder as FuturesGetOrderByOrderIdReqBuilder
from kucoin_universal_sdk.generate.futures.order.model_get_order_by_order_id_resp import GetOrderByOrderIdResp
from kucoin_universal_sdk.generate.margin.order.model_add_order_req import \
AddOrderReqBuilder as MarginAddOrderReqBuilder, AddOrderReq as MarginAddOrderReq
from kucoin_universal_sdk.generate.margin.order.model_cancel_order_by_order_id_req import CancelOrderByOrderIdReqBuilder
from kucoin_universal_sdk.generate.margin.order.model_get_order_by_order_id_req import \
GetOrderByOrderIdReqBuilder as MarginGetOrderByOrderIdReqBuilder
from kucoin_universal_sdk.generate.service.account_api import AccountService
from kucoin_universal_sdk.generate.service.futures_api import FuturesService
from kucoin_universal_sdk.generate.service.margin_api import MarginService
from kucoin_universal_sdk.generate.service.spot_api import SpotService
from kucoin_universal_sdk.generate.spot.market.model_get_ticker_req import GetTickerReqBuilder
from kucoin_universal_sdk.generate.spot.order.model_add_order_sync_req import AddOrderSyncReqBuilder, AddOrderSyncReq
from kucoin_universal_sdk.generate.spot.order.model_cancel_order_by_order_id_sync_req import \
CancelOrderByOrderIdSyncReqBuilder
from kucoin_universal_sdk.generate.spot.order.model_cancel_order_by_order_id_sync_resp import \
CancelOrderByOrderIdSyncResp
from kucoin_universal_sdk.generate.spot.order.model_get_order_by_order_id_req import GetOrderByOrderIdReqBuilder
from kucoin_universal_sdk.model.client_option import ClientOptionBuilder
from kucoin_universal_sdk.model.constants import GLOBAL_API_ENDPOINT, GLOBAL_FUTURES_API_ENDPOINT, \
GLOBAL_BROKER_API_ENDPOINT
from kucoin_universal_sdk.model.transport_option import TransportOptionBuilder
SPOT_SYMBOL = "DOGE-USDT"
FUTURES_SYMBOL = "DOGEUSDTM"
BASE_CURRENCY = "USDT"
# Maximum time to wait for an order to fill
MAX_PLACE_ORDER_WAIT_TIME_SEC = 15
class MarketSide(Enum):
BUY = "buy"
SELL = "sell"
class MarketType(Enum):
SPOT = "SPOT"
MARGIN = "MARGIN"
FUTURES = "FUTURES"
def check_available_balance(account_service: AccountService, price: float, amount: float,
marketType: MarketType) -> bool:
"""
Checks if the available balance is sufficient for the trade.
Args:
account_service (AccountService): The account API client instance.
price (float): The price of the asset.
amount (float): The amount of the asset to trade.
marketType (MarketType): The type of the market.
Returns:
bool: True if the balance is sufficient, False otherwise.
"""
trade_value = price * amount
if marketType == MarketType.SPOT:
# check spot account
get_spot_account_resp = account_service.get_account_api().get_spot_account_list(GetSpotAccountListReqBuilder()
.set_type(
GetSpotAccountListReq.TypeEnum.TRADE)
.set_currency(BASE_CURRENCY)
.build())
spot_available_balance = sum(float(account_data.available) for account_data in get_spot_account_resp.data)
logging.info(
f"[SPOT] Available {BASE_CURRENCY} balance: {spot_available_balance:.2f}, Required: {trade_value:.2f}")
return spot_available_balance >= trade_value
elif marketType == MarketType.FUTURES:
# check futures account
futures_available_balance = account_service.get_account_api().get_futures_account(GetFuturesAccountReqBuilder()
.set_currency(BASE_CURRENCY)
.build()).available_balance
logging.info(
f"[FUTURES] Available {BASE_CURRENCY} balance: {futures_available_balance:.2f}, Required: {trade_value:.2f}")
return futures_available_balance >= trade_value
else:
# check margin account
margin_available_balance = float(account_service.get_account_api().get_cross_margin_account(
GetCrossMarginAccountReqBuilder().set_query_type(
GetCrossMarginAccountReq.QueryTypeEnum.MARGIN).set_quote_currency(
GetCrossMarginAccountReq.QuoteCurrencyEnum.USDT).build()).total_asset_of_quote_currency)
logging.info(
f"[MARGIN] Available {BASE_CURRENCY} balance: {margin_available_balance:.2f}, Required: {trade_value:.2f}")
return margin_available_balance >= trade_value
def get_last_traded_price(spot_service: SpotService, futures_service: FuturesService):
"""
Fetches the last traded price for the specified spot and futures symbols.
Args:
spot_service (SpotService): The API client for spot market.
futures_service (FuturesService): The API client for futures market.
Returns:
tuple[float, float]: The last traded price for spot and futures markets.
"""
spot_price = float(spot_service.get_market_api().get_ticker(
GetTickerReqBuilder().set_symbol(SPOT_SYMBOL).build()).price)
futures_price = float(futures_service.get_market_api().get_symbol(
GetSymbolReqBuilder().set_symbol(FUTURES_SYMBOL).build()).last_trade_price)
logging.info(f"[PRICE] Spot Price: {spot_price:.5f}, Futures Price: {futures_price:.5f}")
return spot_price, futures_price
def funding_rate_arbitrage_strategy(futures_service: FuturesService, spot_service: SpotService,
margin_service: MarginService, account_service: AccountService,
amount: float, threshold: float) -> None:
"""
Funding Rate Arbitrage Strategy.
This strategy exploits differences in funding rates between futures and spot/margin markets.
It operates by identifying arbitrage opportunities based on funding rates and executing corresponding
long or short positions in the appropriate markets.
Strategy Logic:
1. Fetch the funding rate for the specified futures symbol.
2. If the funding rate's absolute value is below the specified threshold, exit the strategy.
3. If the funding rate is positive:
- LONG the spot market and SHORT the futures market.
4. If the funding rate is negative:
- SHORT the margin market and LONG the futures market.
5. Ensure sufficient balance in each account before placing orders.
6. Monitor order status, and cancel unfilled orders after a timeout.
Args:
futures_service (FuturesService): The API client for the futures market.
spot_service (SpotService): The API client for the spot market.
margin_service (MarginService): The API client for the margin market.
account_service (AccountService): The API client for account management.
amount (float): The amount to trade for arbitrage.
threshold (float): The minimum funding rate threshold to trigger the strategy.
Returns:
None
"""
try:
# Step 1: Fetch funding rate
funding_rate_req = GetCurrentFundingRateReqBuilder().set_symbol(FUTURES_SYMBOL).build()
funding_rate = futures_service.get_funding_fees_api().get_current_funding_rate(funding_rate_req).value * 100
logging.info(f"[STRATEGY] Funding rate for {FUTURES_SYMBOL}: {funding_rate:.6f}%")
# Step 2: Check if funding rate meets the threshold for arbitrage
if abs(funding_rate) < threshold:
logging.info(
f"[STRATEGY] No arbitrage opportunity: Funding rate ({funding_rate:.6f}%) below threshold ({threshold}%).")
return
# Step 3: Fetch the latest spot and futures prices
spot_price, futures_price = get_last_traded_price(spot_service, futures_service)
# Calculate futures order amount in contracts
futures_multiplier = futures_service.get_market_api().get_symbol(
GetSymbolReqBuilder().set_symbol(FUTURES_SYMBOL).build()).multiplier
futures_amount = math.ceil(amount / futures_multiplier)
# Step 4: Execute arbitrage based on funding rate direction
if funding_rate > 0:
logging.info("[STRATEGY] Positive funding rate detected. Executing LONG spot and SHORT futures arbitrage.")
# Ensure sufficient balance for the spot and futures accounts
if not check_available_balance(account_service, spot_price, amount, MarketType.SPOT):
logging.warning("[STRATEGY] Insufficient balance in spot account.")
return
if not check_available_balance(account_service, futures_price, amount, MarketType.FUTURES):
logging.warning("[STRATEGY] Insufficient balance in futures account.")
return
# Execute orders
if not add_spot_order_wait_fill(spot_service, SPOT_SYMBOL, MarketSide.BUY, amount, spot_price):
logging.warning("[STRATEGY] Failed to execute spot order.")
return
if not add_futures_order_wait_fill(futures_service, FUTURES_SYMBOL, MarketSide.SELL, futures_amount,
futures_price):
logging.warning("[STRATEGY] Failed to execute futures order.")
return
else:
logging.info(
"[STRATEGY] Negative funding rate detected. Executing SHORT margin and LONG futures arbitrage.")
# Ensure sufficient balance for the margin and futures accounts
if not check_available_balance(account_service, spot_price, amount, MarketType.MARGIN):
logging.warning("[STRATEGY] Insufficient balance in margin account.")
return
if not check_available_balance(account_service, futures_price, amount, MarketType.FUTURES):
logging.warning("[STRATEGY] Insufficient balance in futures account.")
return
# Execute orders
if not add_margin_order_wait_fill(margin_service, SPOT_SYMBOL, amount, spot_price):
logging.warning("[STRATEGY] Failed to execute margin order.")
return
if not add_futures_order_wait_fill(futures_service, FUTURES_SYMBOL, MarketSide.BUY, futures_amount,
futures_price):
logging.warning("[STRATEGY] Failed to execute futures order.")
return
logging.info("[STRATEGY] Arbitrage execution completed successfully.")
except Exception as e:
logging.error(f"[STRATEGY] Error executing arbitrage strategy: {e}")
def add_spot_order_wait_fill(spot_service: SpotService, symbol: str, side: MarketSide, amount: float,
price: float) -> bool:
"""
Places a spot order and waits for it to be filled within a specified timeout.
Args:
spot_service (SpotService): The API client for the spot market.
symbol (str): The trading pair symbol (e.g., "BTC-USDT").
side (MarketSide): The side of the trade (MarketSide.BUY or MarketSide.SELL).
amount (float): The amount of the asset to trade.
price (float): The price per unit of the asset.
Returns:
bool: True if the order is filled successfully, False if the order times out or fails.
"""
# Build and place the spot order
add_order_sync_req = (
AddOrderSyncReqBuilder()
.set_client_oid(str(uuid.uuid4()))
.set_side(AddOrderSyncReq.SideEnum.BUY if side == MarketSide.BUY else AddOrderSyncReq.SideEnum.SELL)
.set_symbol(symbol)
.set_type(AddOrderSyncReq.TypeEnum.LIMIT)
.set_remark("arbitrage")
.set_price(f"{price:.4f}")
.set_size(f"{amount:.4f}")
.build()
)
order = spot_service.get_order_api().add_order_sync(add_order_sync_req)
logging.info(f"[SPOT ORDER] Placed {side.value.upper()} order for {amount} {symbol} at {price:.6f}. "
f"Order ID: {order.order_id}")
# Wait for the order to be filled within the timeout period
deadline = datetime.now() + timedelta(seconds=MAX_PLACE_ORDER_WAIT_TIME_SEC)
while datetime.now() < deadline:
order_detail = spot_service.get_order_api().get_order_by_order_id(
GetOrderByOrderIdReqBuilder().set_symbol(symbol).set_order_id(order.order_id).build())
if not order_detail.active:
logging.info(f"[SPOT ORDER] Order filled successfully: {side.value.upper()} {amount} {symbol}. "
f"Order ID: {order.order_id}")
return True
# Wait before checking order status again
logging.info(f"[SPOT ORDER] Checking order status in 1 seconds...")
time.sleep(1)
# Cancel the order if it was not filled within the timeout period
logging.warning(
f"[SPOT ORDER] Order not filled within {MAX_PLACE_ORDER_WAIT_TIME_SEC} seconds. Cancelling order...")
cancel_resp = spot_service.get_order_api().cancel_order_by_order_id_sync(
CancelOrderByOrderIdSyncReqBuilder().set_order_id(order.order_id).set_symbol(symbol).build())
if cancel_resp.status != CancelOrderByOrderIdSyncResp.StatusEnum.DONE:
raise RuntimeError(f"[SPOT ORDER] Failed to cancel order. Order ID: {order.order_id}")
logging.info(f"[SPOT ORDER] Order cancelled successfully. Order ID: {order.order_id}")
return False
def add_futures_order_wait_fill(futures_service: FuturesService, symbol: str, side: MarketSide, amount: int,
price: float) -> bool:
"""
Places a futures order and waits for it to be filled within a specified timeout.
Args:
futures_service (FuturesService): The API client for the futures market.
symbol (str): The trading pair symbol (e.g., "BTCUSDTM").
side (MarketSide): The side of the trade (MarketSide.BUY or MarketSide.SELL).
amount (int): The size of the futures contract to trade.
price (float): The price per contract.
Returns:
bool: True if the order is filled successfully, False if the order times out or fails.
"""
# Build and place the futures order
add_order_sync_req = (
AddOrderReqBuilder()
.set_client_oid(str(uuid.uuid4()))
.set_side(AddOrderReq.SideEnum.BUY if side == MarketSide.BUY else AddOrderReq.SideEnum.SELL)
.set_symbol(symbol)
.set_type(AddOrderReq.TypeEnum.LIMIT)
.set_remark("arbitrage")
.set_price(f"{price:.4f}")
.set_leverage(1) # Default leverage set to 1
.set_size(amount)
.build()
)
order = futures_service.get_order_api().add_order(add_order_sync_req)
logging.info(f"[FUTURES ORDER] Placed {side.value.upper()} order for {amount} {symbol} at {price:.6f}. "
f"Order ID: {order.order_id}")
# Wait for the order to be filled within the timeout period
deadline = datetime.now() + timedelta(seconds=MAX_PLACE_ORDER_WAIT_TIME_SEC)
while datetime.now() < deadline:
# Wait before checking order status again
logging.info(f"[FUTURES ORDER] Checking order status in 1 seconds...")
time.sleep(1)
order_detail = futures_service.get_order_api().get_order_by_order_id(
FuturesGetOrderByOrderIdReqBuilder().set_order_id(order.order_id).build())
if order_detail.status == GetOrderByOrderIdResp.StatusEnum.DONE:
logging.info(f"[FUTURES ORDER] Order filled successfully: {side.value.upper()} {amount} {symbol}. "
f"Order ID: {order.order_id}")
return True
# Cancel the order if it was not filled within the timeout period
logging.warning(
f"[FUTURES ORDER] Order not filled within {MAX_PLACE_ORDER_WAIT_TIME_SEC} seconds. Cancelling order...")
cancel_resp = futures_service.get_order_api().cancel_order_by_id(
CancelOrderByIdReqBuilder().set_order_id(order.order_id).build())
if order.order_id not in cancel_resp.cancelled_order_ids:
raise RuntimeError(f"[FUTURES ORDER] Failed to cancel order. Order ID: {order.order_id}")
logging.info(f"[FUTURES ORDER] Order cancelled successfully. Order ID: {order.order_id}")
return False
def add_margin_order_wait_fill(margin_service: MarginService, symbol: str, amount: float, price: float) -> bool:
"""
Places a margin order and waits for it to be filled within a specified timeout.
Args:
margin_service (MarginService): The API client for the margin market.
symbol (str): The trading pair symbol (e.g., "BTC-USDT").
amount (float): The amount of the asset to trade.
price (float): The price per unit of the asset.
Returns:
bool: True if the order is filled successfully, False if the order times out or fails.
"""
# Build and place the margin order
add_order_sync_req = (
MarginAddOrderReqBuilder()
.set_client_oid(str(uuid.uuid4()))
.set_side(MarginAddOrderReq.SideEnum.BUY)
.set_symbol(symbol)
.set_type(MarginAddOrderReq.TypeEnum.LIMIT)
.set_is_isolated(False)
.set_auto_borrow(True) # Automatically borrow if necessary
.set_auto_repay(True) # Automatically repay borrowed amount
.set_price(f"{price:.4f}")
.set_size(f"{amount:.4f}")
.build()
)
order = margin_service.get_order_api().add_order(add_order_sync_req)
logging.info(f"[MARGIN ORDER] Placed SELL order for {amount} {symbol} at {price:.6f}. "
f"Order ID: {order.order_id}")
# Wait for the order to be filled within the timeout period
deadline = datetime.now() + timedelta(seconds=MAX_PLACE_ORDER_WAIT_TIME_SEC)
while datetime.now() < deadline:
order_detail = margin_service.get_order_api().get_order_by_order_id(
MarginGetOrderByOrderIdReqBuilder().set_symbol(symbol).set_order_id(order.order_id).build())
if not order_detail.active:
logging.info(f"[MARGIN ORDER] Order filled successfully: SELL {amount} {symbol}. "
f"Order ID: {order.order_id}")
return True
# Wait before checking order status again
logging.info(f"[MARGIN ORDER] Checking order status in 1 seconds...")
time.sleep(1)
# Cancel the order if it was not filled within the timeout period
logging.warning(
f"[MARGIN ORDER] Order not filled within {MAX_PLACE_ORDER_WAIT_TIME_SEC} seconds. Cancelling order...")
cancel_resp = margin_service.get_order_api().cancel_order_by_order_id(
CancelOrderByOrderIdReqBuilder().set_order_id(order.order_id).set_symbol(symbol).build())
if cancel_resp.order_id != '':
raise RuntimeError(f"[MARGIN ORDER] Failed to cancel order. Order ID: {order.order_id}")
logging.info(f"[MARGIN ORDER] Order cancelled successfully. Order ID: {order.order_id}")
return False
if __name__ == "__main__":
"""
Main function to run the funding rate arbitrage strategy for a specific duration.
"""
# Initialize APIs
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s %(levelname)s - %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'
)
key = os.getenv("API_KEY", "")
secret = os.getenv("API_SECRET", "")
passphrase = os.getenv("API_PASSPHRASE", "")
http_transport_option = (
TransportOptionBuilder()
.set_keep_alive(True)
.set_max_pool_size(10)
.set_max_connection_per_pool(10)
.build()
)
client_option = (
ClientOptionBuilder()
.set_key(key)
.set_secret(secret)
.set_passphrase(passphrase)
.set_spot_endpoint(GLOBAL_API_ENDPOINT)
.set_futures_endpoint(GLOBAL_FUTURES_API_ENDPOINT)
.set_broker_endpoint(GLOBAL_BROKER_API_ENDPOINT)
.set_transport_option(http_transport_option)
.build()
)
client = DefaultClient(client_option)
kucoin_rest_service = client.rest_service()
futures_service = kucoin_rest_service.get_futures_service()
spot_service = kucoin_rest_service.get_spot_service()
margin_service = kucoin_rest_service.get_margin_service()
account_service = kucoin_rest_service.get_account_service()
amount = 100 # Amount to trade
threshold = 0.005 # Minimum profit threshold
logging.info(f"Starting funding rate arbitrage strategy...")
funding_rate_arbitrage_strategy(futures_service, spot_service, margin_service, account_service, amount, threshold)