bbgo_origin/pkg/bbgo/position.go

181 lines
4.2 KiB
Go
Raw Normal View History

2021-01-20 08:08:14 +00:00
package bbgo
import (
2021-02-16 08:40:11 +00:00
"fmt"
"sync"
"time"
2021-02-16 08:40:11 +00:00
2021-01-20 08:08:14 +00:00
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
"github.com/slack-go/slack"
2021-01-20 08:08:14 +00:00
)
type Position struct {
Symbol string `json:"symbol"`
BaseCurrency string `json:"baseCurrency"`
QuoteCurrency string `json:"quoteCurrency"`
2021-01-20 08:15:34 +00:00
Base fixedpoint.Value `json:"base"`
Quote fixedpoint.Value `json:"quote"`
AverageCost fixedpoint.Value `json:"averageCost"`
ExchangeFeeRates map[types.ExchangeName]fixedpoint.Value `json:"exchangeFeeRates"`
sync.Mutex
2021-01-20 08:08:14 +00:00
}
func (p *Position) SetExchangeFeeRate(ex types.ExchangeName, rate fixedpoint.Value) {
if p.ExchangeFeeRates == nil {
p.ExchangeFeeRates = make(map[types.ExchangeName]fixedpoint.Value)
}
p.ExchangeFeeRates[ex] = rate
}
func (p *Position) SlackAttachment() slack.Attachment {
2021-05-15 17:16:03 +00:00
p.Lock()
averageCost := p.AverageCost
base := p.Base
quote := p.Quote
p.Unlock()
var posType = ""
var color = ""
if p.Base == 0 {
color = "#cccccc"
posType = "Closed"
} else if p.Base > 0 {
posType = "Long"
color = "#228B22"
} else if p.Base < 0 {
posType = "Short"
color = "#DC143C"
}
title := util.Render(posType+` Position {{ .Symbol }} `, p)
return slack.Attachment{
// Pretext: "",
// Text: text,
Title: title,
Color: color,
Fields: []slack.AttachmentField{
2021-05-15 17:16:03 +00:00
{Title: "Average Cost", Value: util.FormatFloat(averageCost.Float64(), 2), Short: true},
{Title: p.BaseCurrency, Value: util.FormatFloat(base.Float64(), 4), Short: true},
{Title: p.QuoteCurrency, Value: util.FormatFloat(quote.Float64(), 2)},
},
Footer: util.Render("update time {{ . }}", time.Now().Format(time.RFC822)),
// FooterIcon: "",
}
}
func (p *Position) PlainText() string {
2021-05-15 16:45:08 +00:00
return fmt.Sprintf("Position %s: average cost = %f, base = %f, quote = %f",
p.Symbol,
p.AverageCost.Float64(),
p.Base.Float64(),
p.Quote.Float64(),
)
}
2021-05-15 02:02:04 +00:00
func (p *Position) String() string {
return fmt.Sprintf("POSITION %s: average cost = %f, base = %f, quote = %f",
2021-02-16 08:40:11 +00:00
p.Symbol,
p.AverageCost.Float64(),
p.Base.Float64(),
p.Quote.Float64(),
)
}
func (p *Position) BindStream(stream types.Stream) {
stream.OnTradeUpdate(func(trade types.Trade) {
2021-01-20 09:37:23 +00:00
if p.Symbol == trade.Symbol {
p.AddTrade(trade)
}
})
}
func (p *Position) AddTrades(trades []types.Trade) (fixedpoint.Value, bool) {
var totalProfitAmount fixedpoint.Value
for _, trade := range trades {
if profitAmount, profit := p.AddTrade(trade); profit {
totalProfitAmount += profitAmount
}
}
return totalProfitAmount, totalProfitAmount != 0
}
2021-01-20 08:08:14 +00:00
func (p *Position) AddTrade(t types.Trade) (fixedpoint.Value, bool) {
p.Lock()
defer p.Unlock()
2021-01-20 08:08:14 +00:00
price := fixedpoint.NewFromFloat(t.Price)
quantity := fixedpoint.NewFromFloat(t.Quantity)
2021-01-20 16:49:01 +00:00
quoteQuantity := fixedpoint.NewFromFloat(t.QuoteQuantity)
fee := fixedpoint.NewFromFloat(t.Fee)
switch t.FeeCurrency {
case p.BaseCurrency:
quantity -= fee
case p.QuoteCurrency:
quoteQuantity -= fee
}
2021-01-20 08:08:14 +00:00
// Base > 0 means we're in long position
// Base < 0 means we're in short position
2021-01-20 08:08:14 +00:00
switch t.Side {
case types.SideTypeBuy:
if p.Base < 0 {
// handling short-to-long position
if p.Base+quantity > 0 {
closingProfit := (p.AverageCost - price).Mul(-p.Base)
p.Base += quantity
2021-01-20 16:49:01 +00:00
p.Quote -= quoteQuantity
p.AverageCost = price
return closingProfit, true
} else {
// covering short position
p.Base += quantity
2021-01-20 16:49:01 +00:00
p.Quote -= quoteQuantity
return (p.AverageCost - price).Mul(quantity), true
}
}
2021-01-20 08:08:14 +00:00
2021-01-20 16:49:01 +00:00
p.AverageCost = (p.AverageCost.Mul(p.Base) + quoteQuantity).Div(p.Base + quantity)
2021-01-20 08:08:14 +00:00
p.Base += quantity
2021-01-20 16:49:01 +00:00
p.Quote -= quoteQuantity
2021-01-20 08:08:14 +00:00
return 0, false
case types.SideTypeSell:
if p.Base > 0 {
// long-to-short
if p.Base-quantity < 0 {
closingProfit := (price - p.AverageCost).Mul(p.Base)
p.Base -= quantity
2021-01-20 16:49:01 +00:00
p.Quote += quoteQuantity
p.AverageCost = price
return closingProfit, true
} else {
p.Base -= quantity
2021-01-20 16:49:01 +00:00
p.Quote += quoteQuantity
return (price - p.AverageCost).Mul(quantity), true
}
}
// handling short position
2021-01-20 16:49:01 +00:00
p.AverageCost = (p.AverageCost.Mul(-p.Base) + quoteQuantity).Div(-p.Base + quantity)
2021-01-20 08:08:14 +00:00
p.Base -= quantity
2021-01-20 16:49:01 +00:00
p.Quote += quoteQuantity
2021-01-20 08:08:14 +00:00
return 0, false
2021-01-20 08:08:14 +00:00
}
return 0, false
}