mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-14 02:53:50 +00:00
fix trailing zero trim
This commit is contained in:
parent
22e4da3775
commit
33801a4fbc
|
@ -90,28 +90,57 @@ func (trade Trade) PositionChange() fixedpoint.Value {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func trimTrailingZero(a string) string {
|
||||||
|
index := strings.Index(a, ".")
|
||||||
|
if index == -1 {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
var c byte
|
||||||
|
var i int
|
||||||
|
for i = len(a) - 1; i >= 0; i-- {
|
||||||
|
c = a[i]
|
||||||
|
if c == '0' {
|
||||||
|
continue
|
||||||
|
} else if c == '.' {
|
||||||
|
return a[0:i]
|
||||||
|
} else {
|
||||||
|
return a[0 : i+1]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func trimTrailingZeroFloat(a float64) string {
|
||||||
|
return trimTrailingZero(fmt.Sprintf("%f", a))
|
||||||
|
}
|
||||||
|
|
||||||
|
// String is for console output
|
||||||
func (trade Trade) String() string {
|
func (trade Trade) String() string {
|
||||||
return fmt.Sprintf("TRADE %s %s %4s %s @ %s orderID %d %s amount %f",
|
return fmt.Sprintf("TRADE %s %s %4s %s @ %s amount %s fee %s %s orderID %d %s",
|
||||||
trade.Exchange.String(),
|
trade.Exchange.String(),
|
||||||
trade.Symbol,
|
trade.Symbol,
|
||||||
trade.Side,
|
trade.Side,
|
||||||
strings.TrimRight(fmt.Sprintf("%f", trade.Quantity), "0"),
|
trimTrailingZeroFloat(trade.Quantity),
|
||||||
strings.TrimRight(fmt.Sprintf("%f", trade.Price), "0"),
|
trimTrailingZeroFloat(trade.Price),
|
||||||
|
trimTrailingZeroFloat(trade.QuoteQuantity),
|
||||||
|
trimTrailingZeroFloat(trade.Fee),
|
||||||
|
trade.FeeCurrency,
|
||||||
trade.OrderID,
|
trade.OrderID,
|
||||||
trade.Time.Time().Format(time.StampMilli),
|
trade.Time.Time().Format(time.StampMilli),
|
||||||
trade.QuoteQuantity)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PlainText is used for telegram-styled messages
|
// PlainText is used for telegram-styled messages
|
||||||
func (trade Trade) PlainText() string {
|
func (trade Trade) PlainText() string {
|
||||||
return fmt.Sprintf("Trade %s %s %s %s @ %s, amount %f, fee %f %s",
|
return fmt.Sprintf("Trade %s %s %s %s @ %s, amount %s, fee %s %s",
|
||||||
trade.Exchange.String(),
|
trade.Exchange.String(),
|
||||||
trade.Symbol,
|
trade.Symbol,
|
||||||
trade.Side,
|
trade.Side,
|
||||||
strings.TrimRight(fmt.Sprintf("%f", trade.Quantity), "0"),
|
trimTrailingZeroFloat(trade.Quantity),
|
||||||
strings.TrimRight(fmt.Sprintf("%f", trade.Price), "0"),
|
trimTrailingZeroFloat(trade.Price),
|
||||||
trade.QuoteQuantity,
|
trimTrailingZeroFloat(trade.QuoteQuantity),
|
||||||
trade.Fee,
|
trimTrailingZeroFloat(trade.Fee),
|
||||||
trade.FeeCurrency)
|
trade.FeeCurrency)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,10 +162,10 @@ func (trade Trade) SlackAttachment() slack.Attachment {
|
||||||
Color: color,
|
Color: color,
|
||||||
Fields: []slack.AttachmentField{
|
Fields: []slack.AttachmentField{
|
||||||
{Title: "Exchange", Value: trade.Exchange.String(), Short: true},
|
{Title: "Exchange", Value: trade.Exchange.String(), Short: true},
|
||||||
{Title: "Price", Value: util.FormatFloat(trade.Price, 2), Short: true},
|
{Title: "Price", Value: trimTrailingZeroFloat(trade.Price), Short: true},
|
||||||
{Title: "Quantity", Value: util.FormatFloat(trade.Quantity, 4), Short: true},
|
{Title: "Quantity", Value: trimTrailingZeroFloat(trade.Quantity), Short: true},
|
||||||
{Title: "QuoteQuantity", Value: util.FormatFloat(trade.QuoteQuantity, 2), Short: true},
|
{Title: "QuoteQuantity", Value: trimTrailingZeroFloat(trade.QuoteQuantity), Short: true},
|
||||||
{Title: "Fee", Value: util.FormatFloat(trade.Fee, 4), Short: true},
|
{Title: "Fee", Value: trimTrailingZeroFloat(trade.Fee), Short: true},
|
||||||
{Title: "FeeCurrency", Value: trade.FeeCurrency, Short: true},
|
{Title: "FeeCurrency", Value: trade.FeeCurrency, Short: true},
|
||||||
{Title: "Liquidity", Value: liquidity, Short: true},
|
{Title: "Liquidity", Value: liquidity, Short: true},
|
||||||
{Title: "Order ID", Value: strconv.FormatUint(trade.OrderID, 10), Short: true},
|
{Title: "Order ID", Value: strconv.FormatUint(trade.OrderID, 10), Short: true},
|
||||||
|
|
50
pkg/types/trade_test.go
Normal file
50
pkg/types/trade_test.go
Normal file
|
@ -0,0 +1,50 @@
|
||||||
|
package types
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_trimTrailingZero(t *testing.T) {
|
||||||
|
type args struct {
|
||||||
|
a string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
args args
|
||||||
|
want string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "trailing floating zero",
|
||||||
|
args: args{
|
||||||
|
a: "1.23400000",
|
||||||
|
},
|
||||||
|
want: "1.234",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "trailing zero of an integer",
|
||||||
|
args: args{
|
||||||
|
a: "1.00000",
|
||||||
|
},
|
||||||
|
want: "1",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "non trailing zero",
|
||||||
|
args: args{
|
||||||
|
a: "1.0001234567",
|
||||||
|
},
|
||||||
|
want: "1.0001234567",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "integer",
|
||||||
|
args: args{
|
||||||
|
a: "1200000",
|
||||||
|
},
|
||||||
|
want: "1200000",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := trimTrailingZero(tt.args.a); got != tt.want {
|
||||||
|
t.Errorf("trimTrailingZero() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user