forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
position_test.go
158 lines (121 loc) · 3.38 KB
/
position_test.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
package techan
import (
"testing"
"time"
"github.com/sdcoffey/big"
"github.com/stretchr/testify/assert"
)
func TestPosition_NoOrders_IsNew(t *testing.T) {
position := new(Position)
assert.True(t, position.IsNew())
}
func TestPosition_NewPosition_IsOpen(t *testing.T) {
order := Order{
Side: BUY,
Amount: big.ONE,
Price: big.NewFromString("2"),
}
position := NewPosition(order)
assert.True(t, position.IsOpen())
assert.False(t, position.IsNew())
assert.False(t, position.IsClosed())
}
func TestNewPosition_WithBuy_IsLong(t *testing.T) {
order := Order{
Side: BUY,
Amount: big.ONE,
Price: big.NewFromString("2"),
}
position := NewPosition(order)
assert.True(t, position.IsLong())
}
func TestNewPosition_WithSell_IsShort(t *testing.T) {
order := Order{
Side: SELL,
Amount: big.ONE,
Price: big.NewFromString("2"),
}
position := NewPosition(order)
assert.True(t, position.IsShort())
}
func TestPosition_Enter(t *testing.T) {
position := new(Position)
order := Order{
Side: BUY,
Amount: big.ONE,
Price: big.NewFromString("2"),
}
position.Enter(order)
assert.True(t, position.IsOpen())
assert.EqualValues(t, order.Amount, position.EntranceOrder().Amount)
assert.EqualValues(t, order.Price, position.EntranceOrder().Price)
assert.EqualValues(t, order.ExecutionTime, position.EntranceOrder().ExecutionTime)
}
func TestPosition_Close(t *testing.T) {
position := new(Position)
entranceOrder := Order{
Side: BUY,
Amount: big.ONE,
Price: big.NewFromString("2"),
}
position.Enter(entranceOrder)
assert.True(t, position.IsOpen())
assert.EqualValues(t, entranceOrder.Amount, position.EntranceOrder().Amount)
assert.EqualValues(t, entranceOrder.Price, position.EntranceOrder().Price)
assert.EqualValues(t, entranceOrder.ExecutionTime, position.EntranceOrder().ExecutionTime)
exitOrder := Order{
Side: SELL,
Amount: big.ONE,
Price: big.NewFromString("4"),
ExecutionTime: time.Now(),
}
position.Exit(exitOrder)
assert.True(t, position.IsClosed())
assert.EqualValues(t, exitOrder.Amount, position.ExitOrder().Amount)
assert.EqualValues(t, exitOrder.Price, position.ExitOrder().Price)
assert.EqualValues(t, exitOrder.ExecutionTime, position.ExitOrder().ExecutionTime)
}
func TestPosition_CostBasis(t *testing.T) {
t.Run("When entrance order nil, returns 0", func(t *testing.T) {
p := new(Position)
assert.EqualValues(t, "0", p.CostBasis().String())
})
t.Run("When entracne order not nil, returns cost basis", func(t *testing.T) {
p := new(Position)
order := Order{
Side: BUY,
Amount: big.ONE,
Price: big.NewFromString("2"),
}
p.Enter(order)
assert.EqualValues(t, "2.00", p.CostBasis().FormattedString(2))
})
}
func TestPosition_ExitValue(t *testing.T) {
t.Run("when not closed, returns 0", func(t *testing.T) {
p := new(Position)
order := Order{
Side: BUY,
Amount: big.ONE,
Price: big.NewFromString("2"),
}
p.Enter(order)
assert.EqualValues(t, "0.00", p.ExitValue().FormattedString(2))
})
t.Run("when closed, returns exit value", func(t *testing.T) {
p := new(Position)
order := Order{
Side: BUY,
Amount: big.ONE,
Price: big.NewFromString("2"),
}
p.Enter(order)
order = Order{
Side: SELL,
Amount: big.ONE,
Price: big.NewFromString("12"),
}
p.Exit(order)
assert.EqualValues(t, "12.00", p.ExitValue().FormattedString(2))
})
}