From fe21ef24b046f04d82842115468f6341aafff74c Mon Sep 17 00:00:00 2001 From: Jacob Plaster Date: Fri, 19 Jul 2019 10:31:27 +0700 Subject: [PATCH] rest: add derivative status and set collateral endpoints --- .../examples/rest/get_authenticated_data.py | 3 +- bfxapi/examples/rest/get_public_data.py | 6 +++ bfxapi/rest/bfx_rest.py | 52 +++++++++++++++++-- 3 files changed, 54 insertions(+), 7 deletions(-) diff --git a/bfxapi/examples/rest/get_authenticated_data.py b/bfxapi/examples/rest/get_authenticated_data.py index daafdba..82472c2 100644 --- a/bfxapi/examples/rest/get_authenticated_data.py +++ b/bfxapi/examples/rest/get_authenticated_data.py @@ -12,8 +12,7 @@ API_SECRET=os.getenv("BFX_SECRET") bfx = Client( API_KEY=API_KEY, API_SECRET=API_SECRET, - logLevel='DEBUG', - rest_host='https://test.bitfinex.com/v2' + logLevel='DEBUG' ) now = int(round(time.time() * 1000)) diff --git a/bfxapi/examples/rest/get_public_data.py b/bfxapi/examples/rest/get_public_data.py index 0509a9a..1f289b9 100644 --- a/bfxapi/examples/rest/get_public_data.py +++ b/bfxapi/examples/rest/get_public_data.py @@ -38,12 +38,18 @@ async def log_mul_tickers(): print ("Tickers:") print (tickers) +async def log_derivative_status(): + status = await bfx.rest.get_derivative_status('tBTCF0:USTF0') + print ("Deriv status:") + print (status) + async def run(): await log_historical_candles() await log_historical_trades() await log_books() await log_ticker() await log_mul_tickers() + await log_derivative_status() t = asyncio.ensure_future(run()) asyncio.get_event_loop().run_until_complete(t) diff --git a/bfxapi/rest/bfx_rest.py b/bfxapi/rest/bfx_rest.py index cd71b54..c60859b 100644 --- a/bfxapi/rest/bfx_rest.py +++ b/bfxapi/rest/bfx_rest.py @@ -33,7 +33,7 @@ class BfxRest: async def fetch(self, endpoint, params=""): """ - Fetch a GET request from the bitfinex host + Send a GET request to the bitfinex api @return reponse """ @@ -49,7 +49,7 @@ class BfxRest: async def post(self, endpoint, data={}, params=""): """ - Request a POST to the bitfinex host + Send a pre-signed POST request to the bitfinex api @return response """ @@ -151,7 +151,7 @@ class BfxRest: Get tickers for the given symbol. Tickers shows you the current best bid and ask, as well as the last trade price. - @parms symbols symbol string: pair symbol i.e tBTCUSD + @param symbols symbol string: pair symbol i.e tBTCUSD @return Array [ SYMBOL, BID, BID_SIZE, ASK, ASK_SIZE, DAILY_CHANGE, DAILY_CHANGE_PERC, LAST_PRICE, VOLUME, HIGH, LOW ] """ @@ -164,7 +164,7 @@ class BfxRest: Get tickers for the given symbols. Tickers shows you the current best bid and ask, as well as the last trade price. - @parms symbols Array: array of symbols i.e [tBTCUSD, tETHUSD] + @param symbols Array: array of symbols i.e [tBTCUSD, tETHUSD] @return Array [ SYMBOL, BID, BID_SIZE, ASK, ASK_SIZE, DAILY_CHANGE, DAILY_CHANGE_PERC, LAST_PRICE, VOLUME, HIGH, LOW ] """ @@ -172,6 +172,31 @@ class BfxRest: ticker = await self.fetch(endpoint) return ticker + async def get_derivative_status(self, symbol): + """ + Gets platform information for derivative symbol. + + @param derivativeSymbol string: i.e tBTCF0:USTF0 + @return [KEY/SYMBOL, MTS, PLACEHOLDER, DERIV_PRICE, SPOT_PRICE, PLACEHOLDER, INSURANCE_FUND_BALANCE4, + PLACEHOLDER, PLACEHOLDER, FUNDING_ACCRUED, FUNDING_STEP, PLACEHOLDER] + """ + statuses = await self.get_derivative_statuses([symbol]) + if len(statuses) > 0: + return statuses[0] + return [] + + async def get_derivative_statuses(self, symbols): + """ + Gets platform information for a collection of derivative symbols. + + @param derivativeSymbols Array: array of symbols i.e [tBTCF0:USTF0 ...] or ["ALL"] + @return [KEY/SYMBOL, MTS, PLACEHOLDER, DERIV_PRICE, SPOT_PRICE, PLACEHOLDER, INSURANCE_FUND_BALANCE4, + PLACEHOLDER, PLACEHOLDER, FUNDING_ACCRUED, FUNDING_STEP, PLACEHOLDER] + """ + endpoint = "status/deriv?keys={}".format(','.join(symbols)) + status = await self.fetch(endpoint) + return status + ################################################## # Authenticated Data # ################################################## @@ -327,7 +352,7 @@ class BfxRest: return [FundingCredit.from_raw_credit(c) for c in credits] ################################################## - # Orders # + # Orders # ################################################## async def __submit_order(self, symbol, amount, price, oType=Order.Type.LIMIT, @@ -368,3 +393,20 @@ class BfxRest: payload['sell_price_oco'] = stop_sell_price endpoint = 'order/new' return await self.post(endpoint, data=payload) + + ################################################## + # Derivatives # + ################################################## + + async def set_derivative_collateral(self, symbol, collateral): + """ + Update the amount of callateral used to back a derivative position. + + @param symbol of the derivative i.e 'tBTCF0:USTF0' + @param collateral: amount of collateral/value to apply to the open position + """ + endpoint = 'auth/w/deriv/collateral/set' + payload = {} + payload['symbol'] = symbol + payload['collateral'] = collateral + return await self.post(endpoint, data=payload)