-
-
Notifications
You must be signed in to change notification settings - Fork 274
/
order_processor.go
61 lines (50 loc) · 1.93 KB
/
order_processor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package bbgo
import (
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/pkg/errors"
)
var (
ErrQuoteBalanceLevelTooLow = errors.New("quote balance level is too low")
ErrInsufficientQuoteBalance = errors.New("insufficient quote balance")
ErrAssetBalanceLevelTooLow = errors.New("asset balance level too low")
ErrInsufficientAssetBalance = errors.New("insufficient asset balance")
ErrAssetBalanceLevelTooHigh = errors.New("asset balance level too high")
)
// AdjustQuantityByMaxAmount adjusts the quantity to make the amount greater than the given minAmount
func AdjustQuantityByMaxAmount(quantity, currentPrice, maxAmount fixedpoint.Value) fixedpoint.Value {
// modify quantity for the min amount
amount := currentPrice.Mul(quantity)
if amount.Compare(maxAmount) < 0 {
return quantity
}
ratio := maxAmount.Div(amount)
return quantity.Mul(ratio)
}
// AdjustQuantityByMinAmount adjusts the quantity to make the amount greater than the given minAmount
func AdjustQuantityByMinAmount(quantity, currentPrice, minAmount fixedpoint.Value) fixedpoint.Value {
// modify quantity for the min amount
amount := currentPrice.Mul(quantity)
if amount.Compare(minAmount) < 0 {
ratio := minAmount.Div(amount)
quantity = quantity.Mul(ratio)
}
return quantity
}
// AdjustFloatQuantityByMinAmount adjusts the quantity to make the amount greater than the given minAmount
func AdjustFloatQuantityByMinAmount(quantity, currentPrice, minAmount fixedpoint.Value) fixedpoint.Value {
// modify quantity for the min amount
amount := currentPrice.Mul(quantity)
if amount.Compare(minAmount) < 0 {
ratio := minAmount.Div(amount)
return quantity.Mul(ratio)
}
return quantity
}
func AdjustFloatQuantityByMaxAmount(quantity fixedpoint.Value, price fixedpoint.Value, maxAmount fixedpoint.Value) fixedpoint.Value {
amount := price.Mul(quantity)
if amount.Compare(maxAmount) > 0 {
ratio := maxAmount.Div(amount)
return quantity.Mul(ratio)
}
return quantity
}