From eb0c79942c23ee74f5bfd9988344ba38319ed2ee Mon Sep 17 00:00:00 2001 From: Kevin Cotugno Date: Fri, 8 Sep 2017 07:35:47 -0700 Subject: [PATCH] First/last trade --- postgres/trade_service.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/postgres/trade_service.go b/postgres/trade_service.go index 211f7ff..9e54320 100644 --- a/postgres/trade_service.go +++ b/postgres/trade_service.go @@ -17,6 +17,10 @@ const ( trades_in_date_range = `SELECT id, trade_id, product, price, size, buy, ` + `sell, timestamp FROM trades WHERE product = $1 AND timestamp >= $2 ` + `AND timestamp < $3;` + trade_first = `SELECT id, trade_id, product, price, size, buy, sell, timestamp ` + + `FROM trades WHERE product = $1 ORDER BY trade_id ASC LIMIT 1;` + trade_last = `SELECT id, trade_id, product, price, size, buy, sell, timestamp ` + + `FROM trades WHERE product = $1 ORDER BY trade_id DESC LIMIT 1;` ) type TradeService struct { @@ -94,3 +98,29 @@ func (t *TradeService) TradesInDateRange(product string, start, return trades, nil } + +func (t *TradeService) FirstTrade(product string) (*tacitus.Trade, error) { + var tr tacitus.Trade + + row := t.client.db.QueryRow(trade_first, product) + if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy, + &tr.Sell, &tr.Timestamp); err != nil { + + return nil, err + } + + return &tr, nil +} + +func (t *TradeService) LastTrade(product string) (*tacitus.Trade, error) { + var tr tacitus.Trade + + row := t.client.db.QueryRow(trade_last, product) + if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy, + &tr.Sell, &tr.Timestamp); err != nil { + + return nil, err + } + + return &tr, nil +}