mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-26 00:35:15 +00:00
add order query api
This commit is contained in:
parent
be750b94df
commit
32645f228b
30
frontend/pages/orders.js
Normal file
30
frontend/pages/orders.js
Normal file
|
@ -0,0 +1,30 @@
|
|||
import React from 'react';
|
||||
|
||||
import {makeStyles} from '@material-ui/core/styles';
|
||||
import Container from '@material-ui/core/Container';
|
||||
import Typography from '@material-ui/core/Typography';
|
||||
import Box from '@material-ui/core/Box';
|
||||
import Paper from '@material-ui/core/Paper';
|
||||
|
||||
const useStyles = makeStyles((theme) => ({
|
||||
paper: {
|
||||
padding: theme.spacing(2),
|
||||
},
|
||||
}));
|
||||
|
||||
export default function Orders() {
|
||||
const classes = useStyles();
|
||||
|
||||
return (
|
||||
<Container>
|
||||
<Box m={4}>
|
||||
<Paper className={classes.paper}>
|
||||
<Typography variant="h4" component="h2" gutterBottom>
|
||||
Orders
|
||||
</Typography>
|
||||
</Paper>
|
||||
</Box>
|
||||
</Container>
|
||||
);
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/cors"
|
||||
|
@ -29,6 +30,35 @@ func RunServer(ctx context.Context, userConfig *Config, environ *Environment) er
|
|||
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
r.GET("/api/orders", 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
|
||||
}
|
||||
|
||||
orders, err := environ.OrderService.Query(service.OrderQueryOptions{
|
||||
Exchange: types.ExchangeName(exchange),
|
||||
Symbol: symbol,
|
||||
LastGID: lastGID,
|
||||
Order: "DESC",
|
||||
})
|
||||
if err != nil {
|
||||
c.Status(http.StatusBadRequest)
|
||||
log.WithError(err).Error("order query error")
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"orders": orders,
|
||||
})
|
||||
})
|
||||
|
||||
r.GET("/api/trading-volume", func(c *gin.Context) {
|
||||
period := c.DefaultQuery("period", "day")
|
||||
segment := c.DefaultQuery("segment", "exchange")
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package service
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/pkg/errors"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
@ -42,10 +45,49 @@ func (s *OrderService) QueryLast(ex types.ExchangeName, symbol string, isMargin
|
|||
return nil, rows.Err()
|
||||
}
|
||||
|
||||
func (s *OrderService) Query(ex types.ExchangeName, symbol string) ([]types.Order, error) {
|
||||
rows, err := s.DB.NamedQuery(`SELECT * FROM orders WHERE exchange = :exchange AND symbol = :symbol ORDER BY gid ASC`, map[string]interface{}{
|
||||
"exchange": ex,
|
||||
"symbol": symbol,
|
||||
type OrderQueryOptions struct {
|
||||
Exchange types.ExchangeName
|
||||
Symbol string
|
||||
LastGID int64
|
||||
Order string
|
||||
}
|
||||
|
||||
func (s *OrderService) Query(options OrderQueryOptions) ([]types.Order, error) {
|
||||
// ascending
|
||||
ordering := "ASC"
|
||||
if len(options.Order) > 0 {
|
||||
ordering = options.Order
|
||||
}
|
||||
|
||||
var where []string
|
||||
if options.LastGID > 0 {
|
||||
switch ordering {
|
||||
case "ASC":
|
||||
where = append(where, "gid > :gid")
|
||||
case "DESC":
|
||||
where = append(where, "gid < :gid")
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if len(options.Exchange) > 0 {
|
||||
where = append(where, "exchange = :exchange")
|
||||
}
|
||||
if len(options.Symbol) > 0 {
|
||||
where = append(where, "symbol = :symbol")
|
||||
}
|
||||
|
||||
sql := `SELECT * FROM orders`
|
||||
if len(where) > 0 {
|
||||
sql += ` WHERE ` + strings.Join(where, " AND ")
|
||||
}
|
||||
sql += ` ORDER BY gid ` + ordering
|
||||
sql += ` LIMIT ` + strconv.Itoa(500)
|
||||
|
||||
rows, err := s.DB.NamedQuery(sql, map[string]interface{}{
|
||||
"exchange": options.Exchange,
|
||||
"symbol": options.Symbol,
|
||||
"gid": options.LastGID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
|
|
@ -69,7 +69,7 @@ type SubmitOrder struct {
|
|||
Price float64 `json:"price" db:"price"`
|
||||
StopPrice float64 `json:"stopPrice" db:"stop_price"`
|
||||
|
||||
Market Market `json:"market" db:"-"`
|
||||
Market Market `json:"-" db:"-"`
|
||||
|
||||
// TODO: we can probably remove these field
|
||||
StopPriceString string `json:"-"`
|
||||
|
|
Loading…
Reference in New Issue
Block a user