Since its been a 2 months, i am trying to work on the tick data but i am unable to get in to my excel sheet. Can any body give me a same code where i am getting the tick data to my excel sheet for every 10 seconds.
import datetime from xml.dom import minidom from kiteconnect import KiteTicker import json import logging import pandas as pd import os import time import threading import xml.etree.ElementTree as ET # from datetime import datetime
# Load API Key and Access Token from Config File def load_api_keys(): try: with open('Zerodha credentials.json', 'r') as file: config = json.load(file) logging.info("API credentials loaded successfully.") return config['api_key'], config['access_token'] except FileNotFoundError: logging.error("Config file not found.") exit(1) except KeyError as e: logging.error("Config file is missing the key: %s", e) exit(1)
# Load API keys api_key, access_token = load_api_keys()
# Instantiate KiteTicker and Assign Callbacks kws = KiteTicker(api_key, access_token)
# Load instrument tokens from file try: with open('FINAL_NFO-AFTER_Instruments.json', 'r') as file: instrument_tokens_data = json.load(file) logging.debug("Instrument tokens data loaded successfully.") except FileNotFoundError: logging.error("Instrument tokens file not found.") exit(1) except json.JSONDecodeError: logging.error("Error decoding the instrument tokens file.") exit(1)
# WebSocket Callback def on_connect(ws, response): logging.debug("Connected to WebSocket.")
if not instrument_tokens_data: # Check if data is loaded logging.error("Instrument tokens data is empty or not loaded.") return
# Subscribe to the instrument tokens instrument_tokens = [inst['instrument_token'] for inst in instrument_tokens_data] ws.subscribe(instrument_tokens) # Subscribing to the tokens
# Set mode to MODE_FULL with instrument tokens ws.set_mode(ws.MODE_FULL, instrument_tokens) logging.info(f"Subscribed to tokens: {instrument_tokens}")
# Append the raw tick data directly to ticks_data if ticks: ticks_data.extend(ticks)
# Optional: Save or process the raw ticks_data further as needed # For example, you could log the tick data or save it periodically to a file
# Global variables # Step 4: WebSocket Callbacks and Ticks Data Handling ticks_data = {} # Store tick data ticks_data_1 = pd.DataFrame() # Initialize empty DataFrame for ticks_data_1
# Save ticks to JSON def save_ticks_to_json(ticks): if ticks: # Convert datetime objects to strings for tick in ticks: for key, value in tick.items(): if isinstance(value, datetime.datetime): # Check if value is a datetime object tick[key] = value.isoformat() # Convert to ISO 8601 string
timestamp = time.strftime("%Y-%m-%d_%H-%M-%S") file_path = f'Tick_Data_{timestamp}.json' with open(file_path, 'w') as file: json.dump(ticks, file, indent=4) logging.info(f"Created new JSON file '{file_path}' and saved ticks.")
# Save ticks to XML def save_ticks_to_xml(ticks): if ticks: root = ET.Element('ticks') for tick in ticks: tick_elem = ET.SubElement(root, 'tick') for key, value in tick.items(): if value is not None: # Avoid NoneType values ET.SubElement(tick_elem, key).text = str(value)
# Define the XML file path with a timestamp timestamp = time.strftime("%Y-%m-%d_%H-%M-%S") file_path = f'Tick_Data_{timestamp}.xml'
# Save to XML file with open(file_path, 'wb') as file: rough_string = ET.tostring(root, 'utf-8') reparsed = minidom.parseString(rough_string) file.write(reparsed.toprettyxml(indent="\t").encode('utf-8'))
logging.info(f"Created new XML file '{file_path}' and saved ticks.")
# Save ticks to XLSX def save_ticks_to_xlsx(ticks): if ticks: timestamp = time.strftime("%Y-%m-%d_%H-%M-%S") file_path = f'Tick_Data_{timestamp}.xlsx' df = pd.DataFrame(ticks) df.to_excel(file_path, index=False) logging.info(f"Created new XLSX file '{file_path}' and saved ticks.")
# Timer to save ticks periodically def save_ticks_periodically(): global ticks_data while True: if ticks_data: save_ticks_to_json(ticks_data) save_ticks_to_xml(ticks_data) save_ticks_to_xlsx(ticks_data) # ticks_data = {} # Clear the tick data after saving time.sleep(30)
# Start the periodic save function in a separate thread threading.Thread(target=save_ticks_periodically).start()
# Keep script running while True: time.sleep(180)
This is my code. i want to understand where am i making wrong.
from xml.dom import minidom
from kiteconnect import KiteTicker
import json
import logging
import pandas as pd
import os
import time
import threading
import xml.etree.ElementTree as ET
# from datetime import datetime
# Configure logging
logging.basicConfig(level=logging.DEBUG)
# Load API Key and Access Token from Config File
def load_api_keys():
try:
with open('Zerodha credentials.json', 'r') as file:
config = json.load(file)
logging.info("API credentials loaded successfully.")
return config['api_key'], config['access_token']
except FileNotFoundError:
logging.error("Config file not found.")
exit(1)
except KeyError as e:
logging.error("Config file is missing the key: %s", e)
exit(1)
# Load API keys
api_key, access_token = load_api_keys()
# Instantiate KiteTicker and Assign Callbacks
kws = KiteTicker(api_key, access_token)
# Load instrument tokens from file
try:
with open('FINAL_NFO-AFTER_Instruments.json', 'r') as file:
instrument_tokens_data = json.load(file)
logging.debug("Instrument tokens data loaded successfully.")
except FileNotFoundError:
logging.error("Instrument tokens file not found.")
exit(1)
except json.JSONDecodeError:
logging.error("Error decoding the instrument tokens file.")
exit(1)
# WebSocket Callback
def on_connect(ws, response):
logging.debug("Connected to WebSocket.")
if not instrument_tokens_data: # Check if data is loaded
logging.error("Instrument tokens data is empty or not loaded.")
return
# Subscribe to the instrument tokens
instrument_tokens = [inst['instrument_token'] for inst in instrument_tokens_data]
ws.subscribe(instrument_tokens) # Subscribing to the tokens
# Set mode to MODE_FULL with instrument tokens
ws.set_mode(ws.MODE_FULL, instrument_tokens)
logging.info(f"Subscribed to tokens: {instrument_tokens}")
def on_ticks(ws, ticks):
logging.debug("Ticks: %s", ticks)
# Append the raw tick data directly to ticks_data
if ticks:
ticks_data.extend(ticks)
# Optional: Save or process the raw ticks_data further as needed
# For example, you could log the tick data or save it periodically to a file
# Global variables # Step 4: WebSocket Callbacks and Ticks Data Handling
ticks_data = {} # Store tick data
ticks_data_1 = pd.DataFrame() # Initialize empty DataFrame for ticks_data_1
def on_close(ws, code, reason):
logging.debug("WebSocket closed with code: %s, reason: %s", code, reason)
ws.stop()
def on_error(ws, code, reason):
logging.error("Error: %s, %s", code, reason)
def on_noreconnect(ws):
logging.error("AutoReconnect: WebSocket couldn't reconnect. Check your internet connectivity.")
def on_reconnect(ws, attempt_count):
logging.debug("AutoReconnect: WebSocket reconnected successfully in attempt %d.", attempt_count)
# Assign callbacks
kws.on_ticks = on_ticks
kws.on_connect = on_connect
kws.on_close = on_close
kws.on_error = on_error
kws.on_noreconnect = on_noreconnect
kws.on_reconnect = on_reconnect
# Start WebSocket
kws.connect(threaded=True)
# Save ticks to JSON
def save_ticks_to_json(ticks):
if ticks:
# Convert datetime objects to strings
for tick in ticks:
for key, value in tick.items():
if isinstance(value, datetime.datetime): # Check if value is a datetime object
tick[key] = value.isoformat() # Convert to ISO 8601 string
timestamp = time.strftime("%Y-%m-%d_%H-%M-%S")
file_path = f'Tick_Data_{timestamp}.json'
with open(file_path, 'w') as file:
json.dump(ticks, file, indent=4)
logging.info(f"Created new JSON file '{file_path}' and saved ticks.")
# Save ticks to XML
def save_ticks_to_xml(ticks):
if ticks:
root = ET.Element('ticks')
for tick in ticks:
tick_elem = ET.SubElement(root, 'tick')
for key, value in tick.items():
if value is not None: # Avoid NoneType values
ET.SubElement(tick_elem, key).text = str(value)
# Define the XML file path with a timestamp
timestamp = time.strftime("%Y-%m-%d_%H-%M-%S")
file_path = f'Tick_Data_{timestamp}.xml'
# Save to XML file
with open(file_path, 'wb') as file:
rough_string = ET.tostring(root, 'utf-8')
reparsed = minidom.parseString(rough_string)
file.write(reparsed.toprettyxml(indent="\t").encode('utf-8'))
logging.info(f"Created new XML file '{file_path}' and saved ticks.")
# Save ticks to XLSX
def save_ticks_to_xlsx(ticks):
if ticks:
timestamp = time.strftime("%Y-%m-%d_%H-%M-%S")
file_path = f'Tick_Data_{timestamp}.xlsx'
df = pd.DataFrame(ticks)
df.to_excel(file_path, index=False)
logging.info(f"Created new XLSX file '{file_path}' and saved ticks.")
# Timer to save ticks periodically
def save_ticks_periodically():
global ticks_data
while True:
if ticks_data:
save_ticks_to_json(ticks_data)
save_ticks_to_xml(ticks_data)
save_ticks_to_xlsx(ticks_data)
# ticks_data = {} # Clear the tick data after saving
time.sleep(30)
# Start the periodic save function in a separate thread
threading.Thread(target=save_ticks_periodically).start()
# Keep script running
while True:
time.sleep(180)
This is my code. i want to understand where am i making wrong.