-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
asset.go
148 lines (120 loc) · 3.16 KB
/
asset.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
package types
import (
"fmt"
"sort"
"time"
"github.com/slack-go/slack"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
type Asset struct {
Currency string `json:"currency" db:"currency"`
Total fixedpoint.Value `json:"total" db:"total"`
NetAsset fixedpoint.Value `json:"netAsset" db:"net_asset"`
Interest fixedpoint.Value `json:"interest" db:"interest"`
// InUSD is net asset in USD
InUSD fixedpoint.Value `json:"inUSD" db:"net_asset_in_usd"`
// InBTC is net asset in BTC
InBTC fixedpoint.Value `json:"inBTC" db:"net_asset_in_btc"`
Time time.Time `json:"time" db:"time"`
Locked fixedpoint.Value `json:"lock" db:"lock" `
Available fixedpoint.Value `json:"available" db:"available"`
Borrowed fixedpoint.Value `json:"borrowed" db:"borrowed"`
PriceInUSD fixedpoint.Value `json:"priceInUSD" db:"price_in_usd"`
}
type AssetMap map[string]Asset
func (m AssetMap) InUSD() (total fixedpoint.Value) {
for _, a := range m {
if a.InUSD.IsZero() {
continue
}
total = total.Add(a.InUSD)
}
return total
}
func (m AssetMap) PlainText() (o string) {
var assets = m.Slice()
// sort assets
sort.Slice(assets, func(i, j int) bool {
return assets[i].InUSD.Compare(assets[j].InUSD) > 0
})
sumUsd := fixedpoint.Zero
sumBTC := fixedpoint.Zero
for _, a := range assets {
usd := a.InUSD
btc := a.InBTC
if !a.InUSD.IsZero() {
o += fmt.Sprintf(" %s: %s (≈ %s) (≈ %s)",
a.Currency,
a.NetAsset.String(),
USD.FormatMoney(usd),
BTC.FormatMoney(btc),
) + "\n"
sumUsd = sumUsd.Add(usd)
sumBTC = sumBTC.Add(btc)
} else {
o += fmt.Sprintf(" %s: %s",
a.Currency,
a.NetAsset.String(),
) + "\n"
}
}
o += fmt.Sprintf("Net Asset Value: (≈ %s) (≈ %s)",
USD.FormatMoney(sumUsd),
BTC.FormatMoney(sumBTC),
)
return o
}
func (m AssetMap) Slice() (assets []Asset) {
for _, a := range m {
assets = append(assets, a)
}
return assets
}
func (m AssetMap) SlackAttachment() slack.Attachment {
var fields []slack.AttachmentField
var netAssetInBTC, netAssetInUSD fixedpoint.Value
var assets = m.Slice()
// sort assets
sort.Slice(assets, func(i, j int) bool {
return assets[i].InUSD.Compare(assets[j].InUSD) > 0
})
for _, a := range assets {
netAssetInUSD = netAssetInUSD.Add(a.InUSD)
netAssetInBTC = netAssetInBTC.Add(a.InBTC)
}
for _, a := range assets {
if !a.InUSD.IsZero() {
text := fmt.Sprintf("%s (≈ %s) (≈ %s) (%s)",
a.NetAsset.String(),
USD.FormatMoney(a.InUSD),
BTC.FormatMoney(a.InBTC),
a.InUSD.Div(netAssetInUSD).FormatPercentage(2),
)
if !a.Borrowed.IsZero() {
text += fmt.Sprintf(" Borrowed: %s", a.Borrowed.String())
}
fields = append(fields, slack.AttachmentField{
Title: a.Currency,
Value: text,
Short: false,
})
} else {
text := a.NetAsset.String()
if !a.Borrowed.IsZero() {
text += fmt.Sprintf(" Borrowed: %s", a.Borrowed.String())
}
fields = append(fields, slack.AttachmentField{
Title: a.Currency,
Value: text,
Short: false,
})
}
}
return slack.Attachment{
Title: fmt.Sprintf("Net Asset Value %s (≈ %s)",
USD.FormatMoney(netAssetInUSD),
BTC.FormatMoney(netAssetInBTC),
),
Fields: fields,
}
}