add trades query api

This commit is contained in:
c9s 2021-01-29 18:48:00 +08:00
parent cc8133a90e
commit fbbe304dfb
5 changed files with 98 additions and 16 deletions

View File

@ -206,7 +206,10 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
if strings.HasPrefix(symbol, tradingFeeCurrency) {
trades, err = environ.TradeService.QueryForTradingFeeCurrency(session.Exchange.Name(), symbol, tradingFeeCurrency)
} else {
trades, err = environ.TradeService.Query(session.Exchange.Name(), symbol)
trades, err = environ.TradeService.Query(service.QueryTradesOptions{
Exchange: session.Exchange.Name(),
Symbol: symbol,
})
}
if err != nil {

View File

@ -30,6 +30,34 @@ func RunServer(ctx context.Context, userConfig *Config, environ *Environment) er
c.JSON(http.StatusOK, gin.H{"message": "pong"})
})
r.GET("/api/trades", func(c *gin.Context) {
exchange := c.Query("exchange")
symbol := c.Query("symbol")
gidStr := c.DefaultQuery("gid", "0")
lastGID, err := strconv.ParseInt(gidStr, 10, 64)
if err != nil {
log.WithError(err).Error("last gid parse error")
c.Status(http.StatusBadRequest)
return
}
trades, err := environ.TradeService.Query(service.QueryTradesOptions{
Exchange: types.ExchangeName(exchange),
Symbol: symbol,
LastGID: lastGID,
Ordering: "DESC",
})
if err != nil {
c.Status(http.StatusBadRequest)
log.WithError(err).Error("order query error")
return
}
c.JSON(http.StatusOK, gin.H{
"trades": trades,
})
})
r.GET("/api/orders/closed", func(c *gin.Context) {
exchange := c.Query("exchange")
symbol := c.Query("symbol")
@ -42,11 +70,11 @@ func RunServer(ctx context.Context, userConfig *Config, environ *Environment) er
return
}
orders, err := environ.OrderService.Query(service.OrderQueryOptions{
orders, err := environ.OrderService.Query(service.QueryOrdersOptions{
Exchange: types.ExchangeName(exchange),
Symbol: symbol,
LastGID: lastGID,
Order: "DESC",
Ordering: "DESC",
})
if err != nil {
c.Status(http.StatusBadRequest)

View File

@ -64,7 +64,10 @@ var PnLCmd = &cobra.Command{
log.Infof("loading all trading fee currency related trades: %s", symbol)
trades, err = tradeService.QueryForTradingFeeCurrency(exchange.Name(), symbol, tradingFeeCurrency)
} else {
trades, err = tradeService.Query(exchange.Name(), symbol)
trades, err = tradeService.Query(service.QueryTradesOptions{
Exchange: exchange.Name(),
Symbol: symbol,
})
}
if err != nil {

View File

@ -50,18 +50,19 @@ type AggOrder struct {
AveragePrice *float64 `json:"averagePrice" db:"average_price"`
}
type OrderQueryOptions struct {
type QueryOrdersOptions struct {
Exchange types.ExchangeName
Symbol string
LastGID int64
Order string
Ordering string
}
func (s *OrderService) Query(options OrderQueryOptions) ([]AggOrder, error) {
func (s *OrderService) Query(options QueryOrdersOptions) ([]AggOrder, error) {
// ascending
ordering := "ASC"
if len(options.Order) > 0 {
ordering = options.Order
switch v := strings.ToUpper(options.Ordering); v {
case "DESC", "ASC":
ordering = options.Ordering
}
var where []string

View File

@ -1,6 +1,7 @@
package service
import (
"strconv"
"strings"
"time"
@ -157,11 +158,57 @@ func (s *TradeService) QueryForTradingFeeCurrency(ex types.ExchangeName, symbol
return s.scanRows(rows)
}
func (s *TradeService) Query(ex types.ExchangeName, symbol string) ([]types.Trade, error) {
rows, err := s.DB.NamedQuery(`SELECT * FROM trades WHERE exchange = :exchange AND symbol = :symbol ORDER BY gid ASC`, map[string]interface{}{
"exchange": ex,
"symbol": symbol,
})
type QueryTradesOptions struct {
Exchange types.ExchangeName
Symbol string
LastGID int64
Ordering string
}
func (s *TradeService) Query(options QueryTradesOptions) ([]types.Trade, error) {
ordering := "ASC"
switch v := strings.ToUpper(options.Ordering); v {
case "DESC", "ASC":
ordering = v
}
var where []string
if len(options.Exchange) > 0 {
where = append(where, `exchange = :exchange`)
}
if len(options.Symbol) > 0 {
where = append(where, `symbol = :symbol`)
}
if options.LastGID > 0 {
switch ordering {
case "ASC":
where = append(where, "gid > :gid")
case "DESC":
where = append(where, "gid < :gid")
}
}
sql := `SELECT * FROM trades`
if len(where) > 0 {
sql += ` WHERE ` + strings.Join(where, " AND ")
}
sql += ` ORDER BY gid ` + ordering
sql += ` LIMIT ` + strconv.Itoa(500)
log.Info(sql)
args := map[string]interface{}{
"exchange": options.Exchange,
"symbol": options.Symbol,
}
rows, err := s.DB.NamedQuery(sql, args)
if err != nil {
return nil, err
}