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

  • vilasvardam
    Hi Team,

    I was referring to the discussion in above thread in lieu of facing errors in my coding script ie pywintypes.com_error: (-2147221008, 'CoInitialize has not been called.', None, None) and error ie pywintypes.com_error: (-2147417842, 'The application called an interface that was marshalled for a different thread.', None, None) post implanting advise of Rakeshr Sir.

    I have identical requirement as mentioned initially in discussion to have kite data in excel sheet by creating new thread.

    As Rakeshr Sir pointed out that -: are you using win32com. client anywhere ?

    Hence It seems it is cause of above error. Is it possible to remove above setting or how ? Or what exact code need to mention in script.

    COULD YOU PLEASE LITTLE ELABORATE ON ISSUE TO ENLIGHTEN NEW USERS.

    I have also referred to the discussion as per below link.
    https://kite.trade/forum/discussion/comment/25535/#Comment_25535

    Rakeshr Sir has posted draft script on the issue of Create another thread and perform the required operation in 2nd thread using threaded=True and made valuable suggestion to user to make his script workable. (please refer above link)

    However in same script by user, observed excel file was used without any issue . How?

    I am new to this coding field and trying to learn from learned members on forum .

    Thanks and Regards,
    Vilas
  • rakeshr
    Hence It seems it is cause of above error. Is it possible to remove above setting or how ? Or what exact code need to mention in script
    We won't be able to assist regarding 3rd party lib dependency here you are referring to win32com.client or other excel libs. You can highlight all kiteconnect-related queries here.
Sign In or Register to comment.