style: fix PnLEmojiMargin

This commit is contained in:
c9s 2024-11-15 15:56:17 +08:00
parent 02151afc2d
commit 8922ed78bf
No known key found for this signature in database
GPG Key ID: 7385E7E464CB0A54

View File

@ -1,6 +1,8 @@
package style
import (
"strings"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
@ -27,40 +29,32 @@ func PnLSignString(pnl fixedpoint.Value) string {
}
func PnLEmojiSimple(pnl fixedpoint.Value) string {
if pnl.IsZero() {
return ""
}
if pnl.Sign() < 0 {
return LossEmoji
} else {
return ProfitEmoji
}
}
// PnLEmojiMargin returns the emoji representation of the PnL with margin and resolution
func PnLEmojiMargin(pnl, margin, resolution fixedpoint.Value) string {
if margin.IsZero() {
return PnLEmojiSimple(pnl)
}
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
level := max((margin.Neg()).Div(resolution).Int(), MaxEmojiRepeat)
for i := 1; i < level; i++ {
out += LossEmoji
}
return out
level := min((margin.Neg()).Div(resolution).Int(), MaxEmojiRepeat)
return strings.Repeat(LossEmoji, level)
}
if pnl.IsZero() {
return out
}
out = ProfitEmoji
level := max(margin.Div(resolution).Int(), MaxEmojiRepeat)
for i := 1; i < level; i++ {
out += ProfitEmoji
}
return out
level := min(margin.Div(resolution).Int(), MaxEmojiRepeat)
return strings.Repeat(ProfitEmoji, level)
}