-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a slightly involved example with maps and lists
- Loading branch information
Showing
6 changed files
with
630 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# grpc demo | ||
|
||
* `MarketDataService` implements `GetOHLC` RPC call. | ||
* `GetOHLC` takes in the list of stocks as well as the start and the end dates. | ||
* `GetOHLC` returns the open, high, low, close prices for the given list of stocks. | ||
|
||
## How to Run | ||
|
||
1. Create an virtualenv and install requirements. | ||
```bash | ||
cd grpc | ||
pip3 install -r requirements.txt | ||
``` | ||
|
||
1. (Optional) Generate the compiled files. | ||
```bash | ||
python -m grpc_tools.protoc --proto_path=. --python_out=. --grpc_python_out=. market_data.proto | ||
``` | ||
|
||
1. Run Server in one terminal window: | ||
```bash | ||
python3 server.py | ||
``` | ||
|
||
1. Run Client in another terminal: | ||
```bash | ||
python3 server.py | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from datetime import date | ||
|
||
import grpc | ||
import pandas as pd | ||
|
||
import market_data_pb2 | ||
import market_data_pb2_grpc | ||
|
||
|
||
def run(): | ||
channel = grpc.insecure_channel('localhost:50051') | ||
stub = market_data_pb2_grpc.MarketDataStub(channel) | ||
|
||
stocks = ['TSLA', 'F', 'AAPL', 'MSFT', 'FB'] | ||
start_date = date(2018, 1, 1).strftime('%Y-%m-%d') | ||
end_date = date(2018, 9, 1).strftime('%Y-%m-%d') | ||
|
||
request = market_data_pb2.OHLCRequest(ticker=stocks, start_date=start_date, end_date=end_date) | ||
|
||
response = stub.GetOHLC(request) | ||
|
||
open_df = pd.DataFrame(index=response.dates) | ||
high_df = pd.DataFrame(index=response.dates) | ||
low_df = pd.DataFrame(index=response.dates) | ||
close_df = pd.DataFrame(index=response.dates) | ||
|
||
for stock in response.open: | ||
open_df[stock] = response.open[stock].price | ||
high_df[stock] = response.high[stock].price | ||
low_df[stock] = response.low[stock].price | ||
close_df[stock] = response.close[stock].price | ||
|
||
print('Last day Open: ') | ||
print(open_df.tail(1)) | ||
print('Last day Close: ') | ||
print(close_df.tail(1)) | ||
|
||
|
||
if __name__ == '__main__': | ||
run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
syntax = "proto3"; | ||
package market_data; | ||
|
||
message OHLCRequest { | ||
// List of stocks | ||
repeated string ticker = 3; | ||
|
||
// Start date | ||
string start_date = 1; | ||
|
||
// End date | ||
string end_date = 2; | ||
} | ||
|
||
message OHLCReply { | ||
// OHLCRet | ||
message OHLCRet { | ||
// Stock price list | ||
repeated double price = 1; | ||
} | ||
|
||
// List of dates | ||
repeated string dates = 1; | ||
|
||
map<string, OHLCRet> open = 2; | ||
map<string, OHLCRet> high = 3; | ||
map<string, OHLCRet> low = 4; | ||
map<string, OHLCRet> close = 5; | ||
} | ||
|
||
service MarketData { | ||
// Get the [Open, High, Low, Close] for the given list of stocks | ||
// and start/end dates | ||
rpc GetOHLC(OHLCRequest) returns (OHLCReply); | ||
} |
Oops, something went wrong.