mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 06:44:22 +01:00
Merge pull request #153 from itsdeka/margin_info
Added Margin Info endpoint & claim position
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
1.1.16
|
||||
-) Implemented Margin Info (rest)
|
||||
-) Implemented claim position (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)
|
||||
|
||||
@@ -79,6 +79,15 @@ async def log_funding_credits_history():
|
||||
print ("Funding credit history:")
|
||||
[ print (c) for c in credit ]
|
||||
|
||||
async def log_margin_info():
|
||||
margin_info = await bfx.rest.get_margin_info('tBTCUSD')
|
||||
print(margin_info)
|
||||
sym_all = await bfx.rest.get_margin_info('sym_all') # list of Margin Info
|
||||
for margin_info in sym_all:
|
||||
print(margin_info)
|
||||
base = await bfx.rest.get_margin_info('base')
|
||||
print(base)
|
||||
|
||||
async def run():
|
||||
await log_wallets()
|
||||
await log_active_orders()
|
||||
@@ -90,6 +99,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())
|
||||
|
||||
@@ -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"
|
||||
|
||||
47
bfxapi/models/margin_info.py
Normal file
47
bfxapi/models/margin_info.py
Normal file
@@ -0,0 +1,47 @@
|
||||
"""
|
||||
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 {} buy={} sell={} tradable_balance={} gross_balance={}" \
|
||||
"".format(self.symbol, self.buy, self.sell, self. tradable_balance, self. gross_balance)
|
||||
48
bfxapi/models/margin_info_base.py
Normal file
48
bfxapi/models/margin_info_base.py
Normal file
@@ -0,0 +1,48 @@
|
||||
"""
|
||||
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 user_pl={} user_swaps={} margin_balance={} margin_net={} margin_min={}" \
|
||||
"".format(self.user_pl, self.user_swaps, self.margin_balance, self.margin_net, self.margin_min)
|
||||
@@ -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.
|
||||
@@ -924,6 +940,28 @@ class BfxRest:
|
||||
raw_notification = await self.post(endpoint, payload)
|
||||
return Notification.from_raw_notification(raw_notification)
|
||||
|
||||
async def claim_position(self, position_id, amount):
|
||||
"""
|
||||
The claim feature allows the use of funds you have in your Margin Wallet
|
||||
to settle a leveraged position as an exchange buy or sale
|
||||
|
||||
# Attributes
|
||||
@param position_id: id of the position
|
||||
@param amount: amount to claim
|
||||
@return Array [ MTS, TYPE, MESSAGE_ID, null, [SYMBOL, POSITION_STATUS,
|
||||
AMOUNT, BASE_PRICE, MARGIN_FUNDING, MARGIN_FUNDING_TYPE, PLACEHOLDER,
|
||||
PLACEHOLDER, PLACEHOLDER, PLACEHOLDER, PLACEHOLDER, POSITION_ID, MTS_CREATE,
|
||||
MTS_UPDATE, PLACEHOLDER, POS_TYPE, PLACEHOLDER, COLLATERAL, MIN_COLLATERAL,
|
||||
META], CODE, STATUS, TEXT]
|
||||
"""
|
||||
payload = {
|
||||
"id": position_id,
|
||||
"amount": f"{amount * -1}"
|
||||
}
|
||||
endpoint = "auth/w/position/claim"
|
||||
message = await self.post(endpoint, payload)
|
||||
return message
|
||||
|
||||
async def get_auth_pulse_hist(self, is_public=None):
|
||||
"""
|
||||
Allows you to retrieve your private pulse history or the public pulse history with an additional UID_LIKED field.
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
This module contains the current version of the bfxapi lib
|
||||
"""
|
||||
|
||||
__version__ = '1.1.15'
|
||||
__version__ = '1.1.16'
|
||||
|
||||
Reference in New Issue
Block a user