-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
reporter.go
151 lines (121 loc) · 3.11 KB
/
reporter.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
package bbgo
import (
"regexp"
"github.com/robfig/cron/v3"
)
type PnLReporter interface {
Run()
}
type baseReporter struct {
notifier Notifier
cron *cron.Cron
environment *Environment
}
type PnLReporterManager struct {
baseReporter
reporters []PnLReporter
}
func NewPnLReporter(notifier Notifier) *PnLReporterManager {
return &PnLReporterManager{
baseReporter: baseReporter{
notifier: notifier,
cron: cron.New(),
},
}
}
func (manager *PnLReporterManager) AverageCostBySymbols(symbols ...string) *AverageCostPnLReporter {
reporter := &AverageCostPnLReporter{
baseReporter: manager.baseReporter,
Symbols: symbols,
}
manager.reporters = append(manager.reporters, reporter)
return reporter
}
type AverageCostPnLReporter struct {
baseReporter
Sessions []string
Symbols []string
}
func (reporter *AverageCostPnLReporter) Of(sessions ...string) *AverageCostPnLReporter {
reporter.Sessions = sessions
return reporter
}
func (reporter *AverageCostPnLReporter) When(specs ...string) *AverageCostPnLReporter {
for _, spec := range specs {
_, err := reporter.cron.AddJob(spec, reporter)
if err != nil {
panic(err)
}
}
return reporter
}
func (reporter *AverageCostPnLReporter) Run() {
// FIXME: this is causing cyclic import
/*
for _, sessionName := range reporter.Sessions {
session := reporter.environment.sessions[sessionName]
calculator := &pnl.AverageCostCalculator{
TradingFeeCurrency: session.Exchange.PlatformFeeCurrency(),
}
for _, symbol := range reporter.Symbols {
report := calculator.NetValue(symbol, session.Trades[symbol].Copy(), session.lastPrices[symbol])
report.Print()
}
}
*/
}
type PatternChannelRouter struct {
routes map[*regexp.Regexp]string
}
func NewPatternChannelRouter(routes map[string]string) *PatternChannelRouter {
router := &PatternChannelRouter{
routes: make(map[*regexp.Regexp]string),
}
if routes != nil {
router.AddRoute(routes)
}
return router
}
func (router *PatternChannelRouter) AddRoute(routes map[string]string) {
if routes == nil {
return
}
if router.routes == nil {
router.routes = make(map[*regexp.Regexp]string)
}
for pattern, channel := range routes {
router.routes[regexp.MustCompile(pattern)] = channel
}
}
func (router *PatternChannelRouter) Route(text string) (channel string, ok bool) {
for pattern, channel := range router.routes {
if pattern.MatchString(text) {
ok = true
return channel, ok
}
}
return channel, ok
}
type ObjectChannelHandler func(obj interface{}) (channel string, ok bool)
type ObjectChannelRouter struct {
routes []ObjectChannelHandler
}
func NewObjectChannelRouter() *ObjectChannelRouter {
return &ObjectChannelRouter{}
}
func (router *ObjectChannelRouter) AddRoute(f ObjectChannelHandler) {
router.routes = append(router.routes, f)
}
func (router *ObjectChannelRouter) Route(obj interface{}) (channel string, ok bool) {
for _, f := range router.routes {
channel, ok = f(obj)
if ok {
return
}
}
return
}
type TradeReporter struct {
*Notifiability
}
const TemplateOrderReport = `:handshake: {{ .Symbol }} {{ .Side }} Order Update @ {{ .Price }}`