Giter Site home page Giter Site logo

jhshengxy / futures-spot-arbitrage-okex-v5 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from aureliano90/futures-spot-arbitrage-okex-v5

0.0 0.0 0.0 710 KB

An async program for futures-spot arbitrage on OKEx using V5 API.

License: GNU Affero General Public License v3.0

Python 100.00%

futures-spot-arbitrage-okex-v5's Introduction

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.

Features

  • Command line interface;
  • Sort and output historical funding rates over a given period;
  • Analyze historical funding rates and volatility (taken as NATR) to find most profitable underlying for arbitrage;
  • Open a position by longing spot and shorting perpetual futures equally and simultaneously
    • Use historical tickers and statistics to open position when futures have max premium, assuming that the premium over a period of time satisfies Gaussian distribution;
    • Accelerate when a desired premium does not appear by given time;

Premium

  • Close a position by selling spot and closing short on perpetual futures
    • Use historical tickers and statistics to close position when futures have max discount;
    • Accelerate when a desired discount does not appear by given time;
    • Proactively close a position when the predicted funding rate is low enough such that it is better off to reopen later;
  • Monitor existing positions
    • Sell spot and close short to add margin when price rises to avoid liquidation;
    • Reduce margin to buy spot and open short when price drops to maintain exposure;
  • Precise order size control and leverage management;
  • Store tickers, funding rates, portfolio and transactions in MongoDB;
  • Calculate current and historical PnL, APR and APY;
  • Plot the distribution of premium/discount for an underlying over a given period.

Features may be added

  • Typical arbitrage of perpetual futures and spot is passively receiving funding fees while keeping the position open. However it is profitable to proactively close a portion of the position when the futures premium surges and reopen it later when the premium subdues.

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.

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

futures-spot-arbitrage-okex-v5's People

Contributors

aureliano90 avatar shuhuiluo avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.