Aureliano90 / Futures-Spot-Arbitrage-OKEx-V5

An async program for futures-spot arbitrage on OKEx using V5 API.
GNU Affero General Public License v3.0
142 stars 49 forks source link
arbitrage-trading okex-bot okex-v5-api perpetual-futures

Futures-Spot-Arbitrage-OKEx-V5

Introduction

An asynchronous object-oriented program for arbitrage between perpetual futures and spot on OKEx using V5 API. Initially written in April 2021 for my personal use. Modified and optimized OKEx V3 API SDK according to V5 API documentation.

Chinese and English support, completed with annotations and docstrings.

From the author

This project was started when I was a newbie in Python and software development. I am glad it gathers a decent number of stars and hopefully some usage. However it feels outdated and lacking maintenance even though it still functions and makes money.

Therefore I'm soliciting feedbacks or suggestions to make an improvement plan. Given enough feedbacks and community contribution, an improvement plan will take effect. Please see the list of issues or create new issues.

Features

Premium

Features may be added

Optimizations

Implemented asyncio and websocket. Web IOs are parallelized where possible. AsyncClient is initialzed as a class member instead of Context Manager to avoid constantly creating and killing sessions which has non-negligible overheads. Special care was taken for proper client closure. A custom semaphore was created to control concurrent REST API access. Websocket is used to fetch real time price feed. Websocket streaming functions are used as AsyncGenerators for elegant integration.

Classes are given __await__ attribute where necessary and can be initialized asynchronously. Decorator @call_coroutine was created to call coroutines directly in normal context instead of loop.run_until_complete(coro). So simply call coro in normal context and await coro in async context.

Installation

Install Python 3.8+ and required packages.

python setup.py install or pip install -r requirements.txt

git submodule update --init --recursive

Install MongoDB.

Store API keys in .env (use account 3 for demo trading).

Set language='cn' for Chinese and language='en' for English in config.py.

Simply python main.py.

Contributing

Contributions are welcome. Please open an issue if you have any questions or suggestions.

Background

Futures spot arbitrage in crypto is profitable because there is a huge demand for long leverage in the crypto market. Arbitrageurs act as the counterparty to buyers in the perpetual swap market. They effectively multiply and transfer the buying pressure in the perpetual swap market to the spot market. Leverage comes at a cost. Therefore arbitrageurs or market makers are entitled to charge interest on futures buyers, just like stockbrokers charge interest for margin. As a result the APY depends on the market sentiment, ranging from 10% to 100%+.

Disclaimer

The author does not assume responsibilities for the use of this program, nor is warranty granted.

License

This program is licensed under the AGPL-3.0 License. See LICENSE for full disclosure.

Permissions of this license are conditioned on making available complete source code of licensed works and modifications, which include larger works using a licensed work, under the same license. Copyright and license notices must be preserved. Contributors provide an express grant of patent rights. When a modified version is used to provide a service over a network, the complete source code of the modified version must be made available.

Reference

[1] OKEx V5 API

[2] Cryptocurrency Spot-Futures Arbitrage Strategy Report

[3] Alternative Opportunities In Crypto Space: Spot-Futures Arbitrage