61 lines
1.8 KiB
Python
61 lines
1.8 KiB
Python
import asyncio
|
|
import json
|
|
import math
|
|
import pandas as pd
|
|
import os
|
|
from datetime import datetime, timezone
|
|
import websockets
|
|
import numpy as np
|
|
import talib
|
|
import requests
|
|
|
|
WSS_URL = "wss://ws-live-data.polymarket.com"
|
|
|
|
HIST_TRADES = np.empty((0, 2))
|
|
|
|
|
|
async def rtds_stream():
|
|
global HIST_TRADES
|
|
|
|
async with websockets.connect(WSS_URL) as websocket:
|
|
print(f"Connected to {WSS_URL}")
|
|
|
|
subscribe_msg = {
|
|
"action": "subscribe",
|
|
"subscriptions": [
|
|
{
|
|
"topic": "crypto_prices_chainlink",
|
|
"type": "*",
|
|
"filters": "{\"symbol\":\"btc/usd\"}"
|
|
}
|
|
]
|
|
}
|
|
|
|
await websocket.send(json.dumps(subscribe_msg))
|
|
|
|
try:
|
|
async for message in websocket:
|
|
if isinstance(message, str):
|
|
try:
|
|
data = json.loads(message)
|
|
if data['payload'].get('value', None) is not None:
|
|
print(f'🤑 BTC Chainlink Last Px: {data['payload']['value']:_.4f}; TS: {pd.to_datetime(data['timestamp'], unit='ms')}')
|
|
else:
|
|
print(f'Initial or unexpected data struct, skipping: {data}')
|
|
continue
|
|
except (json.JSONDecodeError, ValueError):
|
|
print(f'Message not in JSON format, skipping: {message}')
|
|
continue
|
|
else:
|
|
raise ValueError(f'Type: {type(data)} not expected: {message}')
|
|
|
|
|
|
except websockets.ConnectionClosed:
|
|
print("Connection closed by server.")
|
|
|
|
|
|
if __name__ == '__main__':
|
|
try:
|
|
asyncio.run(rtds_stream())
|
|
except KeyboardInterrupt:
|
|
print("Stream stopped.") |