Skip to main content

Definedge Securities Integrate API Python Client

Project description

Introduction

This is the official Python API client for the Definege Securities Integrate API platform.

The Definedge Securities API platform Integrate allows you to build trading and investment services platforms, execute strategies, and much more. You can execute & modify orders in real time in equities, derivatives, currencies and commodities. You can also manage user portfolios, access live market data and much more with a lightning fast API.

We offer resource-based URLs that accept JSON or form-encoded requests. The response is returned as JSON-encoded responses using Standard HTTP response codes, verbs, after authentication.

Installation

Install the Definedge Securities Integrate Python API client. For more information, see the Python API client documentation.

  • Using pip:

pip install pyintegrate
  • or Using poetry:

poetry add pyintegrate

Usage

Order placement

Simple example to place an order and get the order book:

from logging import INFO, basicConfig, info
from typing import Any

from integrate import ConnectToIntegrate, IntegrateOrders

basicConfig(level=INFO)

# Initialise the connection and login.
conn = ConnectToIntegrate()
conn.login(
    api_token="YOUR_API_TOKEN",
    api_secret="YOUR_API_SECRET",
)

io = IntegrateOrders(conn)

try:
    order: dict[str, Any] = io.place_order(
        exchange=conn.EXCHANGE_TYPE_NSE,
        order_type=conn.ORDER_TYPE_BUY,
        price=0,
        price_type=conn.PRICE_TYPE_MARKET,
        product_type=conn.PRODUCT_TYPE_INTRADAY,
        quantity=1,
        tradingsymbol="SBIN-EQ",
    )
    info(f"Order placed: {order}")
except Exception as e:
    info(f"Order placement failed: {e}")

# Get order book.
info(io.orders())

WebSocket streaming

Simple example to stream live quotes, get order and depth updates:

from logging import INFO, basicConfig, info

from integrate import ConnectToIntegrate, IntegrateWebSocket

basicConfig(level=INFO)


# Callback called when the WebSocket connection is established and the login is successful.
def on_login(iws: IntegrateWebSocket) -> None:
    # Subscribe to a list of symbols (TCS and TATAMOTORS here).
    tokens: list[tuple[str, str]] = [
        (iws.c2i.EXCHANGE_TYPE_NSE, "11536"),
        (iws.c2i.EXCHANGE_TYPE_NSE, "3456"),
    ]
    # Subscribe to a list of symbols. You can have different lists for different subscriptions.
    iws.subscribe(iws.c2i.SUBSCRIPTION_TYPE_TICK, tokens)
    iws.subscribe(iws.c2i.SUBSCRIPTION_TYPE_ORDER, tokens)
    iws.subscribe(iws.c2i.SUBSCRIPTION_TYPE_DEPTH, tokens)


# Callback to receive ticks.
def on_tick_update(iws: IntegrateWebSocket, tick: dict[str, str]) -> None:
    # Callback to receive ticks.
    info(f"Ticks: {tick}")


# Callback to receive order updates.
def on_order_update(iws: IntegrateWebSocket, order: dict[str, str]) -> None:
    info(f"Order update : {order}")


# Callback to receive bid-ask depth updates.
def on_depth_update(iws: IntegrateWebSocket, depth: dict[str, str]) -> None:
    info(f"Depth update : {depth}")


# Callback to receive acknowledgements of the requests sent.
def on_acknowledgement(iws: IntegrateWebSocket, ack: dict[str, str]) -> None:
    info(f"Ack : {ack}")


# Callback to receive Python exceptions.
def on_exception(iws: IntegrateWebSocket, e: Exception) -> None:
    info(f"Exception : {e}")
    # Below will close the WebSocket connection.
    # iws.close_on_exception("Closing connection due to exception")


# Callback to run on WebSocket close.
def on_close(iws: IntegrateWebSocket, code: int, reason: str) -> None:
    info(f"Closed : {code} {reason}")
    # Below will stop the event loop and the program will exit.
    # iws.stop()


# Initialise the connection and login.
conn = ConnectToIntegrate()
conn.login(
    api_token="YOUR_API_TOKEN",
    api_secret="YOUR_API_SECRET",
)
iws = IntegrateWebSocket(conn)

# Assign the callbacks.
iws.on_login = on_login
iws.on_tick_update = on_tick_update
iws.on_order_update = on_order_update
iws.on_depth_update = on_depth_update
iws.on_acknowledgement = on_acknowledgement
iws.on_exception = on_exception
iws.on_close = on_close

# Blocking WebSocket connection below. Nothing after this will run.
# You have to use the callbacks for further management.
# If you receive an SSL Error after login, then replace the below line with:
# iws.connect(ssl_verify=False)
iws.connect()

Check out more examples in the examples folder.

Contributing

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

pyintegrate-1.2.0.tar.gz (20.5 kB view details)

Uploaded Source

Built Distribution

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

pyintegrate-1.2.0-py3-none-any.whl (23.7 kB view details)

Uploaded Python 3

File details

Details for the file pyintegrate-1.2.0.tar.gz.

File metadata

  • Download URL: pyintegrate-1.2.0.tar.gz
  • Upload date:
  • Size: 20.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyintegrate-1.2.0.tar.gz
Algorithm Hash digest
SHA256 add61bedec2c4ef7f533701ea8f00f3d6ac8dd5a107b8a739a8e2ea8cf9fb2f0
MD5 cf461cd1603647e5846ea6bcce54dbe7
BLAKE2b-256 b552c34fc9cdc080157d3f6ea47ce620f5d76b6ef30423b9edeac691ebd48c6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyintegrate-1.2.0.tar.gz:

Publisher: main.yml on Definedge-Securities/pyintegrate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyintegrate-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: pyintegrate-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 23.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for pyintegrate-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a3269580374efeec1287d1ed7a5958b78002fc48e7b4283f2d3f64cc171e4d47
MD5 701cb82802d354a7bec74710b8665cd7
BLAKE2b-256 36a238b03a1af6cb0a9d2c3f81c56cf2cf60a06431d2ef1f171649977fdc7802

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyintegrate-1.2.0-py3-none-any.whl:

Publisher: main.yml on Definedge-Securities/pyintegrate

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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