diff --git a/CHANGELOG b/CHANGELOG index 54f19e5..64f29fd 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,6 @@ +1.1.16 +-) Implemented Margin Info (rest) + 1.1.15 -) Added 'ids' parameter to get_order_history() -) Added an example to show how it is possible to spawn multiple bfx ws instances to comply with the open subscriptions number constraint (max. 25) diff --git a/bfxapi/examples/rest/get_authenticated_data.py b/bfxapi/examples/rest/get_authenticated_data.py index 052b22d..f065bfa 100644 --- a/bfxapi/examples/rest/get_authenticated_data.py +++ b/bfxapi/examples/rest/get_authenticated_data.py @@ -79,6 +79,14 @@ async def log_funding_credits_history(): print ("Funding credit history:") [ print (c) for c in credit ] +async def log_margin_info(): + m1 = await bfx.rest.get_margin_info('tBTCUSD') + print (m1) + m2 = await bfx.rest.get_margin_info('sym_all') + print (m2) + m3 = await bfx.rest.get_margin_info('base') + print (m3) + async def run(): await log_wallets() await log_active_orders() @@ -90,6 +98,7 @@ async def run(): await log_funding_offer_history() await log_funding_credits() await log_funding_credits_history() + await log_margin_info() t = asyncio.ensure_future(run()) diff --git a/bfxapi/models/__init__.py b/bfxapi/models/__init__.py index 76b5ce9..b9b4b17 100644 --- a/bfxapi/models/__init__.py +++ b/bfxapi/models/__init__.py @@ -20,5 +20,7 @@ from .ticker import Ticker from .funding_ticker import FundingTicker from .ledger import Ledger from .funding_trade import FundingTrade +from .margin_info import MarginInfo +from .margin_info_base import MarginInfoBase NAME = "models" diff --git a/bfxapi/models/margin_info.py b/bfxapi/models/margin_info.py new file mode 100644 index 0000000..514be41 --- /dev/null +++ b/bfxapi/models/margin_info.py @@ -0,0 +1,46 @@ +""" +Module used to describe all of the different data types +""" + +import datetime + +class MarginInfoModel: + """ + Enum used to index the different values in a raw margin info array + """ + TRADABLE_BALANCE = 0 + GROSS_BALANCE = 1 + BUY = 2 + SELL = 3 + +class MarginInfo: + """ + SYMBOL string + TRADABLE BALANCE float + GROSS_BALANCE float + BUY + SELL + """ + + def __init__(self, symbol, tradable_balance, gross_balance, buy, sell): + # pylint: disable=invalid-name + self.symbol = symbol + self.tradable_balance = tradable_balance + self.gross_balance = gross_balance + self.buy = buy + self.sell = sell + + @staticmethod + def from_raw_margin_info(raw_margin_info): + """ + Generate a MarginInfo object from a raw margin info array + """ + symbol = raw_margin_info[1] + tradable_balance = raw_margin_info[2][MarginInfoModel.TRADABLE_BALANCE] + gross_balance = raw_margin_info[2][MarginInfoModel.GROSS_BALANCE] + buy = raw_margin_info[2][MarginInfoModel.BUY] + sell = raw_margin_info[2][MarginInfoModel.SELL] + return MarginInfo(symbol, tradable_balance, gross_balance, buy, sell) + + def __str__(self): + return "Margin Info {}".format(self.symbol) diff --git a/bfxapi/models/margin_info_base.py b/bfxapi/models/margin_info_base.py new file mode 100644 index 0000000..200bc52 --- /dev/null +++ b/bfxapi/models/margin_info_base.py @@ -0,0 +1,47 @@ +""" +Module used to describe all of the different data types +""" + +import datetime + +class MarginInfoBaseModel: + """ + Enum used to index the different values in a raw margin info array + """ + USER_PL = 0 + USER_SWAPS = 1 + MARGIN_BALANCE = 2 + MARGIN_NET = 3 + MARGIN_MIN = 4 + +class MarginInfoBase: + """ + USER_PL float + USER_SWAPS float + MARGIN_BALANCE float + MARGIN_NET float + MARGIN_MIN float + """ + + def __init__(self, user_pl, user_swaps, margin_balance, margin_net, margin_min): + # pylint: disable=invalid-name + self.user_pl = user_pl + self.user_swaps = user_swaps + self.margin_balance = margin_balance + self.margin_net = margin_net + self.margin_min = margin_min + + @staticmethod + def from_raw_margin_info(raw_margin_info): + """ + Generate a MarginInfoBase object from a raw margin info array + """ + user_pl = raw_margin_info[1][MarginInfoBaseModel.USER_PL] + user_swaps = raw_margin_info[1][MarginInfoBaseModel.USER_SWAPS] + margin_balance = raw_margin_info[1][MarginInfoBaseModel.MARGIN_BALANCE] + margin_net = raw_margin_info[1][MarginInfoBaseModel.MARGIN_NET] + margin_min = raw_margin_info[1][MarginInfoBaseModel.MARGIN_MIN] + return MarginInfoBase(user_pl, user_swaps, margin_balance, margin_net, margin_min) + + def __str__(self): + return "Margin Info Base" diff --git a/bfxapi/rest/bfx_rest.py b/bfxapi/rest/bfx_rest.py index 9a97057..ba7d9b9 100644 --- a/bfxapi/rest/bfx_rest.py +++ b/bfxapi/rest/bfx_rest.py @@ -10,7 +10,7 @@ import datetime from ..utils.custom_logger import CustomLogger from ..utils.auth import generate_auth_headers, calculate_order_flags, gen_unique_cid -from ..models import Wallet, Order, Position, Trade, FundingLoan, FundingOffer, FundingTrade +from ..models import Wallet, Order, Position, Trade, FundingLoan, FundingOffer, FundingTrade, MarginInfoBase, MarginInfo from ..models import FundingCredit, Notification, Ledger @@ -385,6 +385,22 @@ class BfxRest: raw_wallets = await self.post(endpoint) return [Wallet(*rw[:5]) for rw in raw_wallets] + async def get_margin_info(self, symbol='base'): + """ + Get account margin information (like P/L, Swaps, Margin Balance, Tradable Balance and others). + Use different keys (base, SYMBOL, sym_all) to retrieve different kinds of data. + + @return Array + """ + endpoint = f"auth/r/info/margin/{symbol}" + raw_margin_info = await self.post(endpoint) + if symbol == 'base': + return MarginInfoBase.from_raw_margin_info(raw_margin_info) + elif symbol == 'sym_all': + return [MarginInfo.from_raw_margin_info(record) for record in raw_margin_info] + else: + return MarginInfo.from_raw_margin_info(raw_margin_info) + async def get_active_orders(self, symbol): """ Get all of the active orders associated with API_KEY - Requires authentication. diff --git a/bfxapi/version.py b/bfxapi/version.py index acd9c7c..3bf093c 100644 --- a/bfxapi/version.py +++ b/bfxapi/version.py @@ -2,4 +2,4 @@ This module contains the current version of the bfxapi lib """ -__version__ = '1.1.15' +__version__ = '1.1.16'