forked from Beastlorion/Dexalot-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
normalize.go
91 lines (84 loc) · 3.01 KB
/
normalize.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
88
89
90
91
package orders
import (
"github.com/Abso1ut3Zer0/Dexalot-go/src/marketdata/instr"
"github.com/Abso1ut3Zer0/Dexalot-go/src/marketdata/types/side"
"github.com/Abso1ut3Zer0/Dexalot-go/src/trading/refdata"
)
func NormalizeMakerWithPrecision[T instr.Instrument](order *Limit[T], pricePrecision, qtyPrecision int) error {
err := RoundPricePassiveWithPrecision(order, pricePrecision)
if err != nil {
return err
}
RoundQtyWithPrecision(order, qtyPrecision)
return nil
}
func NormalizeTakerWithPrecision[T instr.Instrument](order *Limit[T], pricePrecision, qtyPrecision int) error {
switch order.Side {
case side.BUY:
qtyTerm := order.Qty * order.Price // we can't breach the total term qty due to inventory constraints
RoundPriceAggressiveWithPrecision(order, pricePrecision)
order.Qty = qtyTerm / order.Price
RoundQtyWithPrecision(order, qtyPrecision)
return nil
case side.SELL:
RoundPriceAggressiveWithPrecision(order, pricePrecision)
RoundQtyWithPrecision(order, qtyPrecision)
return nil
default:
return NoSideError
}
}
func NormalizeMakerWithIncrement[T instr.Instrument](order *Limit[T], priceIncrement, qtyIncrement float64) error {
err := RoundPricePassiveWithIncrement(order, priceIncrement)
if err != nil {
return err
}
RoundQtyWithIncrement(order, qtyIncrement)
return nil
}
func NormalizeTakerWithIncrement[T instr.Instrument](order *Limit[T], priceIncrement, qtyIncrement float64) error {
switch order.Side {
case side.BUY:
qtyTerm := order.Qty * order.Price // we can't breach the total term qty due to inventory constraints
RoundPriceAggressiveWithIncrement(order, priceIncrement)
order.Qty = qtyTerm / order.Price
RoundQtyWithIncrement(order, qtyIncrement)
return nil
case side.SELL:
RoundPriceAggressiveWithIncrement(order, priceIncrement)
RoundQtyWithIncrement(order, qtyIncrement)
return nil
default:
return NoSideError
}
}
func NormalizeMakerWithRefData[T instr.Instrument](order *Limit[T], refData *refdata.Composite) error {
if refData == nil || refData.PriceQty == nil {
return refdata.NilRefDataError
}
switch refData.PriceQty.(type) {
case *refdata.Precision:
rd, _ := refData.PriceQty.(*refdata.Precision)
return NormalizeMakerWithPrecision(order, rd.PricePrecision, rd.QtyPrecision)
case *refdata.Increment:
rd, _ := refData.PriceQty.(*refdata.Increment)
return NormalizeMakerWithIncrement(order, rd.PriceIncrement, rd.QtyIncrement)
default:
return refdata.UnsupportedRefDataError
}
}
func NormalizeTakerWithRefData[T instr.Instrument](order *Limit[T], refData *refdata.Composite) error {
if refData == nil || refData.PriceQty == nil {
return refdata.NilRefDataError
}
switch refData.PriceQty.(type) {
case *refdata.Precision:
rd, _ := refData.PriceQty.(*refdata.Precision)
return NormalizeTakerWithPrecision(order, rd.PricePrecision, rd.QtyPrecision)
case *refdata.Increment:
rd, _ := refData.PriceQty.(*refdata.Increment)
return NormalizeTakerWithIncrement(order, rd.PriceIncrement, rd.QtyIncrement)
default:
return refdata.UnsupportedRefDataError
}
}