-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscriber_client.py
60 lines (48 loc) · 1.91 KB
/
subscriber_client.py
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
import grpc
import ticker_service_pb2
import ticker_service_pb2_grpc
import random # For random price generation
def get_tickers(stub):
"""Get available tickers from the server."""
request = ticker_service_pb2.TickerRequest()
response = stub.GetTickers(request)
print("Available Tickers:")
for ticker in response.tickers:
print(f"{ticker.symbol}: {ticker.name}")
return response.tickers
def stream_market_data(stub, ticker_symbol):
"""Stream market data for a specific ticker."""
request = ticker_service_pb2.TickerRequest(ticker_symbol=ticker_symbol)
print("subscriber connected")
print(request)
for market_data in stub.ConnectToMarketData(request):
print(f"Market Data for {market_data.ticker_symbol}: Bid {market_data.best_bid_price} Ask {market_data.best_ask_price}")
def submit_limit_order(stub, ticker_symbol, side, price, quantity):
"""Submit a limit order."""
request = ticker_service_pb2.LimitOrderRequest(
ticker_symbol=ticker_symbol,
side=side,
price=price,
quantity=quantity
)
response = stub.SubmitLimitOrder(request)
print(f"Limit Order Submitted. Order ID: {response.order_id}")
def submit_market_order(stub, ticker_symbol, side, quantity):
"""Submit a market order."""
request = ticker_service_pb2.MarketOrderRequest(
ticker_symbol=ticker_symbol,
side=side,
quantity=quantity
)
response = stub.SubmitMarketOrder(request)
print(f"Market Order Submitted. Order ID: {response.order_id}")
def run():
with grpc.insecure_channel('localhost:50051') as channel:
stub = ticker_service_pb2_grpc.TickerServiceStub(channel)
# 1. Get available tickers
get_tickers(stub)
# 3. Stream market data for a given ticker
print("\nStreaming Market Data for AMZN:")
stream_market_data(stub, "AMZN")
if __name__ == '__main__':
run()