Skip to content

Commit

Permalink
Modify the interface
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed May 4, 2020
1 parent e4c28e3 commit d5feaa7
Show file tree
Hide file tree
Showing 27 changed files with 312 additions and 265 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ func main() {
log.Printf("%#v", ob)
})
// 订阅成交记录
ws.SubscribeTrades(market, func(trades []Trade) {
ws.SubscribeTrades(market, func(trades []*Trade) {
log.Printf("%#v", trades)
})
// 订阅订单成交信息
ws.SubscribeOrders(market, func(orders []Order) {
ws.SubscribeOrders(market, func(orders []*Order) {
log.Printf("%#v", orders)
})
// 订阅持仓信息
ws.SubscribePositions(market, func(positions []Position) {
ws.SubscribePositions(market, func(positions []*Position) {
log.Printf("%#v", positions)
})

Expand Down
6 changes: 3 additions & 3 deletions README_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ func main() {
log.Printf("%#v", ob)
})
// 订阅成交记录
ws.SubscribeTrades(market, func(trades []Trade) {
ws.SubscribeTrades(market, func(trades []*Trade) {
log.Printf("%#v", trades)
})
// 订阅订单成交信息
ws.SubscribeOrders(market, func(orders []Order) {
ws.SubscribeOrders(market, func(orders []*Order) {
log.Printf("%#v", orders)
})
// 订阅持仓信息
ws.SubscribePositions(market, func(positions []Position) {
ws.SubscribePositions(market, func(positions []*Position) {
log.Printf("%#v", positions)
})

Expand Down
26 changes: 13 additions & 13 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,41 +184,41 @@ type Exchange interface {
SetLeverRate(value float64) (err error)

// 开多
OpenLong(symbol string, orderType OrderType, price float64, size float64) (result Order, err error)
OpenLong(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error)

// 开空
OpenShort(symbol string, orderType OrderType, price float64, size float64) (result Order, err error)
OpenShort(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error)

// 平多
CloseLong(symbol string, orderType OrderType, price float64, size float64) (result Order, err error)
CloseLong(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error)

// 平空
CloseShort(symbol string, orderType OrderType, price float64, size float64) (result Order, err error)
CloseShort(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error)

// 下单
PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, size float64,
opts ...PlaceOrderOption) (result Order, err error)
opts ...PlaceOrderOption) (result *Order, err error)

// 获取活跃委托单列表
GetOpenOrders(symbol string, opts ...OrderOption) (result []Order, err error)
GetOpenOrders(symbol string, opts ...OrderOption) (result []*Order, err error)

// 获取委托信息
GetOrder(symbol string, id string, opts ...OrderOption) (result Order, err error)
GetOrder(symbol string, id string, opts ...OrderOption) (result *Order, err error)

// 撤销全部委托单
CancelAllOrders(symbol string, opts ...OrderOption) (err error)

// 撤销单个委托单
CancelOrder(symbol string, id string, opts ...OrderOption) (result Order, err error)
CancelOrder(symbol string, id string, opts ...OrderOption) (result *Order, err error)

// 修改委托
AmendOrder(symbol string, id string, price float64, size float64, opts ...OrderOption) (result Order, err error)
AmendOrder(symbol string, id string, price float64, size float64, opts ...OrderOption) (result *Order, err error)

// 获取持仓
GetPositions(symbol string) (result []Position, err error)
GetPositions(symbol string) (result []*Position, err error)

// 订阅成交记录
SubscribeTrades(market Market, callback func(trades []Trade)) error
SubscribeTrades(market Market, callback func(trades []*Trade)) error

// 订阅L2 OrderBook
SubscribeLevel2Snapshots(market Market, callback func(ob *OrderBook)) error
Expand All @@ -227,10 +227,10 @@ type Exchange interface {
//SubscribeBalances(market Market, callback func(balance *Balance)) error

// 订阅委托
SubscribeOrders(market Market, callback func(orders []Order)) error
SubscribeOrders(market Market, callback func(orders []*Order)) error

// 订阅持仓
SubscribePositions(market Market, callback func(positions []Position)) error
SubscribePositions(market Market, callback func(positions []*Position)) error

// 运行一次(回测系统调用)
RunEventLoopOnce() (err error) // Run sim match for backtest only
Expand Down
6 changes: 3 additions & 3 deletions examples/hbdm-ws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func main() {
log.Printf("%#v", ob)
})
// 订阅成交记录
ws.SubscribeTrades(market, func(trades []Trade) {
ws.SubscribeTrades(market, func(trades []*Trade) {
log.Printf("%#v", trades)
})
// 订阅订单成交信息
ws.SubscribeOrders(market, func(orders []Order) {
ws.SubscribeOrders(market, func(orders []*Order) {
log.Printf("%#v", orders)
})
// 订阅持仓信息
ws.SubscribePositions(market, func(positions []Position) {
ws.SubscribePositions(market, func(positions []*Position) {
log.Printf("%#v", positions)
})

Expand Down
6 changes: 3 additions & 3 deletions examples/hbdmswap-ws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ func main() {
log.Printf("%#v", ob)
})
// 订阅成交记录
ws.SubscribeTrades(market, func(trades []Trade) {
ws.SubscribeTrades(market, func(trades []*Trade) {
log.Printf("%#v", trades)
})
// 订阅订单成交信息
ws.SubscribeOrders(market, func(orders []Order) {
ws.SubscribeOrders(market, func(orders []*Order) {
log.Printf("%#v", orders)
})
// 订阅持仓信息
ws.SubscribePositions(market, func(positions []Position) {
ws.SubscribePositions(market, func(positions []*Position) {
log.Printf("%#v", positions)
})

Expand Down
6 changes: 3 additions & 3 deletions examples/okex-futures-ws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func main() {
log.Printf("%#v", ob)
})
// 订阅成交记录
ws.SubscribeTrades(market, func(trades []Trade) {
ws.SubscribeTrades(market, func(trades []*Trade) {
log.Printf("%#v", trades)
})
// 订阅订单成交信息
ws.SubscribeOrders(market, func(orders []Order) {
ws.SubscribeOrders(market, func(orders []*Order) {
log.Printf("%#v", orders)
})
// 订阅持仓信息
ws.SubscribePositions(market, func(positions []Position) {
ws.SubscribePositions(market, func(positions []*Position) {
log.Printf("%#v", positions)
})

Expand Down
6 changes: 3 additions & 3 deletions examples/okex-swap-ws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ func main() {
log.Printf("%#v", ob)
})
// 订阅成交记录
ws.SubscribeTrades(market, func(trades []Trade) {
ws.SubscribeTrades(market, func(trades []*Trade) {
log.Printf("%#v", trades)
})
// 订阅订单成交信息
ws.SubscribeOrders(market, func(orders []Order) {
ws.SubscribeOrders(market, func(orders []*Order) {
log.Printf("%#v", orders)
})
// 订阅持仓信息
ws.SubscribePositions(market, func(positions []Position) {
ws.SubscribePositions(market, func(positions []*Position) {
log.Printf("%#v", positions)
})

Expand Down
37 changes: 20 additions & 17 deletions exchanges/binancefutures/binancefutures.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,24 +158,24 @@ func (b *BinanceFutures) SetLeverRate(value float64) (err error) {
return
}

func (b *BinanceFutures) OpenLong(symbol string, orderType OrderType, price float64, size float64) (result Order, err error) {
func (b *BinanceFutures) OpenLong(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error) {
return b.PlaceOrder(symbol, Buy, orderType, price, size)
}

func (b *BinanceFutures) OpenShort(symbol string, orderType OrderType, price float64, size float64) (result Order, err error) {
func (b *BinanceFutures) OpenShort(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error) {
return b.PlaceOrder(symbol, Sell, orderType, price, size)
}

func (b *BinanceFutures) CloseLong(symbol string, orderType OrderType, price float64, size float64) (result Order, err error) {
func (b *BinanceFutures) CloseLong(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error) {
return b.PlaceOrder(symbol, Sell, orderType, price, size, OrderReduceOnlyOption(true))
}

func (b *BinanceFutures) CloseShort(symbol string, orderType OrderType, price float64, size float64) (result Order, err error) {
func (b *BinanceFutures) CloseShort(symbol string, orderType OrderType, price float64, size float64) (result *Order, err error) {
return b.PlaceOrder(symbol, Buy, orderType, price, size, OrderReduceOnlyOption(true))
}

func (b *BinanceFutures) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
size float64, opts ...PlaceOrderOption) (result Order, err error) {
size float64, opts ...PlaceOrderOption) (result *Order, err error) {
params := ParsePlaceOrderParameter(opts...)
service := b.client.NewCreateOrderService().
Symbol(symbol).
Expand Down Expand Up @@ -216,7 +216,7 @@ func (b *BinanceFutures) PlaceOrder(symbol string, direction Direction, orderTyp
return
}

func (b *BinanceFutures) GetOpenOrders(symbol string, opts ...OrderOption) (result []Order, err error) {
func (b *BinanceFutures) GetOpenOrders(symbol string, opts ...OrderOption) (result []*Order, err error) {
service := b.client.NewListOpenOrdersService().
Symbol(symbol)
var res []*futures.Order
Expand All @@ -230,7 +230,7 @@ func (b *BinanceFutures) GetOpenOrders(symbol string, opts ...OrderOption) (resu
return
}

func (b *BinanceFutures) GetOrder(symbol string, id string, opts ...OrderOption) (result Order, err error) {
func (b *BinanceFutures) GetOrder(symbol string, id string, opts ...OrderOption) (result *Order, err error) {
var orderID int64
orderID, err = strconv.ParseInt(id, 10, 64)
if err != nil {
Expand All @@ -248,7 +248,7 @@ func (b *BinanceFutures) GetOrder(symbol string, id string, opts ...OrderOption)
return
}

func (b *BinanceFutures) CancelOrder(symbol string, id string, opts ...OrderOption) (result Order, err error) {
func (b *BinanceFutures) CancelOrder(symbol string, id string, opts ...OrderOption) (result *Order, err error) {
var orderID int64
orderID, err = strconv.ParseInt(id, 10, 64)
if err != nil {
Expand All @@ -273,11 +273,11 @@ func (b *BinanceFutures) CancelAllOrders(symbol string, opts ...OrderOption) (er
return
}

func (b *BinanceFutures) AmendOrder(symbol string, id string, price float64, size float64, opts ...OrderOption) (result Order, err error) {
func (b *BinanceFutures) AmendOrder(symbol string, id string, price float64, size float64, opts ...OrderOption) (result *Order, err error) {
return
}

func (b *BinanceFutures) GetPositions(symbol string) (result []Position, err error) {
func (b *BinanceFutures) GetPositions(symbol string) (result []*Position, err error) {
var res []*futures.PositionRisk
res, err = b.client.NewGetPositionRiskService().
Do(context.Background())
Expand All @@ -291,7 +291,7 @@ func (b *BinanceFutures) GetPositions(symbol string) (result []Position, err err
if useFilter && v.Symbol != symbol {
continue
}
position := Position{}
position := &Position{}
position.Symbol = v.Symbol
size := util.ParseFloat64(v.PositionAmt)
if size != 0 {
Expand All @@ -304,7 +304,8 @@ func (b *BinanceFutures) GetPositions(symbol string) (result []Position, err err
return
}

func (b *BinanceFutures) convertOrder(order *futures.Order) (result Order) {
func (b *BinanceFutures) convertOrder(order *futures.Order) (result *Order) {
result = &Order{}
result.ID = fmt.Sprint(order.OrderID)
result.Symbol = order.Symbol
result.Price = util.ParseFloat64(order.Price)
Expand All @@ -322,7 +323,8 @@ func (b *BinanceFutures) convertOrder(order *futures.Order) (result Order) {
return
}

func (b *BinanceFutures) convertOrder1(order *futures.CreateOrderResponse) (result Order) {
func (b *BinanceFutures) convertOrder1(order *futures.CreateOrderResponse) (result *Order) {
result = &Order{}
result.ID = fmt.Sprint(order.OrderID)
result.Symbol = order.Symbol
result.Price = util.ParseFloat64(order.Price)
Expand All @@ -340,7 +342,8 @@ func (b *BinanceFutures) convertOrder1(order *futures.CreateOrderResponse) (resu
return
}

func (b *BinanceFutures) convertOrder2(order *futures.CancelOrderResponse) (result Order) {
func (b *BinanceFutures) convertOrder2(order *futures.CancelOrderResponse) (result *Order) {
result = &Order{}
result.ID = fmt.Sprint(order.OrderID)
result.Symbol = order.Symbol
result.Price = util.ParseFloat64(order.Price)
Expand Down Expand Up @@ -407,19 +410,19 @@ func (b *BinanceFutures) orderStatus(status futures.OrderStatusType) OrderStatus
}
}

func (b *BinanceFutures) SubscribeTrades(market Market, callback func(trades []Trade)) error {
func (b *BinanceFutures) SubscribeTrades(market Market, callback func(trades []*Trade)) error {
return ErrNotImplemented
}

func (b *BinanceFutures) SubscribeLevel2Snapshots(market Market, callback func(ob *OrderBook)) error {
return ErrNotImplemented
}

func (b *BinanceFutures) SubscribeOrders(market Market, callback func(orders []Order)) error {
func (b *BinanceFutures) SubscribeOrders(market Market, callback func(orders []*Order)) error {
return ErrNotImplemented
}

func (b *BinanceFutures) SubscribePositions(market Market, callback func(positions []Position)) error {
func (b *BinanceFutures) SubscribePositions(market Market, callback func(positions []*Position)) error {
return ErrNotImplemented
}

Expand Down
Loading

0 comments on commit d5feaa7

Please sign in to comment.