import random
import json
import time
import asyncio
import nats
SYMBOLS = [
"RELIANCE",
"TCS",
"INFY",
"HDFCBANK",
"ICICIBANK",
"SBIN",
"HDFC",
"BAJFINANCE",
"NSE_INR_BANKNIFTY_INDEX",
"LT",
]
NATS_SERVER = "nats://localhost:4222"
SUBJECT = "marketdepth.data"
RATE = 20000.0
def generate_levels(count, base_price):
levels = []
for i in range(count):
price = round((base_price + random.uniform(-10, 10) + (i * 0.1)), 2)
quantity = random.randint(1, 100)
levels.append([price, quantity])
return levels
async def publish_messages():
nc = await nats.connect(NATS_SERVER)
print(f"Connected to NATS server at {NATS_SERVER}")
delay = 1.0 / RATE
while True:
symbol = random.choice(SYMBOLS)
current_time = int(time.time() * 1000)
depth = random.randint(1, 20)
market_data = {
"exch_timestamp": current_time,
"unified_symbol": symbol,
"bids": generate_levels(depth, 100.0),
"asks": generate_levels(depth, 101.0),
}
message = json.dumps(market_data).encode()
await nc.publish(SUBJECT, message)
await asyncio.sleep(delay)
async def main():
await publish_messages()
if __name__ == "__main__":
asyncio.run(main())