-
Notifications
You must be signed in to change notification settings - Fork 142
/
tradingrecord.go
51 lines (42 loc) · 1.48 KB
/
tradingrecord.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
package techan
// TradingRecord is an object describing a series of trades made and a current position
type TradingRecord struct {
Trades []*Position
currentPosition *Position
}
// NewTradingRecord returns a new TradingRecord
func NewTradingRecord() (t *TradingRecord) {
t = new(TradingRecord)
t.Trades = make([]*Position, 0)
t.currentPosition = new(Position)
return t
}
// CurrentPosition returns the current position in this record
func (tr *TradingRecord) CurrentPosition() *Position {
return tr.currentPosition
}
// LastTrade returns the last trade executed in this record
func (tr *TradingRecord) LastTrade() *Position {
if len(tr.Trades) == 0 {
return nil
}
return tr.Trades[len(tr.Trades)-1]
}
// Operate takes an order and adds it to the current TradingRecord. It will only add the order if:
// - The current position is open and the passed order was executed after the entrance order
// - The current position is new and the passed order was executed after the last exit order
func (tr *TradingRecord) Operate(order Order) {
if tr.currentPosition.IsOpen() {
if order.ExecutionTime.Before(tr.CurrentPosition().EntranceOrder().ExecutionTime) {
return
}
tr.currentPosition.Exit(order)
tr.Trades = append(tr.Trades, tr.currentPosition)
tr.currentPosition = new(Position)
} else if tr.currentPosition.IsNew() {
if tr.LastTrade() != nil && order.ExecutionTime.Before(tr.LastTrade().ExitOrder().ExecutionTime) {
return
}
tr.currentPosition.Enter(order)
}
}