(20241231) Too many thing happening...

This commit is contained in:
2024-12-31 07:34:49 +00:00
parent f67263320e
commit a07c762586
13 changed files with 406 additions and 162 deletions
-87
View File
@@ -1,87 +0,0 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Monday, 30th Dec. 2024
OBJECTIVE:
To provide an easy way to work with '.json' data and files.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# System-level activities:
import io
# To work with the JSON standard:
import json
# To work with files:
from utils_v2.system import files
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass
+53 -23
View File
@@ -43,7 +43,7 @@ import os
# My utils:
from utils_v2.string import json
from utils_v2.system import files
from utils_v2.queue.kafka import ProducerKafka, ConsumerKafka, create_config
from utils_v2.queue.kafka import ProducerKafka, ConsumerKafka
# To make HTTP calls:
import httpx
@@ -77,6 +77,9 @@ from kiteconnect import KiteConnect, KiteTicker
# *****************************************************************************************************************
# For user management:
accounts = {}
# For Zerodha and related to ticks:
INSTRUMENT_TOKENS = []
INSTRUMENT_LOOKUP = {}
@@ -85,32 +88,31 @@ INSTRUMENT_LOOKUP = {}
cwd = files.get_cwd()
parent_dir = cwd
kafka_consumer = ConsumerKafka(
topic = "tickers",
config = create_config(
topic = "tick-listners",
config = ConsumerKafka.create_config(
bootstrap_servers = "del.ditscentre.in:9092",
# buffer_memory = 3_35_54_432,
group_id = "123",
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")
),
debug = False
)
kafka_producer = ProducerKafka(
topic = "tickers",
config = create_config(
config = ProducerKafka.create_config(
bootstrap_servers = "del.ditscentre.in:9092",
# buffer_memory = 3_35_54_432,
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")
),
debug = False
)
@@ -145,8 +147,6 @@ def to_kafka(tick: TradingTick) -> bool:
success = False
summary = tick.summary
print(json.to_string(summary))
print(json.to_string(tick.model_dump(), default=str))
summary["messageType"] = "ticks"
success = kafka_producer.produce(value = summary)
if not success:
@@ -187,14 +187,14 @@ def on_ticks(ws, ticks):
# ---------------------------------------------------------------------------------------------------------------------
def main():
def init():
# Global vars:
global INSTRUMENT_TOKENS
global INSTRUMENT_LOOKUP
# Load Zerodha credentials:
creds = json.from_file(r"../../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"]
@@ -250,6 +250,36 @@ def main():
kite_ws.connect(threaded = True)
# ---------------------------------------------------------------------------------------------------------------------
def main():
while True:
messages = kafka_consumer.consume(count = 1, timeout = 5.0)
for message in messages:
token_key = message["tokenKey"]
account = None
# Create the websocket:
if accounts[token_key] not in list(accounts.keys()):
pass
account = accounts[token_key]
# Subscribe/unsubscribe:
ws = account["ws"]
if message["action"] == "subscribe":
ws.subscribe(message["brokerTokens"])
ws.set_mode(ws.MODE_FULL, message["brokerTokens"])
pass
elif message["action"] == "unsubscribe":
ws.unsubscribe(message["brokerTokens"])
pass
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
@@ -259,5 +289,5 @@ def main():
if __name__ == "__main__":
init()
main()
while True: time.sleep(3_600.00)