forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tradingrecord_test.go
105 lines (84 loc) · 2.57 KB
/
tradingrecord_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
package techan
import (
"testing"
"time"
"github.com/sdcoffey/big"
"github.com/stretchr/testify/assert"
)
func TestNewTradingRecord(t *testing.T) {
record := NewTradingRecord()
assert.Len(t, record.Trades, 0)
assert.True(t, record.CurrentPosition().IsNew())
}
func TestTradingRecord_CurrentTrade(t *testing.T) {
record := NewTradingRecord()
yesterday := time.Now().Add(-time.Hour * 24)
record.Operate(Order{
Side: BUY,
Amount: big.ONE,
Price: big.NewFromString("2"),
ExecutionTime: yesterday,
})
assert.EqualValues(t, "1", record.CurrentPosition().EntranceOrder().Amount.String())
assert.EqualValues(t, "2", record.CurrentPosition().EntranceOrder().Price.String())
assert.EqualValues(t, yesterday.UnixNano(),
record.CurrentPosition().EntranceOrder().ExecutionTime.UnixNano())
now := time.Now()
record.Operate(Order{
Side: SELL,
Amount: big.NewFromString("3"),
Price: big.NewFromString("4"),
ExecutionTime: now,
})
assert.True(t, record.CurrentPosition().IsNew())
lastTrade := record.LastTrade()
assert.EqualValues(t, "3", lastTrade.ExitOrder().Amount.String())
assert.EqualValues(t, "4", lastTrade.ExitOrder().Price.String())
assert.EqualValues(t, now.UnixNano(),
lastTrade.ExitOrder().ExecutionTime.UnixNano())
}
func TestTradingRecord_Enter(t *testing.T) {
t.Run("Does not add trades older than last trade", func(t *testing.T) {
record := NewTradingRecord()
now := time.Now()
record.Operate(Order{
Side: BUY,
Amount: big.ONE,
Price: big.NewFromString("2"),
ExecutionTime: now,
})
record.Operate(Order{
Side: SELL,
Amount: big.NewFromString("2"),
Price: big.NewFromString("2"),
ExecutionTime: now.Add(time.Minute),
})
record.Operate(Order{
Side: BUY,
Amount: big.NewFromString("2"),
Price: big.NewFromString("2"),
ExecutionTime: now.Add(-time.Minute),
})
assert.True(t, record.CurrentPosition().IsNew())
assert.Len(t, record.Trades, 1)
})
}
func TestTradingRecord_Exit(t *testing.T) {
t.Run("Does not add trades older than last trade", func(t *testing.T) {
record := NewTradingRecord()
now := time.Now()
record.Operate(Order{
Side: BUY,
Amount: big.ONE,
Price: big.NewFromString("2"),
ExecutionTime: now,
})
record.Operate(Order{
Side: SELL,
Amount: big.NewFromString("2"),
Price: big.NewFromString("2"),
ExecutionTime: now.Add(-time.Minute),
})
assert.True(t, record.CurrentPosition().IsOpen())
})
}