-
Notifications
You must be signed in to change notification settings - Fork 2
/
position.go
221 lines (169 loc) · 4.51 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
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
220
221
package gotrader
import (
"github.com/cornelk/hashmap"
"go.uber.org/atomic"
)
// Side represents the type of position, which can be short or long
type Side int
const (
//Short represents a selling position
Short Side = iota
// Long represents a buying position
Long
)
func (s Side) String() string {
names := [...]string{"SHORT", "LONG"}
return names[s]
}
// Position represents the total exposure in a single side of an instrument.
// Is the aggregation of all the trades of that side.
type Position struct {
side Side
trades *hashmap.HashMap
tradesTimeOrder *sortedTrades
tradesNumber *atomic.Int32
units *atomic.Int32
unrealizedNetProfit float64
unrealizedEffectiveProfit float64
marginUsed float64
chargedFees float64
averagePrice float64
}
/**************************
*
* Internal Methods
*
***************************/
func newPosition(side Side) *Position {
return &Position{
side: side,
trades: &hashmap.HashMap{},
tradesTimeOrder: newSortedTrades(),
tradesNumber: atomic.NewInt32(0),
units: atomic.NewInt32(0),
}
}
func (p *Position) openTrade(trade *Trade) {
p.tradesTimeOrder.Append(trade.id)
p.trades.Set(trade.id, trade)
p.tradesNumber.Inc()
p.averagePrice = (p.averagePrice*float64(p.units.Load()) + trade.openPrice*float64(trade.units)) /
float64(p.units.Load()+trade.units)
p.units.Add(trade.units)
trade.calculateMarginUsed()
p.marginUsed += trade.marginUsed
}
func (p *Position) closeTrade(trade *Trade) {
p.tradesTimeOrder.Delete(trade.id)
p.trades.Del(trade.id)
p.tradesNumber.Dec()
p.averagePrice = (p.averagePrice*float64(p.units.Load()) - trade.openPrice*float64(trade.units)) /
float64(p.units.Load()-trade.units)
p.units.Sub(trade.units)
trade.calculateMarginUsed()
p.marginUsed -= trade.marginUsed
}
func (p *Position) calculateUnrealized() {
unrealizedNet := 0.0
unrealizedEffective := 0.0
averagePrice := 0.0 // recalculate to prevent possible cumulative errors
totalUnits := 0.0
chargedFees := 0.0
for kv := range p.trades.Iter() {
trade := kv.Value.(*Trade)
trade.calculateUnrealized()
unrealizedNet += trade.unrealizedNetProfit
unrealizedEffective += trade.unrealizedEffectiveProfit
chargedFees += trade.chargedFees.Load()
averagePrice = (averagePrice*totalUnits + trade.openPrice*float64(trade.units)) / (totalUnits + float64(trade.units))
totalUnits += float64(trade.units)
}
p.unrealizedNetProfit = unrealizedNet
p.unrealizedEffectiveProfit = unrealizedEffective
p.averagePrice = averagePrice
p.chargedFees = chargedFees
}
func (p *Position) calculateMarginUsed() {
marginUsed := 0.0
for kv := range p.trades.Iter() {
trade := kv.Value.(*Trade)
trade.calculateMarginUsed()
marginUsed += trade.marginUsed
}
p.marginUsed = marginUsed
}
/**************************
*
* Accessible Methods
*
***************************/
func (p *Position) Side() Side {
return p.side
}
func (p *Position) TradeByOrder(index int) *Trade {
return p.Trade(p.tradesTimeOrder.Get(index))
}
func (p *Position) TradesByAscendingOrder(tradesNumber int) <-chan *Trade {
ch := make(chan *Trade)
go func() {
for id := range p.tradesTimeOrder.AscendIter(tradesNumber) {
tr, exist := p.trades.GetStringKey(id)
if exist {
ch <- tr.(*Trade)
}
}
close(ch)
}()
return ch
}
func (p *Position) TradesByDescendingOrder(tradesNumber int) <-chan *Trade {
ch := make(chan *Trade)
go func() {
for id := range p.tradesTimeOrder.DescendIter(tradesNumber) {
tr, exist := p.trades.GetStringKey(id)
if exist {
ch <- tr.(*Trade)
}
}
close(ch)
}()
return ch
}
func (p *Position) Trade(id string) *Trade {
trade, exist := p.trades.GetStringKey(id)
if exist {
return trade.(*Trade)
}
return nil
}
func (p *Position) Trades() <-chan *Trade {
ch := make(chan *Trade)
go func() {
for kv := range p.trades.Iter() {
ch <- kv.Value.(*Trade)
}
close(ch)
}()
return ch
}
func (p *Position) TradesNumber() int32 {
return p.tradesNumber.Load()
}
func (p *Position) Units() int32 {
return p.units.Load()
}
func (p *Position) UnrealizedNetProfit() float64 {
return p.unrealizedNetProfit
}
func (p *Position) UnrealizedEffectiveProfit() float64 {
return p.unrealizedEffectiveProfit
}
func (p *Position) MarginUsed() float64 {
return p.marginUsed
}
func (p *Position) ChargedFees() float64 {
return p.chargedFees
}
func (p *Position) AveragePrice() float64 {
return p.averagePrice
}