add abstract exchange interface

This commit is contained in:
c9s 2020-06-18 18:54:14 +08:00
parent ed00243734
commit e62f1f2479
2 changed files with 65 additions and 0 deletions

31
bbgo/context.go Normal file
View File

@ -0,0 +1,31 @@
package bbgo
import "time"
type TradingContext struct {
KLineWindowSize int
KLineWindows map[string]KLineWindow
AverageBidPrice float64
Stock float64
Profit float64
CurrentPrice float64
Trades []Trade
TradeStartTime time.Time
Symbol string
}
func (c *TradingContext) AddKLine(kline KLine) KLineWindow {
var klineWindow = c.KLineWindows[kline.Interval]
klineWindow.Add(kline)
if c.KLineWindowSize > 0 {
klineWindow.Truncate(c.KLineWindowSize)
}
return klineWindow
}
func (c *TradingContext) UpdatePnL() {
c.AverageBidPrice, c.Stock, c.Profit, _ = CalculateCostAndProfit(c.Trades, c.CurrentPrice)
}

34
bbgo/market.go Normal file
View File

@ -0,0 +1,34 @@
package bbgo
import "strconv"
type Market struct {
Symbol string
PricePrecision int
VolumePrecision int
}
func (m Market) FormatPrice(val float64) string {
return strconv.FormatFloat(val, 'f', m.PricePrecision, 64)
}
func (m Market) FormatVolume(val float64) string {
return strconv.FormatFloat(val, 'f', m.VolumePrecision, 64)
}
// Binance Markets, this should be defined per exchange
var Markets = map[string]Market{
"BTCUSDT": {
Symbol: "BTCUSDT",
PricePrecision: 2,
VolumePrecision: 8,
},
}
func FindMarket(symbol string) (m Market, ok bool) {
m , ok = Markets[symbol]
return m, ok
}