Hello,
I have trying for quite some time now to convert live tick data into OHLC of various intervals. I imaging i would have to store these somewhere.
Assuming i want the tick data and the OHLC of just 1 stock, can you help me with the sample code to stream & convert the tick data into OHLC?
Thanks!!
You can use the pandas resample function for the same. Store your OHLC tick data in a pandas dataframe and apply the resample function on this OHLC data for your desired frequency like seconds (S), minutely (T, min), hourly (H) etc.
Please see the documentation link for the function below.
https://pandas.pydata.org/pandas-docs/version/0.23/generated/pandas.DataFrame.resample.html
Link for possible frequencies -
http://pandas.pydata.org/pandas-docs/stable/timeseries.html#offset-aliases
Thank you, will try it out and post the sample code here
The following is the code for Kite connect web socket. How do I extract to a csv/ to a data frame here?
import logging; from kiteconnect import KiteTicker logging.basicConfig(level=logging.DEBUG) # Initialise kws = KiteTicker(api_key, access_token) def on_ticks(ws, ticks): # Callback to receive ticks. logging.debug(“Ticks: {}”.format(ticks)) def on_connect(ws, response): # Callback on successful connect. # Subscribe to a list of instrument_tokens (RELIANCE and ACC here). ws.subscribe([738561, 5633]) # Set RELIANCE to tick in full
mode. ws.set_mode(ws.MODE_FULL, [
This code should help you to store the tick data in dataframe and then in csv file: def on_tick(ticks, ws): logging.debug(ticks) # Explicit mention that the variable you are modifying are global global tick_df_all global tick_df_latest for tick in ticks: #print(tick,“\n”) tick_df_latest=json_normalize(tick) timestamp = str(datetime.datetime.now()) tick_df_latest[‘timestamp’]=timestamp #print(tick_df_latest) tick_df_all = ti