Files
Funding_Rate/ws_extended_orderbook.py

144 lines
5.3 KiB
Python
Raw Normal View History

2026-04-22 05:24:40 +00:00
import asyncio
import json
import logging
import socket
import traceback
from datetime import datetime
from typing import AsyncContextManager
import numpy as np
import pandas as pd
import requests.packages.urllib3.util.connection as urllib3_cn # type: ignore
from sqlalchemy import text
import websockets
from sqlalchemy.ext.asyncio import create_async_engine
import valkey
import os
from dotenv import load_dotenv
### Allow only ipv4 ###
def allowed_gai_family():
return socket.AF_INET
urllib3_cn.allowed_gai_family = allowed_gai_family
### Database ###
USE_DB: bool = False
USE_VK: bool = True
VK_TICKER = 'fut_ticker_extended'
2026-04-30 04:32:49 +00:00
CON: AsyncContextManager
VAL_KEY: valkey.Valkey
2026-04-22 05:24:40 +00:00
### Logging ###
load_dotenv()
2026-04-30 04:32:49 +00:00
LOG_FILEPATH: str = f'{os.getenv("LOGS_PATH")}/Fund_Rate_Extended_OB.log'
2026-04-22 05:24:40 +00:00
### CONSTANTS ###
2026-04-30 04:32:49 +00:00
SYMBOL: str = 'ETH-USD'
2026-04-22 05:24:40 +00:00
### Globals ###
ALLOW_SYMBOL_CHG: bool = True
2026-04-22 05:24:40 +00:00
### Websocket ###
async def ws_stream():
2026-04-30 04:32:49 +00:00
global SYMBOL
while True:
CHANGE_SYMBOL = False
WSS_URL = f"wss://api.starknet.extended.exchange/stream.extended.exchange/v1/orderbooks/{SYMBOL}?depth=1"
async for websocket in websockets.connect(WSS_URL):
if CHANGE_SYMBOL:
break
logging.info(f"Connected to {WSS_URL}")
try:
async for message in websocket:
### Update Symbol if Algo Outputs Change ###
if ALLOW_SYMBOL_CHG:
vk_get: str = VAL_KEY.get(name='fr_algo_working_symbol') # ty:ignore[invalid-assignment]
if vk_get:
best_symbol_by_exchange: dict = json.loads(s=vk_get)
best_symbol: str = best_symbol_by_exchange['EXTEND']['symbol']
if best_symbol != SYMBOL:
logging.info(f'Symbol Change: {SYMBOL} -> {best_symbol}')
SYMBOL = best_symbol
CHANGE_SYMBOL = True
await websocket.close()
break
else:
logging.warning('Extend Orderbook WS: "fr_algo_working_symbol" is None; not switching to new symbol...')
2026-04-30 04:32:49 +00:00
ts_arrival = round(datetime.now().timestamp()*1000)
if isinstance(message, str):
try:
data = json.loads(message)
if data.get('type', None) is not None:
# print(f'OB: {data}')
VAL_KEY_OBJ = json.dumps({
'sequence_id': data['seq'],
'timestamp_arrival': ts_arrival,
'timestamp_msg': data['ts'],
'symbol': data['data']['m'],
'best_bid_px': float(data['data']['b'][0]['p']),
'best_bid_qty': float(data['data']['b'][0]['q']),
'best_ask_px': float(data['data']['a'][0]['p']),
'best_ask_qty': float(data['data']['a'][0]['q']),
})
VAL_KEY.set(VK_TICKER, VAL_KEY_OBJ)
continue
else:
logging.info(f'Initial or unexpected data struct, skipping: {data}')
continue
except (json.JSONDecodeError, ValueError):
logging.warning(f'Message not in JSON format, skipping: {message}')
2026-04-22 05:24:40 +00:00
continue
2026-04-30 04:32:49 +00:00
else:
raise ValueError(f'Type: {type(data)} not expected: {message}')
except websockets.ConnectionClosed as e:
logging.error(f'Connection closed: {e}')
logging.error(traceback.format_exc())
continue
except Exception as e:
logging.error(f'Connection closed: {e}')
logging.error(traceback.format_exc())
2026-04-22 05:24:40 +00:00
async def main():
global VAL_KEY
global CON
if USE_VK:
VAL_KEY = valkey.Valkey(host='localhost', port=6379, db=0)
else:
logging.warning("VALKEY NOT BEING USED, NO DATA WILL BE PUBLISHED")
2026-04-30 04:32:49 +00:00
raise NotImplementedError('Cannot run without VK')
2026-04-22 05:24:40 +00:00
if USE_DB:
2026-04-30 04:32:49 +00:00
raise NotImplementedError('DB not implemented')
# engine = create_async_engine('mysql+asyncmy://root:pwd@localhost/fund_rate')
# async with engine.connect() as CON:
# # await create_rtds_btcusd_table(CON=CON)
# await ws_stream()
2026-04-22 05:24:40 +00:00
else:
logging.warning("DATABASE NOT BEING USED, NO DATA WILL BE RECORDED")
await ws_stream()
if __name__ == '__main__':
START_TIME = round(datetime.now().timestamp()*1000)
logging.info(f'Log FilePath: {LOG_FILEPATH}')
logging.basicConfig(
force=True,
filename=LOG_FILEPATH,
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
filemode='w'
)
logging.info(f"STARTED: {START_TIME}")
try:
asyncio.run(main())
except KeyboardInterrupt:
logging.info("Stream stopped")