123COMMENT

This commit is contained in:
2025-09-30 14:08:04 +05:30
parent 379fb67aae
commit a81fdb6dd4
19 changed files with 1917 additions and 0 deletions
+136
View File
@@ -0,0 +1,136 @@
import sys
sys.path.append(".")
sys.path.append("..")
import random
import time
import socketio
import asyncio
import datetime
import requests
tg_alert = False
# Create a Socket.IO server instance
sio = socketio.AsyncServer(cors_allowed_origins = "*")
# Create an aiohttp web application
from aiohttp import web
app = web.Application()
# Attach the Socket.IO server to the aiohttp application
sio.attach(app)
from utils_v2.system import files
from utils_v2.queue.async_kafka import ProducerKafka, ConsumerKafka, get_ssl_context
import os
# Define the test params:
TOPIC = "tickers"
BOOTSTRAP_SERVERS = "del.ditscentre.in:9092"
# SSL_CONTEXT = get_ssl_context(
# ca_file = "../../creds/kafka/cert_authority.pem",
# cert_file = "../../creds/kafka/fullchain.pem",
# key_file = "../../creds/kafka/privkey.pem"
# )
cwd = files.get_cwd()
# parent_dir = files.get_parent_directory(cwd, 2)
parent_dir = cwd
print("CWD:", cwd)
# print("PD:", parent_dir)
SSL_CONTEXT = get_ssl_context(
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")
)
my_consumer = ConsumerKafka(
topic = TOPIC,
bootstrap_servers = BOOTSTRAP_SERVERS,
security_protocol = "SSL",
ssl_context = SSL_CONTEXT
)
# Event: Client connects
@sio.event
async def connect(sid, environ):
print(f"Client {sid} connected")
if tg_alert:
requests.post(
url = r"https://api.thecaoffice.com/converse/tech/alert/chat/backend",
json = {
"type": "info",
"chatClient": "telegram",
"chatId": "-4206946032",
# "chatId": "1275560043",
"message": f"*SocketIO Connected!*\n👍 SID: {sid}"
}
)
# Event: Client disconnects
@sio.event
async def disconnect(sid):
print(f"Client {sid} disconnected")
if tg_alert:
requests.post(
url = r"https://api.thecaoffice.com/converse/tech/alert/chat/backend",
json = {
"type": "info",
"chatClient": "telegram",
"chatId": "-4206946032",
# "chatId": "1275560043",
"message": f"*SocketIO Disconnected!*\n❌ SID: {sid}"
}
)
@sio.event
async def message(sid, data):
print("MESSAGE:", data)
# Function to generate random data
async def broadcast_one_tick(tick):
await sio.emit("ticks", tick)
# Function to broadcast data every second asynchronously
async def broadcast_ticks():
while True:
messages = await my_consumer.consume(count = 100, timeout = 1.0)
print(f"Received {len(messages)} tick(s)")
tasks = [broadcast_one_tick(m["value"]) for m in messages]
if tasks: results = await asyncio.gather(*tasks)
# Start broadcasting random data using asyncio
async def start_broadcast():
await broadcast_ticks()
# Main function to run the aiohttp server and the broadcasting
async def main():
# Start broadcasting random data in the background
asyncio.create_task(start_broadcast())
# Run the web server
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, '0.0.0.0', 5214)
print("Server running on http://0.0.0.0:5214")
await site.start()
# Keep the server running
while True:
await asyncio.sleep(3600) # Keep the server alive for 1 hour or adjust as needed
# Run the main asyncio event loop
if __name__ == '__main__':
asyncio.run(main())
@@ -0,0 +1,154 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Saturday, 28th Dec., 2024
OBJECTIVE:
To provide a quick way to test out passthrough messages over Kafka.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# System-level:
import os
# Utils:
from utils_v2.string import json
from utils_v2.system import files
from utils_v2.date_time import date_time
from utils_v2.queue.async_kafka import ProducerKafka, ConsumerKafka, get_ssl_context
# For async activities:
import asyncio
# Common:
from shared import constants
# For random choices:
import random
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
# Define the test params:
TOPIC = "socket-io-bcast"
BOOTSTRAP_SERVERS = "del.ditscentre.in:9092"
cwd = files.get_cwd()
pdir = files.get_parent_directory(cwd, depth = 2)
SSL_CONTEXT = ssl_context = get_ssl_context(
ca_file = os.path.join(pdir, "creds", "kafka", "cert_authority.pem"),
cert_file = os.path.join(pdir, "creds", "kafka", "fullchain.pem"),
key_file = os.path.join(pdir, "creds", "kafka", "privkey.pem")
)
async def keep_producing():
# Create the producer:
my_producer = ProducerKafka(
topic = TOPIC,
bootstrap_servers = BOOTSTRAP_SERVERS,
security_protocol = "SSL",
ssl_context = SSL_CONTEXT
)
# Create a message for the producer to produce:
strategy_message = {
"to": "FC9O3N75Ax7B1v4NAAAD",
"event": "Strategy",
"namespace": "/finstitutions/trading",
"data": {
"message": "Your strategy ABC says buy XYZ.",
"playSound": True
}
}
corporate_action_message = {
"to": "FC9O3N75Ax7B1v4NAAAD",
"event": "Corporate Action",
"namespace": "/finstitutions/trading",
"data": {
"message": "Stock ABC has a corporate action tomorrow.",
"playSound": True,
"date": "29-Dec-2024",
"action": "Extraordinary Board Meeting"
}
}
# Keep sending the message in intervals:
while True:
producer_message = random.choice([
strategy_message,
corporate_action_message
])
success = await my_producer.produce(producer_message)
print("PRODUCED:", success)
await asyncio.sleep(1.0)
async def main():
await asyncio.gather(*[keep_producing()])
asyncio.run(main())
+367
View File
@@ -0,0 +1,367 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Saturday, 21st Dec. 2024
OBJECTIVE:
To simulate stock market updates to test on SocketIO.
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
import os
# My utils:
from utils_v2.string import json
# For pseudo-random simulations:
import random
# To work with SocketIO
import socketio
from aiohttp import web
# To make HTTP calls:
import httpx
# To work with date and time:
import datetime
import time
# For asynchronous behaviour:
import asyncio
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# For SocketIO:
# Create a Socket.IO server instance
sio = socketio.AsyncServer(cors_allowed_origins = "*")
app = web.Application()
sio.attach(app)
# A list of stocks to simulate:
SYMBOL_TO_PRICE_MAP = {
"HDFCBANK": {
"prevClose": 1_763.95,
"ltp": 1_771.50,
"totVol": 55_96_931,
"buyVol": 16_79_079,
"sellVol": 39_17_852,
},
"RELIANCE": {
"prevClose": 1_213.35,
"ltp": 1_205.30,
"totVol": 7_34_568,
"buyVol": 1_04_873,
"sellVol": 6_29_695,
},
"INFY": {
"prevClose": 1_925.70,
"ltp": 1_922.15,
"totVol": 5_54_108,
"buyVol": 2_61_593,
"sellVol": 2_92_515,
},
"TCS": {
"prevClose": 4_203.50,
"ltp": 4_170.30,
"totVol": 7_24_932,
"buyVol": 1_34_666,
"sellVol": 5_90_266,
},
"HINDUNILVR": {
"prevClose": 2_312.95,
"ltp": 2_333.90,
"totVol": 5_04_533,
"buyVol": 9_252,
"sellVol": 4_95_281,
},
"ITC": {
"prevClose": 463.20,
"ltp": 464.65,
"totVol": 7_07_905,
"buyVol": 3_27_422,
"sellVol": 3_80_483,
},
"KOTAKBANK": {
"prevClose": 1_751.65,
"ltp": 1_743.55,
"totVol": 4_49_104,
"buyVol": 2_47_489,
"sellVol": 2_01_615,
}
}
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
tg_update = False
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
@sio.event
async def before_connect(sid, environ):
print(f"Checking connection attempt from {sid}.")
# # Simulate a failed connection based on some conditions (for example, invalid IP or header)
# user_agent = environ.get('HTTP_USER_AGENT', '')
# if 'BadUserAgent' in user_agent:
# print(f"Rejected connection from {sid} due to invalid User-Agent.")
# return False # This will reject the connection attempt
return True # Allow connection
# ---------------------------------------------------------------------------------------------------------------------
@sio.event
async def connect(sid, environ):
print(f"Client {sid} connected")
if tg_update:
async with httpx.AsyncClient() as client:
try: await client.post(
url = r"https://api.thecaoffice.com/converse/tech/alert/chat/backend",
json = {
"type": "info",
"chatClient": "telegram",
"chatId": "-4206946032",
# "chatId": "1275560043",
"message": f"*SocketIO Connected!*\n👍 SID: {sid}"
}
)
except: pass
# ---------------------------------------------------------------------------------------------------------------------
@sio.event
async def disconnect(sid):
print(f"Client {sid} disconnected")
if tg_update:
async with httpx.AsyncClient() as client:
try: await client.post(
url = r"https://api.thecaoffice.com/converse/tech/alert/chat/backend",
json = {
"type": "info",
"chatClient": "telegram",
"chatId": "-4206946032",
# "chatId": "1275560043",
"message": f"*SocketIO Disconnected!*\n❌ SID: {sid}"
}
)
except: pass
def round_tick(price):
return round(price * 20) / 20
# ---------------------------------------------------------------------------------------------------------------------
def simulate_one_stock(symbol, price):
global SYMBOL_TO_PRICE_MAP
# Simulate a change in the price:
pos_bias = [1] * 10
no_bias = [0] * 1
neg_bias = [-1] * 10
bias = random.choice(pos_bias + no_bias + neg_bias)
change_factor = random.random() / 100.0
change = price * change_factor * bias
ltp = round_tick(price + change)
# Simulate the volume.
# Assume a trade qty. worth 1L to 10L rupees:
traded_amt = random.uniform(1_00_000, 10_00_000)
ltq = int(traded_amt / price)
SYMBOL_TO_PRICE_MAP[symbol]["totVol"] += ltq
if bias >= 0: SYMBOL_TO_PRICE_MAP[symbol]["buyVol"] += ltq
else: SYMBOL_TO_PRICE_MAP[symbol]["sellVol"] += ltq
# Create the basic JSON payload:
stock_json = {
"symbol": symbol,
"last_traded_quantity": ltq,
"average_traded_price": round_tick(price + (bias * price * (random.random() / 100.0))),
"volume_traded": SYMBOL_TO_PRICE_MAP[symbol]["totVol"],
"total_buy_quantity": SYMBOL_TO_PRICE_MAP[symbol]["buyVol"],
"total_sell_quantity": SYMBOL_TO_PRICE_MAP[symbol]["sellVol"],
"ohlc": {
"open": round_tick(price + (price * 0.005)),
"high": round_tick(price + (price * 0.015)),
"low": round_tick(price - (price * 0.015)),
"close": ltp
},
"change": ((ltp - SYMBOL_TO_PRICE_MAP[symbol]["prevClose"]) / SYMBOL_TO_PRICE_MAP[symbol]["prevClose"]) * 100,
"last_trade_time": (datetime.datetime.now() - datetime.timedelta(seconds = random.uniform(0.0, 2.5))).strftime("%Y-%m-%d %H:%M:%S"),
"oi": 0,
"oi_day_high": 0,
"oi_day_low": 0,
"exchange_timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"depth": {
"buy": [
{
"quantity": random.randint(0, 100),
"price": round(ltp - 0.05, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp - 0.10, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp - 0.15, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp - 0.20, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp - 0.25, 2),
"orders": random.randint(0, 10)
}
],
"sell": [
{
"quantity": random.randint(0, 100),
"price": round(ltp + 0.05, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp + 0.10, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp + 0.15, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp + 0.20, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp + 0.25, 2),
"orders": random.randint(0, 10)
}
]
}
}
# Done here:
return stock_json
# ---------------------------------------------------------------------------------------------------------------------
def simulate_ticks_once():
# Pick a no. of stocks to simulate:
count = random.randint(1, len(SYMBOL_TO_PRICE_MAP))
symbols = random.sample(list(SYMBOL_TO_PRICE_MAP.keys()), count)
# Create the tick JSON:
tick_json = [
simulate_one_stock(
symbol = symbol,
price = SYMBOL_TO_PRICE_MAP[symbol]["ltp"]
) for symbol in symbols
]
# Done here:
return tick_json
# ---------------------------------------------------------------------------------------------------------------------
async def broadcast_random_data():
while True:
await sio.emit("ticks", simulate_ticks_once())
await asyncio.sleep(random.uniform(0.15, 1.0))
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
async def server():
# Start broadcasting random data in the background
asyncio.create_task(broadcast_random_data())
# Run the web server
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, "0.0.0.0", 5214)
await site.start()
# Keep the server running
while True:
await asyncio.sleep(3600)
asyncio.run(server())
@@ -0,0 +1,391 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Wednesday, 25th Dec. 2024
OBJECTIVE:
To simulate stock market updates to test on SocketIO.
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
import os
# My utils:
from utils_v2.string import json
# For pseudo-random simulations:
import random
# To work with SocketIO
import socketio
from aiohttp import web
# To make HTTP calls:
import httpx
# To work with date and time:
import datetime
import time
# For asynchronous behaviour:
import asyncio
# Models:
from models.finstitutions.trading.symbols import TradingSymbol
from models.finstitutions.trading.ticks import TradingTick
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# For SocketIO:
sio = socketio.AsyncServer(cors_allowed_origins = "*")
app = web.Application()
sio.attach(app)
# For Zerodha and related to ticks:
SYMBOL_TO_PRICE_MAP = {
"HDFCBANK": {
"prevClose": 1_763.95,
"ltp": 1_771.50,
"totVol": 55_96_931,
"buyVol": 16_79_079,
"sellVol": 39_17_852,
},
"RELIANCE": {
"prevClose": 1_213.35,
"ltp": 1_205.30,
"totVol": 7_34_568,
"buyVol": 1_04_873,
"sellVol": 6_29_695,
},
"INFY": {
"prevClose": 1_925.70,
"ltp": 1_922.15,
"totVol": 5_54_108,
"buyVol": 2_61_593,
"sellVol": 2_92_515,
},
"TCS": {
"prevClose": 4_203.50,
"ltp": 4_170.30,
"totVol": 7_24_932,
"buyVol": 1_34_666,
"sellVol": 5_90_266,
},
"HINDUNILVR": {
"prevClose": 2_312.95,
"ltp": 2_333.90,
"totVol": 5_04_533,
"buyVol": 9_252,
"sellVol": 4_95_281,
},
"ITC": {
"prevClose": 463.20,
"ltp": 464.65,
"totVol": 7_07_905,
"buyVol": 3_27_422,
"sellVol": 3_80_483,
},
"KOTAKBANK": {
"prevClose": 1_751.65,
"ltp": 1_743.55,
"totVol": 4_49_104,
"buyVol": 2_47_489,
"sellVol": 2_01_615,
}
}
SYMBOLS_OF_INTEREST = list(SYMBOL_TO_PRICE_MAP.keys())
INSTRUMENT_TOKENS = []
INSTRUMENT_LOOKUP = {}
SYMBOL_TO_INSTRUMENT_TOKEN_MAP = {}
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
tg_update = False
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
# @sio.event
async def before_connect(sid, environ):
print("CONNECTION REQUEST!")
print("SID:", sid)
print("ENV:", json.to_string(environ, default = str))
return True # Allow connection
# ---------------------------------------------------------------------------------------------------------------------
@sio.event(namespace = "/market")
async def connect(sid, environ):
print(f"Client {sid} connected")
print("ENV:", json.to_string(environ, default = str))
if tg_update:
async with httpx.AsyncClient() as client:
try: await client.post(
url = r"https://api.thecaoffice.com/converse/tech/alert/chat/backend",
json = {
"type": "info",
"chatClient": "telegram",
"chatId": "-4206946032",
# "chatId": "1275560043",
"message": f"*SocketIO Connected!*\n👍 SID: {sid}"
}
)
except: pass
# ---------------------------------------------------------------------------------------------------------------------
@sio.event(namespace = "/market")
async def disconnect(sid):
print(f"Client {sid} disconnected")
if tg_update:
async with httpx.AsyncClient() as client:
try: await client.post(
url = r"https://api.thecaoffice.com/converse/tech/alert/chat/backend",
json = {
"type": "info",
"chatClient": "telegram",
"chatId": "-4206946032",
# "chatId": "1275560043",
"message": f"*SocketIO Disconnected!*\n❌ SID: {sid}"
}
)
except: pass
def round_tick(price):
return round(price * 20) / 20
# ---------------------------------------------------------------------------------------------------------------------
def simulate_one_stock(symbol, price):
global SYMBOL_TO_PRICE_MAP
# Simulate a change in the price:
pos_bias = [1] * 10
no_bias = [0] * 1
neg_bias = [-1] * 10
bias = random.choice(pos_bias + no_bias + neg_bias)
change_factor = random.random() / 100.0
change = price * change_factor * bias
ltp = round_tick(price + change)
# Simulate the volume.
# Assume a trade qty. worth 1L to 10L rupees:
traded_amt = random.uniform(1_00_000, 10_00_000)
ltq = int(traded_amt / price)
SYMBOL_TO_PRICE_MAP[symbol]["totVol"] += ltq
if bias >= 0: SYMBOL_TO_PRICE_MAP[symbol]["buyVol"] += ltq
else: SYMBOL_TO_PRICE_MAP[symbol]["sellVol"] += ltq
# Create the basic JSON payload:
instrument_token = SYMBOL_TO_INSTRUMENT_TOKEN_MAP[symbol]
stock_json = {
"tradable": True,
"symbol": symbol,
"instrument_token": instrument_token,
"last_traded_quantity": ltq,
"average_traded_price": round_tick(price + (bias * price * (random.random() / 100.0))),
"volume_traded": SYMBOL_TO_PRICE_MAP[symbol]["totVol"],
"total_buy_quantity": SYMBOL_TO_PRICE_MAP[symbol]["buyVol"],
"total_sell_quantity": SYMBOL_TO_PRICE_MAP[symbol]["sellVol"],
"last_price": ltp,
"ohlc": {
"open": round_tick(price + (price * 0.005)),
"high": round_tick(price + (price * 0.015)),
"low": round_tick(price - (price * 0.015)),
"close": ltp
},
"change": ((ltp - SYMBOL_TO_PRICE_MAP[symbol]["prevClose"]) / SYMBOL_TO_PRICE_MAP[symbol]["prevClose"]) * 100,
"last_trade_time": (datetime.datetime.now() - datetime.timedelta(seconds = random.uniform(0.0, 2.5))).strftime("%Y-%m-%d %H:%M:%S"),
"oi": 0,
"oi_day_high": 0,
"oi_day_low": 0,
"exchange_timestamp": datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
"depth": {
"buy": [
{
"quantity": random.randint(0, 100),
"price": round(ltp - 0.05, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp - 0.10, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp - 0.15, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp - 0.20, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp - 0.25, 2),
"orders": random.randint(0, 10)
}
],
"sell": [
{
"quantity": random.randint(0, 100),
"price": round(ltp + 0.05, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp + 0.10, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp + 0.15, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp + 0.20, 2),
"orders": random.randint(0, 10)
},
{
"quantity": random.randint(0, 100),
"price": round(ltp + 0.25, 2),
"orders": random.randint(0, 10)
}
]
}
}
# Done here:
return stock_json
# ---------------------------------------------------------------------------------------------------------------------
def simulate_ticks_once():
# Pick a no. of stocks to simulate:
count = random.randint(1, len(SYMBOL_TO_PRICE_MAP))
symbols = random.sample(list(SYMBOL_TO_PRICE_MAP.keys()), count)
# Create the tick JSON:
tick_json = [
simulate_one_stock(
symbol = symbol,
price = SYMBOL_TO_PRICE_MAP[symbol]["ltp"]
) for symbol in symbols
]
# Done here:
return tick_json
# ---------------------------------------------------------------------------------------------------------------------
async def broadcast_random_data():
while True:
simulated_ticks = TradingTick.from_zerodha_kite(
simulate_ticks_once(),
instrument_lookup = INSTRUMENT_LOOKUP
)
for tick in simulated_ticks: await sio.emit("ticks", tick.summary, namespace = "/market")
await asyncio.sleep(random.uniform(0.15, 1.0))
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
# Load the instrument lookup:
print("Loading instruments.")
zerodha_instruments = json.from_file(r"/home/developer/Downloads/zerodha_kite_instruments_20241225.json")
print("Parsing instruments.")
for i in zerodha_instruments:
symbol = TradingSymbol.from_zerodha_kite(i)
if symbol.symbol in SYMBOLS_OF_INTEREST:
INSTRUMENT_TOKENS.append(symbol.brokerToken)
INSTRUMENT_LOOKUP[symbol.brokerToken] = symbol.model_dump()
SYMBOL_TO_INSTRUMENT_TOKEN_MAP[symbol.symbol] = symbol.brokerToken
print("Instruments ready.")
async def server():
# Start broadcasting random data in the background
asyncio.create_task(broadcast_random_data())
# Run the web server
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, "0.0.0.0", 5214)
await site.start()
# Keep the server running
while True:
await asyncio.sleep(3600)
asyncio.run(server())
+260
View File
@@ -0,0 +1,260 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Tuesday, 24th Dec. 2024
OBJECTIVE:
To get live updates from Zerodha and push them to Kafka.
REFERENCES:
N01. YouTube Webinar: https://www.youtube.com/watch?v=9vzd289Eedk
02. Official Example (GitHub): https://github.com/zerodha/pykiteconnect/blob/master/examples/threaded_ticker.py
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# System-level activities:
import io
import os
# My utils:
from utils_v2.string import json
from utils_v2.system import files
from utils_v2.queue.kafka.controllers.kafka import ProducerKafka
# To make HTTP calls:
import httpx
# To work with date and time:
import datetime
import time
# Models:
from models.finstitutions.trading.symbols import TradingSymbol
from models.finstitutions.trading.ticks import TradingTick
# To work with Zerodha's Kite platform:
from kiteconnect import KiteConnect, KiteTicker
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# For Zerodha and related to ticks:
INSTRUMENT_TOKENS = []
INSTRUMENT_LOOKUP = {}
# For Kafka:
cwd = files.get_cwd()
parent_dir = cwd
kafka_producer = ProducerKafka(
topic = "tickers",
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 = "/etc/ssl/dbu/ca.pem",
# cert_file = "/etc/ssl/dbu/fullchain.pem",
# key_file = "/etc/ssl/dbu/privkey.pem"
),
debug = False
)
# For metrics:
tick_count = 0
ticks_since_flush = 0
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
def flush_kafka():
print("FLUSHING!")
kafka_producer.flush()
def to_kafka(tick: TradingTick) -> bool:
global tick_count
global ticks_since_flush
tick_count += 1
ticks_since_flush += 1
if ticks_since_flush >= 50_000:
flush_kafka()
ticks_since_flush = 0
success = False
summary = tick.summary
# print(json.to_string(summary, default=str))
# print(json.to_string(tick.model_dump(), default=str))
summary["messageType"] = "ticks"
summary = json.from_string(json.to_string(summary, default=str))
success = kafka_producer.produce(value = summary)
if not success:
print("ERROR ON TICK NO.:", tick_count)
flush_kafka()
return success
# ---------------------------------------------------------------------------------------------------------------------
def on_connect(ws, response):
print("\n\n")
print("ON CONNECT:")
print("Successfully connected. Response: {}".format(response))
ws.subscribe(INSTRUMENT_TOKENS)
ws.set_mode(ws.MODE_FULL, INSTRUMENT_TOKENS)
print(f"Subscribed to {len(INSTRUMENT_TOKENS):,} tokens in 'Full' mode.")
print("\n\n")
# ---------------------------------------------------------------------------------------------------------------------
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): <6,} | PRODUCED: {success: <6,} | TOTAL: {tick_count: >10,}{' | FAILURE(S)!' if success < len(results)else ''}")
# ---------------------------------------------------------------------------------------------------------------------
def main():
# Global vars:
global INSTRUMENT_TOKENS
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"))
api_key = creds["apiKey"]
access_token = creds["accessToken"]
# Get the instruments of interest:
response = httpx.post(url = r"https://api.thecaoffice.com/markets/watchlist/distincts")
instruments_of_interest = response.json()["data"]["rs0"]
symbols_of_interest = []
broker_tokens_of_interest = []
for i in instruments_of_interest:
symbol = i["symbol"]
broker_token = i["broker_token"]
if broker_token is not None and i["source"] == "zerodha":
symbols_of_interest.append(symbol)
broker_tokens_of_interest.append(broker_token)
print("TOTAL INSTR. OF INTEREST:", f"{len(broker_tokens_of_interest)}/{len(instruments_of_interest)}")
print(broker_tokens_of_interest)
# Create an instance of Zerodha's Kite connection:
kite = KiteConnect(api_key = api_key)
kite.set_access_token(access_token)
# Get the entire list of instruments:
instruments = []
instruments += kite.instruments(exchange = "NSE")
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")
# Pick the instruments of interest:
# instruments = [TradingSymbol.from_zerodha_kite(i) for i in instruments[:1000]]
instruments = [
TradingSymbol.from_zerodha_kite(i) for i in instruments
if str(i["instrument_token"]) in broker_tokens_of_interest
]
# instruments = [
# TradingSymbol.from_zerodha_kite(i) for i in instruments
# if i["instrument_token"] in [109760007]
# ]
print("SELECTED INSTRUMENTS:", len(instruments))
# Create the lookup:
for i in instruments:
INSTRUMENT_TOKENS.append(i.brokerToken)
INSTRUMENT_LOOKUP[i.brokerToken] = i.model_dump()
print("LOOK-UP READY!")
# Start the websocket with Zerodha:
kite_ws = KiteTicker(
api_key = api_key,
access_token = access_token
)
# Assign the callbacks:
kite_ws.on_connect = on_connect
kite_ws.on_ticks = on_ticks
# If you choose to go threaded, you will need to work purely with callbacks.
# You will need to have an infinite loop in the main thread.
print("STARTING WS...")
kite_ws.connect(threaded = True)
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
main()
while True: time.sleep(3_600.00)