Skip to main content

Notbank API client library

Project description

Notbank Python SDK

main page

sign up in Notbank.

Installation

To install Notbank use pip

pip install notbank

Documentation

This sdk makes use of the api of Notbank.

Quick start

Client creation

There are two communication protocols supported by the Notbank client. Communication via websocket, and via rest. Communication via websocket requires connection and permits subscriptions, other than that they are equivalent.

from notbank_python_sdk.requests_models import *
from notbank_python_sdk.client_connection_factory import new_websocket_client_connection, new_rest_client_connection
from notbank_python_sdk.error import NotbankException
from notbank_python_sdk.notbank_client import NotbankClient

# a websocket client
try:
    websocket_connection = new_websocket_client_connection()
    websocket_connection.connect()
    client = NotbankClient(websocket_connection)

    # an http client
    rest_connection = new_rest_client_connection()
    client = NotbankClient(rest_connection)
except NotbankException as e:
    print(e)

Error handling

All internal notbank client and notbank server errors inherit from NotbankException, and all client methods may throw it (e.g. invalid request, request timeout, ...)

# client : NotbankClient : ....
try:
    orderbook = client.get_order_book(OrderBookRequest("BTCUSDT", 1, 1))
except NotbankException as e:
    print(e)

Put order at the top of book example

import random
from decimal import Decimal

from notbank_python_sdk.notbank_client import NotbankClient
from notbank_python_sdk.client_connection_factory import new_websocket_client_connection

account_id: int = 13  # must be user account id

# connect to notbank
websocket_connection = new_websocket_client_connection()
websocket_connection.connect()
client = NotbankClient(websocket_connection)

# authentication (same for rest client or websocket client)
authenticate = client.authenticate(
    AuthenticateRequest(
        api_public_key="api-public-key",
        api_secret_key="api-secret-key",
        user_id="user-id",
    )
)
if not authenticate.authenticated:
    raise Exception("client not authenticated")

# get USDT user balance (also known as position)
positions = client.get_account_positions(
    GetAccountPositionsRequest(account_id))
usdt_balance = None
product = "USDT"
market_pair = "BTCUSDT"
for position in positions:
    if position.product_symbol == product:
        usdt_balance = position
if usdt_balance is None:
    raise Exception("user has no balance")

# define order_amount (between all usdt_balance and half usdt_balance)
total_balance = usdt_balance.amount
quantity_to_spend = total_balance - \
    Decimal(random.random()) * (total_balance/2)

# define order_price (around market top)
orderbook = client.get_order_book(
    OrderBookRequest(market_pair, level=2, depth=5))
top_orderbook = orderbook.bids[0]
delta = Decimal(random.randrange(10, 100))/1000
order_price = top_orderbook.price + delta
order_quantity = quantity_to_spend / order_price

# send order
instrument = client.get_instrument_by_symbol(market_pair)
request = SendOrderRequest(
    instrument=instrument,
    account_id=account_id,
    time_in_force=TimeInForce.GTC,
    side=Side.Buy,
    quantity=order_quantity,
    limit_price=order_price,
    order_type=OrderType.Limit,
)
response = client.send_order(request)

# handle order result
if response.status == SendOrderStatus.REJECTED:
    # order was rejected
    raise Exception("rejected order")
else:
    # order was accepted
    order_id = response.order_id
    print(order_id)

# disconnect
client.close()

Websocket reconnection

The websocket client reconnects automatically in case of unexpected disconnection.

On reconnection the client resubscribes to what it was already subscribed before the reconnection. This means that the on_snapshot handler of subscriptions will be called once per reconnection, and on_update handlers will be continuoslly called before and after reconnections.

authentication is also kept on reconnection, by re-authenticating the new connection with the last authentication request instance

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

notbank-1.2.0a1.tar.gz (57.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

notbank-1.2.0a1-py3-none-any.whl (97.5 kB view details)

Uploaded Python 3

File details

Details for the file notbank-1.2.0a1.tar.gz.

File metadata

  • Download URL: notbank-1.2.0a1.tar.gz
  • Upload date:
  • Size: 57.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.16

File hashes

Hashes for notbank-1.2.0a1.tar.gz
Algorithm Hash digest
SHA256 e0ea0b5be52d7926ccdcdc453eea122041dc702f0ccc54eed188488c9952df23
MD5 02a357972bc68eda6551090fbefb8576
BLAKE2b-256 d068812f78c75ff1010a21b9a4e42b17474811dcbe883e6791dd4b63d1f77a10

See more details on using hashes here.

File details

Details for the file notbank-1.2.0a1-py3-none-any.whl.

File metadata

  • Download URL: notbank-1.2.0a1-py3-none-any.whl
  • Upload date:
  • Size: 97.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.16

File hashes

Hashes for notbank-1.2.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9e9d47e0c13b7cdb69c35f4e5adcb65136c672a1297e15493fd8bbc3d7651a5
MD5 e6b7ecc42ef88f60823f20d29b7fd4dd
BLAKE2b-256 7ef05f181da309785c594dce7ff0e51992b8f3cb05e42beabf855b8d755eef82

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page