-
Notifications
You must be signed in to change notification settings - Fork 22
/
engine.go
161 lines (138 loc) · 4.73 KB
/
engine.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package spot
import (
"errors"
"time"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
)
var (
// ErrTimeSequence signals that time sequence is not in a non-decreasing order.
ErrTimeSequence = errors.New("received a time that's before the last received time")
// ErrNegativeScalingFactor indicates that a negative scaling factor was supplied to the engine.
ErrNegativeScalingFactor = errors.New("scaling factor can't be negative")
)
// Engine allows tracking price changes and verifying them against the theoretical levels implied by the RangeProvider (risk model).
type Engine struct {
marketID string
tWindow time.Duration
sFactor num.Decimal
now time.Time
scheduledTruncate time.Time
current []uint64
previous []timestampedTotalStake
max timestampedTotalStake
positionFactor num.Decimal
}
type timestampedTotalStake struct {
Time time.Time
TotalStake uint64
}
// NewEngine returns a new instance of target stake calculation Engine.
func NewEngine(parameters types.TargetStakeParameters, marketID string, positionFactor num.Decimal) *Engine {
return &Engine{
marketID: marketID,
tWindow: time.Duration(parameters.TimeWindow) * time.Second,
sFactor: parameters.ScalingFactor,
positionFactor: positionFactor,
}
}
// UpdateTimeWindow updates the time windows used in target stake calculation.
func (e *Engine) UpdateTimeWindow(tWindow time.Duration) {
e.tWindow = tWindow
}
// UpdateScalingFactor updates the scaling factor used in target stake calculation
// if it's non-negative and returns an error otherwise.
func (e *Engine) UpdateScalingFactor(sFactor num.Decimal) error {
if sFactor.IsNegative() {
return ErrNegativeScalingFactor
}
e.sFactor = sFactor
return nil
}
// RecordTotalStake records open interest history so that target stake can be calculated.
func (e *Engine) RecordTotalStake(ts uint64, now time.Time) error {
if now.Before(e.now) {
return ErrTimeSequence
}
if ts >= e.max.TotalStake {
e.max = timestampedTotalStake{Time: now, TotalStake: ts}
}
if now.After(e.now) {
// get max from current before updating timestamp
e.previous = append(e.previous, e.getMaxFromCurrent())
e.current = make([]uint64, 0, len(e.current))
e.now = now
}
e.current = append(e.current, ts)
if e.now.After(e.scheduledTruncate) {
e.truncateHistory(e.minTime(now))
}
return nil
}
// GetTargetStake returns target stake based current time.
func (e *Engine) GetTargetStake(now time.Time) *num.Uint {
if minTime := e.minTime(now); minTime.After(e.max.Time) {
e.computeMaxTotalStake(minTime)
}
value, _ := num.UintFromDecimal(num.DecimalFromInt64(int64(e.max.TotalStake)).Mul(e.sFactor))
return value
}
func (e *Engine) UpdateParameters(parameters types.TargetStakeParameters) {
e.sFactor = parameters.ScalingFactor
e.tWindow = time.Duration(parameters.TimeWindow) * time.Second
}
func (e *Engine) getMaxFromCurrent() timestampedTotalStake {
if len(e.current) == 0 {
return timestampedTotalStake{Time: e.now, TotalStake: 0}
}
m := e.current[0]
for i := 1; i < len(e.current); i++ {
if e.current[i] > m {
m = e.current[i]
}
}
return timestampedTotalStake{Time: e.now, TotalStake: m}
}
func (e *Engine) computeMaxTotalStake(minTime time.Time) {
m := e.getMaxFromCurrent()
e.truncateHistory(minTime)
var j int
for i := 0; i < len(e.previous); i++ {
if e.previous[i].TotalStake > m.TotalStake {
m = e.previous[i]
j = i
}
}
e.max = m
// remove entries less than max as these won't ever be needed anyway
e.previous = e.previous[j:]
}
// minTime returns the lower bound of the sliding time window.
func (e *Engine) minTime(now time.Time) time.Time {
return now.Add(-e.tWindow)
}
func (e *Engine) truncateHistory(minTime time.Time) {
var i int
for i = 0; i < len(e.previous); i++ {
if !e.previous[i].Time.Before(minTime) {
break
}
}
e.previous = e.previous[i:]
// Truncate at least every 2 time windows in case not called before to prevent excessive memory usage
e.scheduledTruncate = e.now.Add(2 * e.tWindow)
}