Files
ForexBot2/ws_twelvedata.py
T
Harwinder 3a09dfd62f Strip backtesting, rebalance indicator weights toward leading signals
Remove backtest.py, backtest_gpu.py, metrics.py, local CSV data,
Dukascopy loader, and all backtest CLI modes from main.py. Rebalance
engine weights: leading indicators (divergence, RSI, stochastic, BB)
now carry 66% of signal weight vs 34% for lagging (EMA, MACD). Add
ws_twelvedata.py for live tick streaming. Drop pyarrow and tqdm deps.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-10 23:40:15 -04:00

56 lines
1.5 KiB
Python

"""Live tick stream from Twelve Data WebSocket API."""
import json
import asyncio
import websockets
from datetime import datetime, timezone
API_KEY = "f563b22ffdfd4924955decbe1a8c8701"
WS_URL = f"wss://ws.twelvedata.com/v1/quotes/price?apikey={API_KEY}"
SYMBOLS = ["EUR/USD", "GBP/USD", "USD/JPY"]
async def stream_ticks():
async with websockets.connect(WS_URL) as ws:
subscribe = {
"action": "subscribe",
"params": {
"symbols": ",".join(SYMBOLS),
},
}
await ws.send(json.dumps(subscribe))
print(f"Subscribed to: {', '.join(SYMBOLS)}")
print("-" * 60)
async for raw in ws:
msg = json.loads(raw)
if msg.get("event") == "subscribe-status":
print(f"Status: {msg.get('status')} — {msg.get('message', '')}")
continue
if msg.get("event") == "heartbeat":
continue
if msg.get("event") == "price":
ts = datetime.fromtimestamp(msg["timestamp"], tz=timezone.utc)
symbol = msg["symbol"]
price = msg["price"]
print(f"{ts:%H:%M:%S} | {symbol:<8} | {price}")
async def main():
print(f"Connecting to Twelve Data WebSocket...")
print(f"API key: ...{API_KEY[-6:]}")
try:
await stream_ticks()
except KeyboardInterrupt:
print("\nStopped.")
except websockets.exceptions.ConnectionClosed as e:
print(f"\nConnection closed: {e}")
if __name__ == "__main__":
asyncio.run(main())