(20241221) Zerodha Auth Ready. Users can now integrate Kite.

This commit is contained in:
2024-12-21 15:22:55 +05:30
parent 8904ea394d
commit 1249841b05
15 changed files with 568 additions and 464 deletions
+251 -104
View File
@@ -6,16 +6,15 @@
DATE:
Create: Saturday, 18th May, 2022
Update: Thursday, 22nd Aug. 2024
Saturday, 21st Dec. 2024
OBJECTIVE:
To provide an easy way to work with '.json' data and files.
To simulate stock market updates to test on SocketIO.
REFERENCES:
1) https://www.w3schools.com/python/python_json.asp
N/A
DOWNLOADS:
@@ -38,12 +37,27 @@ sys.path.append("..")
# System-level activities:
import io
import os
# To work with the JSON standard:
import json
# My utils:
from utils_v2.string import json
# To work with files:
from utils_v2.system import files
# 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
# *****************************************************************************************************************
@@ -53,7 +67,64 @@ from utils_v2.system import files
# *****************************************************************************************************************
# --- Nothing Yet
# 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,
}
}
# *****************************************************************************************************************
@@ -73,121 +144,181 @@ from utils_v2.system import files
# *****************************************************************************************************************
def from_string(json_data):
"""
Decodes a JSON string to a pythonic variable like a dict.
:param json_data: The JSON string to decode.
:return: The decoded pythonic variable.
"""
python_data = json.loads(json_data)
return python_data
# ---------------------------------------------------------------------------------------------------------------------
def to_string(
python_data,
indent = 4,
default = None,
separators = None,
no_space = False
):
"""
Converts the given pythonic data to a JSON string.
:param python_data: The input data like a dict.
:param indent: The tab-width for pretty presentation.
:param default: The function to use on something that cannot be directly parsed into a JSON string.
:param separators: Custom separators to use.
:param no_space: If you want a dense JSON string that saves memory by not using spaces or tabs or line-breaks. Not
good for human readability, very good for saving memory. WARNING: THIS OVERRIDES EVERY OTHER PARAMETER EXCEPT
'default'.
:return: The JSON string representation of the input pythonic data.
"""
if no_space:
json_data = json.dumps(
python_data,
default = default,
separators = (',', ':')
@sio.event
async def connect(sid, environ):
print(f"Client {sid} connected")
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
else:
json_data = json.dumps(
python_data,
indent = indent,
default = default,
separators = separators
# ---------------------------------------------------------------------------------------------------------------------
@sio.event
async def disconnect(sid):
print(f"Client {sid} disconnected")
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
return json_data
def round_tick(price):
return round(price * 20) / 20
# ---------------------------------------------------------------------------------------------------------------------
def from_file(file):
def simulate_one_stock(symbol, price):
"""
Reads a JSON file and returns it as a pythonic variable like a dict.
:param file: The path to the file on the disk or a file held in RAM as a BytesIO object.
:return: The decoded pythonic variable.
"""
global SYMBOL_TO_PRICE_MAP
if isinstance(file, io.BytesIO):
file.seek(0)
json_data = file.getvalue()
else: json_data = files.read_file(file)
python_data = from_string(json_data)
return python_data
# 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 to_file(
file,
python_data,
indent = 4,
default = None,
separators = None,
no_space = False
):
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)
:param file: Either a path to a file on disk, or a buffer in RAM in the form of a BytesIO object.
:param python_data: The pythonic data to be converted to the JSON string.
:param indent: The tab-width for pretty presentation.
:param default: The function to use on something that cannot be directly parsed into a JSON string.
:param separators: Custom separators to use.
:param no_space: If you want a dense JSON string that saves memory by not using spaces or tabs or line-breaks. Not
good for human readability, very good for saving memory. WARNING: THIS OVERRIDES EVERY OTHER PARAMETER EXCEPT
'default'.
:return: True/False if a path was given, else the same BytesIO object with the written JSON data.
"""
# Create the tick JSON:
tick_json = [
simulate_one_stock(
symbol = symbol,
price = SYMBOL_TO_PRICE_MAP[symbol]["ltp"]
) for symbol in symbols
]
json_data = to_string(
python_data,
indent = indent,
default = default,
separators = separators,
no_space = no_space
)
# Done here:
return tick_json
if isinstance(file, io.BytesIO):
file.write(json_data.encode("utf-8"))
file.seek(0)
return file
else:
try:
files.write_file(file, json_data, mode = "w")
return True
except: return False
# ---------------------------------------------------------------------------------------------------------------------
async def broadcast_random_data():
while True:
await sio.emit("ticks", simulate_ticks_once())
await asyncio.sleep(random.uniform(0.15, 1.0))
# *****************************************************************************************************************
@@ -199,4 +330,20 @@ def to_file(
if __name__ == "__main__":
pass
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", 5000)
print("Server running on http://0.0.0.0:5000")
await site.start()
# Keep the server running
while True:
await asyncio.sleep(3600)
asyncio.run(server())