-
Notifications
You must be signed in to change notification settings - Fork 3
/
tick.go
219 lines (190 loc) · 5.54 KB
/
tick.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package amm
import (
"math"
"math/big"
"math/rand"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// TickPrecision represents a tick precision.
type TickPrecision int
func (prec TickPrecision) PriceToDownTick(price sdk.Dec) sdk.Dec {
return PriceToDownTick(price, int(prec))
}
func (prec TickPrecision) PriceToUpTick(price sdk.Dec) sdk.Dec {
return PriceToUpTick(price, int(prec))
}
func (prec TickPrecision) UpTick(price sdk.Dec) sdk.Dec {
return UpTick(price, int(prec))
}
func (prec TickPrecision) DownTick(price sdk.Dec) sdk.Dec {
return DownTick(price, int(prec))
}
func (prec TickPrecision) HighestTick() sdk.Dec {
return HighestTick(int(prec))
}
func (prec TickPrecision) LowestTick() sdk.Dec {
return LowestTick(int(prec))
}
func (prec TickPrecision) TickToIndex(tick sdk.Dec) int {
return TickToIndex(tick, int(prec))
}
func (prec TickPrecision) TickFromIndex(i int) sdk.Dec {
return TickFromIndex(i, int(prec))
}
func (prec TickPrecision) RoundPrice(price sdk.Dec) sdk.Dec {
return RoundPrice(price, int(prec))
}
func (prec TickPrecision) TickGap(price sdk.Dec) sdk.Dec {
return TickGap(price, int(prec))
}
func (prec TickPrecision) RandomTick(r *rand.Rand, minPrice, maxPrice sdk.Dec) sdk.Dec {
return RandomTick(r, minPrice, maxPrice, int(prec))
}
// char returns the characteristic(integral part) of
// log10(x * pow(10, sdk.Precision)).
func char(x sdk.Dec) int {
if x.IsZero() {
panic("cannot calculate log10 for 0")
}
return len(x.BigInt().Text(10)) - 1
}
// pow10 returns pow(10, n - sdk.Precision).
func pow10(n int) sdk.Dec {
x := big.NewInt(10)
x.Exp(x, big.NewInt(int64(n)), nil)
return sdk.NewDecFromBigIntWithPrec(x, sdk.Precision)
}
// isPow10 returns whether x is a power of 10 or not.
func isPow10(x sdk.Dec) bool {
b := x.BigInt()
if b.Sign() <= 0 {
return false
}
ten := big.NewInt(10)
if b.Cmp(ten) == 0 {
return true
}
zero := big.NewInt(0)
m := new(big.Int)
for b.Cmp(ten) >= 0 {
b.DivMod(b, ten, m)
if m.Cmp(zero) != 0 {
return false
}
}
return b.Cmp(big.NewInt(1)) == 0
}
// PriceToDownTick returns the highest price tick under(or equal to) the price.
func PriceToDownTick(price sdk.Dec, prec int) sdk.Dec {
b := price.BigInt()
l := char(price)
d := int64(l - prec)
if d > 0 {
p := big.NewInt(10)
p.Exp(p, big.NewInt(d), nil)
b.Quo(b, p).Mul(b, p)
}
return sdk.NewDecFromBigIntWithPrec(b, sdk.Precision)
}
// PriceToUpTick returns the lowest price tick greater or equal than
// the price.
func PriceToUpTick(price sdk.Dec, prec int) sdk.Dec {
tick := PriceToDownTick(price, prec)
if !tick.Equal(price) {
return UpTick(tick, prec)
}
return tick
}
// UpTick returns the next lowest price tick above the price.
func UpTick(price sdk.Dec, prec int) sdk.Dec {
tick := PriceToDownTick(price, prec)
if tick.Equal(price) {
l := char(price)
return price.Add(pow10(l - prec))
}
l := char(tick)
return tick.Add(pow10(l - prec))
}
// DownTick returns the next highest price tick under the price.
// DownTick doesn't check if the price is the lowest price tick.
func DownTick(price sdk.Dec, prec int) sdk.Dec {
tick := PriceToDownTick(price, prec)
if tick.Equal(price) {
l := char(price)
var d sdk.Dec
if isPow10(price) {
d = pow10(l - prec - 1)
} else {
d = pow10(l - prec)
}
return price.Sub(d)
}
return tick
}
// HighestTick returns the highest possible price tick.
func HighestTick(prec int) sdk.Dec {
i := big.NewInt(2)
// Maximum 315 bits possible, but take slightly less value for safety.
i.Exp(i, big.NewInt(300), nil).Sub(i, big.NewInt(1))
return PriceToDownTick(sdk.NewDecFromBigIntWithPrec(i, sdk.Precision), prec)
}
// LowestTick returns the lowest possible price tick.
func LowestTick(prec int) sdk.Dec {
return sdk.NewDecWithPrec(1, int64(sdk.Precision-prec))
}
// TickToIndex returns a tick index for given price.
// Tick index 0 means the lowest possible price fit in ticks.
func TickToIndex(price sdk.Dec, prec int) int {
b := price.BigInt()
l := len(b.Text(10)) - 1
d := int64(l - prec)
if d > 0 {
q := big.NewInt(10)
q.Exp(q, big.NewInt(d), nil)
b.Quo(b, q)
}
p := int(math.Pow10(prec))
b.Sub(b, big.NewInt(int64(p)))
return (l-prec)*9*p + int(b.Int64())
}
// TickFromIndex returns a price for given tick index.
// See TickToIndex for more details about tick indices.
func TickFromIndex(i, prec int) sdk.Dec {
p := int(math.Pow10(prec))
l := i/(9*p) + prec
t := big.NewInt(int64(p + i%(p*9)))
if l > prec {
m := big.NewInt(10)
m.Exp(m, big.NewInt(int64(l-prec)), nil)
t.Mul(t, m)
}
return sdk.NewDecFromBigIntWithPrec(t, sdk.Precision)
}
// RoundTickIndex returns rounded tick index using banker's rounding.
func RoundTickIndex(i int) int {
return (i + 1) / 2 * 2
}
// RoundPrice returns rounded price using banker's rounding.
func RoundPrice(price sdk.Dec, prec int) sdk.Dec {
tick := PriceToDownTick(price, prec)
if price.Equal(tick) {
return price
}
return TickFromIndex(RoundTickIndex(TickToIndex(tick, prec)), prec)
}
// TickGap returns tick gap at given price.
func TickGap(price sdk.Dec, prec int) sdk.Dec {
tick := PriceToDownTick(price, prec)
l := char(tick)
return pow10(l - prec)
}
// RandomTick returns a random tick within range [minPrice, maxPrice].
// If prices are not on ticks, then prices are adjusted to the nearest
// ticks.
func RandomTick(r *rand.Rand, minPrice, maxPrice sdk.Dec, prec int) sdk.Dec {
minPrice = PriceToUpTick(minPrice, prec)
maxPrice = PriceToDownTick(maxPrice, prec)
minPriceIdx := TickToIndex(minPrice, prec)
maxPriceIdx := TickToIndex(maxPrice, prec)
return TickFromIndex(minPriceIdx+r.Intn(maxPriceIdx-minPriceIdx), prec)
}