forked from llehouerou/go-degiro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposition.go
132 lines (116 loc) · 2.76 KB
/
position.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
package degiro
import (
"sync"
log "github.com/sirupsen/logrus"
)
type PositionCache struct {
cache []Position
mu sync.RWMutex
}
func newPositionCache() PositionCache {
return PositionCache{cache: []Position{}}
}
func (c *PositionCache) Clear() {
c.mu.Lock()
defer c.mu.Unlock()
c.cache = []Position{}
}
func (c *PositionCache) Add(positions []Position) {
c.mu.Lock()
defer c.mu.Unlock()
c.cache = append(c.cache, positions...)
}
func (c *PositionCache) Update(positions []Position) {
c.mu.Lock()
defer c.mu.Unlock()
for i, position := range c.cache {
for _, p := range positions {
if position.ProductId == p.ProductId {
position.Size = p.Size
c.cache[i] = position
}
}
}
}
func (c *PositionCache) Remove(positionsIds []string) {
c.mu.Lock()
defer c.mu.Unlock()
cacheloop:
for i := len(c.cache) - 1; i >= 0; i-- {
for _, id := range positionsIds {
if c.cache[i].ProductId == id {
c.cache[i] = c.cache[len(c.cache)-1]
c.cache = c.cache[:len(c.cache)-1]
continue cacheloop
}
}
}
}
func (c *PositionCache) Get(productId string) []Position {
c.mu.RLock()
defer c.mu.RUnlock()
var res []Position
for _, position := range c.cache {
if position.ProductId == productId {
res = append(res, position)
}
}
return res
}
type Position struct {
ProductId string
Size int
}
type updatePositionsResponse struct {
LastUpdated int `json:"lastUpdated"`
Positions []updatePositionResponse `json:"value"`
}
func (r updatePositionsResponse) ConvertToPositions() (added []Position, updated []Position, removed []string) {
for _, position := range r.Positions {
if position.IsRemoved {
removed = append(removed, position.Id)
continue
}
newPosition, err := position.ConvertToPosition()
if err != nil {
log.Warnf("converting response to position list: %v", err)
continue
}
if position.IsAdded {
added = append(added, newPosition)
continue
}
updated = append(updated, newPosition)
}
return added, updated, removed
}
type updatePositionResponse struct {
Id string `json:"id"`
IsAdded bool `json:"isAdded"`
IsRemoved bool `json:"isRemoved"`
Value []struct {
Name string `json:"name"`
Value interface{} `json:"value"`
} `json:"value"`
}
func (r updatePositionResponse) ConvertToPosition() (Position, error) {
res := Position{}
res.ProductId = r.Id
for _, property := range r.Value {
switch property.Name {
case "size":
res.Size = int(property.Value.(float64))
}
}
return res, nil
}
func (c *Client) GetOpenedPositionForProduct(productId string) (Position, bool) {
positions := c.positions.Get(productId)
if len(positions) == 0 {
return Position{}, false
}
if positions[0].Size == 0 {
return Position{}, false
}
return positions[0], true
}