Added Margin Info endpoint

This commit is contained in:
itsdeka
2021-06-10 18:17:13 +02:00
parent 4a8d3e48b0
commit 98825aae10
7 changed files with 125 additions and 2 deletions

View File

@@ -1,3 +1,6 @@
1.1.16
-) Implemented Margin Info (rest)
1.1.15 1.1.15
-) Added 'ids' parameter to get_order_history() -) 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) -) 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)

View File

@@ -79,6 +79,14 @@ async def log_funding_credits_history():
print ("Funding credit history:") print ("Funding credit history:")
[ print (c) for c in credit ] [ 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(): async def run():
await log_wallets() await log_wallets()
await log_active_orders() await log_active_orders()
@@ -90,6 +98,7 @@ async def run():
await log_funding_offer_history() await log_funding_offer_history()
await log_funding_credits() await log_funding_credits()
await log_funding_credits_history() await log_funding_credits_history()
await log_margin_info()
t = asyncio.ensure_future(run()) t = asyncio.ensure_future(run())

View File

@@ -20,5 +20,7 @@ from .ticker import Ticker
from .funding_ticker import FundingTicker from .funding_ticker import FundingTicker
from .ledger import Ledger from .ledger import Ledger
from .funding_trade import FundingTrade from .funding_trade import FundingTrade
from .margin_info import MarginInfo
from .margin_info_base import MarginInfoBase
NAME = "models" NAME = "models"

View File

@@ -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)

View File

@@ -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"

View File

@@ -10,7 +10,7 @@ import datetime
from ..utils.custom_logger import CustomLogger from ..utils.custom_logger import CustomLogger
from ..utils.auth import generate_auth_headers, calculate_order_flags, gen_unique_cid 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 from ..models import FundingCredit, Notification, Ledger
@@ -385,6 +385,22 @@ class BfxRest:
raw_wallets = await self.post(endpoint) raw_wallets = await self.post(endpoint)
return [Wallet(*rw[:5]) for rw in raw_wallets] 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): async def get_active_orders(self, symbol):
""" """
Get all of the active orders associated with API_KEY - Requires authentication. Get all of the active orders associated with API_KEY - Requires authentication.

View File

@@ -2,4 +2,4 @@
This module contains the current version of the bfxapi lib This module contains the current version of the bfxapi lib
""" """
__version__ = '1.1.15' __version__ = '1.1.16'