Skip to content

Commit

Permalink
[All] Add AmendOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed Feb 25, 2020
1 parent 6ae92b6 commit 0e363d9
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 42 deletions.
24 changes: 23 additions & 1 deletion broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,37 @@ import (
)

type Broker interface {
// 订阅事件
Subscribe(event string, param string, listener interface{})

// 获取账号信息
GetAccountSummary(currency string) (result AccountSummary, err error)

// 获取订单薄(OrderBook)
GetOrderBook(symbol string, depth int) (result OrderBook, err error)
PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, amount float64,

// 下单
PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, size float64,
postOnly bool, reduceOnly bool) (result Order, err error)

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

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

// 撤销全部委托单
CancelAllOrders(symbol string) (err error)

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

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

// 获取持仓
GetPosition(symbol string) (result Position, err error)

// 运行一次(回测系统调用)
RunEventLoopOnce() (err error) // Run sim match for backtest only
}
16 changes: 13 additions & 3 deletions brokers/bitmex-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (b *BitMEXBroker) GetOrderBook(symbol string, depth int) (result OrderBook,
}

func (b *BitMEXBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
amount float64, postOnly bool, reduceOnly bool) (result Order, err error) {
size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
var side string
var _orderType string
if direction == Buy {
Expand All @@ -75,7 +75,7 @@ func (b *BitMEXBroker) PlaceOrder(symbol string, direction Direction, orderType
execInst += "ReduceOnly"
}
var order swagger.Order
order, err = b.client.PlaceOrder(side, _orderType, 0, price, int32(amount), "", execInst, symbol)
order, err = b.client.PlaceOrder(side, _orderType, 0, price, int32(size), "", execInst, symbol)
if err != nil {
return
}
Expand Down Expand Up @@ -120,6 +120,16 @@ func (b *BitMEXBroker) CancelAllOrders(symbol string) (err error) {
return
}

func (b *BitMEXBroker) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) {
var resp swagger.Order
resp, err = b.client.AmendOrder2(id, "", "", 0, float32(size), 0, 0, price, 0, 0, "")
if err != nil {
return
}
result = b.convertOrder(&resp)
return
}

func (b *BitMEXBroker) GetPosition(symbol string) (result Position, err error) {
var ret swagger.Position
ret, err = b.client.GetPosition(symbol)
Expand All @@ -136,7 +146,7 @@ func (b *BitMEXBroker) convertOrder(order *swagger.Order) (result Order) {
result.ID = order.OrderID
result.Symbol = order.Symbol
result.Price = order.Price
result.Amount = float64(order.OrderQty)
result.Size = float64(order.OrderQty)
result.Direction = b.convertDirection(order.Side)
result.Type = b.convertOrderType(order.OrdType)
result.AvgPrice = order.AvgPx
Expand Down
32 changes: 18 additions & 14 deletions brokers/bitmex-sim-broker/simbroker.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ func (b *BitMEXSimBroker) GetOrderBook(symbol string, depth int) (result OrderBo
}

func (b *BitMEXSimBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
amount float64, postOnly bool, reduceOnly bool) (result Order, err error) {
size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
_id, _ := util2.NextID()
id := fmt.Sprintf("%v", _id)
order := &Order{
ID: id,
Symbol: symbol,
Price: price,
Amount: amount,
Size: size,
AvgPrice: 0,
FilledAmount: 0,
Direction: direction,
Expand Down Expand Up @@ -130,7 +130,7 @@ func (b *BitMEXSimBroker) matchMarketOrder(order *Order) (err error) {
//最大委托数量 10,000,000
//最小合约数量 1

if order.Amount > OrderSizeLimit {
if order.Size > OrderSizeLimit {
err = errors.New("Rejected, maximum size of order is 1,000,000")
return
}
Expand All @@ -143,18 +143,18 @@ func (b *BitMEXSimBroker) matchMarketOrder(order *Order) (err error) {
// leverage := sizeCurrency / margin
// 需要满足: sizeCurrency <= margin * 100
// 可开仓数量: <= margin * 100 * price(ask/bid)
var maxAmount float64
var maxSize float64

// 市价成交
if order.Direction == Buy {
maxAmount = margin * 100 * tick.Ask
if order.Amount > maxAmount {
err = errors.New(fmt.Sprintf("Rejected, maximum size of future position is %v", maxAmount))
maxSize = margin * 100 * tick.Ask
if order.Size > maxSize {
err = errors.New(fmt.Sprintf("Rejected, maximum size of future position is %v", maxSize))
return
}

price := tick.Ask
size := order.Amount
size := order.Size

// trade fee
fee := size / price * b.takerFeeRate
Expand All @@ -165,14 +165,14 @@ func (b *BitMEXSimBroker) matchMarketOrder(order *Order) (err error) {
// Update position
b.updatePosition(order.Symbol, size, price)
} else if order.Direction == Sell {
maxAmount = margin * 100 * tick.Bid
if order.Amount > maxAmount {
err = errors.New(fmt.Sprintf("Rejected, maximum size of future position is %v", maxAmount))
maxSize = margin * 100 * tick.Bid
if order.Size > maxSize {
err = errors.New(fmt.Sprintf("Rejected, maximum size of future position is %v", maxSize))
return
}

price := tick.Bid
size := order.Amount
size := order.Size

// trade fee
fee := size / price * b.takerFeeRate
Expand Down Expand Up @@ -200,7 +200,7 @@ func (b *BitMEXSimBroker) matchLimitOrder(order *Order, immediate bool) (err err
}

// match trade
size := order.Amount
size := order.Size
var fee float64

// trade fee
Expand All @@ -224,7 +224,7 @@ func (b *BitMEXSimBroker) matchLimitOrder(order *Order, immediate bool) (err err
}

// match trade
size := order.Amount
size := order.Size
var fee float64

// trade fee
Expand Down Expand Up @@ -406,6 +406,10 @@ func (b *BitMEXSimBroker) CancelAllOrders(symbol string) (err error) {
return
}

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

func (b *BitMEXSimBroker) GetPosition(symbol string) (result Position, err error) {
position, ok := b.positions[symbol]
if !ok {
Expand Down
10 changes: 7 additions & 3 deletions brokers/bybit-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (b *BybitBroker) GetOrderBook(symbol string, depth int) (result OrderBook,
}

func (b *BybitBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
amount float64, postOnly bool, reduceOnly bool) (result Order, err error) {
size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
var side string
var _orderType string
var timeInForce string
Expand All @@ -76,7 +76,7 @@ func (b *BybitBroker) PlaceOrder(symbol string, direction Direction, orderType O
timeInForce = "GoodTillCancel"
}
var order rest.Order
order, err = b.client.CreateOrder(side, _orderType, price, int(amount), timeInForce, reduceOnly, symbol)
order, err = b.client.CreateOrder(side, _orderType, price, int(size), timeInForce, reduceOnly, symbol)
if err != nil {
return
}
Expand Down Expand Up @@ -127,6 +127,10 @@ func (b *BybitBroker) CancelAllOrders(symbol string) (err error) {
return
}

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

func (b *BybitBroker) GetPosition(symbol string) (result Position, err error) {
var ret rest.Position
ret, err = b.client.GetPosition(symbol)
Expand All @@ -143,7 +147,7 @@ func (b *BybitBroker) convertOrder(order *rest.Order) (result Order) {
result.ID = order.OrderID
result.Symbol = order.Symbol
result.Price = order.Price
result.Amount = order.Qty
result.Size = order.Qty
result.Direction = b.convertDirection(order.Side)
result.Type = b.convertOrderType(order.OrderType)
if order.CumExecQty > 0 && order.CumExecValue > 0 {
Expand Down
32 changes: 28 additions & 4 deletions brokers/deribit-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func (b *DiribitBroker) GetOrderBook(symbol string, depth int) (result OrderBook
}

func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
amount float64, postOnly bool, reduceOnly bool) (result Order, err error) {
size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
var _orderType string
if orderType == OrderTypeLimit {
_orderType = models.OrderTypeLimit
Expand All @@ -105,7 +105,7 @@ func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType
var ret models.BuyResponse
ret, err = b.client.Buy(&models.BuyParams{
InstrumentName: symbol,
Amount: amount,
Amount: size,
Type: _orderType,
//Label: "",
Price: price,
Expand All @@ -125,7 +125,7 @@ func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType
var ret models.SellResponse
ret, err = b.client.Sell(&models.SellParams{
InstrumentName: symbol,
Amount: amount,
Amount: size,
Type: _orderType,
//Label: "",
Price: price,
Expand Down Expand Up @@ -189,6 +189,30 @@ func (b *DiribitBroker) CancelAllOrders(symbol string) (err error) {
return
}

func (b *DiribitBroker) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) {
params := &models.EditParams{
OrderID: "",
Amount: 0,
Price: 0,
PostOnly: false,
Advanced: "",
StopPrice: 0,
}
if price > 0 {
params.Price = price
}
if size > 0 {
params.Amount = size
}
var resp models.EditResponse
resp, err = b.client.Edit(params)
if err != nil {
return
}
result = b.convertOrder(&resp.Order)
return
}

func (b *DiribitBroker) GetPosition(symbol string) (result Position, err error) {
var ret models.Position
ret, err = b.client.GetPosition(&models.GetPositionParams{InstrumentName: symbol})
Expand All @@ -205,7 +229,7 @@ func (b *DiribitBroker) convertOrder(order *models.Order) (result Order) {
result.ID = order.OrderID
result.Symbol = order.InstrumentName
result.Price = order.Price.ToFloat64()
result.Amount = order.Amount
result.Size = order.Amount
result.Direction = b.convertDirection(order.Direction)
result.Type = b.convertOrderType(order.OrderType)
result.AvgPrice = order.AveragePrice
Expand Down
36 changes: 20 additions & 16 deletions brokers/deribit-sim-broker/simbroker.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ func (b *DiribitSimBroker) GetOrderBook(symbol string, depth int) (result OrderB
}

func (b *DiribitSimBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
amount float64, postOnly bool, reduceOnly bool) (result Order, err error) {
size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
_id, _ := util2.NextID()
id := fmt.Sprintf("%v", _id)
order := &Order{
ID: id,
Symbol: symbol,
Price: price,
Amount: amount,
Size: size,
AvgPrice: 0,
FilledAmount: 0,
Direction: direction,
Expand Down Expand Up @@ -130,15 +130,15 @@ func (b *DiribitSimBroker) matchMarketOrder(order *Order) (err error) {
// Invalid size - not multiple of contract size ($10)
// 数量必须是10的整数倍

if int(order.Amount)%10 != 0 {
if int(order.Size)%10 != 0 {
err = errors.New("Invalid size - not multiple of contract size ($10)")
return
}

position := b.getPosition(order.Symbol)

if int(position.Size+order.Amount) > PositionSizeLimit ||
int(position.Size-order.Amount) < -PositionSizeLimit {
if int(position.Size+order.Size) > PositionSizeLimit ||
int(position.Size-order.Size) < -PositionSizeLimit {
err = errors.New("Rejected, maximum size of future position is $1,000,000")
return
}
Expand All @@ -151,18 +151,18 @@ func (b *DiribitSimBroker) matchMarketOrder(order *Order) (err error) {
// leverage := sizeCurrency / margin
// 需要满足: sizeCurrency <= margin * 100
// 可开仓数量: <= margin * 100 * price(ask/bid)
var maxAmount float64
var maxSize float64

// 市价成交
if order.Direction == Buy {
maxAmount = margin * 100 * tick.Ask
if order.Amount > maxAmount {
err = errors.New(fmt.Sprintf("Rejected, maximum size of future position is %v", maxAmount))
maxSize = margin * 100 * tick.Ask
if order.Size > maxSize {
err = errors.New(fmt.Sprintf("Rejected, maximum size of future position is %v", maxSize))
return
}

price := tick.Ask
size := order.Amount
size := order.Size

// trade fee
fee := size / price * b.takerFeeRate
Expand All @@ -173,14 +173,14 @@ func (b *DiribitSimBroker) matchMarketOrder(order *Order) (err error) {
// Update position
b.updatePosition(order.Symbol, size, price)
} else if order.Direction == Sell {
maxAmount = margin * 100 * tick.Bid
if order.Amount > maxAmount {
err = errors.New(fmt.Sprintf("Rejected, maximum size of future position is %v", maxAmount))
maxSize = margin * 100 * tick.Bid
if order.Size > maxSize {
err = errors.New(fmt.Sprintf("Rejected, maximum size of future position is %v", maxSize))
return
}

price := tick.Bid
size := order.Amount
size := order.Size

// trade fee
fee := size / price * b.takerFeeRate
Expand Down Expand Up @@ -208,7 +208,7 @@ func (b *DiribitSimBroker) matchLimitOrder(order *Order, immediate bool) (err er
}

// match trade
size := order.Amount
size := order.Size
var fee float64

// trade fee
Expand All @@ -232,7 +232,7 @@ func (b *DiribitSimBroker) matchLimitOrder(order *Order, immediate bool) (err er
}

// match trade
size := order.Amount
size := order.Size
var fee float64

// trade fee
Expand Down Expand Up @@ -414,6 +414,10 @@ func (b *DiribitSimBroker) CancelAllOrders(symbol string) (err error) {
return
}

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

func (b *DiribitSimBroker) GetPosition(symbol string) (result Position, err error) {
position, ok := b.positions[symbol]
if !ok {
Expand Down
Loading

0 comments on commit 0e363d9

Please sign in to comment.