added extend symbol change for ws
This commit is contained in:
@@ -29,68 +29,88 @@ USE_DB: bool = True
|
||||
USE_VK: bool = True
|
||||
|
||||
VK_LAST_TRADE = 'fut_last_trade_extended'
|
||||
CON: AsyncContextManager | None = None
|
||||
VAL_KEY = None
|
||||
CON: AsyncContextManager
|
||||
VAL_KEY: valkey.Valkey
|
||||
|
||||
### Logging ###
|
||||
load_dotenv()
|
||||
LOG_FILEPATH: str = os.getenv("LOGS_PATH") + '/Fund_Rate_Extended_Trades.log'
|
||||
LOG_FILEPATH: str = f'{os.getenv("LOGS_PATH")}/Fund_Rate_Extended_Trades.log'
|
||||
|
||||
### CONSTANTS ###
|
||||
WS_SYMBOL: str = 'ETH-USD'
|
||||
SYMBOL: str = 'ETH-USD'
|
||||
|
||||
### Globals ###
|
||||
WSS_URL = f"wss://api.starknet.extended.exchange/stream.extended.exchange/v1/publicTrades/{WS_SYMBOL}"
|
||||
ALLOW_SYMBOL_CHG: bool = False
|
||||
|
||||
|
||||
### Websocket ###
|
||||
async def ws_stream():
|
||||
async for websocket in websockets.connect(WSS_URL):
|
||||
logging.info(f"Connected to {WSS_URL}")
|
||||
try:
|
||||
async for message in websocket:
|
||||
ts_arrival = round(datetime.now().timestamp()*1000)
|
||||
if isinstance(message, str):
|
||||
try:
|
||||
data = json.loads(message)
|
||||
if data.get('data', None) is not None:
|
||||
if data['seq'] == 1: # Skip first msg that has historical trades
|
||||
global SYMBOL
|
||||
|
||||
while True:
|
||||
CHANGE_SYMBOL: bool = False
|
||||
WSS_URL = f"wss://api.starknet.extended.exchange/stream.extended.exchange/v1/publicTrades/{SYMBOL}"
|
||||
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:
|
||||
best_symbol_by_exchange: dict = json.loads(s=VAL_KEY.get(name='fr_algo_working_symbol')) # ty:ignore[invalid-argument-type]
|
||||
best_symbol: str = f'{best_symbol_by_exchange['EXTEND']['lh_asset']}-{best_symbol_by_exchange['EXTEND']['rh_asset']}'
|
||||
if best_symbol != SYMBOL:
|
||||
logging.info(f'Symbol Change: {SYMBOL} -> {best_symbol}')
|
||||
SYMBOL = best_symbol
|
||||
CHANGE_SYMBOL = True
|
||||
await websocket.close()
|
||||
break
|
||||
|
||||
ts_arrival = round(datetime.now().timestamp()*1000)
|
||||
if isinstance(message, str):
|
||||
try:
|
||||
data = json.loads(message)
|
||||
if data.get('data', None) is not None:
|
||||
# print(data)
|
||||
if data['seq'] == 1: # Skip first msg that has historical trades
|
||||
continue
|
||||
list_for_df = []
|
||||
for t in data['data']:
|
||||
trade_obj = {
|
||||
'sequence_id': data['seq'],
|
||||
'timestamp_arrival': ts_arrival,
|
||||
'timestamp_msg': data['ts'],
|
||||
'timestamp_trade': t['T'],
|
||||
'symbol': t['m'],
|
||||
'side_taker': t['S'],
|
||||
'trade_type': t['tT'],
|
||||
'price': float(t['p']),
|
||||
'qty': float(t['q']),
|
||||
'trade_id': t['i'],
|
||||
'is_buyer_mkt_maker': True if t['S']=='SELL' else False,
|
||||
}
|
||||
list_for_df.append(trade_obj)
|
||||
# VAL_KEY.set(VK_LAST_TRADE, json.dumps(trade_obj))
|
||||
if USE_DB:
|
||||
await db.insert_df_to_mysql(table_name='fr_extended_mkt_trades', params=list_for_df, CON=CON)
|
||||
pass
|
||||
continue
|
||||
list_for_df = []
|
||||
for t in data['data']:
|
||||
trade_obj = {
|
||||
'sequence_id': data['seq'],
|
||||
'timestamp_arrival': ts_arrival,
|
||||
'timestamp_msg': data['ts'],
|
||||
'timestamp_trade': t['T'],
|
||||
'symbol': t['m'],
|
||||
'side_taker': t['S'],
|
||||
'trade_type': t['tT'],
|
||||
'price': float(t['p']),
|
||||
'qty': float(t['q']),
|
||||
'trade_id': t['i'],
|
||||
'is_buyer_mkt_maker': True if t['S']=='SELL' else False,
|
||||
}
|
||||
list_for_df.append(trade_obj)
|
||||
# VAL_KEY.set(VK_LAST_TRADE, json.dumps(trade_obj))
|
||||
if USE_DB:
|
||||
await db.insert_df_to_mysql(table_name='fr_extended_mkt_trades', params=list_for_df, CON=CON)
|
||||
pass
|
||||
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}')
|
||||
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}')
|
||||
continue
|
||||
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())
|
||||
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())
|
||||
|
||||
|
||||
async def main():
|
||||
@@ -100,8 +120,8 @@ async def main():
|
||||
if USE_VK:
|
||||
VAL_KEY = valkey.Valkey(host='localhost', port=6379, db=0)
|
||||
else:
|
||||
VAL_KEY = None
|
||||
logging.warning("VALKEY NOT BEING USED, NO DATA WILL BE PUBLISHED")
|
||||
raise NotImplementedError('Cannot run without VK')
|
||||
|
||||
if USE_DB:
|
||||
engine = create_async_engine('mysql+asyncmy://root:pwd@localhost/fund_rate')
|
||||
@@ -109,9 +129,9 @@ async def main():
|
||||
await extended_db.create_fr_extended_mkt_trades(CON=CON)
|
||||
await ws_stream()
|
||||
else:
|
||||
CON = None
|
||||
logging.warning("DATABASE NOT BEING USED, NO DATA WILL BE RECORDED")
|
||||
await ws_stream()
|
||||
raise NotImplementedError('Cannot run without DB')
|
||||
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
Reference in New Issue
Block a user