Skip to content

Commit

Permalink
Merge pull request #9 from aoki-h-jp/feature/1.2.0/figure-fr-history
Browse files Browse the repository at this point in the history
Feature/1.2.0/figure fr history
  • Loading branch information
aoki-h-jp authored Mar 17, 2023
2 parents 01fd616 + f7bff82 commit 5c8c0a2
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 1 deletion.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions examples/fetch_funding_rate_history.py
Original file line number Diff line number Diff line change
@@ -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')
36 changes: 36 additions & 0 deletions frarb/frarb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
Binary file added img/readme_funding_rate_history.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ccxt
pandas
rich
rich
matplotlib

0 comments on commit 5c8c0a2

Please sign in to comment.