Resetting utils subtree.
This commit is contained in:
@@ -1,135 +0,0 @@
|
||||
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 = "kft_file_upload"
|
||||
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,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())
|
||||
@@ -152,10 +152,23 @@ def main():
|
||||
kite.set_access_token(access_token)
|
||||
|
||||
# Get a list of instruments to work with:
|
||||
instruments = kite.instruments(exchange = "MCX")
|
||||
instruments = kite.instruments(exchange = "NSE")
|
||||
instruments = instruments[:100]
|
||||
instruments = [TradingSymbol.from_zerodha_kite(i) for i in instruments]
|
||||
|
||||
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")
|
||||
|
||||
print("INSTRUMENTS:", len(instruments_csv))
|
||||
json.to_file(r"/home/developer/Downloads/zerodha_kite_instruments.json", instruments_csv, default = str)
|
||||
while True: pass
|
||||
|
||||
# Create the lookup:
|
||||
for i in instruments:
|
||||
INSTRUMENT_TOKENS.append(i.brokerToken)
|
||||
|
||||
Reference in New Issue
Block a user