script is running as expected on local machine but not on google colab

tusharwagh10
Why this script is running as expected on local machine but not on google colab.
https://colab.research.google.com/drive/14lSyOomoz1V6UsrIp5cobUxgCWz-hZsS?usp=sharing

Error to be focused on : In main : kws not connected
Error to be ignored : Connection error: 0 - error parsing tokens.
  • rakeshr
    error parsing tokens.
    As the error says, you are passing the wrong instrument token. You need to fetch the correct access token from the complete instrument list.
  • tahseen
    I went and before I can think of anything, my first reaction is - "Man what is this inside on_ticks ? "
    Sorry, I got stuck looking at this piece of code itself. This snippet is a death nail to on_ticks
    def on_ticks(ws, ticks):
    #ticks=[dict[ ticks[0]['instrument_token']],ticks[0]['timestamp'],ticks[0]['last_price']]
    print("In on_ticks")
    print(ticks)
    # saving instrument data in csv file
    for tick in ticks:
    data = [str(datetime.now()),tick["last_price"]]
    tradingsymbol = tradingsymbol_token_df[tradingsymbol_token_df.instrument_token == tick["instrument_token"]]["tradingsymbol"].iloc[0]
    filename = tradingsymbol + ".csv"
    filepath = os.path.join(data_dir,filename)
    with open(filepath, 'a') as f_object:
    # Pass this file object to csv.writer()
    # and get a writer object
    writer_object = writer(f_object)
    # Pass the list as an argument into
    # the writerow()
    writer_object.writerow(data)
    #Close the file object
    f_object.close()
  • tusharwagh10
    @rakeshr,
    instrument token is generated in the further/below steps i.e in while loop. So in the first iteration I have passed empty list. Please ignore this error. I want to know about this error : "In main : kws not connected".
    This code is running fine on local system.
  • tusharwagh10
    @tahseen,
    I am writing lastprice and current datetime in csv file.
    tradingsymbol = tradingsymbol_token_df[tradingsymbol_token_df.instrument_token == tick["instrument_token"]]["tradingsymbol"].iloc[0]

    This line seems complex. Here, I am generating trading symbol from instrument token as tick data does provide trading symbol for corresponding instrument token.
  • tahseen
    @tusharwagh10
    I don't find your code complex, just saying approach is not optimal
    Instead of all that data frame, you could simple create map of instrument token and trading symbol in the beginning of the program. Don't keep all instruments, just the ones you want
    on_ticks should not have that bulk of code in the first place

    Last but not the least, the line that you mentioned, instead of that use

    # Do this in the beginning of the program
    df = pd.read_csv("all_instruments", low_memory = False)
    instrument_map = dict( zip ( df.instrument_token, df.tradingsymbol ) )
    df = None


    ##### somewhere later in the program

    instrument_symbol = instrument_map[ tick["instrument_token"] ]

    But please don't choke the on_ticks function


  • tusharwagh10
    okay.
    Can you please tell why am I getting "In main : kws not connected" ?
Sign In or Register to comment.