mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 14:55:16 +00:00
add abstract exchange interface
This commit is contained in:
parent
ed00243734
commit
e62f1f2479
31
bbgo/context.go
Normal file
31
bbgo/context.go
Normal 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
34
bbgo/market.go
Normal 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
|
||||||
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user