Skip to content

Commit

Permalink
Merge pull request #13 from aoki-h-jp/feature/1.2.1/fixsetup
Browse files Browse the repository at this point in the history
Feature/1.2.1/fixsetup
  • Loading branch information
aoki-h-jp authored Aug 25, 2023
2 parents 5c8c0a2 + 7d2188e commit 0f1f7cf
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 21 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ This library can detect perpetual contract with a large divergence in funding ra


```bash
git clone https://github.com/aoki-h-jp/funding-rate-arbitrage.git
pip install funding-rate-arbitrage
pip install git+https://github.com/aoki-h-jp/funding-rate-arbitrage
```

## Usage
### Fetch FR & commission

```python
from frarb import FundingRateArbitrage
from funding_rate_arbitrage.frarb import FundingRateArbitrage

fr = FundingRateArbitrage()

Expand All @@ -42,8 +42,9 @@ cm_binance = fr.get_commission(exchange='binance', trade='futures', taker=False)
```

### Fetch FR history

```python
from frarb import FundingRateArbitrage
from funding_rate_arbitrage.frarb import FundingRateArbitrage

fr = FundingRateArbitrage()

Expand All @@ -54,7 +55,7 @@ fr.fetch_funding_rate_history(exchange='binance', symbol='BTC/USDT:USDT')


### Display large FR divergence on single CEX
```python
```bash
# display large funding rate divergence on bybit
>>> fr.display_large_divergence_single_exchange(exchange='bybit', display_num=5)
Funding Rate [%] Commission [%] Revenue [/100 USDT]
Expand Down Expand Up @@ -132,7 +133,7 @@ Commission: 0.38 %
```

### Display large FR divergence between CEX
```python
```bash
# display large funding rate divergence between CEX.
>>> fr.display_large_divergence_multi_exchange(display_num=5, sorted_by='divergence')
binance bybit okx bitget gate coinex Divergence [%] Commission [%] Revenue [/100 USDT]
Expand Down
2 changes: 1 addition & 1 deletion examples/fetch_funding_rate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
An example of fetching funding rate
"""
from frarb import FundingRateArbitrage
from funding_rate_arbitrage.frarb import FundingRateArbitrage


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion examples/fetch_funding_rate_all_exchanges.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
An example of fetching funding rate
"""
from frarb import FundingRateArbitrage
from funding_rate_arbitrage.frarb import FundingRateArbitrage


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions examples/fetch_funding_rate_history.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""
An example of fetching funding rate history
"""
from frarb import FundingRateArbitrage
from funding_rate_arbitrage.frarb import FundingRateArbitrage


if __name__ == '__main__':
# fetch from binance
fr = FundingRateArbitrage()
# figure funding rate history
fr.fetch_funding_rate_history(exchange='binance', symbol='BTC/USDT:USDT')
fr.figure_funding_rate_history(exchange='binance', symbol='BTC/USDT:USDT')
2 changes: 1 addition & 1 deletion examples/get_commission.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
An example of getting commission.
"""
from frarb import FundingRateArbitrage
from funding_rate_arbitrage.frarb import FundingRateArbitrage


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion examples/get_large_divergence_multi_exchange.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
An example of getting large divergence between multi exchange.
"""
from frarb import FundingRateArbitrage
from funding_rate_arbitrage.frarb import FundingRateArbitrage


if __name__ == '__main__':
Expand Down
2 changes: 1 addition & 1 deletion examples/get_large_divergence_single_exchange.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""
An example of displaying large divergence by single exchange.
"""
from frarb import FundingRateArbitrage
from funding_rate_arbitrage.frarb import FundingRateArbitrage


if __name__ == '__main__':
Expand Down
3 changes: 0 additions & 3 deletions frarb/__init__.py

This file was deleted.

4 changes: 4 additions & 0 deletions funding_rate_arbitrage/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
"""
Funding Rate Arbitrage: A trading strategy that takes advantage of the difference in funding rates between perpetual swaps.
"""
import funding_rate_arbitrage.frarb
36 changes: 33 additions & 3 deletions frarb/frarb.py → funding_rate_arbitrage/frarb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
import logging
from datetime import datetime
import ccxt
from numpy import ndarray
from rich import print
from rich.logging import RichHandler
from ccxt import ExchangeError
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

Expand Down Expand Up @@ -49,21 +51,35 @@ def fetch_all_funding_rate(exchange: str) -> dict:
return fr_d

@staticmethod
def fetch_funding_rate_history(exchange: str, symbol: str) -> None:
def fetch_funding_rate_history(exchange: str, symbol: str) -> tuple:
"""
Fetch funding rates on all perpetual contracts listed on the exchange.
Fetch funding rates on perpetual contracts listed on the exchange.
Args:
exchange (str): Name of exchange (binance, bybit, ...)
symbol (str): Symbol (BTC/USDT:USDT, ETH/USDT:USDT, ...).
Returns (dict): Dict of perpetual contract pair and funding rate.
Returns (tuple): settlement time, funding rate.
"""
ex = getattr(ccxt, exchange)()
funding_history_dict = ex.fetch_funding_rate_history(symbol=symbol)
funding_time = [datetime.fromtimestamp(d['timestamp'] * 0.001) for d in funding_history_dict]
funding_rate = [d['fundingRate'] * 100 for d in funding_history_dict]
return funding_time, funding_rate

def figure_funding_rate_history(self, exchange: str, symbol: str) -> None:
"""
Figure funding rates on perpetual contracts listed on the exchange.
Args:
exchange (str): Name of exchange (binance, bybit, ...)
symbol (str): Symbol (BTC/USDT:USDT, ETH/USDT:USDT, ...).
Returns: None
"""
funding_time, funding_rate = self.fetch_funding_rate_history(exchange=exchange, symbol=symbol)
plt.plot(funding_time, funding_rate, label='funding rate')
plt.hlines(
xmin=funding_time[0],
Expand All @@ -82,6 +98,20 @@ def fetch_funding_rate_history(exchange: str, symbol: str) -> None:
plt.tight_layout()
plt.show()

def get_funding_rate_volatility(self, exchange: str, symbol: str) -> ndarray:
"""
Get funding rate standard deviation volatility on all perpetual contracts listed on the exchange.
Args:
exchange (str): Name of exchange (binance, bybit, ...)
symbol (str): Symbol (BTC/USDT:USDT, ETH/USDT:USDT, ...).
Returns: Funding rate standard deviation volatility.
"""
_, funding_rate = self.fetch_funding_rate_history(exchange=exchange, symbol=symbol)
return np.std(funding_rate)

def display_large_divergence_single_exchange(self, exchange: str, minus=False, display_num=10) -> pd.DataFrame:
"""
Display large funding rate divergence on single CEX.
Expand Down
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

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

0 comments on commit 0f1f7cf

Please sign in to comment.