Skip to content

Commit

Permalink
Adding a slightly involved example with maps and lists
Browse files Browse the repository at this point in the history
  • Loading branch information
hardikp committed Sep 14, 2018
1 parent b01d4bc commit d2bc9c3
Show file tree
Hide file tree
Showing 6 changed files with 630 additions and 0 deletions.
28 changes: 28 additions & 0 deletions grpc_advanced/README.md
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
```
40 changes: 40 additions & 0 deletions grpc_advanced/client.py
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()
35 changes: 35 additions & 0 deletions grpc_advanced/market_data.proto
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);
}
Loading

0 comments on commit d2bc9c3

Please sign in to comment.