forked from Beastlorion/Dexalot-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rounding.go
87 lines (77 loc) · 2.66 KB
/
rounding.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package orders
import (
"github.com/Abso1ut3Zer0/Dexalot-go/src/floats"
"github.com/Abso1ut3Zer0/Dexalot-go/src/marketdata/instr"
"github.com/Abso1ut3Zer0/Dexalot-go/src/marketdata/types/side"
"github.com/Abso1ut3Zer0/Dexalot-go/src/price"
)
const bpsToRawFactor = 10_000.0
func Skew[T instr.Instrument](order *Limit[T], skewBps float64) {
order.Price = price.Skew(order.Price, skewBps)
}
func RoundPricePassiveWithPrecision[T instr.Instrument](order *Limit[T], precision int) error {
switch order.Side {
case side.BUY:
order.Price = floats.RoundDirectionalWithPrecision(order.Price, precision, floats.RoundDown)
return nil
case side.SELL:
order.Price = floats.RoundDirectionalWithPrecision(order.Price, precision, floats.RoundUp)
return nil
default:
return NoSideError
}
}
func RoundPricePassiveWithIncrement[T instr.Instrument](order *Limit[T], increment float64) error {
switch order.Side {
case side.BUY:
order.Price = floats.RoundDirectionalWithIncrement(order.Price, increment, floats.RoundDown)
return nil
case side.SELL:
order.Price = floats.RoundDirectionalWithIncrement(order.Price, increment, floats.RoundUp)
return nil
default:
return NoSideError
}
}
func RoundPriceAggressiveWithPrecision[T instr.Instrument](order *Limit[T], precision int) error {
switch order.Side {
case side.BUY:
order.Price = floats.RoundDirectionalWithPrecision(order.Price, precision, floats.RoundUp)
return nil
case side.SELL:
order.Price = floats.RoundDirectionalWithPrecision(order.Price, precision, floats.RoundDown)
return nil
default:
return NoSideError
}
}
func RoundPriceAggressiveWithIncrement[T instr.Instrument](order *Limit[T], increment float64) error {
switch order.Side {
case side.BUY:
order.Price = floats.RoundDirectionalWithIncrement(order.Price, increment, floats.RoundUp)
return nil
case side.SELL:
order.Price = floats.RoundDirectionalWithIncrement(order.Price, increment, floats.RoundDown)
return nil
default:
return NoSideError
}
}
func RoundQtyWithPrecision[T instr.Instrument](order *Limit[T], precision int) {
order.Qty = floats.RoundDirectionalWithPrecision(order.Qty, precision, floats.RoundDown)
}
func RoundQtyWithIncrement[T instr.Instrument](order *Limit[T], increment float64) {
order.Qty = floats.RoundDirectionalWithIncrement(order.Qty, increment, floats.RoundDown)
}
func BoundQtyInDealt[T instr.Instrument](order *Limit[T], inventoryInDealt float64) error {
switch order.Side {
case side.BUY:
order.Qty = floats.UpperBound(order.Qty*order.Price, inventoryInDealt) / order.Price
return nil
case side.SELL:
order.Qty = floats.UpperBound(order.Qty, inventoryInDealt)
return nil
default:
return NoSideError
}
}