Skip to main content

Python library to connect and stream the market data.

Project description

AccelPix Data API

Introduction

Python library to connect and stream the market data. This is websocket and fallback transport based library with all the functionalyties to get eod and live streaming data

Simple and easy integration with your web application/portal, all heavy weight work are back lifted.

Streaming:

  1. Initialize
  2. Register required callbacks in api.callbacks
  3. Do subscribe with symbols list in api.stream

History:

  1. Initialize
  2. Async call to respective methods exposed in api.history

Installation

pip install pix-apidata 

Import

import asyncio
from pix_apidata import *

Initialize

api= apidata_lib.ApiData()
event_loop = asyncio.get_event_loop()

apiKey = "api-access-key" //provided by your data vendor
apiHost = "apidata.accelpix.in" //provided by your data vendor
await api.initialize(apiKey, apiHost) 

# *** IMPORTANT ***

# *** initialize(...) - wait for initialize to complete before making any other API calls.

Callbacks for live streaming

Available callbacks

api.on_trade_update(on_trade)

api.on_best_update(on_best)

api.on_refs_update(on_refs)

api.on_srefs_update(on_srefs)

Trade

#callback to listen for trade data
api.on_trade_update(on_trade)
def on_trade(msg):
  print(msg) # Trade msg
  t = apidata_modules.Trade(msg)
  print(t.ticker , t.oi) #likewise object can be called for id, kind, ticker, segment, price, qty, volume, oi

#response data
  {
    id: 0, 
    ticker: 'NIFTY-1', 
    segmentId: 2, 
    time: 1293704493, 
    price: 13958.6, 
    qty: 75, 
    volume: 1587975, 
    oi: 9700275,
    kind: 'T'
  } 

Best

#callback to listen for bid, ask and respective qty
api.on_best_update(on_best)
def on_best(msg):
  print(msg) # Best msg
  b=apidata_model.Best(msg)
  print(b.ticker , b.bidPrice) #likewise object can be called for segmentId, kind, bidQty, askPrice, askQty

#response data
  {
    ticker: 'NIFTY-1', 
    segmentId: 2, 
    kind: 'B', 
    bidPrice: 13957.45, 
    bidQty: 75, 
    askPrice: 13958.8, 
    askQty: 75, 
    time: 1293704493
  }  

Recent change in Refs data

#callback to listen for change in o, h, l, c, oi and avg data
api.on_trade_ref(on_ref)
def on_ref(msg):
  print(msg) # Refs msg
  ref = apidata_models.Refs(msg)
  print(ref.price) #likewise object can be called for segmentId, kind, ticker

#response data
  {
    kind: 'A',
    ticker: 'NIFTY-1',
    segmentId: 2,
    price: 11781.08984375
  }

Refs snapshot data

#callback to listen for o, h, l, c, oi and avg snapshot
api.on_srefs_update(on_srefs)
def on_srefs(msg):
  print(msg) # Srefs msg
  sref = apidata_models.RefsSnapshot(msg)
  print(sref.high) #likewise object can be called for kind, ticker, segmentId, open, close, high, low, avg, oi

#response data
  {
    kind: 'V', 
    ticker: 'NIFTY-1',
    segmentId: 2, 
    open: 11749.650390625, 
    close: 11681.5498046875,
    avg: 11780.8603515625,
    high: 11822,
    low: 11731.2001953125,
    oi: 10615950
  }

Callbacks for connection status

api.on_connection_started(connection_started)

api.on_connection_stopped(connection_stopped)

# Fired when connection is successful
def connection_started():
  print("Connection started callback")

# Fired when the connection is closed after automatic retry or some issues in networking
# Need to re-establish the connection manually

def connection_stopped():
  print("connection Stopped callback")

Live stream subscription

Subscibe to receive ALL updates of the symbols subscibed

 await api.subscribeAll('NIFTY-1')

Subscibe to receive TRADE updates of the symbols subscibed

 await api.subscribeTrade(['NIFTY-1','BANKNIFTY-1','NIFTY 50'])

Subscibe to receive REFS and BEST updates of the symbols subscibed

await api.subscribeBestAndRefs(['NIFTY-1','BANKNIFTY-1'])

Unsubscribe live stream

# unsubscribe single symbol
 await api.unsubscribeAll(['NIFTY-1'])
# unsubscribe multiple symbol
 await api.unsubscribeAll(['NIFTY-1','BANKNIFTY-1'])

History data - Eod

#*** Continues data
#params: ticker, startDate, endDate
await api.get_eod("NIFTY-1", "20200828", "20200901")

#*** Contract data
#params: underlying ticker, startDate, endDate, contractExpiryDate
await api.get_eod_contract("NIFTY", "20200828", "20200901", "20201029")

#response data
{
  td: '2020-08-28T00:00:00',
  op: 11630,
  hp: 11708,
  lp: 11617.05,
  cp: 11689.05,
  vol: 260625,
  oi: 488325
}

History data - Inraday

Provides intra-eod bars with the time resolution in minutes (default:'5' mins)

You can set minute resolution to '1', '5', '10' and so on.

Custom minute resolution also supported like '3', '7' and so on.

#*** Continues data
#params: ticker, startDate, endDate, resolution
await api.get_intra_eod("NIFTY-1", "20200828", "20200901", "5")

#*** Contract data
#params: underlying ticker, startDate, endDate, contractExpiryDate
await api.get_intra_eod_contract("NIFTY", "20200828", "20200901", "20201029", "5")

#response data
{
  td: '2020-08-28T09:15:00',
  op: 11630,
  hp: 11643.45,
  lp: 11630,
  cp: 11639.8,
  vol: 4575,
  oi: 440475
}

History data - Ticks

Provides back log ticks from the date time specified till current live time, that is the ticks available till request hit the server.

#params: ticker, fromDateTime
await api.get_back_ticks("BANKNIFTY-1", "20201016 15:00:00")

#response data
{
  td: 2020-11-16T15:00:01.000Z,
  pr: 23600,
  vol: 125,
  oi: 1692375
}

Example

import asyncio
import time
from pix_apidata import *

api = apidata_lib.ApiData()
event_loop = asyncio.get_event_loop()

async def main():
    api.on_connection_started(connection_started)
    api.on_connection_stopped(connection_stopped)
    api.on_trade_update(on_trade)
    api.on_best_update(on_best)
    api.on_refs_update(on_refs)
    api.on_srefs_update(on_srefs)
    key = "your-api-key"
    host = "apidata.accelpix.in"
    s = await api.initialize(key, host)
    print(s)

    his = await api.get_eod_contract("NIFTY","20200828", "20200901", "20201029")
    print("History : ",his)

    syms = ['NIFTY-1', 'BANKNIFTY-1']
    await api.subscribeAll(syms)
    print("Subscribe Done")
    
    time.sleep(1)
    await api.unsubscribeAll(['NIFTY-1'])
    print("Unsubscribe Done")

def on_trade(msg):
    trd = apidata_models.Trade(msg)
    print("Trade : ",trd)

def on_best(msg):
    bst = apidata_models.Best(msg)
    print("Best : ",bst)

def on_refs(msg):
    ref = apidata_models.Refs(msg)
    print("Refs : ",ref)

def on_srefs(msg):
    sref = apidata_models.RefsSnapshot(msg)
    print("Refs Snapshot : ",sref) 

def connection_started():
    print("Connection started callback")

def connection_stopped():
    print("Connection stopped callback")
  
event_loop.create_task(main())
try:
   event_loop.run_forever()
finally:
   event_loop.close()

Powered by ticAnalytics®

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

pix_apidata-1.2.6.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

pix_apidata-1.2.6-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file pix_apidata-1.2.6.tar.gz.

File metadata

  • Download URL: pix_apidata-1.2.6.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.0

File hashes

Hashes for pix_apidata-1.2.6.tar.gz
Algorithm Hash digest
SHA256 aaebbf7413a62c9a857b13d68597ebd85a153ee313cbca87f3f64d594546f71f
MD5 84ac1084adb69f96901d061400656ba3
BLAKE2b-256 04d5bb0aad755430aa0cdc13ab5c8d7f628c5ab1f298b8aa22fec2fba878d36e

See more details on using hashes here.

File details

Details for the file pix_apidata-1.2.6-py3-none-any.whl.

File metadata

  • Download URL: pix_apidata-1.2.6-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.3.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.0 CPython/3.9.0

File hashes

Hashes for pix_apidata-1.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 597870fc918851e85e6d174e03f56e2859af6bfd8aab95da815043d01ec97a40
MD5 a6af6a95dea08f33bf153cc8a1cc1851
BLAKE2b-256 0ab0f9001dc8ea2d1fa871288d8e07955e63cb5f1509b3a3eca87c72a1069370

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