forked from sdcoffey/techan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
strategy.go
41 lines (33 loc) · 1.3 KB
/
strategy.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
package techan
// Strategy is an interface that describes desired entry and exit trading behavior
type Strategy interface {
ShouldEnter(index int, record *TradingRecord) bool
ShouldExit(index int, record *TradingRecord) bool
}
// RuleStrategy is a strategy based on rules and an unstable period. The two rules determine whether a position should
// be created or closed, and the unstable period is an index before no positions should be created or exited
type RuleStrategy struct {
EntryRule Rule
ExitRule Rule
UnstablePeriod int
}
// ShouldEnter will return true when the index is less than the unstable period and the entry rule is satisfied
func (rs RuleStrategy) ShouldEnter(index int, record *TradingRecord) bool {
if rs.EntryRule == nil {
panic("entry rule cannot be nil")
}
if index > rs.UnstablePeriod && record.CurrentPosition().IsNew() {
return rs.EntryRule.IsSatisfied(index, record)
}
return false
}
// ShouldExit will return true when the index is less than the unstable period and the exit rule is satisfied
func (rs RuleStrategy) ShouldExit(index int, record *TradingRecord) bool {
if rs.ExitRule == nil {
panic("exit rule cannot be nil")
}
if index > rs.UnstablePeriod && record.CurrentPosition().IsOpen() {
return rs.ExitRule.IsSatisfied(index, record)
}
return false
}