diff --git a/stock/examples.json b/stock/examples.json index b7dfcb2..b34a2e7 100644 --- a/stock/examples.json +++ b/stock/examples.json @@ -47,28 +47,35 @@ "description": "Returns historic order book for a given date", "request": { "stock": "AAPL", - "date": "2020-10-01" + "date": "2020-10-01", + "start": "2020-10-01T10:00:00Z", + "end": "2020-10-01T11:00:00Z", + "limit": 3 }, "response": { "symbol": "AAPL", "date": "2020-10-01", "orders": [ { - "bid_price": 101, - "bid_size": 112, - "timestamp": "2020-10-01T08:00:00.037685138Z" + "ask_price": 117.49, + "bid_price": 117.3, + "ask_size": 1, + "bid_size": 1, + "timestamp": "2020-10-01T10:00:02.672770187Z" }, { - "bid_price": 116.52, - "bid_size": 1, - "timestamp": "2020-10-01T08:00:00.053150454Z" + "ask_price": 117.47, + "bid_price": 117.35, + "ask_size": 2, + "bid_size": 1, + "timestamp": "2020-10-01T10:00:33.258111144Z" }, { - "ask_price": 116.92, - "bid_price": 116.7, - "ask_size": 1, - "bid_size": 3, - "timestamp": "2020-10-01T08:00:17.417598642Z" + "ask_price": 117.47, + "bid_price": 117.3, + "ask_size": 2, + "bid_size": 2, + "timestamp": "2020-10-01T10:00:33.386282508Z" } ] } diff --git a/stock/handler/stock.go b/stock/handler/stock.go index bb11705..771c6d7 100644 --- a/stock/handler/stock.go +++ b/stock/handler/stock.go @@ -71,13 +71,20 @@ func (s *Stock) OrderBook(ctx context.Context, req *pb.OrderBookRequest, rsp *pb uri := fmt.Sprintf("%shistory/stock/all?apikey=%s&stock=%s&date=%s&limit=%d", s.Api, s.Key, req.Stock, req.Date, req.Limit) - if req.Start > 0 { - uri = fmt.Sprintf("%s&ts=%d", uri, req.Start) + if len(req.Start) > 0 { + t, err := time.Parse(time.RFC3339Nano, req.Start) + if err != nil { + return errors.BadRequest("stock.orderbook", "invalid start datetime") + } + uri = fmt.Sprintf("%s&ts=%d", uri, t.UTC().UnixNano()) } - if req.End > 0 { - uri = fmt.Sprintf("%s&te=%d", uri, req.End) + if len(req.End) > 0 { + t, err := time.Parse(time.RFC3339Nano, req.End) + if err != nil { + return errors.BadRequest("stock.orderbook", "invalid end datetime") + } + uri = fmt.Sprintf("%s&te=%d", uri, t.UTC().UnixNano()) } - resp, err := http.Get(uri) if err != nil { logger.Errorf("Failed to get orderbook: %v\n", err) diff --git a/stock/proto/stock.pb.go b/stock/proto/stock.pb.go index eafd7f5..7ae105e 100644 --- a/stock/proto/stock.pb.go +++ b/stock/proto/stock.pb.go @@ -114,10 +114,10 @@ type OrderBookRequest struct { Stock string `protobuf:"bytes,1,opt,name=stock,proto3" json:"stock,omitempty"` // the date in format YYYY-MM-dd Date string `protobuf:"bytes,2,opt,name=date,proto3" json:"date,omitempty"` - // optional nanosecond timestamp start - Start int64 `protobuf:"varint,3,opt,name=start,proto3" json:"start,omitempty"` - // optional nanosecond timestamp end - End int64 `protobuf:"varint,4,opt,name=end,proto3" json:"end,omitempty"` + // optional RFC3339Nano start time e.g 2006-01-02T15:04:05.999999999Z07:00 + Start string `protobuf:"bytes,3,opt,name=start,proto3" json:"start,omitempty"` + // optional RFC3339Nano end time e.g 2006-01-02T15:04:05.999999999Z07:00 + End string `protobuf:"bytes,4,opt,name=end,proto3" json:"end,omitempty"` // limit number of prices Limit int32 `protobuf:"varint,5,opt,name=limit,proto3" json:"limit,omitempty"` } @@ -168,18 +168,18 @@ func (x *OrderBookRequest) GetDate() string { return "" } -func (x *OrderBookRequest) GetStart() int64 { +func (x *OrderBookRequest) GetStart() string { if x != nil { return x.Start } - return 0 + return "" } -func (x *OrderBookRequest) GetEnd() int64 { +func (x *OrderBookRequest) GetEnd() string { if x != nil { return x.End } - return 0 + return "" } func (x *OrderBookRequest) GetLimit() int32 { @@ -681,9 +681,9 @@ var file_proto_stock_proto_rawDesc = []byte{ 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x74, 0x6f, 0x63, 0x6b, 0x12, 0x12, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x65, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x64, 0x61, 0x74, 0x65, - 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x03, 0x52, + 0x12, 0x14, 0x0a, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05, 0x73, 0x74, 0x61, 0x72, 0x74, 0x12, 0x10, 0x0a, 0x03, 0x65, 0x6e, 0x64, 0x18, 0x04, 0x20, - 0x01, 0x28, 0x03, 0x52, 0x03, 0x65, 0x6e, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, 0x6d, 0x69, + 0x01, 0x28, 0x09, 0x52, 0x03, 0x65, 0x6e, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x05, 0x20, 0x01, 0x28, 0x05, 0x52, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x22, 0x65, 0x0a, 0x11, 0x4f, 0x72, 0x64, 0x65, 0x72, 0x42, 0x6f, 0x6f, 0x6b, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x16, 0x0a, 0x06, 0x73, 0x79, 0x6d, 0x62, 0x6f, 0x6c, 0x18, 0x01, 0x20, diff --git a/stock/proto/stock.proto b/stock/proto/stock.proto index 30ec6aa..64a6eaa 100644 --- a/stock/proto/stock.proto +++ b/stock/proto/stock.proto @@ -30,10 +30,10 @@ message OrderBookRequest { string stock = 1; // the date in format YYYY-MM-dd string date = 2; - // optional nanosecond timestamp start - int64 start = 3; - // optional nanosecond timestamp end - int64 end = 4; + // optional RFC3339Nano start time e.g 2006-01-02T15:04:05.999999999Z07:00 + string start = 3; + // optional RFC3339Nano end time e.g 2006-01-02T15:04:05.999999999Z07:00 + string end = 4; // limit number of prices int32 limit = 5; }