2021-01-22 16:50:23 +00:00
|
|
|
package bbgo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-01-24 10:42:36 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2021-01-29 05:15:44 +00:00
|
|
|
"strconv"
|
2021-01-25 08:56:02 +00:00
|
|
|
"time"
|
2021-01-22 16:50:23 +00:00
|
|
|
|
2021-01-25 08:56:02 +00:00
|
|
|
"github.com/gin-contrib/cors"
|
2021-01-22 16:50:23 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-01-29 03:19:37 +00:00
|
|
|
"github.com/markbates/pkger"
|
2021-01-26 09:21:18 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-01-24 12:13:05 +00:00
|
|
|
|
2021-01-26 09:21:18 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/service"
|
2021-01-24 12:13:05 +00:00
|
|
|
"github.com/c9s/bbgo/pkg/types"
|
2021-01-22 16:50:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func RunServer(ctx context.Context, userConfig *Config, environ *Environment) error {
|
|
|
|
r := gin.Default()
|
2021-01-25 08:56:02 +00:00
|
|
|
r.Use(cors.New(cors.Config{
|
|
|
|
AllowOrigins: []string{"*"},
|
|
|
|
AllowHeaders: []string{"Origin"},
|
|
|
|
ExposeHeaders: []string{"Content-Length"},
|
|
|
|
AllowCredentials: true,
|
|
|
|
MaxAge: 12 * time.Hour,
|
|
|
|
}))
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/ping", func(c *gin.Context) {
|
2021-01-24 10:42:36 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
2021-01-22 16:50:23 +00:00
|
|
|
})
|
|
|
|
|
2021-01-29 10:48:00 +00:00
|
|
|
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,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-01-29 10:34:03 +00:00
|
|
|
r.GET("/api/orders/closed", func(c *gin.Context) {
|
2021-01-29 05:15:44 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-01-29 10:48:00 +00:00
|
|
|
orders, err := environ.OrderService.Query(service.QueryOrdersOptions{
|
2021-01-29 05:15:44 +00:00
|
|
|
Exchange: types.ExchangeName(exchange),
|
|
|
|
Symbol: symbol,
|
|
|
|
LastGID: lastGID,
|
2021-01-29 10:48:00 +00:00
|
|
|
Ordering: "DESC",
|
2021-01-29 05:15:44 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
c.Status(http.StatusBadRequest)
|
|
|
|
log.WithError(err).Error("order query error")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"orders": orders,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-01-26 09:21:18 +00:00
|
|
|
r.GET("/api/trading-volume", func(c *gin.Context) {
|
|
|
|
period := c.DefaultQuery("period", "day")
|
2021-01-28 10:51:35 +00:00
|
|
|
segment := c.DefaultQuery("segment", "exchange")
|
|
|
|
startTimeStr := c.Query("start-time")
|
2021-01-26 09:21:18 +00:00
|
|
|
|
2021-01-28 10:51:35 +00:00
|
|
|
var startTime time.Time
|
|
|
|
|
|
|
|
if startTimeStr != "" {
|
|
|
|
v, err := time.Parse(time.RFC3339, startTimeStr)
|
|
|
|
if err != nil {
|
|
|
|
c.Status(http.StatusBadRequest)
|
|
|
|
log.WithError(err).Error("start-time format incorrect")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
startTime = v
|
|
|
|
|
|
|
|
} else {
|
|
|
|
switch period {
|
|
|
|
case "day":
|
|
|
|
startTime = time.Now().AddDate(0, 0, -30)
|
|
|
|
|
|
|
|
case "month":
|
|
|
|
startTime = time.Now().AddDate(0, -6, 0)
|
|
|
|
|
|
|
|
case "year":
|
|
|
|
startTime = time.Now().AddDate(-2, 0, 0)
|
|
|
|
|
|
|
|
default:
|
|
|
|
startTime = time.Now().AddDate(0, 0, -7)
|
|
|
|
|
|
|
|
}
|
2021-01-26 09:21:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rows, err := environ.TradeService.QueryTradingVolume(startTime, service.TradingVolumeQueryOptions{
|
2021-01-28 10:51:35 +00:00
|
|
|
SegmentBy: segment,
|
|
|
|
GroupByPeriod: period,
|
2021-01-26 09:21:18 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("trading volume query error")
|
|
|
|
c.Status(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-26 10:10:08 +00:00
|
|
|
c.JSON(http.StatusOK, gin.H{"tradingVolumes": rows})
|
2021-01-26 09:21:18 +00:00
|
|
|
return
|
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/sessions", func(c *gin.Context) {
|
2021-01-29 04:55:11 +00:00
|
|
|
var sessions []*ExchangeSession
|
|
|
|
for _, session := range environ.Sessions() {
|
|
|
|
sessions = append(sessions, session)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"sessions": sessions})
|
2021-01-24 10:42:36 +00:00
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/assets", func(c *gin.Context) {
|
|
|
|
totalAssets := types.AssetMap{}
|
|
|
|
|
|
|
|
for _, session := range environ.sessions {
|
|
|
|
balances := session.Account.Balances()
|
2021-01-26 09:21:18 +00:00
|
|
|
|
|
|
|
if err := session.UpdatePrices(ctx); err != nil {
|
|
|
|
log.WithError(err).Error("price update failed")
|
|
|
|
c.Status(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
assets := balances.Assets(session.lastPrices)
|
|
|
|
|
|
|
|
for currency, asset := range assets {
|
|
|
|
totalAssets[currency] = asset
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"assets": totalAssets})
|
|
|
|
})
|
|
|
|
|
|
|
|
r.GET("/api/sessions/:session", func(c *gin.Context) {
|
2021-01-24 10:42:36 +00:00
|
|
|
sessionName := c.Param("session")
|
|
|
|
session, ok := environ.Session(sessionName)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("session %s not found", sessionName)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"session": session})
|
2021-01-22 16:50:23 +00:00
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/sessions/:session/trades", func(c *gin.Context) {
|
2021-01-24 10:42:36 +00:00
|
|
|
sessionName := c.Param("session")
|
|
|
|
session, ok := environ.Session(sessionName)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("session %s not found", sessionName)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"trades": session.Trades})
|
2021-01-22 16:50:23 +00:00
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/sessions/:session/open-orders", func(c *gin.Context) {
|
2021-01-24 12:13:05 +00:00
|
|
|
sessionName := c.Param("session")
|
|
|
|
session, ok := environ.Session(sessionName)
|
2021-01-22 16:50:23 +00:00
|
|
|
|
2021-01-24 12:13:05 +00:00
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("session %s not found", sessionName)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
marketOrders := make(map[string][]types.Order)
|
|
|
|
for symbol, orderStore := range session.orderStores {
|
|
|
|
marketOrders[symbol] = orderStore.Orders()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"orders": marketOrders})
|
2021-01-22 16:50:23 +00:00
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/sessions/:session/account", func(c *gin.Context) {
|
2021-01-24 12:18:04 +00:00
|
|
|
sessionName := c.Param("session")
|
|
|
|
session, ok := environ.Session(sessionName)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("session %s not found", sessionName)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"account": session.Account})
|
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/sessions/:session/account/balances", func(c *gin.Context) {
|
2021-01-24 12:18:04 +00:00
|
|
|
sessionName := c.Param("session")
|
|
|
|
session, ok := environ.Session(sessionName)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("session %s not found", sessionName)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if session.Account == nil {
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("the account of session %s is nil", sessionName)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"balances": session.Account.Balances()})
|
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/sessions/:session/symbols", func(c *gin.Context) {
|
2021-01-22 16:50:23 +00:00
|
|
|
|
2021-01-24 12:13:05 +00:00
|
|
|
sessionName := c.Param("session")
|
|
|
|
session, ok := environ.Session(sessionName)
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusNotFound, gin.H{"error": fmt.Sprintf("session %s not found", sessionName)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var symbols []string
|
|
|
|
for s := range session.loadedSymbols {
|
|
|
|
symbols = append(symbols, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{"symbols": symbols})
|
2021-01-22 16:50:23 +00:00
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/sessions/:session/pnl", func(c *gin.Context) {
|
2021-01-22 16:50:23 +00:00
|
|
|
c.JSON(200, gin.H{"message": "pong"})
|
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/sessions/:session/market/:symbol/open-orders", func(c *gin.Context) {
|
2021-01-22 16:50:23 +00:00
|
|
|
c.JSON(200, gin.H{"message": "pong"})
|
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/sessions/:session/market/:symbol/trades", func(c *gin.Context) {
|
2021-01-22 16:50:23 +00:00
|
|
|
c.JSON(200, gin.H{"message": "pong"})
|
|
|
|
})
|
|
|
|
|
2021-01-25 07:32:17 +00:00
|
|
|
r.GET("/api/sessions/:session/market/:symbol/pnl", func(c *gin.Context) {
|
2021-01-22 16:50:23 +00:00
|
|
|
c.JSON(200, gin.H{"message": "pong"})
|
|
|
|
})
|
|
|
|
|
2021-01-29 03:19:37 +00:00
|
|
|
fs := pkger.Dir("/frontend/out")
|
|
|
|
r.NoRoute(func(c *gin.Context) {
|
|
|
|
http.FileServer(fs).ServeHTTP(c.Writer, c.Request)
|
|
|
|
})
|
|
|
|
|
2021-01-22 16:50:23 +00:00
|
|
|
return r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
|
|
|
|
}
|