-
Notifications
You must be signed in to change notification settings - Fork 1
/
trading.go
49 lines (40 loc) · 1.37 KB
/
trading.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
package porttools
import (
"errors"
"github.com/jakeschurch/porttools/instrument"
"github.com/jakeschurch/porttools/order"
)
var (
// ErrOrderNotValid indicates that an order is not valid, and should not be sent to the pipeline.
ErrOrderNotValid = errors.New("Order does not meet criteria as a valid order")
)
// Algorithm is an interface that needs to be implemented in the pipeline by a user to fill orders based on the conditions that they specify.
type Algorithm interface {
EntryCheck(instrument.Quote) (*order.Order, error)
ExitCheck(order.Order, instrument.Tick) (*order.Order, error)
}
// ------------------------------------------------------------------
// Strategy ...
type Strategy struct {
Algorithm Algorithm
}
// NewStrategy creates a new Strategy instance used in the backtesting process.
func NewStrategy(a Algorithm) Strategy {
return Strategy{
Algorithm: a,
}
}
// CheckEntryLogic ...TODO
func (s Strategy) CheckEntryLogic(q instrument.Quote) (entryOrder *order.Order, err error) {
if entryOrder, err = s.Algorithm.EntryCheck(q); err != nil {
return nil, ErrOrderNotValid
}
return entryOrder, nil
}
// CheckExitLogic ...TODO
func (s Strategy) CheckExitLogic(o order.Order, t instrument.Tick) (exitOrder *order.Order, err error) {
if exitOrder, err = s.Algorithm.ExitCheck(o, t); err != nil {
return nil, ErrOrderNotValid
}
return exitOrder, nil
}