bbgo_origin/pkg/style/pnl.go

61 lines
1.1 KiB
Go
Raw Normal View History

2022-08-22 18:12:26 +00:00
package style
import (
2024-11-15 07:56:17 +00:00
"strings"
2022-08-22 18:12:26 +00:00
"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.IsZero() {
return ""
}
2024-11-15 07:56:17 +00:00
if pnl.Sign() < 0 {
return LossEmoji
} else {
return ProfitEmoji
}
2022-08-22 18:12:26 +00:00
}
2024-11-15 07:56:17 +00:00
// PnLEmojiMargin returns the emoji representation of the PnL with margin and resolution
func PnLEmojiMargin(pnl, margin, resolution fixedpoint.Value) string {
2022-08-22 18:12:26 +00:00
if margin.IsZero() {
return PnLEmojiSimple(pnl)
}
if pnl.IsZero() {
2024-11-15 07:56:17 +00:00
return ""
2022-08-22 18:12:26 +00:00
}
2024-11-15 07:56:17 +00:00
if pnl.Sign() < 0 {
2024-11-15 11:47:31 +00:00
level := min(margin.Abs().Div(resolution).Int(), MaxEmojiRepeat)
2024-11-15 07:56:17 +00:00
return strings.Repeat(LossEmoji, level)
2022-08-22 18:12:26 +00:00
}
2024-11-15 02:49:56 +00:00
2024-11-15 07:56:17 +00:00
level := min(margin.Div(resolution).Int(), MaxEmojiRepeat)
return strings.Repeat(ProfitEmoji, level)
2022-08-22 18:12:26 +00:00
}