Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
f0cii committed Mar 25, 2020
1 parent b6cdb89 commit f19440b
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 64 deletions.
6 changes: 4 additions & 2 deletions broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ type Broker interface {
GetOrderBook(symbol string, depth int) (result OrderBook, err error)

// 设置合约类型
SetContractType(contractType string) (err error)
// pair: 交易对,如: BTC-USD(OKEX) BTC(HBDM)
// contractType: W1,W2,Q1,Q2
SetContractType(pair string, contractType string) (err error)

// 获取当前设置的合约ID
GetContractType() (symbol string, err error)
GetContractID() (symbol string, err error)

// 设置杠杆大小
SetLeverRate(value float64) (err error)
Expand Down
4 changes: 2 additions & 2 deletions brokers/bitmex-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ func (b *BitMEXBroker) GetOrderBook(symbol string, depth int) (result OrderBook,
return
}

func (b *BitMEXBroker) SetContractType(contractType string) (err error) {
func (b *BitMEXBroker) SetContractType(pair string, contractType string) (err error) {
return
}

func (b *BitMEXBroker) GetContractType() (symbol string, err error) {
func (b *BitMEXBroker) GetContractID() (symbol string, err error) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ func (b *BitMEXSimBroker) GetOrderBook(symbol string, depth int) (result OrderBo
return
}

func (b *BitMEXSimBroker) SetContractType(contractType string) (err error) {
func (b *BitMEXSimBroker) SetContractType(pair string, contractType string) (err error) {
return
}

func (b *BitMEXSimBroker) GetContractType() (symbol string, err error) {
func (b *BitMEXSimBroker) GetContractID() (symbol string, err error) {
return
}

Expand Down Expand Up @@ -337,7 +337,7 @@ func (b *BitMEXSimBroker) getPosition(symbol string) *Position {
} else {
position = &Position{
Symbol: symbol,
OpenI: time.Time{},
OpenTime: time.Time{},
OpenPrice: 0,
Size: 0,
AvgPrice: 0,
Expand Down
4 changes: 2 additions & 2 deletions brokers/bybit-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ func (b *BybitBroker) GetOrderBook(symbol string, depth int) (result OrderBook,
return
}

func (b *BybitBroker) SetContractType(contractType string) (err error) {
func (b *BybitBroker) SetContractType(pair string, contractType string) (err error) {
return
}

func (b *BybitBroker) GetContractType() (symbol string, err error) {
func (b *BybitBroker) GetContractID() (symbol string, err error) {
return
}

Expand Down
4 changes: 2 additions & 2 deletions brokers/deribit-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ func (b *DiribitBroker) GetOrderBook(symbol string, depth int) (result OrderBook
return
}

func (b *DiribitBroker) SetContractType(contractType string) (err error) {
func (b *DiribitBroker) SetContractType(pair string, contractType string) (err error) {
return
}

func (b *DiribitBroker) GetContractType() (symbol string, err error) {
func (b *DiribitBroker) GetContractID() (symbol string, err error) {
return
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ func (b *DiribitSimBroker) GetOrderBook(symbol string, depth int) (result OrderB
return
}

func (b *DiribitSimBroker) SetContractType(contractType string) (err error) {
func (b *DiribitSimBroker) SetContractType(pair string, contractType string) (err error) {
return
}

func (b *DiribitSimBroker) GetContractType() (symbol string, err error) {
func (b *DiribitSimBroker) GetContractID() (symbol string, err error) {
return
}

Expand Down Expand Up @@ -345,7 +345,7 @@ func (b *DiribitSimBroker) getPosition(symbol string) *Position {
} else {
position = &Position{
Symbol: symbol,
OpenI: time.Time{},
OpenTime: time.Time{},
OpenPrice: 0,
Size: 0,
AvgPrice: 0,
Expand Down
File renamed without changes.
101 changes: 64 additions & 37 deletions brokers/hbdm-broker/broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,20 @@ import (

const StatusOK = "ok"

// HuobiBroker the Huobi broker
type HuobiBroker struct {
client *hbdm.Client
contractType string // 合约类型
leverRate int // 杠杆倍数
// HBDMBroker the Huobi DM broker
type HBDMBroker struct {
client *hbdm.Client
pair string // 交易对 BTC/ETH/...
_contractType string // 合约类型
contractType string // 合约类型(HBDM)
leverRate int // 杠杆倍数
}

func (b *HuobiBroker) Subscribe(event string, param string, listener interface{}) {
func (b *HBDMBroker) Subscribe(event string, param string, listener interface{}) {

}

func (b *HuobiBroker) GetAccountSummary(currency string) (result AccountSummary, err error) {
func (b *HBDMBroker) GetAccountSummary(currency string) (result AccountSummary, err error) {
var account hbdm.AccountInfoResult
account, err = b.client.GetAccountInfo(currency)
if err != nil {
Expand All @@ -46,7 +48,7 @@ func (b *HuobiBroker) GetAccountSummary(currency string) (result AccountSummary,
return
}

func (b *HuobiBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) {
func (b *HBDMBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) {
var ret hbdm.MarketDepthResult

ret, err = b.client.GetMarketDepth(b.contractType, "step0")
Expand Down Expand Up @@ -74,23 +76,49 @@ func (b *HuobiBroker) GetOrderBook(symbol string, depth int) (result OrderBook,
}

// 设置合约类型
// 如"BTC_CW"表示BTC当周合约,"BTC_NW"表示BTC次周合约,"BTC_CQ"表示BTC季度合约
func (b *HuobiBroker) SetContractType(contractType string) (err error) {
b.contractType = contractType
// pair: BTC/ETH/...
func (b *HBDMBroker) SetContractType(pair string, contractType string) (err error) {
// // 如"BTC_CW"表示BTC当周合约,"BTC_NW"表示BTC次周合约,"BTC_CQ"表示BTC季度合约
b.pair = pair
b._contractType = contractType
var contractAlias string
switch contractType {
case ContractTypeNone:
case ContractTypeW1:
contractAlias = "this_week"
case ContractTypeW2:
contractAlias = "next_week"
case ContractTypeQ1:
contractAlias = "quarter"
}
b.contractType = contractAlias
return
}

func (b *HuobiBroker) GetContractType() (symbol string, err error) {
return
func (b *HBDMBroker) GetContractID() (symbol string, err error) {
var ret hbdm.ContractInfoResult
ret, err = b.client.GetContractInfo(b.pair, b.contractType, "")
if err != nil {
return
}
for _, v := range ret.Data {
// log.Printf("%#v", v)
if v.Symbol == b.pair &&
v.ContractType == b.contractType {
symbol = v.ContractCode
return
}
}
return "", fmt.Errorf("not found")
}

// 设置杠杆大小
func (b *HuobiBroker) SetLeverRate(value float64) (err error) {
func (b *HBDMBroker) SetLeverRate(value float64) (err error) {
b.leverRate = int(value)
return
}

func (b *HuobiBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
func (b *HBDMBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64,
stopPx float64, size float64, postOnly bool, reduceOnly bool) (result Order, err error) {
var orderResult hbdm.OrderResult
var _direction string
Expand Down Expand Up @@ -154,7 +182,7 @@ func (b *HuobiBroker) PlaceOrder(symbol string, direction Direction, orderType O
return
}

func (b *HuobiBroker) GetOpenOrders(symbol string) (result []Order, err error) {
func (b *HBDMBroker) GetOpenOrders(symbol string) (result []Order, err error) {
var ret hbdm.OpenOrdersResult
ret, err = b.client.GetOpenOrders(
symbol,
Expand All @@ -174,7 +202,7 @@ func (b *HuobiBroker) GetOpenOrders(symbol string) (result []Order, err error) {
return
}

func (b *HuobiBroker) GetOrder(symbol string, id string) (result Order, err error) {
func (b *HBDMBroker) GetOrder(symbol string, id string) (result Order, err error) {
var ret hbdm.OrderInfoResult
var _id, _ = strconv.ParseInt(id, 10, 64)
ret, err = b.client.OrderInfo(symbol, _id, 0)
Expand All @@ -193,7 +221,7 @@ func (b *HuobiBroker) GetOrder(symbol string, id string) (result Order, err erro
return
}

func (b *HuobiBroker) CancelOrder(symbol string, id string) (result Order, err error) {
func (b *HBDMBroker) CancelOrder(symbol string, id string) (result Order, err error) {
var ret hbdm.CancelResult
var _id, _ = strconv.ParseInt(id, 10, 64)
ret, err = b.client.Cancel(symbol, _id, 0)
Expand All @@ -209,15 +237,15 @@ func (b *HuobiBroker) CancelOrder(symbol string, id string) (result Order, err e
return
}

func (b *HuobiBroker) CancelAllOrders(symbol string) (err error) {
func (b *HBDMBroker) CancelAllOrders(symbol string) (err error) {
return
}

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

func (b *HuobiBroker) GetPosition(symbol string) (result Position, err error) {
func (b *HBDMBroker) GetPosition(symbol string) (result Position, err error) {
result.Symbol = symbol

var ret hbdm.PositionInfoResult
Expand All @@ -235,19 +263,18 @@ func (b *HuobiBroker) GetPosition(symbol string) (result Position, err error) {
return
}

//position := ret.Data[0]

//if position > 0 {
// result.Size = position.BuyAmount
// result.AvgPrice = position.BuyPriceAvg
//} else if position.SellAmount > 0 {
// result.Size = -position.SellAmount
// result.AvgPrice = position.SellPriceAvg
//}
position := ret.Data[0]
if position.Direction == "buy" {
result.Size = position.Volume
} else if position.Direction == "sell" {
result.Size = -position.Volume
}
result.AvgPrice = position.CostHold
result.OpenPrice = position.CostOpen
return
}

func (b *HuobiBroker) convertOrder(symbol string, order *hbdm.Order) (result Order) {
func (b *HBDMBroker) convertOrder(symbol string, order *hbdm.Order) (result Order) {
result.ID = order.OrderIDStr
result.Symbol = symbol
result.Price = order.Price
Expand All @@ -267,7 +294,7 @@ func (b *HuobiBroker) convertOrder(symbol string, order *hbdm.Order) (result Ord
return
}

func (b *HuobiBroker) orderDirection(order *hbdm.Order) Direction {
func (b *HBDMBroker) orderDirection(order *hbdm.Order) Direction {
if order.Direction == "buy" {
return Buy
} else if order.Direction == "sell" {
Expand All @@ -276,7 +303,7 @@ func (b *HuobiBroker) orderDirection(order *hbdm.Order) Direction {
return Buy
}

func (b *HuobiBroker) orderType(order *hbdm.Order) OrderType {
func (b *HBDMBroker) orderType(order *hbdm.Order) OrderType {
/*
order_price_type 订单报价类型 订单报价类型 订单报价类型 "limit":限价 "opponent":对手价 "post_only":只做maker单,post only下单只受用户持仓数量限制,optimal_5:最优5档、optimal_10:最优10档、optimal_20:最优20档,ioc:IOC订单,fok:FOK订单
*/
Expand All @@ -293,7 +320,7 @@ func (b *HuobiBroker) orderType(order *hbdm.Order) OrderType {
return OrderTypeLimit
}

func (b *HuobiBroker) orderStatus(order *hbdm.Order) OrderStatus {
func (b *HBDMBroker) orderStatus(order *hbdm.Order) OrderStatus {
/*
订单状态 (1准备提交 2准备提交 3已提交 4部分成交 5部分成交已撤单
6全部成交 7已撤单 11撤单中)
Expand All @@ -316,11 +343,11 @@ func (b *HuobiBroker) orderStatus(order *hbdm.Order) OrderStatus {
}
}

func (b *HuobiBroker) RunEventLoopOnce() (err error) {
func (b *HBDMBroker) RunEventLoopOnce() (err error) {
return
}

func NewBroker(addr string, accessKey string, secretKey string) *HuobiBroker {
func NewBroker(addr string, accessKey string, secretKey string) *HBDMBroker {
//baseURL := "https://api.hbdm.com"
apiParams := &hbdm.ApiParameter{
Debug: false,
Expand All @@ -331,7 +358,7 @@ func NewBroker(addr string, accessKey string, secretKey string) *HuobiBroker {
PrivateKeyPrime256: "",
}
client := hbdm.NewClient(apiParams)
return &HuobiBroker{
return &HBDMBroker{
client: client,
}
}
13 changes: 12 additions & 1 deletion brokers/hbdm-broker/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newTestBroker() Broker {
return NewBroker(baseURL, accessKey, secretKey)
}

func TestHuobiBroker_GetAccountSummary(t *testing.T) {
func TestHBDMBroker_GetAccountSummary(t *testing.T) {
b := newTestBroker()
accountSummary, err := b.GetAccountSummary("BTC")
if err != nil {
Expand All @@ -30,3 +30,14 @@ func TestHuobiBroker_GetAccountSummary(t *testing.T) {
}
t.Logf("%#v", accountSummary)
}

func TestHBDMBroker_GetContractID(t *testing.T) {
b := newTestBroker()
b.SetContractType("BTC", ContractTypeW1)
symbol, err := b.GetContractID()
if err != nil {
t.Error(err)
return
}
t.Logf("%v", symbol)
}
Loading

0 comments on commit f19440b

Please sign in to comment.