-
-
Notifications
You must be signed in to change notification settings - Fork 296
/
trade.go
255 lines (215 loc) · 6.34 KB
/
trade.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package types
import (
"database/sql"
"fmt"
"strconv"
"strings"
"sync"
"time"
"github.com/slack-go/slack"
"github.com/c9s/bbgo/pkg/fixedpoint"
"github.com/c9s/bbgo/pkg/util/templateutil"
)
func init() {
// make sure we can cast Trade to PlainText
_ = PlainText(Trade{})
_ = PlainText(&Trade{})
}
type TradeSlice struct {
mu sync.Mutex
Trades []Trade
}
func (s *TradeSlice) Copy() []Trade {
s.mu.Lock()
slice := make([]Trade, len(s.Trades))
copy(slice, s.Trades)
s.mu.Unlock()
return slice
}
func (s *TradeSlice) Reverse() {
slice := s.Trades
for i, j := 0, len(slice)-1; i < j; i, j = i+1, j-1 {
slice[i], slice[j] = slice[j], slice[i]
}
}
func (s *TradeSlice) Append(t Trade) {
s.mu.Lock()
s.Trades = append(s.Trades, t)
s.mu.Unlock()
}
func (s *TradeSlice) Truncate(size int) {
s.mu.Lock()
if len(s.Trades) > size {
s.Trades = s.Trades[len(s.Trades)-1-size:]
}
s.mu.Unlock()
}
type Trade struct {
// GID is the global ID
GID int64 `json:"gid" db:"gid"`
// ID is the source trade ID
ID uint64 `json:"id" db:"id"`
OrderID uint64 `json:"orderID" db:"order_id"`
Exchange ExchangeName `json:"exchange" db:"exchange"`
Price fixedpoint.Value `json:"price" db:"price"`
Quantity fixedpoint.Value `json:"quantity" db:"quantity"`
QuoteQuantity fixedpoint.Value `json:"quoteQuantity" db:"quote_quantity"`
Symbol string `json:"symbol" db:"symbol"`
Side SideType `json:"side" db:"side"`
IsBuyer bool `json:"isBuyer" db:"is_buyer"`
IsMaker bool `json:"isMaker" db:"is_maker"`
Time Time `json:"tradedAt" db:"traded_at"`
Fee fixedpoint.Value `json:"fee" db:"fee"`
FeeCurrency string `json:"feeCurrency" db:"fee_currency"`
// FeeDiscounted is an optional field which indicates whether the trade is using the platform fee token for discount.
// When FeeDiscounted = true, means the fee is deducted outside the trade
// By default, it's set to false.
// This is only used by the MAX exchange
FeeDiscounted bool `json:"feeDiscounted" db:"-"`
IsMargin bool `json:"isMargin" db:"is_margin"`
IsFutures bool `json:"isFutures" db:"is_futures"`
IsIsolated bool `json:"isIsolated" db:"is_isolated"`
// The following fields are null-able fields
// StrategyID is the strategy that execute this trade
StrategyID sql.NullString `json:"strategyID" db:"strategy"`
// PnL is the profit and loss value of the executed trade
PnL sql.NullFloat64 `json:"pnl" db:"pnl"`
}
func (trade Trade) CsvHeader() []string {
return []string{"id", "order_id", "exchange", "symbol", "price", "quantity", "quote_quantity", "side", "is_buyer", "is_maker", "fee", "fee_currency", "time"}
}
func (trade Trade) CsvRecords() [][]string {
return [][]string{
{
strconv.FormatUint(trade.ID, 10),
strconv.FormatUint(trade.OrderID, 10),
trade.Exchange.String(),
trade.Symbol,
trade.Price.String(),
trade.Quantity.String(),
trade.QuoteQuantity.String(),
trade.Side.String(),
strconv.FormatBool(trade.IsBuyer),
strconv.FormatBool(trade.IsMaker),
trade.Fee.String(),
trade.FeeCurrency,
trade.Time.Time().Format(time.RFC1123),
},
}
}
// PositionChange returns the position delta of this trade
// BUY trade -> positive quantity
// SELL trade -> negative quantity
func (trade Trade) PositionChange() fixedpoint.Value {
q := trade.Quantity
switch trade.Side {
case SideTypeSell:
return q.Neg()
case SideTypeBuy:
return q
case SideTypeSelf:
return fixedpoint.Zero
}
return fixedpoint.Zero
}
/*func trimTrailingZero(a string) string {
index := strings.Index(a, ".")
if index == -1 {
return a
}
var c byte
var i int
for i = len(a) - 1; i >= 0; i-- {
c = a[i]
if c == '0' {
continue
} else if c == '.' {
return a[0:i]
} else {
return a[0 : i+1]
}
}
return a
}
func trimTrailingZero(a float64) string {
return trimTrailingZero(fmt.Sprintf("%f", a))
}*/
// String is for console output
func (trade Trade) String() string {
return fmt.Sprintf("TRADE %s %s %4s %-4s @ %-6s | AMOUNT %s | FEE %s %s | OrderID %d | TID %d | %s",
trade.Exchange.String(),
trade.Symbol,
trade.Side,
trade.Quantity.String(),
trade.Price.String(),
trade.QuoteQuantity.String(),
trade.Fee.String(),
trade.FeeCurrency,
trade.OrderID,
trade.ID,
trade.Time.Time().Format(time.StampMilli),
)
}
// PlainText is used for telegram-styled messages
func (trade Trade) PlainText() string {
return fmt.Sprintf("Trade %s %s %s %s @ %s, amount %s, fee %s %s",
trade.Exchange.String(),
trade.Symbol,
trade.Side,
trade.Quantity.String(),
trade.Price.String(),
trade.QuoteQuantity.String(),
trade.Fee.String(),
trade.FeeCurrency)
}
var slackTradeTextTemplate = ":handshake: Trade {{ .Symbol }} {{ .Side }} {{ .Quantity }} @ {{ .Price }}"
func (trade Trade) SlackAttachment() slack.Attachment {
var color = "#DC143C"
if trade.IsBuyer {
color = "#228B22"
}
liquidity := trade.Liquidity()
text := templateutil.Render(slackTradeTextTemplate, trade)
footerIcon := ExchangeFooterIcon(trade.Exchange)
return slack.Attachment{
Text: text,
// Title: ...
// Pretext: pretext,
Color: color,
Fields: []slack.AttachmentField{
{Title: "Exchange", Value: trade.Exchange.String(), Short: true},
{Title: "Price", Value: trade.Price.String(), Short: true},
{Title: "Quantity", Value: trade.Quantity.String(), Short: true},
{Title: "QuoteQuantity", Value: trade.QuoteQuantity.String(), Short: true},
{Title: "Fee", Value: trade.Fee.String(), Short: true},
{Title: "FeeCurrency", Value: trade.FeeCurrency, Short: true},
{Title: "Liquidity", Value: liquidity, Short: true},
{Title: "Order ID", Value: strconv.FormatUint(trade.OrderID, 10), Short: true},
},
FooterIcon: footerIcon,
Footer: strings.ToLower(trade.Exchange.String()) + templateutil.Render(" creation time {{ . }}", trade.Time.Time().Format(time.StampMilli)),
}
}
func (trade Trade) Liquidity() (o string) {
if trade.IsMaker {
o = "MAKER"
} else {
o = "TAKER"
}
return o
}
func (trade Trade) Key() TradeKey {
return TradeKey{
Exchange: trade.Exchange,
ID: trade.ID,
Side: trade.Side,
}
}
type TradeKey struct {
Exchange ExchangeName
ID uint64
Side SideType
}
func (k TradeKey) String() string {
return k.Exchange.String() + strconv.FormatUint(k.ID, 10) + k.Side.String()
}