(20241226) Change and Percent Change bug fixed for Zerodha's ticks.

This commit is contained in:
2024-12-26 11:53:47 +05:30
parent a8449ebaef
commit 8e89575032
3 changed files with 53 additions and 28 deletions
+23 -19
View File
@@ -88,13 +88,14 @@ kafka_producer = ProducerKafka(
topic = "tickers",
config = create_config(
bootstrap_servers = "del.ditscentre.in:9092",
buffer_size = 1,
security_protocol = "SSL",
# ca_file = r"../../creds/kafka/cert_authority.pem",
# cert_file = r"../../creds/kafka/fullchain.pem",
# key_file = r"../../creds/kafka/privkey.pem"
ca_file = os.path.join(parent_dir, "creds", "kafka", "cert_authority.pem"),
cert_file = os.path.join(parent_dir, "creds", "kafka", "fullchain.pem"),
key_file = os.path.join(parent_dir, "creds", "kafka", "privkey.pem")
ca_file = r"../../creds/kafka/cert_authority.pem",
cert_file = r"../../creds/kafka/fullchain.pem",
key_file = r"../../creds/kafka/privkey.pem"
# ca_file = os.path.join(parent_dir, "creds", "kafka", "cert_authority.pem"),
# cert_file = os.path.join(parent_dir, "creds", "kafka", "fullchain.pem"),
# key_file = os.path.join(parent_dir, "creds", "kafka", "privkey.pem")
)
)
@@ -110,6 +111,7 @@ def to_kafka(tick: TradingTick) -> bool:
success = False
summary = tick.summary
summary["messageType"] = "ticks"
success = kafka_producer.produce(value = summary)
return success
@@ -133,7 +135,10 @@ def on_connect(ws, response):
def on_ticks(ws, ticks):
# print("TICK SAMPLE:", json.to_string(ticks, default=str))
ticks = TradingTick.from_zerodha_kite(ticks = ticks, instrument_lookup = INSTRUMENT_LOOKUP)
# print("TICK SAMPLE:", json.to_string(ticks[0].model_dump(), default=str))
# print("TICK SAMPLE:", json.to_string(ticks[0].summary, default=str))
results = [to_kafka(tick) for tick in ticks]
success = sum(results)
print(f"TICKS: {len(ticks): <4} | PRODUCED: {success: <4}{' | FAILURE(S)!' if success < len(results)else ''}")
@@ -149,8 +154,8 @@ def main():
global INSTRUMENT_LOOKUP
# Load Zerodha credentials:
# creds = json.from_file(r"../../creds/zerodha/api.json")
creds = json.from_file(os.path.join(parent_dir, "creds", "zerodha", "api.json"))
creds = json.from_file(r"../../creds/zerodha/api.json")
# creds = json.from_file(os.path.join(parent_dir, "creds", "zerodha", "api.json"))
api_key = creds["apiKey"]
access_token = creds["accessToken"]
@@ -158,19 +163,18 @@ def main():
kite = KiteConnect(api_key = api_key)
kite.set_access_token(access_token)
# Get a list of instruments to work with:
# Get the entire list of instruments:
instruments = kite.instruments(exchange = "NSE")
instruments = instruments[:100]
instruments = [TradingSymbol.from_zerodha_kite(i) for i in instruments]
instruments += kite.instruments(exchange = "NFO")
instruments += kite.instruments(exchange = "BSE")
instruments += kite.instruments(exchange = "BFO")
instruments += kite.instruments(exchange = "MCX")
instruments += kite.instruments(exchange = "CDS")
instruments += kite.instruments(exchange = "BCD")
instruments_csv = []
instruments_csv += kite.instruments(exchange = "NSE")
instruments_csv += kite.instruments(exchange = "NFO")
# instruments_csv += kite.instruments(exchange = "BSE")
# instruments_csv += kite.instruments(exchange = "BFO")
# instruments_csv += kite.instruments(exchange = "MCX")
# instruments_csv += kite.instruments(exchange = "CDS")
# instruments_csv += kite.instruments(exchange = "BCD")
# Pick the instruments of interest:
instruments = instruments[:1000]
instruments = [TradingSymbol.from_zerodha_kite(i) for i in instruments]
# Create the lookup:
for i in instruments: