Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release/1.0.0 #4

Merged
merged 11 commits into from
Mar 15, 2023
9 changes: 9 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
MIT License

Copyright (c) 2023 Hirotaka Aoki

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,29 @@ This library can detect perpetual contract with a large divergence in funding ra

**NOTE: This library does not include the feature to perform automatic funding rate arbitrage.**

[//]: # (## Installation)
## Installation

[//]: # ()
[//]: # (```bash)

[//]: # (pip install funding-rate-arbitrage)
```bash

[//]: # (```)
git clone https://github.com/aoki-h-jp/funding-rate-arbitrage.git
pip install funding-rate-arbitrage

```

## Usage
```python
from frarb import FundingRateArbitrage

fr = FundingRateArbitrage()

# fetch all perp funding rate on binance
fr_binance = fr.fetch_all_funding_rate(exchange='binance')

# get commission on binance with futures, maker
cm_binance = fr.get_commission(exchange='binance', trade='futures', taker=False)
```

[//]: # (## Example)

## Disclaimer
This project is for educational purposes only. You should not construe any such information or other material as legal,
Expand Down
10 changes: 10 additions & 0 deletions examples/fetch_funding_rate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
"""
An example of fetching funding rate
"""
from frarb import FundingRateArbitrage


if __name__ == '__main__':
# fetch from binance
fr = FundingRateArbitrage()
print(fr.fetch_all_funding_rate(exchange='binance'))
12 changes: 12 additions & 0 deletions examples/fetch_funding_rate_all_exchanges.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
An example of fetching funding rate
"""
from frarb import FundingRateArbitrage


if __name__ == '__main__':
# fetch from all exchanges
fr = FundingRateArbitrage()
for ex in fr.get_exchanges():
print(ex)
print(fr.fetch_all_funding_rate(exchange=ex))
19 changes: 19 additions & 0 deletions examples/get_commission.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
An example of getting commission.
"""
from frarb import FundingRateArbitrage


if __name__ == '__main__':
fr = FundingRateArbitrage()
# binance futures maker commission with BNB
print('binance futures maker commission with BNB')
print(fr.get_commission(exchange='binance', trade='futures', taker=False, by_token=True))

# bybit spot taker commission
print('bybit spot taker commission')
print(fr.get_commission(exchange='bybit', trade='spot'))

# OKX spot maker commission
print('OKX spot maker commission')
print(fr.get_commission(exchange='okx', trade='spot', taker=False))
3 changes: 3 additions & 0 deletions frarb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .frarb import FundingRateArbitrage

__all__ = ["FundingRateArbitrage"]
150 changes: 150 additions & 0 deletions frarb/frarb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import ccxt
import pandas as pd


class FundingRateArbitrage:
def __init__(self):
self.exchanges = ['binance', 'bybit', 'okx', 'bitget', 'gate', 'coinex']

@staticmethod
def fetch_all_funding_rate(exchange: str) -> dict:
"""
Fetch funding rates on all perpetual contracts listed on the exchange.

Args:
exchange (str): Name of exchange (binance, bybit, ...)

Returns (dict): Dict of perpetual contract pair and funding rate.

"""
ex = getattr(ccxt, exchange)()
info = ex.load_markets()
perp = [p for p in info if info[p]['linear']]
return {p: ex.fetch_funding_rate(p)['fundingRate'] for p in perp}

def get_exchanges(self) -> list:
"""
Get a list of exchanges.
Returns (list): List of exchanges.

"""
return self.exchanges

def add_exchanges(self, exchange: str) -> list:
"""
Add exchanges.
Args:
exchange (str): Name of the exchange you want to add.

Returns (list): List of exchanges.

"""
self.exchanges.append(exchange)
return self.exchanges

@staticmethod
def get_commission(exchange: str, trade: str, taker=True, by_token=False) -> float:
"""
Get commission.
TODO: Get with ccxt or CEX API.
Args:
exchange (str): Name of exchanges (binance, bybit, ...)
trade (str): Spot Trade or Futures Trade
taker (bool): is Taker or is Maker
by_token (bool): Pay with exchange tokens (BNB, CET, ...)

Returns (float): Commission.

"""
# https://www.binance.com/en/fee/schedule
if exchange == 'binance':
if trade == 'spot':
if by_token:
return 0.075
else:
return 0.1
elif trade == 'futures':
if taker:
if by_token:
return 0.036
else:
return 0.04
else:
if by_token:
return 0.018
else:
return 0.02
else:
raise KeyError

# https://www.bybit.com/ja-JP/help-center/bybitHC_Article?id=360039261154&language=ja
if exchange == 'bybit':
if trade == 'spot':
return 0.1
elif trade == 'futures':
if taker:
return 0.06
else:
return 0.01
else:
raise KeyError

# https://www.okx.com/fees
if exchange == 'okx':
if trade == 'spot':
if taker:
return 0.1
else:
return 0.08
elif trade == 'futures':
if taker:
return 0.05
else:
return 0.02
else:
raise KeyError

# https://www.bitget.com/ja/rate/
if exchange == 'bitget':
if trade == 'spot':
if by_token:
return 0.08
else:
return 0.1
elif trade == 'futures':
if taker:
return 0.051
else:
return 0.017
else:
raise KeyError

# https://www.gate.io/ja/fee
if exchange == 'gate':
if trade == 'spot':
if by_token:
return 0.15
else:
return 0.2
elif trade == 'futures':
if taker:
return 0.05
else:
return 0.015
else:
raise KeyError

# https://www.coinex.zone/fees?type=spot&market=normal
if exchange == 'gate':
if trade == 'spot':
if by_token:
return 0.16
else:
return 0.2
elif trade == 'futures':
if taker:
return 0.05
else:
return 0.03
else:
raise KeyError
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ccxt
pandas
12 changes: 12 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from setuptools import setup

setup(
name='funding-rate-arbitrage',
version="1.0.0",
description='A framework to help you easily perform funding rate arbitrage on major centralized cryptocurrency '
'exchanges.',
install_requires=['ccxt', 'pandas'],
author='aoki-h-jp',
author_email='[email protected]',
license='MIT'
)