mirror of
https://github.com/c9s/bbgo.git
synced 2024-11-22 06:53:52 +00:00
pkg/exchange: support broker id
This commit is contained in:
parent
5a1249e871
commit
b1f86adab5
|
@ -65,26 +65,40 @@ var log = logrus.WithFields(logrus.Fields{
|
||||||
var ErrSymbolRequired = errors.New("symbol is a required parameter")
|
var ErrSymbolRequired = errors.New("symbol is a required parameter")
|
||||||
|
|
||||||
type Exchange struct {
|
type Exchange struct {
|
||||||
key, secret, passphrase string
|
key, secret, passphrase, brokerId string
|
||||||
|
|
||||||
client *okexapi.RestClient
|
client *okexapi.RestClient
|
||||||
timeNowFunc func() time.Time
|
timeNowFunc func() time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(key, secret, passphrase string) *Exchange {
|
type Option func(exchange *Exchange)
|
||||||
|
|
||||||
|
func WithBrokerId(id string) Option {
|
||||||
|
return func(exchange *Exchange) {
|
||||||
|
exchange.brokerId = id
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(key, secret, passphrase string, opts ...Option) *Exchange {
|
||||||
client := okexapi.NewClient()
|
client := okexapi.NewClient()
|
||||||
|
|
||||||
if len(key) > 0 && len(secret) > 0 {
|
if len(key) > 0 && len(secret) > 0 {
|
||||||
client.Auth(key, secret, passphrase)
|
client.Auth(key, secret, passphrase)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Exchange{
|
ex := &Exchange{
|
||||||
key: key,
|
key: key,
|
||||||
secret: secret,
|
secret: secret,
|
||||||
passphrase: passphrase,
|
passphrase: passphrase,
|
||||||
client: client,
|
client: client,
|
||||||
timeNowFunc: time.Now,
|
timeNowFunc: time.Now,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for _, o := range opts {
|
||||||
|
o(ex)
|
||||||
|
}
|
||||||
|
|
||||||
|
return ex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *Exchange) Name() types.ExchangeName {
|
func (e *Exchange) Name() types.ExchangeName {
|
||||||
|
@ -263,6 +277,10 @@ func (e *Exchange) SubmitOrder(ctx context.Context, order types.SubmitOrder) (*t
|
||||||
orderReq.ClientOrderID(order.ClientOrderID)
|
orderReq.ClientOrderID(order.ClientOrderID)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if len(e.brokerId) != 0 {
|
||||||
|
orderReq.Tag(e.brokerId)
|
||||||
|
}
|
||||||
|
|
||||||
timeNow := time.Now()
|
timeNow := time.Now()
|
||||||
orders, err := orderReq.Do(ctx)
|
orders, err := orderReq.Do(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -42,7 +42,7 @@ type PlaceOrderRequest struct {
|
||||||
// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters.
|
// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 32 characters.
|
||||||
clientOrderID *string `param:"clOrdId"`
|
clientOrderID *string `param:"clOrdId"`
|
||||||
|
|
||||||
// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 8 characters.
|
// A combination of case-sensitive alphanumerics, all numbers, or all letters of up to 16 characters.
|
||||||
tag *string `param:"tag"`
|
tag *string `param:"tag"`
|
||||||
|
|
||||||
// "buy" or "sell"
|
// "buy" or "sell"
|
||||||
|
|
|
@ -284,15 +284,29 @@ func (r *PlaceOrderRequest) Do(ctx context.Context) ([]OrderResponse, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
var apiResponse APIResponse
|
var apiResponse APIResponse
|
||||||
if err := response.DecodeJSON(&apiResponse); err != nil {
|
|
||||||
return nil, err
|
type responseUnmarshaler interface {
|
||||||
|
Unmarshal(data []byte) error
|
||||||
|
}
|
||||||
|
|
||||||
|
if unmarshaler, ok := interface{}(&apiResponse).(responseUnmarshaler); ok {
|
||||||
|
if err := unmarshaler.Unmarshal(response.Body); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// The line below checks the content type, however, some API server might not send the correct content type header,
|
||||||
|
// Hence, this is commented for backward compatibility
|
||||||
|
// response.IsJSON()
|
||||||
|
if err := response.DecodeJSON(&apiResponse); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type responseValidator interface {
|
type responseValidator interface {
|
||||||
Validate() error
|
Validate() error
|
||||||
}
|
}
|
||||||
validator, ok := interface{}(apiResponse).(responseValidator)
|
|
||||||
if ok {
|
if validator, ok := interface{}(&apiResponse).(responseValidator); ok {
|
||||||
if err := validator.Validate(); err != nil {
|
if err := validator.Validate(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user