-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
strategy.go
180 lines (138 loc) · 4.13 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
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package xnav
import (
"context"
"sync"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/util/templateutil"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/slack-go/slack"
"github.com/c9s/bbgo/pkg/bbgo"
"github.com/c9s/bbgo/pkg/types"
"github.com/c9s/bbgo/pkg/util"
)
const ID = "xnav"
var log = logrus.WithField("strategy", ID)
func init() {
bbgo.RegisterStrategy(ID, &Strategy{})
}
type State struct {
Since int64 `json:"since"`
}
func (s *State) IsOver24Hours() bool {
return types.Over24Hours(time.Unix(s.Since, 0))
}
func (s *State) PlainText() string {
return templateutil.Render(`{{ .Asset }} transfer stats:
daily number of transfers: {{ .DailyNumberOfTransfers }}
daily amount of transfers {{ .DailyAmountOfTransfers.Float64 }}`, s)
}
func (s *State) SlackAttachment() slack.Attachment {
return slack.Attachment{
// Pretext: "",
// Text: text,
Fields: []slack.AttachmentField{},
Footer: templateutil.Render("Since {{ . }}", time.Unix(s.Since, 0).Format(time.RFC822)),
}
}
func (s *State) Reset() {
var beginningOfTheDay = types.BeginningOfTheDay(time.Now().Local())
*s = State{
Since: beginningOfTheDay.Unix(),
}
}
type Strategy struct {
*bbgo.Environment
Interval types.Interval `json:"interval"`
ReportOnStart bool `json:"reportOnStart"`
IgnoreDusts bool `json:"ignoreDusts"`
State *State `persistence:"state"`
}
func (s *Strategy) ID() string {
return ID
}
var Ten = fixedpoint.NewFromInt(10)
func (s *Strategy) CrossSubscribe(sessions map[string]*bbgo.ExchangeSession) {}
func (s *Strategy) recordNetAssetValue(ctx context.Context, sessions map[string]*bbgo.ExchangeSession) {
totalBalances := types.BalanceMap{}
allPrices := map[string]fixedpoint.Value{}
sessionBalances := map[string]types.BalanceMap{}
priceTime := time.Now()
// iterate the sessions and record them
quoteCurrency := "USDT"
for sessionName, session := range sessions {
// update the account balances and the margin information
if _, err := session.UpdateAccount(ctx); err != nil {
log.WithError(err).Errorf("can not update account")
return
}
account := session.GetAccount()
balances := account.Balances()
if err := session.UpdatePrices(ctx, balances.Currencies(), quoteCurrency); err != nil {
log.WithError(err).Error("price update failed")
return
}
sessionBalances[sessionName] = balances
totalBalances = totalBalances.Add(balances)
prices := session.LastPrices()
assets := balances.Assets(prices, priceTime)
// merge prices
for m, p := range prices {
allPrices[m] = p
}
s.Environment.RecordAsset(priceTime, session, assets)
}
displayAssets := types.AssetMap{}
totalAssets := totalBalances.Assets(allPrices, priceTime)
s.Environment.RecordAsset(priceTime, &bbgo.ExchangeSession{Name: "ALL"}, totalAssets)
for currency, asset := range totalAssets {
// calculated if it's dust only when InUSD (usd value) is defined.
if s.IgnoreDusts && !asset.InUSD.IsZero() && asset.InUSD.Compare(Ten) < 0 && asset.InUSD.Compare(Ten.Neg()) > 0 {
continue
}
displayAssets[currency] = asset
}
bbgo.Notify(displayAssets)
if s.State != nil {
if s.State.IsOver24Hours() {
s.State.Reset()
}
bbgo.Sync(ctx, s)
}
}
func (s *Strategy) CrossRun(ctx context.Context, _ bbgo.OrderExecutionRouter, sessions map[string]*bbgo.ExchangeSession) error {
if s.Interval == "" {
return errors.New("interval can not be empty")
}
if s.State == nil {
s.State = &State{}
s.State.Reset()
}
bbgo.OnShutdown(ctx, func(ctx context.Context, wg *sync.WaitGroup) {
defer wg.Done()
bbgo.Sync(ctx, s)
})
if s.ReportOnStart {
s.recordNetAssetValue(ctx, sessions)
}
if s.Environment.BacktestService != nil {
log.Warnf("xnav does not support backtesting")
}
// TODO: if interval is supported, we can use kline as the ticker
if _, ok := types.SupportedIntervals[s.Interval]; ok {
}
go func() {
ticker := time.NewTicker(util.MillisecondsJitter(s.Interval.Duration(), 1000))
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
s.recordNetAssetValue(ctx, sessions)
}
}
}()
return nil
}