Tick data new thread issue

Newbie
Hi, I'm trying to create a new thread to pass live ticks to an excel file. But, I'm getting this error:

pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None)

I've used this as a reference: https://kite.trade/forum/discussion/comment/25535/#Comment_25535

The code that I'm trying to run is:
import xlwings as xw
wb = xw.Book('tick data threaded.xlsx')
sht = wb.sheets('Sheet1')

sht.range('A1').value="Instrument Token"
sht.range('B1').value="LTP"


def on_ticks(ws, ticks):
logging.debug("Ticks: {}".format(ticks))

def on_connect(ws, response):
ws.subscribe([738561, 5633])
ws.set_mode(ws.MODE_FULL, [738561, 5633])


kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.connect(threaded=True)

while True:
def on_ticks(ws, ticks):
thread_data(ticks)


def thread_data(ticks):
row = 2
for data in ticks:
sht.range('A'+str(row)).value=data['instrument_token']
sht.range('B'+str(row)).value=data['last_price']
row = row + 1

kws.on_ticks=on_ticks
Please point out where I'm going wrong.
  • rakeshr
    Are you using win32com.client anywhere?
    If yes, you need to call CoInitialize(). Have a look at this article.
  • Newbie
    Hi, I'll try the given solution over the weekend. Thanks for the help.
  • prithviraj
    I was also facing same error, after adding coInitialize.
    I am facing below errors.


    pywintypes.com_error: (-2147417842, 'The application called an interface that was marshalled for a different thread.', None, None)

    builtins.AttributeError: 'NoneType' object has no attribute 'append'

  • prithviraj
    import xlwings as xw
    from pprint import pprint
    import pdb
    import pandas as pd
    import logging
    from kiteconnect import KiteTicker
    import time
    import datetime
    import pythoncom
    import win32com.client as client


    wb = xw.Book('option.xlsx')
    sht = wb.sheets['sheet1']

    row=1
    logging.basicConfig(level=logging.DEBUG)

    kws = KiteTicker()

    def on_ticks(ws, ticks):
    logging.debug("Ticks: {}".format(ticks))

    def on_connect(ws, response):
    ws.subscribe([55962887])
    ws.set_mode(ws.MODE_FULL, [55962887])

    def on_close(ws, code, reason):
    ws.stop()

    kws.on_ticks = on_ticks
    kws.on_connect = on_connect
    kws.on_close = on_close
    kws.connect(threaded=True)

    while True:
    def on_ticks(ws, ticks):

    pythoncom.CoInitialize()


    helper_method(ticks)


    def helper_method(ticks):

    pythoncom.CoInitialize()

    global row
    try:
    for company_data in ticks:
    sht.range('G4').value=company_data['last_price']
    print(row)
    row=row+1
    except Exception as e:
    raise e
    kws.on_ticks=on_ticks
  • prithviraj
    Any solution found??

  • indiatechpython
    I used ...
    def on_ticks(ws, ticks):
    _thread.start_new_thread(store_ticks, (ticks,))

    def store_ticks(ticks):
    # do Data store activity without endless loop
  • prithviraj
    @indiatechpython
    pls post code it is not working for me
  • indiatechpython
    def onNewTicks(ws, ticks):
    if(timecheck.during_NSEExchangetime() == True):
    #logging.info('New ticks received %s', ticks)
    logging.info('New ticks received')
    try:
    _thread.start_new_thread(store_ticks, (ticks,))
    except:
    logging.info('Exception while creating new thread...')
    else:
    logging.info('Not during non trading hours...')
    ......
    def store_ticks(ticks):
    #logging.info('New ticks received inside store_ticks function %s', ticks)
    for tick in ticks:
    # while loop followed.... for storing the ticks

Sign In or Register to comment.