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

Feature/1.2.0/figure fr history #9

Merged
merged 4 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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