-
Notifications
You must be signed in to change notification settings - Fork 142
/
position.go
78 lines (62 loc) · 2 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
package techan
import "github.com/sdcoffey/big"
// Position is a pair of two Order objects
type Position struct {
orders [2]*Order
}
// NewPosition returns a new Position with the passed-in order as the open order
func NewPosition(openOrder Order) (t *Position) {
t = new(Position)
t.orders[0] = &openOrder
return t
}
// Enter sets the open order to the order passed in
func (p *Position) Enter(order Order) {
p.orders[0] = &order
}
// Exit sets the exit order to the order passed in
func (p *Position) Exit(order Order) {
p.orders[1] = &order
}
// IsLong returns true if the entrance order is a buy order
func (p *Position) IsLong() bool {
return p.EntranceOrder() != nil && p.EntranceOrder().Side == BUY
}
// IsShort returns true if the entrance order is a sell order
func (p *Position) IsShort() bool {
return p.EntranceOrder() != nil && p.EntranceOrder().Side == SELL
}
// IsOpen returns true if there is an entrance order but no exit order
func (p *Position) IsOpen() bool {
return p.EntranceOrder() != nil && p.ExitOrder() == nil
}
// IsClosed returns true of there are both entrance and exit orders
func (p *Position) IsClosed() bool {
return p.EntranceOrder() != nil && p.ExitOrder() != nil
}
// IsNew returns true if there is neither an entrance or exit order
func (p *Position) IsNew() bool {
return p.EntranceOrder() == nil && p.ExitOrder() == nil
}
// EntranceOrder returns the entrance order of this position
func (p *Position) EntranceOrder() *Order {
return p.orders[0]
}
// ExitOrder returns the exit order of this position
func (p *Position) ExitOrder() *Order {
return p.orders[1]
}
// CostBasis returns the price to enter this order
func (p *Position) CostBasis() big.Decimal {
if p.EntranceOrder() != nil {
return p.EntranceOrder().Amount.Mul(p.EntranceOrder().Price)
}
return big.ZERO
}
// ExitValue returns the value accrued by closing the position
func (p *Position) ExitValue() big.Decimal {
if p.IsClosed() {
return p.ExitOrder().Amount.Mul(p.ExitOrder().Price)
}
return big.ZERO
}