-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstock_example.py
More file actions
122 lines (95 loc) · 3.16 KB
/
stock_example.py
File metadata and controls
122 lines (95 loc) · 3.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
FCS Real-time Stock Example - Backend
Pure Python WebSocket client for real-time stock prices.
No browser required - runs directly in terminal.
Install: pip install websocket-client
Run: python stock_example.py
"""
import sys
import os
# Add parent directory to path for import
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from fcs_client_lib import FCSClient
# Configuration
API_KEY = 'fcs_socket_demo' # Replace with your API key
SYMBOLS = ['NASDAQ:AAPL', 'NASDAQ:GOOGL', 'NASDAQ:MSFT', 'NYSE:TSLA']
TIMEFRAME = '1D'
# Create client
client = FCSClient(API_KEY)
# Store prices
prices = {}
@client.on_connected
def on_connected():
print('\n' + '=' * 60)
print(' FCS Real-time Stock Prices - Backend Example')
print('=' * 60)
print(f' API Key: {API_KEY}')
print(f' Symbols: {", ".join(SYMBOLS)}')
print(f' Timeframe: {TIMEFRAME}')
print('=' * 60 + '\n')
# Subscribe to symbols
for symbol in SYMBOLS:
client.join(symbol, TIMEFRAME)
@client.on_message
def on_message(data):
if data.get('type') == 'price' and data.get('prices'):
symbol = data.get('symbol', '')
p = data['prices']
mode = p.get('mode', '')
if mode in ['initial', 'candle']:
prices[symbol] = {
'open': p.get('o'),
'high': p.get('h'),
'low': p.get('l'),
'close': p.get('c'),
'volume': p.get('v'),
'ask': p.get('a'),
'bid': p.get('b')
}
print_price(symbol, prices[symbol])
elif mode == 'askbid':
if symbol in prices:
prices[symbol]['ask'] = p.get('a')
prices[symbol]['bid'] = p.get('b')
prices[symbol]['close'] = p.get('c')
print(f" {symbol}: Ask=${p.get('a')} Bid=${p.get('b')}")
def print_price(symbol, p):
"""Pretty print stock price data."""
parts = symbol.split(':')
exchange = parts[0] if len(parts) > 1 else ''
ticker = parts[1] if len(parts) > 1 else symbol
print(f"\n [{ticker}] ({exchange})")
print(f" Price: ${p['close']}")
print(f" O: ${p['open']} | H: ${p['high']} | L: ${p['low']} | C: ${p['close']}")
if p.get('volume'):
vol = format_volume(p['volume'])
print(f" Volume: {vol}")
if p.get('ask') and p.get('bid'):
print(f" Ask: ${p['ask']} | Bid: ${p['bid']}")
def format_volume(vol):
"""Format volume with K/M suffix."""
try:
v = float(vol)
if v >= 1000000:
return f"{v/1000000:.2f}M"
elif v >= 1000:
return f"{v/1000:.2f}K"
return f"{v:.0f}"
except:
return str(vol)
@client.on_close
def on_close(code, msg):
print(f'\n[!] Connection closed: {code} - {msg}')
@client.on_error
def on_error(error):
print(f'\n[!] Error: {error}')
if __name__ == '__main__':
print('\nConnecting to FCS WebSocket...')
print('Press Ctrl+C to stop\n')
try:
client.connect()
client.run_forever()
except KeyboardInterrupt:
print('\n\nDisconnecting...')
client.disconnect()
print('Goodbye!')