Skip to main content

Sharekhan Data Feed Trading Api Server

Project description

SHARECONNECT-PYTHON

Sharekhan-Python is a Python library that provides a set of tools and functionalities for interacting with the Sharekhan trading platform. 
With Sharekhan-Python, users can access a variety of trading-related information such as stock prices, market trends, historical data,
stream live market data (WebSockets), and more.It also provides the ability to execute trades and orders in real time.

Installation

Use the package manager pip to install shareconnectpython

    pip install shareconnect
    pip install websocket

Usage

    # package import statement
    from SharekhanApi.sharekhanConnect import SharekhanConnect

    # Make a object call

    api_key = "Your API-KEY"
    login = SharekhanConnect(api_key)
    vendor_key = "Vendor key" 
        """Pass the vendor key for vendor login otherwise keep it blank"""
    version_id = "Version Id"
        """Only null/1005/1006 version id is allowed to be passed"""
    state=12345
    url = login.login_url(vendor_key=vendor_key, version_id=version_id)
    """This will provide you the redirect login url which can be used to login in the sharekhan account"""
    print(url)


    """After Successfully Login You will receive the request token value then you have to decrypt the token value by using secret key and then swap the request token which is a combination of RequestId and CustomerId.Then after that decrypt the request token value."""

    request_token = "Valid Request Token Value"
    secret_key = "Your Secret Key value"

   """Use generate session method when you are passing version id """
    session=login.generate_session(request_token,secret_key)
    # Generating access token for version id and pass parameters as it is passed below 
    access_token=login.get_access_token(api_key,session,state,versionId=version_id)

    """Use generate session without version id method when you are not passing version id """
    sessionwithoutvesionId=login.generate_session_without_versionId(request_token,secret_key)
    # Generating access token for without version id 
    access_token=login.get_access_token(api_key,sessionwithoutvesionId,state)

    print(access_token)

    # Make a object for SharekhanConnect class
    """Here we are passing the api-key, access-token and vendor-key(when needed) as a request header parameters"""
    access_token = "Your access token value"
    sharekhan = SharekhanConnect(api_key,access_token)
    print(sharekhan.requestHeaders())       # for printing request headers

    # Place order history

     orderparams={
     "customerId": "XXXXXXX",
     "scripCode": 2475,
     "tradingSymbol": "ONGC",
     "exchange": "NC",
     "transactionType": "B",    --> (B, S, BM, SM, SAM)
     "quantity": 1,
     "disclosedQty": 0,
     "price": "149.5",
     "triggerPrice": "0",
     "rmsCode": "ANY",
     "afterHour": "N",
     "orderType": "NORMAL",
     "channelUser": "XXXXXXX",      (Use LoginId as ChannelUser)
     "validity": "GFD",
     "requestType": "NEW",
     "productType": "INVESTMENT"    --> (INVESTMENT or (INV), BIGTRADE or (BT), BIGTRADEPLUS or (BT+))
     #Below parameters need to be added for FNO trading
    "instrumentType": "FUTCUR";    --> (Future Stocks(FS)/ Future Index(FI)/ Option Index(OI)/ Option Stocks(OS)/ Future Currency(FUTCUR)/ Option Currency(OPTCUR))
    "strikePrice": "-1";
    "optionType": "XX";    --> (XX/PE/CE)
    "expiry": "31/03/2023";
    }

     order=sharekhan.placeOrder(orderparams)
     print("PlaceOrder: {}".format(order))

    # modify order

    orderparams={
     "orderId":"XXXXXXXXXXX",
       "customerId": "XXXXXXX",
     "scripCode": 2475,
     "tradingSymbol": "ONGC",
     "exchange": "NC",
     "transactionType": "B",    --> (B, S, BM, SM, SAM)
     "quantity": 1,
     "disclosedQty": 0,
     "price": "149.5",
     "triggerPrice": "0",
     "rmsCode": "ANY",
     "afterHour": "N",
     "orderType": "NORMAL",
     "channelUser": "XXXXXXX",      (Use LoginId as ChannelUser)
     "validity": "GFD",
     "requestType": "MODIFY",
     "productType": "INVESTMENT"    --> (INVESTMENT or (INV), BIGTRADE or (BT), BIGTRADEPLUS or (BT+))
     #Below parameters need to be added for FNO trading
    "instrumentType": "FUTCUR";    --> (Future Stocks(FS)/ Future Index(FI)/ Option Index(OI)/ Option Stocks(OS)/ Future Currency(FUTCUR)/ Option Currency(OPTCUR))
    "strikePrice": "-1";
    "optionType": "XX";    --> (XX/PE/CE)
    "expiry": "31/03/2023";
    }
    order=sharekhan.modifyOrder(orderparams)
    print("ModifyOrder: {}".format(order))

    # cancel  order

    orderparams={
     "orderId":"XXXXXXX",
     "customerId": "XXXXXXX",
     "scripCode": 2475,
     "tradingSymbol": "ONGC",
     "exchange": "NC",
     "transactionType": "B",    --> (B, S, BM, SM, SAM)
     "quantity": 1,
     "disclosedQty": 0,
     "price": "149.5",
     "triggerPrice": "0",
     "rmsCode": "ANY",
     "afterHour": "N",
     "orderType": "NORMAL",
     "channelUser": "XXXXXXX",      (Use LoginId as ChannelUser)
     "validity": "GFD",
     "requestType": "CANCEL",
     "productType": "INVESTMENT"    --> (INVESTMENT or (INV), BIGTRADE or (BT), BIGTRADEPLUS or (BT+))
     #Below parameters need to be added for FNO trading
    "instrumentType": "FUTCUR";    --> (Future Stocks(FS)/ Future Index(FI)/ Option Index(OI)/ Option Stocks(OS)/ Future Currency(FUTCUR)/ Option Currency(OPTCUR))
    "strikePrice": "-1";
    "optionType": "XX";    --> (XX/PE/CE)
    "expiry": "31/03/2023";
    }
    order=sharekhan.cancelOrder(orderparams)
    print("CancelOrder: {}".format(order))

    # Retrieves all positions

    customerId="customerId < int data type>"
    order=sharekhan.trades(customerId)
    print("Postion Reports: {}".format(order))

    # Retrieve history of an given order

    exchange="exchange value <string>"
    customerId="customerId <int data type>"
    orderId="orderId <int data type>"
    order=sharekhan.exchange(exchange, customerId, orderId)
    print("Order Details: {}".format(order))

    # Retrieves the trade  generated by an order

    exchange="exchange value <string>"
    customerId="customerId <int data type>"
    orderId="orderId <int data type>"
    order=sharekhan.exchangetrades(exchange, customerId, orderId)
    print("Trade Generated By an Order : {}".format(order))


    # services Holdings

    customerId="customerId <int data type>"
    order=sharekhan.holdings(customerId)
    print("Holdings : {}".format(order))

    # Script Master

    exchange="exchange value <string>"
    order=sharekhan.master(exchange)
    print("Script Master : {}".format(order))


    # Historical Data

    exchange="exchange value <string>"
    scripcode="Unique scripcode provided by the broker <int>"
    interval="Available Intervals <string>"
    order=sharekhan.historicaldata(exchange, scripcode, interval)
    print("Holdings Data: {}".format(order))

websocket Programming Testing

    from SharekhanApi.sharekhanWebsocket import SharekhanWebSocket

    params = {
        "access_token": access_token
    }
    action = 1
    mode = 1

    token_list = {"action": "subscribe", "key": ["feed"], "value": [""]}
    feed = {"action": "feed", "key": ["depth"], "value": ["MX250715"]}
    unsubscribefeed = {"action":"unsubscribe","key":["feed"],"value":["NC22,NF37833,NF37834,MX253461,RN7719"]}

    sws = SharekhanWebSocket(access_token)


    def on_data(wsapp, message):
        print("Ticks: {}".format(message))


    def on_open(wsapp):
        print("on open")
        sws.subscribe(token_list)
        # sws.fetchData(feed)
        # sws.unsubscribe(unsubscribefeed)
        # sws.close_connection()


    def on_error(wsapp, error):
        print(error)


    def on_close(wsapp):
        print("Close")


    # Assign the callbacks.
    sws.on_open = on_open
    sws.on_data = on_data
    sws.on_error = on_error
    sws.on_close = on_close

    sws.connect()

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

shareconnect-1.0.0.11.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

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

shareconnect-1.0.0.11-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file shareconnect-1.0.0.11.tar.gz.

File metadata

  • Download URL: shareconnect-1.0.0.11.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for shareconnect-1.0.0.11.tar.gz
Algorithm Hash digest
SHA256 60f555e5d8d1393983d806ac6795409f8fd552fad846e380704041ab0657f194
MD5 05922e1b8fceb0f98f1389b218176d74
BLAKE2b-256 e3f92458c7bb499fd3368cc9ceb8232afe04e7a51f1b3e70a4d8ae5b5810e45b

See more details on using hashes here.

File details

Details for the file shareconnect-1.0.0.11-py3-none-any.whl.

File metadata

File hashes

Hashes for shareconnect-1.0.0.11-py3-none-any.whl
Algorithm Hash digest
SHA256 207f9d4fcc3ac1a9b6a309a17a7ed2e0ee63418ee2f076c9c6c5d44b2c02f768
MD5 440c8298c3231a1dddebf2a430e495d2
BLAKE2b-256 25af93fa90064ce3c1b77099acc7cefcae3aaafb07b354927b2c8b7437dcab34

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