diff --git a/README.md b/README.md index 5645ad8..e7c3a86 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,18 @@ fr_binance = fr.fetch_all_funding_rate(exchange='binance') cm_binance = fr.get_commission(exchange='binance', trade='futures', taker=False) ``` +### Fetch FR history +```python +from frarb import FundingRateArbitrage + +fr = FundingRateArbitrage() + +# figure funding rate history +fr.fetch_funding_rate_history(exchange='binance', symbol='BTC/USDT:USDT') +``` +!['funding rate history example'](./img/readme_funding_rate_history.png) + + ### Display large FR divergence on single CEX ```python # display large funding rate divergence on bybit diff --git a/examples/fetch_funding_rate_history.py b/examples/fetch_funding_rate_history.py new file mode 100644 index 0000000..01dc7df --- /dev/null +++ b/examples/fetch_funding_rate_history.py @@ -0,0 +1,11 @@ +""" +An example of fetching funding rate history +""" +from 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') diff --git a/frarb/frarb.py b/frarb/frarb.py index 5c7fe11..4acc7af 100644 --- a/frarb/frarb.py +++ b/frarb/frarb.py @@ -2,11 +2,13 @@ Main class of funding-rate-arbitrage """ import logging +from datetime import datetime import ccxt from rich import print from rich.logging import RichHandler from ccxt import ExchangeError import pandas as pd +import matplotlib.pyplot as plt logging.basicConfig( level=logging.INFO, @@ -46,6 +48,40 @@ def fetch_all_funding_rate(exchange: str) -> dict: log.exception(f'{p} is not perp.') return fr_d + @staticmethod + def fetch_funding_rate_history(exchange: str, symbol: str) -> None: + """ + Fetch funding rates 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 (dict): Dict of perpetual contract pair and 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] + plt.plot(funding_time, funding_rate, label='funding rate') + plt.hlines( + xmin=funding_time[0], + xmax=funding_time[-1], + y=sum(funding_rate) / len(funding_rate), + label='average', + colors='r', + linestyles='-.' + ) + plt.title(f'Funding rate history {symbol}') + plt.xlabel('timestamp') + plt.ylabel('Funding rate [%]') + plt.xticks(rotation=45) + plt.yticks(rotation=45) + plt.legend() + plt.tight_layout() + plt.show() + def display_large_divergence_single_exchange(self, exchange: str, minus=False, display_num=10) -> pd.DataFrame: """ Display large funding rate divergence on single CEX. diff --git a/img/readme_funding_rate_history.png b/img/readme_funding_rate_history.png new file mode 100644 index 0000000..969a242 Binary files /dev/null and b/img/readme_funding_rate_history.png differ diff --git a/requirements.txt b/requirements.txt index 365a144..f37adf6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ ccxt pandas -rich \ No newline at end of file +rich +matplotlib \ No newline at end of file