-
-
Notifications
You must be signed in to change notification settings - Fork 275
/
exchange.go
170 lines (133 loc) · 4.51 KB
/
exchange.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
package types
import (
"context"
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/c9s/bbgo/pkg/fixedpoint"
)
const DateFormat = "2006-01-02"
type ExchangeName string
const (
ExchangeMax ExchangeName = "max"
ExchangeBinance ExchangeName = "binance"
ExchangeOKEx ExchangeName = "okex"
ExchangeKucoin ExchangeName = "kucoin"
ExchangeBitget ExchangeName = "bitget"
ExchangeBacktest ExchangeName = "backtest"
ExchangeBybit ExchangeName = "bybit"
)
var SupportedExchanges = []ExchangeName{
ExchangeMax,
ExchangeBinance,
ExchangeOKEx,
ExchangeKucoin,
ExchangeBitget,
ExchangeBybit,
// note: we are not using "backtest"
}
func (n *ExchangeName) Value() (driver.Value, error) {
return n.String(), nil
}
func (n *ExchangeName) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
*n = ExchangeName(s)
if !n.IsValid() {
return fmt.Errorf("%s is an invalid exchange name", s)
}
return nil
}
func (n ExchangeName) IsValid() bool {
switch n {
case ExchangeBinance, ExchangeBitget, ExchangeBybit, ExchangeMax, ExchangeOKEx, ExchangeKucoin:
return true
}
return false
}
func (n ExchangeName) String() string {
return string(n)
}
func ValidExchangeName(a string) (ExchangeName, error) {
exName := ExchangeName(strings.ToLower(a))
if !exName.IsValid() {
return "", fmt.Errorf("invalid exchange name: %s", a)
}
return exName, nil
}
type ExchangeMinimal interface {
Name() ExchangeName
PlatformFeeCurrency() string
}
//go:generate mockgen -destination=mocks/mock_exchange.go -package=mocks . Exchange
type Exchange interface {
ExchangeMinimal
ExchangeMarketDataService
ExchangeAccountService
ExchangeTradeService
}
//go:generate mockgen -destination=mocks/mock_exchange_public.go -package=mocks . ExchangePublic
type ExchangePublic interface {
ExchangeMinimal
ExchangeMarketDataService
}
// ExchangeBasic is the new type for replacing the original Exchange interface
type ExchangeBasic = Exchange
// ExchangeOrderQueryService provides an interface for querying the order status via order ID or client order ID
//
//go:generate mockgen -destination=mocks/mock_exchange_order_query.go -package=mocks . ExchangeOrderQueryService
type ExchangeOrderQueryService interface {
QueryOrder(ctx context.Context, q OrderQuery) (*Order, error)
QueryOrderTrades(ctx context.Context, q OrderQuery) ([]Trade, error)
}
type ExchangeAccountService interface {
QueryAccount(ctx context.Context) (*Account, error)
QueryAccountBalances(ctx context.Context) (BalanceMap, error)
}
type ExchangeTradeService interface {
SubmitOrder(ctx context.Context, order SubmitOrder) (createdOrder *Order, err error)
QueryOpenOrders(ctx context.Context, symbol string) (orders []Order, err error)
CancelOrders(ctx context.Context, orders ...Order) error
}
type ExchangeDefaultFeeRates interface {
DefaultFeeRates() ExchangeFee
}
type ExchangeAmountFeeProtect interface {
SetModifyOrderAmountForFee(ExchangeFee)
}
//go:generate mockgen -destination=mocks/mock_exchange_trade_history.go -package=mocks . ExchangeTradeHistoryService
type ExchangeTradeHistoryService interface {
QueryTrades(ctx context.Context, symbol string, options *TradeQueryOptions) ([]Trade, error)
QueryClosedOrders(ctx context.Context, symbol string, since, until time.Time, lastOrderID uint64) (orders []Order, err error)
}
type ExchangeMarketDataService interface {
NewStream() Stream
QueryMarkets(ctx context.Context) (MarketMap, error)
QueryTicker(ctx context.Context, symbol string) (*Ticker, error)
QueryTickers(ctx context.Context, symbol ...string) (map[string]Ticker, error)
QueryKLines(ctx context.Context, symbol string, interval Interval, options KLineQueryOptions) ([]KLine, error)
}
type CustomIntervalProvider interface {
SupportedInterval() map[Interval]int
IsSupportedInterval(interval Interval) bool
}
type ExchangeTransferService interface {
QueryDepositHistory(ctx context.Context, asset string, since, until time.Time) (allDeposits []Deposit, err error)
QueryWithdrawHistory(ctx context.Context, asset string, since, until time.Time) (allWithdraws []Withdraw, err error)
}
type ExchangeWithdrawalService interface {
Withdraw(ctx context.Context, asset string, amount fixedpoint.Value, address string, options *WithdrawalOptions) error
}
type ExchangeRewardService interface {
QueryRewards(ctx context.Context, startTime time.Time) ([]Reward, error)
}
type TradeQueryOptions struct {
StartTime *time.Time
EndTime *time.Time
Limit int64
LastTradeID uint64
}