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()
@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.
@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.
@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
Sorry, I got stuck looking at this piece of code itself. This snippet is a death nail to on_ticks
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.
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.
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 But please don't choke the on_ticks function
Can you please tell why am I getting "In main : kws not connected" ?