mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-10 09:11:55 +00:00
add assets api and price loading
This commit is contained in:
parent
09b3046feb
commit
e47357d1ed
|
@ -221,8 +221,6 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
|||
session.Trades[symbol].Append(trade)
|
||||
})
|
||||
|
||||
session.lastPrices[symbol] = 0.0
|
||||
|
||||
position := &Position{
|
||||
Symbol: symbol,
|
||||
BaseCurrency: market.BaseCurrency,
|
||||
|
@ -255,6 +253,21 @@ func (environ *Environment) Init(ctx context.Context) (err error) {
|
|||
log.Infof("%s account", session.Name)
|
||||
balances.Print()
|
||||
|
||||
for _, b := range balances {
|
||||
priceSymbol := b.Currency + "USDT"
|
||||
startTime := time.Now().Add(-10 * time.Minute)
|
||||
klines, err := session.Exchange.QueryKLines(ctx, priceSymbol, types.Interval1m, types.KLineQueryOptions{
|
||||
Limit: 100,
|
||||
StartTime: &startTime,
|
||||
})
|
||||
|
||||
if err != nil || len(klines) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
session.lastPrices[priceSymbol] = klines[len(klines)-1].Close
|
||||
}
|
||||
|
||||
session.Account.UpdateBalances(balances)
|
||||
session.Account.BindStream(session.Stream)
|
||||
|
||||
|
|
|
@ -12,15 +12,30 @@ import (
|
|||
|
||||
func RunServer(ctx context.Context, userConfig *Config, environ *Environment) error {
|
||||
r := gin.Default()
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
r.GET("/api/ping", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
r.GET("/sessions", func(c *gin.Context) {
|
||||
r.GET("/api/sessions", func(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"sessions": userConfig.Sessions})
|
||||
})
|
||||
|
||||
r.GET("/sessions/:session", func(c *gin.Context) {
|
||||
r.GET("/api/assets", func(c *gin.Context) {
|
||||
totalAssets := types.AssetMap{}
|
||||
|
||||
for _, session := range environ.sessions {
|
||||
balances := session.Account.Balances()
|
||||
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) {
|
||||
sessionName := c.Param("session")
|
||||
session, ok := environ.Session(sessionName)
|
||||
|
||||
|
@ -32,7 +47,7 @@ func RunServer(ctx context.Context, userConfig *Config, environ *Environment) er
|
|||
c.JSON(http.StatusOK, gin.H{"session": session})
|
||||
})
|
||||
|
||||
r.GET("/sessions/:session/trades", func(c *gin.Context) {
|
||||
r.GET("/api/sessions/:session/trades", func(c *gin.Context) {
|
||||
sessionName := c.Param("session")
|
||||
session, ok := environ.Session(sessionName)
|
||||
|
||||
|
@ -44,7 +59,7 @@ func RunServer(ctx context.Context, userConfig *Config, environ *Environment) er
|
|||
c.JSON(http.StatusOK, gin.H{"trades": session.Trades})
|
||||
})
|
||||
|
||||
r.GET("/sessions/:session/open-orders", func(c *gin.Context) {
|
||||
r.GET("/api/sessions/:session/open-orders", func(c *gin.Context) {
|
||||
sessionName := c.Param("session")
|
||||
session, ok := environ.Session(sessionName)
|
||||
|
||||
|
@ -61,7 +76,7 @@ func RunServer(ctx context.Context, userConfig *Config, environ *Environment) er
|
|||
c.JSON(http.StatusOK, gin.H{"orders": marketOrders})
|
||||
})
|
||||
|
||||
r.GET("/sessions/:session/account", func(c *gin.Context) {
|
||||
r.GET("/api/sessions/:session/account", func(c *gin.Context) {
|
||||
sessionName := c.Param("session")
|
||||
session, ok := environ.Session(sessionName)
|
||||
|
||||
|
@ -73,7 +88,7 @@ func RunServer(ctx context.Context, userConfig *Config, environ *Environment) er
|
|||
c.JSON(http.StatusOK, gin.H{"account": session.Account})
|
||||
})
|
||||
|
||||
r.GET("/sessions/:session/account/balances", func(c *gin.Context) {
|
||||
r.GET("/api/sessions/:session/account/balances", func(c *gin.Context) {
|
||||
sessionName := c.Param("session")
|
||||
session, ok := environ.Session(sessionName)
|
||||
|
||||
|
@ -90,7 +105,7 @@ func RunServer(ctx context.Context, userConfig *Config, environ *Environment) er
|
|||
c.JSON(http.StatusOK, gin.H{"balances": session.Account.Balances()})
|
||||
})
|
||||
|
||||
r.GET("/sessions/:session/symbols", func(c *gin.Context) {
|
||||
r.GET("/api/sessions/:session/symbols", func(c *gin.Context) {
|
||||
|
||||
sessionName := c.Param("session")
|
||||
session, ok := environ.Session(sessionName)
|
||||
|
@ -108,19 +123,19 @@ func RunServer(ctx context.Context, userConfig *Config, environ *Environment) er
|
|||
c.JSON(http.StatusOK, gin.H{"symbols": symbols})
|
||||
})
|
||||
|
||||
r.GET("/sessions/:session/pnl", func(c *gin.Context) {
|
||||
r.GET("/api/sessions/:session/pnl", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
r.GET("/sessions/:session/market/:symbol/open-orders", func(c *gin.Context) {
|
||||
r.GET("/api/sessions/:session/market/:symbol/open-orders", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
r.GET("/sessions/:session/market/:symbol/trades", func(c *gin.Context) {
|
||||
r.GET("/api/sessions/:session/market/:symbol/trades", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
r.GET("/sessions/:session/market/:symbol/pnl", func(c *gin.Context) {
|
||||
r.GET("/api/sessions/:session/market/:symbol/pnl", func(c *gin.Context) {
|
||||
c.JSON(200, gin.H{"message": "pong"})
|
||||
})
|
||||
|
||||
|
|
|
@ -78,7 +78,7 @@ func (v *Value) UnmarshalYAML(unmarshal func(a interface{}) error) (err error) {
|
|||
|
||||
func (v Value) MarshalJSON() ([]byte, error) {
|
||||
f := float64(v) / DefaultPow
|
||||
o := fmt.Sprintf("%f", f)
|
||||
o := strconv.FormatFloat(f, 'f', 8, 64)
|
||||
return []byte(o), nil
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,15 @@ func (b Balance) String() string {
|
|||
return fmt.Sprintf("%s: %f", b.Currency, b.Available.Float64())
|
||||
}
|
||||
|
||||
type Asset struct {
|
||||
Currency string `json:"currency"`
|
||||
Total fixedpoint.Value `json:"total"`
|
||||
InUSD fixedpoint.Value `json:"inUSD"`
|
||||
InBTC fixedpoint.Value `json:"inBTC"`
|
||||
}
|
||||
|
||||
type AssetMap map[string]Asset
|
||||
|
||||
type BalanceMap map[string]Balance
|
||||
|
||||
func (m BalanceMap) String() string {
|
||||
|
@ -35,6 +44,43 @@ func (m BalanceMap) String() string {
|
|||
return "BalanceMap[" + strings.Join(ss, ", ") + "]"
|
||||
}
|
||||
|
||||
func (m BalanceMap) Assets(prices map[string]float64) AssetMap {
|
||||
assets := make(AssetMap)
|
||||
|
||||
for currency, b := range m {
|
||||
if b.Locked == 0 && b.Available == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
asset := Asset{
|
||||
Currency: currency,
|
||||
Total: b.Available + b.Locked,
|
||||
}
|
||||
|
||||
btcusdt, hasBtcPrice := prices["BTCUSDT"]
|
||||
|
||||
usdMarkets := []string{currency + "USDT", currency + "USDC", currency + "USD", "USDT" + currency}
|
||||
for _, market := range usdMarkets {
|
||||
if val, ok := prices[market]; ok {
|
||||
|
||||
if strings.HasPrefix(market, "USD") {
|
||||
asset.InUSD = fixedpoint.NewFromFloat(asset.Total.Float64() / val)
|
||||
} else {
|
||||
asset.InUSD = asset.Total.MulFloat64(val)
|
||||
}
|
||||
|
||||
if hasBtcPrice {
|
||||
asset.InBTC = fixedpoint.NewFromFloat(asset.InUSD.Float64() / btcusdt)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
assets[currency] = asset
|
||||
}
|
||||
|
||||
return assets
|
||||
}
|
||||
|
||||
func (m BalanceMap) Print() {
|
||||
for _, balance := range m {
|
||||
if balance.Available == 0 && balance.Locked == 0 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user