An unofficial Python wrapper for the REST and WebSocket APIs of FTX US Derivatives, formerly known as LedgerX.
Project description
ftxusderivatives-python
An unofficial Python wrapper for the REST and WebSocket APIs of FTX US Derivatives, formerly known as LedgerX. I have no affiliation with FTX US Derivatives. Use this at your own risk.
Features
- Implementation of all REST endpoints, detailed here
- WebSocket implementation: live orderbook tops, account balances, open positions info, server heartbeat, reconnect logic
- Simple handling of authentication
- HTTP request error handling and retry logic
- Logging support
Quick Start
Register an account with FTX US Derivatives.
Generate an API key and configure permissions.
Clone the repository to your target directory.
To run unit tests, enter your API key in the test file: tests/test_rest_api.py
, and run python -m pytest
in the project directory.
Here's some example code to get started with.
###############################
# REST API Example
###############################
from src.rest_lx.rest import LxClient
api_key = "" # TODO: Put API key here
# Init REST client
client = LxClient(api_key=api_key)
# list active day-ahead-swap contracts
swaps = client.list_contracts({
'active': True,
'derivative_type': 'day_ahead_swap',
})
# grab BTC day-ahead-swap contract ID
data = swaps['data']
cbtc_swap = filter(lambda data: data['underlying_asset'] == 'CBTC', data)
contract_id = next(cbtc_swap)['id']
print(f"BTC swap contract_id: {contract_id}")
# retrieve your position for BTC day-ahead-swap contract
position = client.retrieve_contract_position(contract_id)
print(f"BTC swap position: {position}")
# get BTC day-ahead-swap contract ticker
ticker = client.get_contract_ticker(contract_id)
print(f"BTC swap ticker: {ticker}")
###############################
# WebSocket Example
###############################
from src.websocket_lx.client import LxWebsocketClient
import time
# Init WebSocket client
ws = LxWebsocketClient(api_key=api_key)
# Subscribe to orderbook-top feed for BTC day-ahead-swap contract
ws.subscribe(contract_id=contract_id)
ws.connect()
# Grab orderbook-top for BTC day-ahead-swap once a second
while True:
top = ws.get_book_top(contract_id=contract_id)
print(top)
time.sleep(1)
Under Development
- Full orderbook state support using WebSockets
Contributing
Contributions, fixes, recommendations, and all other feedback is welcome. If you are fixing a bug, please open an issue first with all relevant details, and mention the issue number in the pull request.
Contact
I can be reached on discord at Nenye#5335. Otherwise, feel free to open a PR or Issue here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for ftxusderivatives-python-0.1.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e68b2526f3f2fa5a62b9db6279d15d1396de04d390f044325fe5e9e00f15a77d |
|
MD5 | a8f781707cd30b952365ea9d6ecab1cf |
|
BLAKE2b-256 | a867fb4844c9cfe9ad9df4a0877e0e4a1774f385589e84ba0162366c5646f236 |
Hashes for ftxusderivatives_python-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c5d28630175cae282c729925a9375c188ac1572128b9cacc51765c4817d41d6 |
|
MD5 | 4ac02c07e2831910e0cf5dec6b57731c |
|
BLAKE2b-256 | 84c0977f6952e72b9a6a9f587a854228fbff45dd5d19930ac57889ff8d51bfe9 |