bbgo_origin/pkg/style/pnl.go

67 lines
1.1 KiB
Go
Raw Normal View History

2022-08-22 18:12:26 +00:00
package style
import (
"github.com/c9s/bbgo/pkg/fixedpoint"
)
var LossEmoji = "🔥"
var ProfitEmoji = "💰"
2024-11-15 02:49:56 +00:00
// 0.1% = 10 bps
2022-08-22 18:12:26 +00:00
var DefaultPnLLevelResolution = fixedpoint.NewFromFloat(0.001)
2024-11-15 02:49:56 +00:00
const MaxEmojiRepeat = 6
2022-08-22 18:12:26 +00:00
func PnLColor(pnl fixedpoint.Value) string {
if pnl.Sign() > 0 {
return GreenColor
}
return RedColor
}
func PnLSignString(pnl fixedpoint.Value) string {
if pnl.Sign() > 0 {
return "+" + pnl.String()
}
return pnl.String()
}
func PnLEmojiSimple(pnl fixedpoint.Value) string {
if pnl.Sign() < 0 {
return LossEmoji
}
if pnl.IsZero() {
return ""
}
return ProfitEmoji
}
func PnLEmojiMargin(pnl, margin, resolution fixedpoint.Value) (out string) {
if margin.IsZero() {
return PnLEmojiSimple(pnl)
}
if pnl.Sign() < 0 {
out = LossEmoji
2024-11-15 02:49:56 +00:00
level := max((margin.Neg()).Div(resolution).Int(), MaxEmojiRepeat)
2022-08-22 18:12:26 +00:00
for i := 1; i < level; i++ {
out += LossEmoji
}
return out
}
if pnl.IsZero() {
return out
}
out = ProfitEmoji
2024-11-15 02:49:56 +00:00
level := max(margin.Div(resolution).Int(), MaxEmojiRepeat)
2022-08-22 18:12:26 +00:00
for i := 1; i < level; i++ {
out += ProfitEmoji
}
2024-11-15 02:49:56 +00:00
2022-08-22 18:12:26 +00:00
return out
}