Merge pull request #173 from c9s/refactor/fixed-point-fee

refactor: use fixedpoint to store fee
This commit is contained in:
YC 2021-03-19 10:08:35 +08:00 committed by GitHub
commit b3f613227a
6 changed files with 15 additions and 15 deletions

View File

@ -47,8 +47,8 @@ type SimplePriceMatching struct {
Account *types.Account
MakerCommission float64 `json:"makerCommission"`
TakerCommission float64 `json:"takerCommission"`
MakerCommission fixedpoint.Value `json:"makerCommission"`
TakerCommission fixedpoint.Value `json:"takerCommission"`
tradeUpdateCallbacks []func(trade types.Trade)
orderUpdateCallbacks []func(order types.Order)
@ -205,9 +205,9 @@ func (m *SimplePriceMatching) newTradeFromOrder(order types.Order, isMaker bool)
// MAX uses 0.050% for maker and 0.15% for taker
var commission = DefaultFeeRate
if isMaker && m.Account.MakerCommission > 0 {
commission = 0.0001 * m.Account.MakerCommission // binance uses 10~15
commission = fixedpoint.NewFromFloat(0.0001).Mul(m.Account.MakerCommission).Float64() // binance uses 10~15
} else if m.Account.TakerCommission > 0 {
commission = 0.0001 * m.Account.TakerCommission // binance uses 10~15
commission = fixedpoint.NewFromFloat(0.0001).Mul(m.Account.TakerCommission).Float64() // binance uses 10~15
}
var fee float64

View File

@ -113,8 +113,8 @@ func (t Backtest) ParseStartTime() (time.Time, error) {
}
type BacktestAccount struct {
MakerCommission float64 `json:"makerCommission"`
TakerCommission float64 `json:"takerCommission"`
MakerCommission fixedpoint.Value `json:"makerCommission"`
TakerCommission fixedpoint.Value `json:"takerCommission"`
BuyerCommission int `json:"buyerCommission"`
SellerCommission int `json:"sellerCommission"`
Balances BacktestAccountBalanceMap `json:"balances" yaml:"balances"`

View File

@ -378,8 +378,8 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
}
a := &types.Account{
MakerCommission: float64(account.MakerCommission),
TakerCommission: float64(account.TakerCommission),
MakerCommission: fixedpoint.NewFromInt64(account.MakerCommission),
TakerCommission: fixedpoint.NewFromInt64(account.TakerCommission),
}
a.UpdateBalances(balances)
return a, nil

View File

@ -75,8 +75,8 @@ func (e *Exchange) QueryAccount(ctx context.Context) (*types.Account, error) {
bps := fixedpoint.NewFromFloat(10000)
a := &types.Account{
MakerCommission: fixedpoint.NewFromFloat(resp.Result.MakerFee).Mul(bps).Float64(),
TakerCommission: fixedpoint.NewFromFloat(resp.Result.TakerFee).Mul(bps).Float64(),
MakerCommission: fixedpoint.NewFromFloat(resp.Result.MakerFee).Mul(bps),
TakerCommission: fixedpoint.NewFromFloat(resp.Result.TakerFee).Mul(bps),
}
balances, err := e.QueryAccountBalances(ctx)

View File

@ -365,6 +365,6 @@ func TestExchange_QueryAccount(t *testing.T) {
expected.Locked = expected.Locked.Sub(expected.Available)
assert.Equal(t, expected, b)
assert.Equal(t, 0.0002*10000, resp.MakerCommission)
assert.Equal(t, 0.0005*10000, resp.TakerCommission)
assert.Equal(t, fixedpoint.NewFromFloat(0.0002).Mul(fixedpoint.NewFromInt64(10000)), resp.MakerCommission)
assert.Equal(t, fixedpoint.NewFromFloat(0.0005).Mul(fixedpoint.NewFromInt64(10000)), resp.TakerCommission)
}

View File

@ -103,9 +103,9 @@ type Account struct {
sync.Mutex `json:"-"`
// bps. 0.15% fee will be 15.
MakerCommission float64 `json:"makerCommission,omitempty"`
TakerCommission float64 `json:"takerCommission,omitempty"`
AccountType string `json:"accountType,omitempty"`
MakerCommission fixedpoint.Value `json:"makerCommission,omitempty"`
TakerCommission fixedpoint.Value `json:"takerCommission,omitempty"`
AccountType string `json:"accountType,omitempty"`
balances BalanceMap
}