Skip to main content

Truedata's Official Python Package

Project description

This is the official python (websocket) repo for TrueData.

What have we covered so far ?

  • Websocket APIs
    • Live data
    • Historical data

How do you use it ?

For beginners

  • Installing from PyPi
python3.7 -m pip install truedata_ws
  • Running tests to ensure everything is in order
python3.7 -m truedata_ws run_all_tests <enter_your_login_id> <enter_your_password>
  • Connecting
from truedata_ws.websocket.TD import TD
td_obj = TD('<enter_your_login_id>', '<enter_your_password>')
  • Connecting using the optional broker_token parameter
    Truedata also partners up with brokers to provide our clients with the optimum trading experience.
    If you are a client that uses this service, please be sure to provide your broker token as well after logging into their service
td_obj = TD('<enter_your_login_id>', '<enter_your_password>', broker_token='<enter_broker_token>')
  • Starting live data

For Single Symbols

req_ids = td_obj.start_live_data(['<enter_symbol>'])
# Example:
# req_id = start_live_data(['CRUDEOIL-I'])
# This returns an single element list that can be used later to reference the data

For Multiple symbols

req_ids = td_obj.start_live_data(['<symbol_1>', '<symbol_2>', '<symbol_3>', ...])
# Example:
# req_ids = td_obj.start_live_data(['CRUDEOIL-I', 'BANKNIFTY-I', 'RELIANCE', 'ITC'])
# This returns a list that can be used to reference data later
  • Accessing touchline data
import time
time.sleep(1)
for req_id in req_ids:
    print(td_obj.touchline_data[req_id])
# You MAY have to wait until for 1 sec for all of the touchline data to populate
  • Sample code for testing market data
from copy import deepcopy
live_data_objs = {}
for req_id in req_ids:    
    live_data_objs[req_id] = deepcopy(td_obj.live_data[req_id])

while True:
    for req_id in req_ids:
        if live_data_objs[req_id] != td_obj.live_data[req_id]:
            print(td_obj.live_data[req_id])
            live_data_objs[req_id] = deepcopy(td_obj.live_data[req_id])
  • Stopping live data
td_obj.stop_live_data(['<symbol_1>', '<symbol_2>', '<symbol_3>', ...])


You can also provide your own list of req_ids (However, they need to have the same length as the number of symbols)...
Or, you can provide a single req_id as an integer and we will return a list by incrementing it for every symbol provided...


  • Getting Historical data

Using no parameters

hist_data_1 = td_obj.get_historic_data('BANKNIFTY-I')
# This returns 1 minute bars from the start of the present day until current time

Using a given a specific ending time

from datetime import datetime
hist_data_2 = td_obj.get_historic_data('BANKNIFTY-I', query_time=datetime(2020, 5, 5, 12, 30))
# Any time can be given here

Using a given duration (For available duration options, please read the limitations section)

hist_data_3 = td_obj.get_historic_data('BANKNIFTY-I', duration='3 D')

Using a specified bar_size (For available bar_size options, please read the limitations section)

hist_data_4 = td_obj.get_historic_data('BANKNIFTY-I', bar_size='30 mins')

Using start time INSTEAD of duration

from dateutil.relativedelta import relativedelta
hist_data_5 = td_obj.get_historic_data('BANKNIFTY-I', start_time=datetime.now()-relativedelta(days=3))

IMPORTANT NOTE: Now that we have covered the basic parameters, you can mix and match the parameters as you please... If a parameter is not specified, the defaults are as follows

query_time = datetime.now()
duration = "1 D"
bar_size = "1 min"

Example of mix and match

hist_data_6 = td_obj.get_historic_data('BANKNIFTY-I', duration='3 D', bar_size='15 mins')

On a side note: You can convert historical data to Pandas DataFrames with a single line

import pandas as pd
df = pd.DataFrame(hist_data_1)
  • Disconnect from the service
td_obj.disconnect()


  • Limitations and caveats for historical data
  1. If you provide both duration and start time, duration will be used and start time will be ignored...
  2. If you provide neither duration nor start time, duration = "1 D" will be used
  3. ONLY the following BAR_SIZES are available
    • tick
    • 1 min
    • 5 mins
    • 15 mins
    • 30 mins
  4. Following annotation can be used for DURATION
    • D = Days
    • W = Weeks
    • M = Months
    • Y = Years






What is the plan going forward ?

  • Ease of contract handling
  • Improved error handling

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

truedata_ws-0.3.2.tar.gz (12.1 kB view details)

Uploaded Source

Built Distribution

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

truedata_ws-0.3.2-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

Details for the file truedata_ws-0.3.2.tar.gz.

File metadata

  • Download URL: truedata_ws-0.3.2.tar.gz
  • Upload date:
  • Size: 12.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5

File hashes

Hashes for truedata_ws-0.3.2.tar.gz
Algorithm Hash digest
SHA256 2adc6d87261f2b9696505f9486f2c002dc952bb6179bc09ea91a63b825c9d65b
MD5 edd92d3a04fa4aab61d8fe3ddedef61a
BLAKE2b-256 992a227c96d4789c30839b0412c3f8e36c342f6e3dda11326d1ede91f3406f62

See more details on using hashes here.

File details

Details for the file truedata_ws-0.3.2-py3-none-any.whl.

File metadata

  • Download URL: truedata_ws-0.3.2-py3-none-any.whl
  • Upload date:
  • Size: 28.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5

File hashes

Hashes for truedata_ws-0.3.2-py3-none-any.whl
Algorithm Hash digest
SHA256 fdf1b4e7b626cf06309a42f7a0a072ba8355fabc68e5ed4e1b9fd082ef3c482a
MD5 620e7fb321a2e951cf55a182ab81dac5
BLAKE2b-256 8009dc3f9dafa89f6666fbcf3180caeef79d756c333c5bdf721398ec5b094f5b

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