"""Live prices, candles and account updates from your hosted MT5, printed as they arrive.

    pip install websockets
    python stream_prices.py 1 EURUSD XAUUSD"""
import os, sys, time
from hostmt5 import HostMT5

acc = HostMT5(os.environ['HOSTMT5_KEY']).account(int(sys.argv[1]))
symbols = sys.argv[2:] or ['EURUSD']
for ev in acc.stream(symbols=symbols, candles=[(symbols[0], 'M1')]):
    t = ev['type']
    if t == 'quote': print(f"{time.strftime('%H:%M:%S')} {ev['symbol']:<10} bid {ev['bid']}  ask {ev['ask']}")
    elif t == 'candle' and ev['closed']: print('   bar closed', ev['symbol'], ev['timeframe'], ev['bar'])
    elif t == 'account': print(f"   equity {ev['equity']} {ev['currency']}  profit {ev['profit']}")
    elif t == 'positions': print(f"   {len(ev['positions'])} open position(s)")
    elif t in ('error', 'status'): print('  ', t, ev.get('message'))
