Skip to content

Commit

Permalink
Merge pull request #3 from aoki-h-jp/feature/1.0.0/commission
Browse files Browse the repository at this point in the history
Feature/1.0.0/commission
  • Loading branch information
aoki-h-jp authored Mar 15, 2023
2 parents 7992b26 + 6cda8b9 commit e78acf6
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 17 deletions.
3 changes: 1 addition & 2 deletions examples/fetch_funding_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
An example of fetching funding rate
"""
from frarb import FundingRateArbitrage
from frarb.utils import Exchange


if __name__ == '__main__':
# fetch from binance
fr = FundingRateArbitrage()
fr.fetch_all_funding_rate(Exchange.BINANCE)
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))
137 changes: 132 additions & 5 deletions frarb/frarb.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,150 @@
import utils
import ccxt
import pandas as pd


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

def fetch_all_funding_rate(self, exchange: utils.Exchange) -> dict:
@staticmethod
def fetch_all_funding_rate(exchange: str) -> dict:
"""
Fetch funding rates on all perpetual contracts listed on the exchange.
Args:
exchange (utils.Exchange): Exchange (binance, bybit, ...)
exchange (str): Name of exchange (binance, bybit, ...)
Returns (dict): Dict of perpetual contract pair and funding rate
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
10 changes: 0 additions & 10 deletions frarb/utils.py

This file was deleted.

0 comments on commit e78acf6

Please sign in to comment.