/
quote_depth.go
60 lines (48 loc) · 1.05 KB
/
quote_depth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package www
import (
"strings"
"github.com/gin-gonic/gin"
"github.com/yzimhao/trading_engine/utils"
"github.com/yzimhao/trading_engine/utils/app"
)
func qutoe_depth(ctx *gin.Context) {
limit := utils.S2Int(ctx.Query("limit"))
symbol := strings.ToLower(ctx.Query("symbol"))
rdc := app.RedisPool().Get()
defer rdc.Close()
data, err := get_depth_data(symbol)
if err != nil {
utils.ResponseFailJson(ctx, "invalid symbol")
return
}
if limit <= 0 || limit > 100 {
limit = 10
}
utils.ResponseOkJson(ctx, gin.H{
"asks": limitSize(data.Asks, limit),
"bids": limitSize(data.Bids, limit),
})
}
func qutoe_latest_price(ctx *gin.Context) {
symbol := strings.ToLower(ctx.Query("symbol"))
rdc := app.RedisPool().Get()
defer rdc.Close()
data, err := get_depth_data(symbol)
if err != nil {
utils.ResponseFailJson(ctx, "invalid symbol")
return
}
utils.ResponseOkJson(ctx, gin.H{
symbol: data.Price,
})
}
func limitSize(arr [][2]string, n int) [][2]string {
a := len(arr)
if n >= a {
n = a
}
if n <= 0 {
n = 0
}
return arr[0:n]
}