From e76d4acb8ffaa148c434445ecb1ef50085573588 Mon Sep 17 00:00:00 2001 From: Frank Zhou <529808348@qq.com> Date: Sun, 12 Apr 2020 11:59:20 +0800 Subject: [PATCH] Rename --- README.md | 2 +- README_en.md | 2 +- .../broker.go | 58 +++++++++---------- .../helper.go | 2 +- brokers/{bitmex-broker => bitmex}/broker.go | 48 +++++++-------- .../{bitmex-broker => bitmex}/broker_test.go | 26 ++++----- brokers/builder.go | 44 +++++++------- brokers/{bybit-broker => bybit}/broker.go | 54 ++++++++--------- .../{bybit-broker => bybit}/broker_test.go | 8 +-- .../broker.go | 58 +++++++++---------- .../broker_test.go | 4 +- .../helper.go | 2 +- .../helper_test.go | 2 +- brokers/{deribit-broker => deribit}/broker.go | 52 ++++++++--------- .../broker_test.go | 36 ++++-------- .../orderbook_local.go | 2 +- .../orderbook_manager.go | 2 +- .../{hbdm-swap-broker => hbdm-swap}/broker.go | 48 +++++++-------- .../broker_test.go | 10 ++-- .../test_config.example.yaml | 0 brokers/{hbdm-swap-broker => hbdm-swap}/ws.go | 2 +- .../ws_test.go | 2 +- brokers/{hbdm-broker => hbdm}/broker.go | 48 +++++++-------- brokers/{hbdm-broker => hbdm}/broker_test.go | 38 ++++++------ .../test_config.example.yaml | 0 brokers/{hbdm-broker => hbdm}/ws.go | 2 +- brokers/{hbdm-broker => hbdm}/ws_test.go | 2 +- .../broker.go | 48 +++++++-------- .../broker_test.go | 38 ++++++------ .../test_config.example.yaml | 0 .../{okex-swap-broker => okex-swap}/broker.go | 58 ++++++++----------- .../broker_test.go | 4 +- .../test_config.example.yaml | 0 examples/backtest/main.go | 4 +- 34 files changed, 342 insertions(+), 364 deletions(-) rename brokers/{bitmex-sim-broker => bitmex-sim}/broker.go (79%) rename brokers/{bitmex-sim-broker => bitmex-sim}/helper.go (96%) rename brokers/{bitmex-broker => bitmex}/broker.go (73%) rename brokers/{bitmex-broker => bitmex}/broker_test.go (68%) rename brokers/{bybit-broker => bybit}/broker.go (76%) rename brokers/{bybit-broker => bybit}/broker_test.go (76%) rename brokers/{deribit-sim-broker => deribit-sim}/broker.go (79%) rename brokers/{deribit-sim-broker => deribit-sim}/broker_test.go (98%) rename brokers/{deribit-sim-broker => deribit-sim}/helper.go (99%) rename brokers/{deribit-sim-broker => deribit-sim}/helper_test.go (90%) rename brokers/{deribit-broker => deribit}/broker.go (79%) rename brokers/{deribit-broker => deribit}/broker_test.go (57%) rename brokers/{deribit-broker => deribit}/orderbook_local.go (99%) rename brokers/{deribit-broker => deribit}/orderbook_manager.go (97%) rename brokers/{hbdm-swap-broker => hbdm-swap}/broker.go (82%) rename brokers/{hbdm-swap-broker => hbdm-swap}/broker_test.go (77%) rename brokers/{hbdm-broker => hbdm-swap}/test_config.example.yaml (100%) rename brokers/{hbdm-swap-broker => hbdm-swap}/ws.go (99%) rename brokers/{hbdm-swap-broker => hbdm-swap}/ws_test.go (97%) rename brokers/{hbdm-broker => hbdm}/broker.go (85%) rename brokers/{hbdm-broker => hbdm}/broker_test.go (77%) rename brokers/{hbdm-swap-broker => hbdm}/test_config.example.yaml (100%) rename brokers/{hbdm-broker => hbdm}/ws.go (99%) rename brokers/{hbdm-broker => hbdm}/ws_test.go (98%) rename brokers/{okex-futures-broker => okex-futures}/broker.go (83%) rename brokers/{okex-futures-broker => okex-futures}/broker_test.go (78%) rename brokers/{okex-futures-broker => okex-futures}/test_config.example.yaml (100%) rename brokers/{okex-swap-broker => okex-swap}/broker.go (82%) rename brokers/{okex-swap-broker => okex-swap}/broker_test.go (96%) rename brokers/{okex-swap-broker => okex-swap}/test_config.example.yaml (100%) diff --git a/README.md b/README.md index c359f50..0e33162 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ ## 主要特性 * 使用简单 * Tick级别回测 -* 支持实盘 +* 支持 WebSocket ## 支持交易所 | name | id | ver | doc | diff --git a/README_en.md b/README_en.md index b09561c..4e51c6e 100644 --- a/README_en.md +++ b/README_en.md @@ -28,7 +28,7 @@ See [@live trading](https://github.com/coinrust/crex/blob/master/examples/live/m ### Main Features * Ease of use. * Tick-level backtesting. -* Backtesting and live-trading functionality. +* WebSocket Supported ### Supported Exchanges | name | id | ver | doc | diff --git a/brokers/bitmex-sim-broker/broker.go b/brokers/bitmex-sim/broker.go similarity index 79% rename from brokers/bitmex-sim-broker/broker.go rename to brokers/bitmex-sim/broker.go index bdb6cbe..ad3d27c 100644 --- a/brokers/bitmex-sim-broker/broker.go +++ b/brokers/bitmex-sim/broker.go @@ -1,4 +1,4 @@ -package bitmex_sim_broker +package bitmex_sim import ( "errors" @@ -22,8 +22,8 @@ type MarginInfo struct { LiquidationPriceShort float64 } -// BitMEXSimBroker the bitmex broker for backtest -type BitMEXSimBroker struct { +// BitMEXSim the bitmex broker for backtest +type BitMEXSim struct { data *data.Data makerFeeRate float64 // -0.00025 // Maker fee rate takerFeeRate float64 // 0.00075 // Taker fee rate @@ -36,11 +36,11 @@ type BitMEXSimBroker struct { symbol string } -func (b *BitMEXSimBroker) GetName() (name string) { +func (b *BitMEXSim) GetName() (name string) { return "BitMEX" } -func (b *BitMEXSimBroker) GetAccountSummary(currency string) (result AccountSummary, err error) { +func (b *BitMEXSim) GetAccountSummary(currency string) (result AccountSummary, err error) { result.Balance = b.balance var symbol string if currency == "XBT" || currency == "BTC" { @@ -61,29 +61,29 @@ func (b *BitMEXSimBroker) GetAccountSummary(currency string) (result AccountSumm return } -func (b *BitMEXSimBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { +func (b *BitMEXSim) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { result = *b.data.GetOrderBook() return } -func (b *BitMEXSimBroker) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { +func (b *BitMEXSim) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { return } -func (b *BitMEXSimBroker) SetContractType(currencyPair string, contractType string) (err error) { +func (b *BitMEXSim) SetContractType(currencyPair string, contractType string) (err error) { b.symbol = currencyPair return } -func (b *BitMEXSimBroker) GetContractID() (symbol string, err error) { +func (b *BitMEXSim) GetContractID() (symbol string, err error) { return b.symbol, nil } -func (b *BitMEXSimBroker) SetLeverRate(value float64) (err error) { +func (b *BitMEXSim) SetLeverRate(value float64) (err error) { return } -func (b *BitMEXSimBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *BitMEXSim) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64, postOnly bool, reduceOnly bool, params map[string]interface{}) (result Order, err error) { _id, _ := util.NextID() id := fmt.Sprintf("%v", _id) @@ -117,7 +117,7 @@ func (b *BitMEXSimBroker) PlaceOrder(symbol string, direction Direction, orderTy } // 撮合成交 -func (b *BitMEXSimBroker) matchOrder(order *Order, immediate bool) (err error) { +func (b *BitMEXSim) matchOrder(order *Order, immediate bool) (err error) { switch order.Type { case OrderTypeMarket: err = b.matchMarketOrder(order) @@ -127,7 +127,7 @@ func (b *BitMEXSimBroker) matchOrder(order *Order, immediate bool) (err error) { return } -func (b *BitMEXSimBroker) matchMarketOrder(order *Order) (err error) { +func (b *BitMEXSim) matchMarketOrder(order *Order) (err error) { if !order.IsOpen() { return } @@ -196,7 +196,7 @@ func (b *BitMEXSimBroker) matchMarketOrder(order *Order) (err error) { return } -func (b *BitMEXSimBroker) matchLimitOrder(order *Order, immediate bool) (err error) { +func (b *BitMEXSim) matchLimitOrder(order *Order, immediate bool) (err error) { if !order.IsOpen() { return } @@ -255,7 +255,7 @@ func (b *BitMEXSimBroker) matchLimitOrder(order *Order, immediate bool) (err err } // 更新持仓 -func (b *BitMEXSimBroker) updatePosition(symbol string, size float64, price float64) { +func (b *BitMEXSim) updatePosition(symbol string, size float64, price float64) { position := b.getPosition(symbol) if position == nil { log.Fatalf("position error symbol=%v", symbol) @@ -269,7 +269,7 @@ func (b *BitMEXSimBroker) updatePosition(symbol string, size float64, price floa } // 增加持仓 -func (b *BitMEXSimBroker) addPosition(position *Position, size float64, price float64) (err error) { +func (b *BitMEXSim) addPosition(position *Position, size float64, price float64) (err error) { if position.Size < 0 && size > 0 || position.Size > 0 && size < 0 { err = errors.New("方向错误") return @@ -294,7 +294,7 @@ func (b *BitMEXSimBroker) addPosition(position *Position, size float64, price fl } // 平仓,超过数量,则开立新仓 -func (b *BitMEXSimBroker) closePosition(position *Position, size float64, price float64) (err error) { +func (b *BitMEXSim) closePosition(position *Position, size float64, price float64) (err error) { if position.Size == 0 { err = errors.New("当前无持仓") return @@ -328,17 +328,17 @@ func (b *BitMEXSimBroker) closePosition(position *Position, size float64, price } // 增加Balance -func (b *BitMEXSimBroker) addBalance(value float64) { +func (b *BitMEXSim) addBalance(value float64) { b.balance += value } // 增加P/L -func (b *BitMEXSimBroker) addPnl(pnl float64) { +func (b *BitMEXSim) addPnl(pnl float64) { b.balance += pnl } // 获取持仓 -func (b *BitMEXSimBroker) getPosition(symbol string) *Position { +func (b *BitMEXSim) getPosition(symbol string) *Position { if position, ok := b.positions[symbol]; ok { return position } else { @@ -354,7 +354,7 @@ func (b *BitMEXSimBroker) getPosition(symbol string) *Position { } } -func (b *BitMEXSimBroker) GetOpenOrders(symbol string) (result []Order, err error) { +func (b *BitMEXSim) GetOpenOrders(symbol string) (result []Order, err error) { for _, v := range b.openOrders { if v.Symbol == symbol { result = append(result, *v) @@ -363,7 +363,7 @@ func (b *BitMEXSimBroker) GetOpenOrders(symbol string) (result []Order, err erro return } -func (b *BitMEXSimBroker) GetOrder(symbol string, id string) (result Order, err error) { +func (b *BitMEXSim) GetOrder(symbol string, id string) (result Order, err error) { order, ok := b.orders[id] if !ok { err = errors.New("not found") @@ -373,7 +373,7 @@ func (b *BitMEXSimBroker) GetOrder(symbol string, id string) (result Order, err return } -func (b *BitMEXSimBroker) CancelOrder(symbol string, id string) (result Order, err error) { +func (b *BitMEXSim) CancelOrder(symbol string, id string) (result Order, err error) { if order, ok := b.orders[id]; ok { if !order.IsOpen() { err = errors.New("status error") @@ -393,7 +393,7 @@ func (b *BitMEXSimBroker) CancelOrder(symbol string, id string) (result Order, e return } -func (b *BitMEXSimBroker) CancelAllOrders(symbol string) (err error) { +func (b *BitMEXSim) CancelAllOrders(symbol string) (err error) { var idsToBeRemoved []string for _, order := range b.openOrders { @@ -416,11 +416,11 @@ 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) { +func (b *BitMEXSim) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { return } -func (b *BitMEXSimBroker) GetPosition(symbol string) (result Position, err error) { +func (b *BitMEXSim) GetPosition(symbol string) (result Position, err error) { position, ok := b.positions[symbol] if !ok { err = errors.New("not found") @@ -430,15 +430,15 @@ func (b *BitMEXSimBroker) GetPosition(symbol string) (result Position, err error return } -func (b *BitMEXSimBroker) RunEventLoopOnce() (err error) { +func (b *BitMEXSim) RunEventLoopOnce() (err error) { for _, order := range b.openOrders { b.matchOrder(order, false) } return } -func NewBroker(data *data.Data, cash float64, makerFeeRate float64, takerFeeRate float64) *BitMEXSimBroker { - return &BitMEXSimBroker{ +func New(data *data.Data, cash float64, makerFeeRate float64, takerFeeRate float64) *BitMEXSim { + return &BitMEXSim{ data: data, balance: cash, makerFeeRate: makerFeeRate, // -0.00025 // Maker 费率 diff --git a/brokers/bitmex-sim-broker/helper.go b/brokers/bitmex-sim/helper.go similarity index 96% rename from brokers/bitmex-sim-broker/helper.go rename to brokers/bitmex-sim/helper.go index cd1ce88..a69ff3f 100644 --- a/brokers/bitmex-sim-broker/helper.go +++ b/brokers/bitmex-sim/helper.go @@ -1,4 +1,4 @@ -package bitmex_sim_broker +package bitmex_sim import ( "github.com/coinrust/crex" diff --git a/brokers/bitmex-broker/broker.go b/brokers/bitmex/broker.go similarity index 73% rename from brokers/bitmex-broker/broker.go rename to brokers/bitmex/broker.go index c0c0d59..d343ea9 100644 --- a/brokers/bitmex-broker/broker.go +++ b/brokers/bitmex/broker.go @@ -1,4 +1,4 @@ -package bitmex_broker +package bitmex import ( . "github.com/coinrust/crex" @@ -8,17 +8,17 @@ import ( "time" ) -// BitMEXBroker the BitMEX broker -type BitMEXBroker struct { +// BitMEX the BitMEX broker +type BitMEX struct { client *bitmex.BitMEX symbol string } -func (b *BitMEXBroker) GetName() (name string) { +func (b *BitMEX) GetName() (name string) { return "BitMEX" } -func (b *BitMEXBroker) GetAccountSummary(currency string) (result AccountSummary, err error) { +func (b *BitMEX) GetAccountSummary(currency string) (result AccountSummary, err error) { var margin swagger.Margin margin, err = b.client.GetMargin() if err != nil { @@ -30,7 +30,7 @@ func (b *BitMEXBroker) GetAccountSummary(currency string) (result AccountSummary return } -func (b *BitMEXBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { +func (b *BitMEX) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { var ret bitmex.OrderBook ret, err = b.client.GetOrderBook(depth, symbol) if err != nil { @@ -52,7 +52,7 @@ func (b *BitMEXBroker) GetOrderBook(symbol string, depth int) (result OrderBook, return } -func (b *BitMEXBroker) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { +func (b *BitMEX) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { //@param "binSize" (string) Time interval to bucket by. Available options: [1m,5m,1h,1d]. var binSize string if strings.HasSuffix(period, "m") { @@ -92,20 +92,20 @@ func (b *BitMEXBroker) GetRecords(symbol string, period string, from int64, end return } -func (b *BitMEXBroker) SetContractType(currencyPair string, contractType string) (err error) { +func (b *BitMEX) SetContractType(currencyPair string, contractType string) (err error) { b.symbol = currencyPair return } -func (b *BitMEXBroker) GetContractID() (symbol string, err error) { +func (b *BitMEX) GetContractID() (symbol string, err error) { return b.symbol, nil } -func (b *BitMEXBroker) SetLeverRate(value float64) (err error) { +func (b *BitMEX) SetLeverRate(value float64) (err error) { return } -func (b *BitMEXBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *BitMEX) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64, postOnly bool, reduceOnly bool, params map[string]interface{}) (result Order, err error) { var side string var _orderType string @@ -142,7 +142,7 @@ func (b *BitMEXBroker) PlaceOrder(symbol string, direction Direction, orderType return } -func (b *BitMEXBroker) GetOpenOrders(symbol string) (result []Order, err error) { +func (b *BitMEX) GetOpenOrders(symbol string) (result []Order, err error) { var ret []swagger.Order ret, err = b.client.GetOrders(symbol) if err != nil { @@ -154,7 +154,7 @@ func (b *BitMEXBroker) GetOpenOrders(symbol string) (result []Order, err error) return } -func (b *BitMEXBroker) GetOrder(symbol string, id string) (result Order, err error) { +func (b *BitMEX) GetOrder(symbol string, id string) (result Order, err error) { var ret swagger.Order ret, err = b.client.GetOrder(id, symbol) if err != nil { @@ -164,7 +164,7 @@ func (b *BitMEXBroker) GetOrder(symbol string, id string) (result Order, err err return } -func (b *BitMEXBroker) CancelOrder(symbol string, id string) (result Order, err error) { +func (b *BitMEX) CancelOrder(symbol string, id string) (result Order, err error) { var order swagger.Order order, err = b.client.CancelOrder(id) if err != nil { @@ -174,12 +174,12 @@ func (b *BitMEXBroker) CancelOrder(symbol string, id string) (result Order, err return } -func (b *BitMEXBroker) CancelAllOrders(symbol string) (err error) { +func (b *BitMEX) CancelAllOrders(symbol string) (err error) { _, err = b.client.CancelAllOrders(symbol) return } -func (b *BitMEXBroker) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { +func (b *BitMEX) 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 { @@ -189,7 +189,7 @@ func (b *BitMEXBroker) AmendOrder(symbol string, id string, price float64, size return } -func (b *BitMEXBroker) GetPosition(symbol string) (result Position, err error) { +func (b *BitMEX) GetPosition(symbol string) (result Position, err error) { var ret swagger.Position ret, err = b.client.GetPosition(symbol) if err != nil { @@ -201,7 +201,7 @@ func (b *BitMEXBroker) GetPosition(symbol string) (result Position, err error) { return } -func (b *BitMEXBroker) convertOrder(order *swagger.Order) (result Order) { +func (b *BitMEX) convertOrder(order *swagger.Order) (result Order) { result.ID = order.OrderID result.Symbol = order.Symbol result.Price = order.Price @@ -221,7 +221,7 @@ func (b *BitMEXBroker) convertOrder(order *swagger.Order) (result Order) { return } -func (b *BitMEXBroker) convertDirection(side string) Direction { +func (b *BitMEX) convertDirection(side string) Direction { switch side { case bitmex.SIDE_BUY: return Buy @@ -232,7 +232,7 @@ func (b *BitMEXBroker) convertDirection(side string) Direction { } } -func (b *BitMEXBroker) convertOrderType(orderType string) OrderType { +func (b *BitMEX) convertOrderType(orderType string) OrderType { switch orderType { case bitmex.ORD_TYPE_LIMIT: return OrderTypeLimit @@ -247,7 +247,7 @@ func (b *BitMEXBroker) convertOrderType(orderType string) OrderType { } } -func (b *BitMEXBroker) orderStatus(order *swagger.Order) OrderStatus { +func (b *BitMEX) orderStatus(order *swagger.Order) OrderStatus { orderState := order.OrdStatus switch orderState { case bitmex.OS_NEW: @@ -265,13 +265,13 @@ func (b *BitMEXBroker) orderStatus(order *swagger.Order) OrderStatus { } } -func (b *BitMEXBroker) RunEventLoopOnce() (err error) { +func (b *BitMEX) RunEventLoopOnce() (err error) { return } -func NewBroker(addr string, accessKey string, secretKey string) *BitMEXBroker { +func New(addr string, accessKey string, secretKey string) *BitMEX { client := bitmex.New(addr, accessKey, secretKey) - return &BitMEXBroker{ + return &BitMEX{ client: client, } } diff --git a/brokers/bitmex-broker/broker_test.go b/brokers/bitmex/broker_test.go similarity index 68% rename from brokers/bitmex-broker/broker_test.go rename to brokers/bitmex/broker_test.go index 3f999f3..e6c41b8 100644 --- a/brokers/bitmex-broker/broker_test.go +++ b/brokers/bitmex/broker_test.go @@ -1,4 +1,4 @@ -package bitmex_broker +package bitmex import ( . "github.com/coinrust/crex" @@ -7,16 +7,16 @@ import ( "time" ) -func newBrokerForTest() *BitMEXBroker { +func newForTest() *BitMEX { apiKey := "eEtTUdma5LgAmryFerX-DAdp" secretKey := "kPjKmu-EIe1E73poRTnUraQWCMWbRq7PZ2-bzP8cnemniMXu" - b := NewBroker(bitmex.HostTestnet, apiKey, secretKey) + b := New(bitmex.HostTestnet, apiKey, secretKey) b.client.SetProxy("127.0.0.1:1080") return b } -func TestBitMEXBroker_GetOrderBook(t *testing.T) { - b := newBrokerForTest() +func TestBitMEX_GetOrderBook(t *testing.T) { + b := newForTest() ob, err := b.GetOrderBook("XBTUSD", 10) if err != nil { t.Error(err) @@ -25,8 +25,8 @@ func TestBitMEXBroker_GetOrderBook(t *testing.T) { t.Logf("%#v", ob) } -func TestBitMEXBroker_GetRecords(t *testing.T) { - b := newBrokerForTest() +func TestBitMEX_GetRecords(t *testing.T) { + b := newForTest() start := time.Now().Add(-time.Hour) end := time.Now() records, err := b.GetRecords("XBTUSD", @@ -40,8 +40,8 @@ func TestBitMEXBroker_GetRecords(t *testing.T) { } } -func TestBitMEXBroker_PlaceOrder(t *testing.T) { - b := newBrokerForTest() +func TestBitMEX_PlaceOrder(t *testing.T) { + b := newForTest() order, err := b.PlaceOrder("XBTUSD", Buy, OrderTypeLimit, @@ -58,8 +58,8 @@ func TestBitMEXBroker_PlaceOrder(t *testing.T) { t.Logf("%#v", order) } -func TestBitMEXBroker_GetOpenOrders(t *testing.T) { - b := newBrokerForTest() +func TestBitMEX_GetOpenOrders(t *testing.T) { + b := newForTest() orders, err := b.GetOpenOrders("XBTUSD") if err != nil { t.Error(err) @@ -70,8 +70,8 @@ func TestBitMEXBroker_GetOpenOrders(t *testing.T) { } } -func TestBitMEXBroker_GetOrder(t *testing.T) { - b := newBrokerForTest() +func TestBitMEX_GetOrder(t *testing.T) { + b := newForTest() order, err := b.GetOrder("XBTUSD", "c90d5194-0f6a-31db-7942-c3caf1f8a055") if err != nil { t.Error(err) diff --git a/brokers/builder.go b/brokers/builder.go index 1b00025..63e701f 100644 --- a/brokers/builder.go +++ b/brokers/builder.go @@ -3,15 +3,15 @@ package brokers import ( "fmt" . "github.com/coinrust/crex" - bitmexbroker "github.com/coinrust/crex/brokers/bitmex-broker" - bybitbroker "github.com/coinrust/crex/brokers/bybit-broker" - deribitbroker "github.com/coinrust/crex/brokers/deribit-broker" - hbdmbroker "github.com/coinrust/crex/brokers/hbdm-broker" - hbdmswapbroker "github.com/coinrust/crex/brokers/hbdm-swap-broker" - okexfuturesbroker "github.com/coinrust/crex/brokers/okex-futures-broker" - okexswapbroker "github.com/coinrust/crex/brokers/okex-swap-broker" - "github.com/frankrap/bitmex-api" - "github.com/frankrap/deribit-api" + "github.com/coinrust/crex/brokers/bitmex" + "github.com/coinrust/crex/brokers/bybit" + "github.com/coinrust/crex/brokers/deribit" + "github.com/coinrust/crex/brokers/hbdm" + "github.com/coinrust/crex/brokers/hbdm-swap" + "github.com/coinrust/crex/brokers/okex-futures" + "github.com/coinrust/crex/brokers/okex-swap" + bitmexapi "github.com/frankrap/bitmex-api" + deribitapi "github.com/frankrap/deribit-api" "log" ) @@ -20,39 +20,39 @@ func NewBroker(brokerName string, accessKey string, secret string, testnet bool, switch brokerName { case BitMEX: if testnet { - addr = bitmex.HostTestnet + addr = bitmexapi.HostTestnet } else { - addr = bitmex.HostReal + addr = bitmexapi.HostReal } - return bitmexbroker.NewBroker(addr, accessKey, secret) + return bitmex.New(addr, accessKey, secret) case Deribit: if testnet { - addr = deribit.TestBaseURL + addr = deribitapi.TestBaseURL } else { - addr = deribit.RealBaseURL + addr = deribitapi.RealBaseURL } - return deribitbroker.NewBroker(addr, accessKey, secret) + return deribit.New(addr, accessKey, secret) case Bybit: if testnet { addr = "https://api-testnet.bybit.com/" } else { addr = "https://api.bybit.com/" } - return bybitbroker.NewBroker(addr, accessKey, secret) + return bybit.New(addr, accessKey, secret) case HBDM: if testnet { addr = "https://api.btcgateway.pro" } else { addr = "https://api.hbdm.com" } - return hbdmbroker.NewBroker(addr, accessKey, secret) + return hbdm.New(addr, accessKey, secret) case HBDMSwap: if testnet { addr = "https://api.btcgateway.pro" } else { addr = "https://api.hbdm.com" } - return hbdmswapbroker.NewBroker(addr, accessKey, secret) + return hbdm_swap.New(addr, accessKey, secret) case OKEXFutures: if testnet { addr = "https://testnet.okex.me" @@ -71,7 +71,7 @@ func NewBroker(brokerName string, accessKey string, secret string, testnet bool, } else { log.Fatalf("passphrase missing") } - return okexfuturesbroker.NewBroker(addr, accessKey, secret, passphrase) + return okex_futures.New(addr, accessKey, secret, passphrase) case OKEXSwap: if testnet { addr = "https://testnet.okex.me" @@ -90,7 +90,7 @@ func NewBroker(brokerName string, accessKey string, secret string, testnet bool, } else { log.Fatalf("passphrase missing") } - return okexswapbroker.NewBroker(addr, accessKey, secret, passphrase) + return okex_swap.New(addr, accessKey, secret, passphrase) default: panic(fmt.Sprintf("broker error [%v]", brokerName)) } @@ -103,13 +103,13 @@ func NewWS(brokerName string, accessKey string, secret string, testnet bool, par if v, ok := params["wsURL"]; ok { wsURL = v } - return hbdmbroker.NewWS(wsURL, accessKey, secret) + return hbdm.NewWS(wsURL, accessKey, secret) case HBDMSwap: wsURL := "wss://api.hbdm.com/swap-ws" if v, ok := params["wsURL"]; ok { wsURL = v } - return hbdmswapbroker.NewWS(wsURL, accessKey, secret) + return hbdm_swap.NewWS(wsURL, accessKey, secret) default: panic(fmt.Sprintf("broker error [%v]", brokerName)) } diff --git a/brokers/bybit-broker/broker.go b/brokers/bybit/broker.go similarity index 76% rename from brokers/bybit-broker/broker.go rename to brokers/bybit/broker.go index e655f82..27c2581 100644 --- a/brokers/bybit-broker/broker.go +++ b/brokers/bybit/broker.go @@ -1,4 +1,4 @@ -package bybit_broker +package bybit import ( "errors" @@ -9,17 +9,17 @@ import ( "time" ) -// BybitBroker the Bybit broker -type BybitBroker struct { +// Bybit the Bybit broker +type Bybit struct { client *rest.ByBit symbol string } -func (b *BybitBroker) GetName() (name string) { +func (b *Bybit) GetName() (name string) { return "Bybit" } -func (b *BybitBroker) GetAccountSummary(currency string) (result AccountSummary, err error) { +func (b *Bybit) GetAccountSummary(currency string) (result AccountSummary, err error) { var balance rest.Balance balance, err = b.client.GetWalletBalance(currency) if err != nil { @@ -32,7 +32,7 @@ func (b *BybitBroker) GetAccountSummary(currency string) (result AccountSummary, return } -func (b *BybitBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { +func (b *Bybit) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { var ob rest.OrderBook ob, err = b.client.GetOrderBook(symbol) if err != nil { @@ -57,7 +57,7 @@ func (b *BybitBroker) GetOrderBook(symbol string, depth int) (result OrderBook, return } -func (b *BybitBroker) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { +func (b *Bybit) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { var values []rest.OHLC values, err = b.client.GetKLine(symbol, period, from, limit) if err != nil { @@ -77,20 +77,20 @@ func (b *BybitBroker) GetRecords(symbol string, period string, from int64, end i return } -func (b *BybitBroker) SetContractType(currencyPair string, contractType string) (err error) { +func (b *Bybit) SetContractType(currencyPair string, contractType string) (err error) { b.symbol = currencyPair return } -func (b *BybitBroker) GetContractID() (symbol string, err error) { +func (b *Bybit) GetContractID() (symbol string, err error) { return b.symbol, nil } -func (b *BybitBroker) SetLeverRate(value float64) (err error) { +func (b *Bybit) SetLeverRate(value float64) (err error) { return } -func (b *BybitBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *Bybit) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64, postOnly bool, reduceOnly bool, params map[string]interface{}) (result Order, err error) { if orderType == OrderTypeLimit || orderType == OrderTypeMarket { return b.placeOrder(symbol, direction, orderType, price, size, postOnly, reduceOnly) @@ -102,7 +102,7 @@ func (b *BybitBroker) PlaceOrder(symbol string, direction Direction, orderType O } } -func (b *BybitBroker) placeOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *Bybit) placeOrder(symbol string, direction Direction, orderType OrderType, price float64, size float64, postOnly bool, reduceOnly bool) (result Order, err error) { var side string var _orderType string @@ -141,7 +141,7 @@ func (b *BybitBroker) placeOrder(symbol string, direction Direction, orderType O return } -func (b *BybitBroker) placeStopOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *Bybit) placeStopOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64, postOnly bool, reduceOnly bool) (result Order, err error) { var side string var _orderType string @@ -184,7 +184,7 @@ func (b *BybitBroker) placeStopOrder(symbol string, direction Direction, orderTy return } -func (b *BybitBroker) GetOpenOrders(symbol string) (result []Order, err error) { +func (b *Bybit) GetOpenOrders(symbol string) (result []Order, err error) { limit := 10 orderStatus := "Created,New,PartiallyFilled,PendingCancel" for page := 1; page <= 5; page++ { @@ -205,7 +205,7 @@ func (b *BybitBroker) GetOpenOrders(symbol string) (result []Order, err error) { return } -func (b *BybitBroker) GetOrder(symbol string, id string) (result Order, err error) { +func (b *Bybit) GetOrder(symbol string, id string) (result Order, err error) { var ret rest.OrderV2 ret, err = b.client.GetOrderByID(id, "", symbol) if err != nil { @@ -215,7 +215,7 @@ func (b *BybitBroker) GetOrder(symbol string, id string) (result Order, err erro return } -func (b *BybitBroker) CancelOrder(symbol string, id string) (result Order, err error) { +func (b *Bybit) CancelOrder(symbol string, id string) (result Order, err error) { var order rest.OrderV2 order, err = b.client.CancelOrderV2(id, "", symbol) if err != nil { @@ -225,12 +225,12 @@ func (b *BybitBroker) CancelOrder(symbol string, id string) (result Order, err e return } -func (b *BybitBroker) CancelAllOrders(symbol string) (err error) { +func (b *Bybit) CancelAllOrders(symbol string) (err error) { _, err = b.client.CancelAllOrder(symbol) return } -func (b *BybitBroker) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { +func (b *Bybit) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { var order rest.Order order, err = b.client.ReplaceOrder(symbol, id, int(size), price) if err != nil { @@ -241,7 +241,7 @@ func (b *BybitBroker) AmendOrder(symbol string, id string, price float64, size f return } -func (b *BybitBroker) GetPosition(symbol string) (result Position, err error) { +func (b *Bybit) GetPosition(symbol string) (result Position, err error) { var ret rest.Position ret, err = b.client.GetPosition(symbol) if err != nil { @@ -253,7 +253,7 @@ func (b *BybitBroker) GetPosition(symbol string) (result Position, err error) { return } -func (b *BybitBroker) convertOrder(order *rest.Order) (result Order) { +func (b *Bybit) convertOrder(order *rest.Order) (result Order) { result.ID = order.OrderID result.Symbol = order.Symbol result.Price = order.Price @@ -273,7 +273,7 @@ func (b *BybitBroker) convertOrder(order *rest.Order) (result Order) { return } -func (b *BybitBroker) convertOrderV2(order *rest.OrderV2) (result Order) { +func (b *Bybit) convertOrderV2(order *rest.OrderV2) (result Order) { result.ID = order.OrderID result.Symbol = order.Symbol result.Price, _ = order.Price.Float64() @@ -294,7 +294,7 @@ func (b *BybitBroker) convertOrderV2(order *rest.OrderV2) (result Order) { return } -func (b *BybitBroker) convertDirection(side string) Direction { +func (b *Bybit) convertDirection(side string) Direction { switch side { case "Buy": return Buy @@ -305,7 +305,7 @@ func (b *BybitBroker) convertDirection(side string) Direction { } } -func (b *BybitBroker) convertOrderType(orderType string) OrderType { +func (b *Bybit) convertOrderType(orderType string) OrderType { switch orderType { case "Limit": return OrderTypeLimit @@ -316,7 +316,7 @@ func (b *BybitBroker) convertOrderType(orderType string) OrderType { } } -func (b *BybitBroker) orderStatus(orderStatus string) OrderStatus { +func (b *Bybit) orderStatus(orderStatus string) OrderStatus { switch orderStatus { case "Created": return OrderStatusCreated @@ -341,11 +341,11 @@ func (b *BybitBroker) orderStatus(orderStatus string) OrderStatus { } } -func (b *BybitBroker) RunEventLoopOnce() (err error) { +func (b *Bybit) RunEventLoopOnce() (err error) { return } -func NewBroker(addr string, accessKey string, secretKey string) *BybitBroker { +func New(addr string, accessKey string, secretKey string) *Bybit { client := rest.New(addr, accessKey, secretKey) for i := 0; i < 3; i++ { err := client.SetCorrectServerTime() @@ -354,7 +354,7 @@ func NewBroker(addr string, accessKey string, secretKey string) *BybitBroker { continue } } - return &BybitBroker{ + return &Bybit{ client: client, } } diff --git a/brokers/bybit-broker/broker_test.go b/brokers/bybit/broker_test.go similarity index 76% rename from brokers/bybit-broker/broker_test.go rename to brokers/bybit/broker_test.go index de24945..801cda6 100644 --- a/brokers/bybit-broker/broker_test.go +++ b/brokers/bybit/broker_test.go @@ -1,4 +1,4 @@ -package bybit_broker +package bybit import ( . "github.com/coinrust/crex" @@ -7,12 +7,12 @@ import ( ) func newForTest() Broker { - b := NewBroker("https://api-testnet.bybit.com/", + b := New("https://api-testnet.bybit.com/", "6IASD6KDBdunn5qLpT", "nXjZMUiB3aMiPaQ9EUKYFloYNd0zM39RjRWF") return b } -func TestBybitBroker_GetOpenOrders(t *testing.T) { +func TestBybit_GetOpenOrders(t *testing.T) { b := newForTest() orders, err := b.GetOpenOrders("BTCUSD") if err != nil { @@ -24,7 +24,7 @@ func TestBybitBroker_GetOpenOrders(t *testing.T) { } } -func TestBybitBroker_GetRecords(t *testing.T) { +func TestBybit_GetRecords(t *testing.T) { b := newForTest() start := time.Now().Add(-time.Hour) records, err := b.GetRecords("BTCUSD", diff --git a/brokers/deribit-sim-broker/broker.go b/brokers/deribit-sim/broker.go similarity index 79% rename from brokers/deribit-sim-broker/broker.go rename to brokers/deribit-sim/broker.go index 3eb6e89..ae2bf53 100644 --- a/brokers/deribit-sim-broker/broker.go +++ b/brokers/deribit-sim/broker.go @@ -1,4 +1,4 @@ -package deribit_sim_broker +package deribit_sim import ( "errors" @@ -22,8 +22,8 @@ type MarginInfo struct { LiquidationPriceShort float64 } -// DiribitSimBroker the deribit broker for backtest -type DiribitSimBroker struct { +// DiribitSim the deribit broker for backtest +type DiribitSim struct { data *data.Data makerFeeRate float64 // -0.00025 // Maker fee rate takerFeeRate float64 // 0.00075 // Taker fee rate @@ -34,11 +34,11 @@ type DiribitSimBroker struct { positions map[string]*Position // Position key: symbol } -func (b *DiribitSimBroker) GetName() (name string) { +func (b *DiribitSim) GetName() (name string) { return "Deribit" } -func (b *DiribitSimBroker) GetAccountSummary(currency string) (result AccountSummary, err error) { +func (b *DiribitSim) GetAccountSummary(currency string) (result AccountSummary, err error) { result.Balance = b.balance var symbol string if currency == "BTC" { @@ -61,28 +61,28 @@ func (b *DiribitSimBroker) GetAccountSummary(currency string) (result AccountSum return } -func (b *DiribitSimBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { +func (b *DiribitSim) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { result = *b.data.GetOrderBook() return } -func (b *DiribitSimBroker) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { +func (b *DiribitSim) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { return } -func (b *DiribitSimBroker) SetContractType(pair string, contractType string) (err error) { +func (b *DiribitSim) SetContractType(pair string, contractType string) (err error) { return } -func (b *DiribitSimBroker) GetContractID() (symbol string, err error) { +func (b *DiribitSim) GetContractID() (symbol string, err error) { return } -func (b *DiribitSimBroker) SetLeverRate(value float64) (err error) { +func (b *DiribitSim) SetLeverRate(value float64) (err error) { return } -func (b *DiribitSimBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *DiribitSim) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64, postOnly bool, reduceOnly bool, params map[string]interface{}) (result Order, err error) { _id, _ := util.NextID() id := fmt.Sprintf("%v", _id) @@ -116,7 +116,7 @@ func (b *DiribitSimBroker) PlaceOrder(symbol string, direction Direction, orderT } // 撮合成交 -func (b *DiribitSimBroker) matchOrder(order *Order, immediate bool) (err error) { +func (b *DiribitSim) matchOrder(order *Order, immediate bool) (err error) { switch order.Type { case OrderTypeMarket: err = b.matchMarketOrder(order) @@ -126,7 +126,7 @@ func (b *DiribitSimBroker) matchOrder(order *Order, immediate bool) (err error) return } -func (b *DiribitSimBroker) matchMarketOrder(order *Order) (err error) { +func (b *DiribitSim) matchMarketOrder(order *Order) (err error) { if !order.IsOpen() { return } @@ -201,7 +201,7 @@ func (b *DiribitSimBroker) matchMarketOrder(order *Order) (err error) { return } -func (b *DiribitSimBroker) matchLimitOrder(order *Order, immediate bool) (err error) { +func (b *DiribitSim) matchLimitOrder(order *Order, immediate bool) (err error) { if !order.IsOpen() { return } @@ -260,7 +260,7 @@ func (b *DiribitSimBroker) matchLimitOrder(order *Order, immediate bool) (err er } // 更新持仓 -func (b *DiribitSimBroker) updatePosition(symbol string, size float64, price float64) { +func (b *DiribitSim) updatePosition(symbol string, size float64, price float64) { position := b.getPosition(symbol) if position == nil { log.Fatalf("position error symbol=%v", symbol) @@ -274,7 +274,7 @@ func (b *DiribitSimBroker) updatePosition(symbol string, size float64, price flo } // 增加持仓 -func (b *DiribitSimBroker) addPosition(position *Position, size float64, price float64) (err error) { +func (b *DiribitSim) addPosition(position *Position, size float64, price float64) (err error) { if position.Size < 0 && size > 0 || position.Size > 0 && size < 0 { err = errors.New("方向错误") return @@ -299,7 +299,7 @@ func (b *DiribitSimBroker) addPosition(position *Position, size float64, price f } // 平仓,超过数量,则开立新仓 -func (b *DiribitSimBroker) closePosition(position *Position, size float64, price float64) (err error) { +func (b *DiribitSim) closePosition(position *Position, size float64, price float64) (err error) { if position.Size == 0 { err = errors.New("当前无持仓") return @@ -333,17 +333,17 @@ func (b *DiribitSimBroker) closePosition(position *Position, size float64, price } // 增加Balance -func (b *DiribitSimBroker) addBalance(value float64) { +func (b *DiribitSim) addBalance(value float64) { b.balance += value } // 增加P/L -func (b *DiribitSimBroker) addPnl(pnl float64) { +func (b *DiribitSim) addPnl(pnl float64) { b.balance += pnl } // 获取持仓 -func (b *DiribitSimBroker) getPosition(symbol string) *Position { +func (b *DiribitSim) getPosition(symbol string) *Position { if position, ok := b.positions[symbol]; ok { return position } else { @@ -359,7 +359,7 @@ func (b *DiribitSimBroker) getPosition(symbol string) *Position { } } -func (b *DiribitSimBroker) GetOpenOrders(symbol string) (result []Order, err error) { +func (b *DiribitSim) GetOpenOrders(symbol string) (result []Order, err error) { for _, v := range b.openOrders { if v.Symbol == symbol { result = append(result, *v) @@ -368,7 +368,7 @@ func (b *DiribitSimBroker) GetOpenOrders(symbol string) (result []Order, err err return } -func (b *DiribitSimBroker) GetOrder(symbol string, id string) (result Order, err error) { +func (b *DiribitSim) GetOrder(symbol string, id string) (result Order, err error) { order, ok := b.orders[id] if !ok { err = errors.New("not found") @@ -378,7 +378,7 @@ func (b *DiribitSimBroker) GetOrder(symbol string, id string) (result Order, err return } -func (b *DiribitSimBroker) CancelOrder(symbol string, id string) (result Order, err error) { +func (b *DiribitSim) CancelOrder(symbol string, id string) (result Order, err error) { if order, ok := b.orders[id]; ok { if !order.IsOpen() { err = errors.New("status error") @@ -398,7 +398,7 @@ func (b *DiribitSimBroker) CancelOrder(symbol string, id string) (result Order, return } -func (b *DiribitSimBroker) CancelAllOrders(symbol string) (err error) { +func (b *DiribitSim) CancelAllOrders(symbol string) (err error) { var idsToBeRemoved []string for _, order := range b.openOrders { @@ -421,11 +421,11 @@ 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) { +func (b *DiribitSim) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { return } -func (b *DiribitSimBroker) GetPosition(symbol string) (result Position, err error) { +func (b *DiribitSim) GetPosition(symbol string) (result Position, err error) { position, ok := b.positions[symbol] if !ok { err = errors.New("not found") @@ -435,15 +435,15 @@ func (b *DiribitSimBroker) GetPosition(symbol string) (result Position, err erro return } -func (b *DiribitSimBroker) RunEventLoopOnce() (err error) { +func (b *DiribitSim) RunEventLoopOnce() (err error) { for _, order := range b.openOrders { b.matchOrder(order, false) } return } -func NewBroker(data *data.Data, cash float64, makerFeeRate float64, takerFeeRate float64) *DiribitSimBroker { - return &DiribitSimBroker{ +func New(data *data.Data, cash float64, makerFeeRate float64, takerFeeRate float64) *DiribitSim { + return &DiribitSim{ data: data, balance: cash, makerFeeRate: makerFeeRate, // -0.00025 // Maker 费率 diff --git a/brokers/deribit-sim-broker/broker_test.go b/brokers/deribit-sim/broker_test.go similarity index 98% rename from brokers/deribit-sim-broker/broker_test.go rename to brokers/deribit-sim/broker_test.go index a0ce13a..d9d44d6 100644 --- a/brokers/deribit-sim-broker/broker_test.go +++ b/brokers/deribit-sim/broker_test.go @@ -1,4 +1,4 @@ -package deribit_sim_broker +package deribit_sim import ( "github.com/coinrust/crex/math" @@ -125,7 +125,7 @@ func TestCalcLiquidationPrice(t *testing.T) { } } -func TestSimBroker_MarginInfo(t *testing.T) { +func TestDiribitSim_MarginInfo(t *testing.T) { info := CalcMarginInfo(10, 6500, 10) t.Logf("%#v", info) } diff --git a/brokers/deribit-sim-broker/helper.go b/brokers/deribit-sim/helper.go similarity index 99% rename from brokers/deribit-sim-broker/helper.go rename to brokers/deribit-sim/helper.go index 98a0160..846f1e1 100644 --- a/brokers/deribit-sim-broker/helper.go +++ b/brokers/deribit-sim/helper.go @@ -1,4 +1,4 @@ -package deribit_sim_broker +package deribit_sim import ( . "github.com/coinrust/crex" diff --git a/brokers/deribit-sim-broker/helper_test.go b/brokers/deribit-sim/helper_test.go similarity index 90% rename from brokers/deribit-sim-broker/helper_test.go rename to brokers/deribit-sim/helper_test.go index 36b4b9b..b36bbd7 100644 --- a/brokers/deribit-sim-broker/helper_test.go +++ b/brokers/deribit-sim/helper_test.go @@ -1,4 +1,4 @@ -package deribit_sim_broker +package deribit_sim import ( . "github.com/coinrust/crex" diff --git a/brokers/deribit-broker/broker.go b/brokers/deribit/broker.go similarity index 79% rename from brokers/deribit-broker/broker.go rename to brokers/deribit/broker.go index 93a3656..24c1782 100644 --- a/brokers/deribit-broker/broker.go +++ b/brokers/deribit/broker.go @@ -1,4 +1,4 @@ -package deribit_broker +package deribit import ( "errors" @@ -9,18 +9,18 @@ import ( "time" ) -// DiribitBroker the deribit broker -type DiribitBroker struct { +// Diribit the deribit broker +type Diribit struct { client *deribit.Client orderBookManager *OrderBookManager emitter *emission.Emitter } -func (b *DiribitBroker) GetName() (name string) { +func (b *Diribit) GetName() (name string) { return "Deribit" } -func (b *DiribitBroker) Subscribe(event string, param string, listener interface{}) { +func (b *Diribit) Subscribe(event string, param string, listener interface{}) { //b.client.Subscribe([]string{ // "announcements", // "book.BTC-PERPETUAL.100.1.100ms", @@ -47,7 +47,7 @@ func (b *DiribitBroker) Subscribe(event string, param string, listener interface } } -func (b *DiribitBroker) handleOrderBook(m *models.OrderBookNotification) { +func (b *Diribit) handleOrderBook(m *models.OrderBookNotification) { b.orderBookManager.Update(m) ob, ok := b.orderBookManager.GetOrderBook(m.InstrumentName) if !ok { @@ -56,7 +56,7 @@ func (b *DiribitBroker) handleOrderBook(m *models.OrderBookNotification) { b.emitter.Emit("orderbook", &ob) } -func (b *DiribitBroker) GetAccountSummary(currency string) (result AccountSummary, err error) { +func (b *Diribit) GetAccountSummary(currency string) (result AccountSummary, err error) { params := &models.GetAccountSummaryParams{ Currency: currency, Extended: false, @@ -72,7 +72,7 @@ func (b *DiribitBroker) GetAccountSummary(currency string) (result AccountSummar return } -func (b *DiribitBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { +func (b *Diribit) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { params := &models.GetOrderBookParams{ InstrumentName: symbol, Depth: depth, @@ -98,7 +98,7 @@ func (b *DiribitBroker) GetOrderBook(symbol string, depth int) (result OrderBook return } -func (b *DiribitBroker) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { +func (b *Diribit) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { if end == 0 { end = time.Now().Unix() } @@ -128,19 +128,19 @@ func (b *DiribitBroker) GetRecords(symbol string, period string, from int64, end return } -func (b *DiribitBroker) SetContractType(currencyPair string, contractType string) (err error) { +func (b *Diribit) SetContractType(currencyPair string, contractType string) (err error) { return } -func (b *DiribitBroker) GetContractID() (symbol string, err error) { +func (b *Diribit) GetContractID() (symbol string, err error) { return } -func (b *DiribitBroker) SetLeverRate(value float64) (err error) { +func (b *Diribit) SetLeverRate(value float64) (err error) { return } -func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *Diribit) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64, postOnly bool, reduceOnly bool, params map[string]interface{}) (result Order, err error) { var _orderType string var trigger string @@ -201,7 +201,7 @@ func (b *DiribitBroker) PlaceOrder(symbol string, direction Direction, orderType return } -func (b *DiribitBroker) GetOpenOrders(symbol string) (result []Order, err error) { +func (b *Diribit) GetOpenOrders(symbol string) (result []Order, err error) { var ret []models.Order ret, err = b.client.GetOpenOrdersByInstrument(&models.GetOpenOrdersByInstrumentParams{ InstrumentName: symbol, @@ -216,7 +216,7 @@ func (b *DiribitBroker) GetOpenOrders(symbol string) (result []Order, err error) return } -func (b *DiribitBroker) GetOrder(symbol string, id string) (result Order, err error) { +func (b *Diribit) GetOrder(symbol string, id string) (result Order, err error) { var ret models.Order ret, err = b.client.GetOrderState(&models.GetOrderStateParams{ OrderID: id, @@ -228,7 +228,7 @@ func (b *DiribitBroker) GetOrder(symbol string, id string) (result Order, err er return } -func (b *DiribitBroker) CancelOrder(symbol string, id string) (result Order, err error) { +func (b *Diribit) CancelOrder(symbol string, id string) (result Order, err error) { var order models.Order order, err = b.client.Cancel(&models.CancelParams{OrderID: id}) if err != nil { @@ -238,14 +238,14 @@ func (b *DiribitBroker) CancelOrder(symbol string, id string) (result Order, err return } -func (b *DiribitBroker) CancelAllOrders(symbol string) (err error) { +func (b *Diribit) CancelAllOrders(symbol string) (err error) { _, err = b.client.CancelAllByInstrument(&models.CancelAllByInstrumentParams{ InstrumentName: symbol, }) return } -func (b *DiribitBroker) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { +func (b *Diribit) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { params := &models.EditParams{ OrderID: id, Amount: 0, @@ -273,7 +273,7 @@ func (b *DiribitBroker) AmendOrder(symbol string, id string, price float64, size return } -func (b *DiribitBroker) GetPosition(symbol string) (result Position, err error) { +func (b *Diribit) GetPosition(symbol string) (result Position, err error) { var ret models.Position ret, err = b.client.GetPosition(&models.GetPositionParams{InstrumentName: symbol}) if err != nil { @@ -285,7 +285,7 @@ func (b *DiribitBroker) GetPosition(symbol string) (result Position, err error) return } -func (b *DiribitBroker) convertOrder(order *models.Order) (result Order) { +func (b *Diribit) convertOrder(order *models.Order) (result Order) { result.ID = order.OrderID result.Symbol = order.InstrumentName result.Price = order.Price.ToFloat64() @@ -301,7 +301,7 @@ func (b *DiribitBroker) convertOrder(order *models.Order) (result Order) { return } -func (b *DiribitBroker) convertDirection(direction string) Direction { +func (b *Diribit) convertDirection(direction string) Direction { switch direction { case models.DirectionBuy: return Buy @@ -312,7 +312,7 @@ func (b *DiribitBroker) convertDirection(direction string) Direction { } } -func (b *DiribitBroker) convertOrderType(orderType string) OrderType { +func (b *Diribit) convertOrderType(orderType string) OrderType { switch orderType { case models.OrderTypeLimit: return OrderTypeLimit @@ -327,7 +327,7 @@ func (b *DiribitBroker) convertOrderType(orderType string) OrderType { } } -func (b *DiribitBroker) orderStatus(order *models.Order) OrderStatus { +func (b *Diribit) orderStatus(order *models.Order) OrderStatus { orderState := order.OrderState switch orderState { case models.OrderStateOpen: @@ -348,11 +348,11 @@ func (b *DiribitBroker) orderStatus(order *models.Order) OrderStatus { } } -func (b *DiribitBroker) RunEventLoopOnce() (err error) { +func (b *Diribit) RunEventLoopOnce() (err error) { return } -func NewBroker(addr string, accessKey string, secretKey string) *DiribitBroker { +func New(addr string, accessKey string, secretKey string) *Diribit { cfg := &deribit.Configuration{ Addr: addr, ApiKey: accessKey, @@ -360,7 +360,7 @@ func NewBroker(addr string, accessKey string, secretKey string) *DiribitBroker { AutoReconnect: true, } client := deribit.New(cfg) - return &DiribitBroker{ + return &Diribit{ client: client, orderBookManager: NewOrderBookManager(), emitter: emission.NewEmitter(), diff --git a/brokers/deribit-broker/broker_test.go b/brokers/deribit/broker_test.go similarity index 57% rename from brokers/deribit-broker/broker_test.go rename to brokers/deribit/broker_test.go index d23e335..994605e 100644 --- a/brokers/deribit-broker/broker_test.go +++ b/brokers/deribit/broker_test.go @@ -1,27 +1,26 @@ -package deribit_broker +package deribit import ( . "github.com/coinrust/crex" "github.com/frankrap/deribit-api" - "log" "testing" "time" ) -func newBroker() Broker { +func newForTest() Broker { apiKey := "AsJTU16U" secretKey := "mM5_K8LVxztN6TjjYpv_cJVGQBvk4jglrEpqkw1b87U" - b := NewBroker(deribit.TestBaseURL, apiKey, secretKey) + b := New(deribit.TestBaseURL, apiKey, secretKey) return b } -func TestDiribitBroker_GetOrderBook(t *testing.T) { - b := newBroker() +func TestDiribit_GetOrderBook(t *testing.T) { + b := newForTest() b.GetOrderBook("BTC-PERPETUAL", 10) } -func TestDiribitBroker_GetRecords(t *testing.T) { - b := newBroker() +func TestDiribit_GetRecords(t *testing.T) { + b := newForTest() start := time.Now().Add(-time.Hour) end := time.Now().UnixNano() / 1e6 records, err := b.GetRecords("BTC-PERPETUAL", @@ -35,21 +34,8 @@ func TestDiribitBroker_GetRecords(t *testing.T) { } } -func TestDiribitBroker_Subscribe(t *testing.T) { - b := newBroker() - //event := "book.ETH-PERPETUAL.100.1.100ms" - param := "book.BTC-PERPETUAL.100ms" - b.Subscribe("orderbook", param, func(e *OrderBook) { - log.Printf("OrderBook: %#v", *e) - }) - - for { - time.Sleep(1 * time.Second) - } -} - -func TestDiribitBroker_PlaceStopOrder(t *testing.T) { - b := newBroker() +func TestDiribit_PlaceStopOrder(t *testing.T) { + b := newForTest() order, err := b.PlaceOrder("BTC-PERPETUAL", Buy, OrderTypeStopMarket, @@ -67,8 +53,8 @@ func TestDiribitBroker_PlaceStopOrder(t *testing.T) { t.Logf("Status: %v", order.Status.String()) } -func TestDiribitBroker_GetOpenOrders(t *testing.T) { - b := newBroker() +func TestDiribit_GetOpenOrders(t *testing.T) { + b := newForTest() orders, err := b.GetOpenOrders("BTC-PERPETUAL") if err != nil { t.Error(err) diff --git a/brokers/deribit-broker/orderbook_local.go b/brokers/deribit/orderbook_local.go similarity index 99% rename from brokers/deribit-broker/orderbook_local.go rename to brokers/deribit/orderbook_local.go index 96b9a58..0106d87 100644 --- a/brokers/deribit-broker/orderbook_local.go +++ b/brokers/deribit/orderbook_local.go @@ -1,4 +1,4 @@ -package deribit_broker +package deribit import ( . "github.com/coinrust/crex" diff --git a/brokers/deribit-broker/orderbook_manager.go b/brokers/deribit/orderbook_manager.go similarity index 97% rename from brokers/deribit-broker/orderbook_manager.go rename to brokers/deribit/orderbook_manager.go index 0cfa597..35c9ce6 100644 --- a/brokers/deribit-broker/orderbook_manager.go +++ b/brokers/deribit/orderbook_manager.go @@ -1,4 +1,4 @@ -package deribit_broker +package deribit import ( . "github.com/coinrust/crex" diff --git a/brokers/hbdm-swap-broker/broker.go b/brokers/hbdm-swap/broker.go similarity index 82% rename from brokers/hbdm-swap-broker/broker.go rename to brokers/hbdm-swap/broker.go index cb4af26..0fe87eb 100644 --- a/brokers/hbdm-swap-broker/broker.go +++ b/brokers/hbdm-swap/broker.go @@ -1,4 +1,4 @@ -package hbdm_swap_broker +package hbdm_swap import ( "fmt" @@ -11,17 +11,17 @@ import ( const StatusOK = "ok" -// HBDMSwapBroker the Huobi DM Swap broker -type HBDMSwapBroker struct { +// HBDMSwap the Huobi DM Swap broker +type HBDMSwap struct { client *hbdmswap.Client leverRate int // 杠杆倍数 } -func (b *HBDMSwapBroker) GetName() (name string) { +func (b *HBDMSwap) GetName() (name string) { return "HBDMSwap" } -func (b *HBDMSwapBroker) GetAccountSummary(currency string) (result AccountSummary, err error) { +func (b *HBDMSwap) GetAccountSummary(currency string) (result AccountSummary, err error) { var account hbdmswap.AccountInfoResult account, err = b.client.GetAccountInfo(currency) if err != nil { @@ -47,7 +47,7 @@ func (b *HBDMSwapBroker) GetAccountSummary(currency string) (result AccountSumma return } -func (b *HBDMSwapBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { +func (b *HBDMSwap) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { var ret hbdmswap.MarketDepthResult var _type = "step0" // 使用step0时,不合并深度获取150档数据 @@ -80,7 +80,7 @@ func (b *HBDMSwapBroker) GetOrderBook(symbol string, depth int) (result OrderBoo return } -func (b *HBDMSwapBroker) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { +func (b *HBDMSwap) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { var _period string if strings.HasSuffix(period, "m") { _period = period[:len(period)-1] + "min" @@ -124,16 +124,16 @@ func (b *HBDMSwapBroker) GetRecords(symbol string, period string, from int64, en } // 设置合约类型 -func (b *HBDMSwapBroker) SetContractType(pair string, contractType string) (err error) { +func (b *HBDMSwap) SetContractType(pair string, contractType string) (err error) { return } -func (b *HBDMSwapBroker) GetContractID() (symbol string, err error) { +func (b *HBDMSwap) GetContractID() (symbol string, err error) { return "", fmt.Errorf("not found") } // 设置杠杆大小 -func (b *HBDMSwapBroker) SetLeverRate(value float64) (err error) { +func (b *HBDMSwap) SetLeverRate(value float64) (err error) { b.leverRate = int(value) return } @@ -158,7 +158,7 @@ func (b *HBDMSwapBroker) SetLeverRate(value float64) (err error) { // "optimal_5_fok":最优5档-FOK下单 // "optimal_10_fok":最优10档-FOK下单 // "optimal_20_fok":最优20档-FOK下单 -func (b *HBDMSwapBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *HBDMSwap) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64, postOnly bool, reduceOnly bool, params map[string]interface{}) (result Order, err error) { var orderResult hbdmswap.OrderResult var _direction string @@ -231,7 +231,7 @@ func (b *HBDMSwapBroker) PlaceOrder(symbol string, direction Direction, orderTyp return } -func (b *HBDMSwapBroker) GetOpenOrders(symbol string) (result []Order, err error) { +func (b *HBDMSwap) GetOpenOrders(symbol string) (result []Order, err error) { var ret hbdmswap.OpenOrdersResult ret, err = b.client.GetOpenOrders( symbol, @@ -253,7 +253,7 @@ func (b *HBDMSwapBroker) GetOpenOrders(symbol string) (result []Order, err error return } -func (b *HBDMSwapBroker) GetOrder(symbol string, id string) (result Order, err error) { +func (b *HBDMSwap) GetOrder(symbol string, id string) (result Order, err error) { var ret hbdmswap.OrderInfoResult var _id, _ = strconv.ParseInt(id, 10, 64) ret, err = b.client.OrderInfo(symbol, _id, 0) @@ -274,7 +274,7 @@ func (b *HBDMSwapBroker) GetOrder(symbol string, id string) (result Order, err e return } -func (b *HBDMSwapBroker) CancelOrder(symbol string, id string) (result Order, err error) { +func (b *HBDMSwap) CancelOrder(symbol string, id string) (result Order, err error) { var ret hbdmswap.CancelResult var _id, _ = strconv.ParseInt(id, 10, 64) ret, err = b.client.Cancel(symbol, _id, 0) @@ -292,15 +292,15 @@ func (b *HBDMSwapBroker) CancelOrder(symbol string, id string) (result Order, er return } -func (b *HBDMSwapBroker) CancelAllOrders(symbol string) (err error) { +func (b *HBDMSwap) CancelAllOrders(symbol string) (err error) { return } -func (b *HBDMSwapBroker) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { +func (b *HBDMSwap) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { return } -func (b *HBDMSwapBroker) GetPosition(symbol string) (result Position, err error) { +func (b *HBDMSwap) GetPosition(symbol string) (result Position, err error) { result.Symbol = symbol var ret hbdmswap.PositionInfoResult @@ -331,7 +331,7 @@ func (b *HBDMSwapBroker) GetPosition(symbol string) (result Position, err error) return } -func (b *HBDMSwapBroker) convertOrder(symbol string, order *hbdmswap.Order) (result Order) { +func (b *HBDMSwap) convertOrder(symbol string, order *hbdmswap.Order) (result Order) { result.ID = order.OrderIDStr result.Symbol = symbol result.Price = order.Price @@ -351,7 +351,7 @@ func (b *HBDMSwapBroker) convertOrder(symbol string, order *hbdmswap.Order) (res return } -func (b *HBDMSwapBroker) orderDirection(order *hbdmswap.Order) Direction { +func (b *HBDMSwap) orderDirection(order *hbdmswap.Order) Direction { if order.Direction == "buy" { return Buy } else if order.Direction == "sell" { @@ -360,7 +360,7 @@ func (b *HBDMSwapBroker) orderDirection(order *hbdmswap.Order) Direction { return Buy } -func (b *HBDMSwapBroker) orderType(order *hbdmswap.Order) OrderType { +func (b *HBDMSwap) orderType(order *hbdmswap.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订单 */ @@ -377,7 +377,7 @@ func (b *HBDMSwapBroker) orderType(order *hbdmswap.Order) OrderType { return OrderTypeLimit } -func (b *HBDMSwapBroker) orderStatus(order *hbdmswap.Order) OrderStatus { +func (b *HBDMSwap) orderStatus(order *hbdmswap.Order) OrderStatus { /* 订单状态 (1准备提交 2准备提交 3已提交 4部分成交 5部分成交已撤单 6全部成交 7已撤单 11撤单中) @@ -400,11 +400,11 @@ func (b *HBDMSwapBroker) orderStatus(order *hbdmswap.Order) OrderStatus { } } -func (b *HBDMSwapBroker) RunEventLoopOnce() (err error) { +func (b *HBDMSwap) RunEventLoopOnce() (err error) { return } -func NewBroker(addr string, accessKey string, secretKey string) *HBDMSwapBroker { +func New(addr string, accessKey string, secretKey string) *HBDMSwap { //baseURL := "https://api.hbdm.com" apiParams := &hbdmswap.ApiParameter{ Debug: false, @@ -415,7 +415,7 @@ func NewBroker(addr string, accessKey string, secretKey string) *HBDMSwapBroker PrivateKeyPrime256: "", } client := hbdmswap.NewClient(apiParams) - return &HBDMSwapBroker{ + return &HBDMSwap{ client: client, } } diff --git a/brokers/hbdm-swap-broker/broker_test.go b/brokers/hbdm-swap/broker_test.go similarity index 77% rename from brokers/hbdm-swap-broker/broker_test.go rename to brokers/hbdm-swap/broker_test.go index f082685..547d324 100644 --- a/brokers/hbdm-swap-broker/broker_test.go +++ b/brokers/hbdm-swap/broker_test.go @@ -1,4 +1,4 @@ -package hbdm_swap_broker +package hbdm_swap import ( . "github.com/coinrust/crex" @@ -8,7 +8,7 @@ import ( "time" ) -func newTestBroker() Broker { +func newForTest() Broker { viper.SetConfigName("test_config") viper.AddConfigPath(".") err := viper.ReadInConfig() @@ -19,11 +19,11 @@ func newTestBroker() Broker { accessKey := viper.GetString("access_key") secretKey := viper.GetString("secret_key") baseURL := "https://api.btcgateway.pro" - return NewBroker(baseURL, accessKey, secretKey) + return New(baseURL, accessKey, secretKey) } -func TestHBDMSwapBroker_GetRecords(t *testing.T) { - b := newTestBroker() +func TestHBDMSwap_GetRecords(t *testing.T) { + b := newForTest() symbol := "BTC-USD" start := time.Now().Add(-time.Hour) end := time.Now() diff --git a/brokers/hbdm-broker/test_config.example.yaml b/brokers/hbdm-swap/test_config.example.yaml similarity index 100% rename from brokers/hbdm-broker/test_config.example.yaml rename to brokers/hbdm-swap/test_config.example.yaml diff --git a/brokers/hbdm-swap-broker/ws.go b/brokers/hbdm-swap/ws.go similarity index 99% rename from brokers/hbdm-swap-broker/ws.go rename to brokers/hbdm-swap/ws.go index ded48d4..94e72c0 100644 --- a/brokers/hbdm-swap-broker/ws.go +++ b/brokers/hbdm-swap/ws.go @@ -1,4 +1,4 @@ -package hbdm_swap_broker +package hbdm_swap import ( "fmt" diff --git a/brokers/hbdm-swap-broker/ws_test.go b/brokers/hbdm-swap/ws_test.go similarity index 97% rename from brokers/hbdm-swap-broker/ws_test.go rename to brokers/hbdm-swap/ws_test.go index 1cc16d7..cd5ba2f 100644 --- a/brokers/hbdm-swap-broker/ws_test.go +++ b/brokers/hbdm-swap/ws_test.go @@ -1,4 +1,4 @@ -package hbdm_swap_broker +package hbdm_swap import ( . "github.com/coinrust/crex" diff --git a/brokers/hbdm-broker/broker.go b/brokers/hbdm/broker.go similarity index 85% rename from brokers/hbdm-broker/broker.go rename to brokers/hbdm/broker.go index 6c45297..c6ec80b 100644 --- a/brokers/hbdm-broker/broker.go +++ b/brokers/hbdm/broker.go @@ -1,4 +1,4 @@ -package hbdm_broker +package hbdm import ( "fmt" @@ -11,8 +11,8 @@ import ( const StatusOK = "ok" -// HBDMBroker the Huobi DM broker -type HBDMBroker struct { +// HBDM the Huobi DM broker +type HBDM struct { client *hbdm.Client pair string // 交易对 BTC/ETH/... _contractType string // 合约类型 @@ -21,11 +21,11 @@ type HBDMBroker struct { leverRate int // 杠杆倍数 } -func (b *HBDMBroker) GetName() (name string) { +func (b *HBDM) GetName() (name string) { return "HBDM" } -func (b *HBDMBroker) GetAccountSummary(currency string) (result AccountSummary, err error) { +func (b *HBDM) GetAccountSummary(currency string) (result AccountSummary, err error) { var account hbdm.AccountInfoResult account, err = b.client.GetAccountInfo(currency) if err != nil { @@ -51,7 +51,7 @@ func (b *HBDMBroker) GetAccountSummary(currency string) (result AccountSummary, return } -func (b *HBDMBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { +func (b *HBDM) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { var ret hbdm.MarketDepthResult var _type = "step0" // 使用step0时,不合并深度获取150档数据 @@ -84,7 +84,7 @@ func (b *HBDMBroker) GetOrderBook(symbol string, depth int) (result OrderBook, e return } -func (b *HBDMBroker) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { +func (b *HBDM) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { var _period string if strings.HasSuffix(period, "m") { _period = period[:len(period)-1] + "min" @@ -129,7 +129,7 @@ func (b *HBDMBroker) GetRecords(symbol string, period string, from int64, end in // 设置合约类型 // pair: BTC/ETH/... -func (b *HBDMBroker) SetContractType(pair string, contractType string) (err error) { +func (b *HBDM) SetContractType(pair string, contractType string) (err error) { // // 如"BTC_CW"表示BTC当周合约,"BTC_NW"表示BTC次周合约,"BTC_CQ"表示BTC季度合约 b.pair = pair b._contractType = contractType @@ -152,7 +152,7 @@ func (b *HBDMBroker) SetContractType(pair string, contractType string) (err erro return } -func (b *HBDMBroker) GetContractID() (symbol string, err error) { +func (b *HBDM) GetContractID() (symbol string, err error) { var ret hbdm.ContractInfoResult ret, err = b.client.GetContractInfo(b.pair, b.contractType, "") if err != nil { @@ -170,7 +170,7 @@ func (b *HBDMBroker) GetContractID() (symbol string, err error) { } // 设置杠杆大小 -func (b *HBDMBroker) SetLeverRate(value float64) (err error) { +func (b *HBDM) SetLeverRate(value float64) (err error) { b.leverRate = int(value) return } @@ -199,7 +199,7 @@ func (b *HBDMBroker) SetLeverRate(value float64) (err error) { // optimal_5:最优5档、optimal_10:最优10档、optimal_20:最优20档下单price价格参数不用传 // "limit":限价,"post_only":只做maker单 需要传价格 // "fok":全部成交或立即取消,"ioc":立即成交并取消剩余。 -func (b *HBDMBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *HBDM) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64, postOnly bool, reduceOnly bool, params map[string]interface{}) (result Order, err error) { var orderResult hbdm.OrderResult var _direction string @@ -275,7 +275,7 @@ func (b *HBDMBroker) PlaceOrder(symbol string, direction Direction, orderType Or return } -func (b *HBDMBroker) GetOpenOrders(symbol string) (result []Order, err error) { +func (b *HBDM) GetOpenOrders(symbol string) (result []Order, err error) { var ret hbdm.OpenOrdersResult ret, err = b.client.GetOpenOrders( b.pair, @@ -297,7 +297,7 @@ func (b *HBDMBroker) GetOpenOrders(symbol string) (result []Order, err error) { return } -func (b *HBDMBroker) GetOrder(symbol string, id string) (result Order, err error) { +func (b *HBDM) 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(b.pair, _id, 0) @@ -318,7 +318,7 @@ func (b *HBDMBroker) GetOrder(symbol string, id string) (result Order, err error return } -func (b *HBDMBroker) CancelOrder(symbol string, id string) (result Order, err error) { +func (b *HBDM) 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(b.pair, _id, 0) @@ -336,15 +336,15 @@ func (b *HBDMBroker) CancelOrder(symbol string, id string) (result Order, err er return } -func (b *HBDMBroker) CancelAllOrders(symbol string) (err error) { +func (b *HBDM) CancelAllOrders(symbol string) (err error) { return } -func (b *HBDMBroker) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { +func (b *HBDM) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { return } -func (b *HBDMBroker) GetPosition(symbol string) (result Position, err error) { +func (b *HBDM) GetPosition(symbol string) (result Position, err error) { result.Symbol = symbol var ret hbdm.PositionInfoResult @@ -375,7 +375,7 @@ func (b *HBDMBroker) GetPosition(symbol string) (result Position, err error) { return } -func (b *HBDMBroker) convertOrder(symbol string, order *hbdm.Order) (result Order) { +func (b *HBDM) convertOrder(symbol string, order *hbdm.Order) (result Order) { result.ID = order.OrderIDStr result.Symbol = symbol result.Price = order.Price @@ -395,7 +395,7 @@ func (b *HBDMBroker) convertOrder(symbol string, order *hbdm.Order) (result Orde return } -func (b *HBDMBroker) orderDirection(order *hbdm.Order) Direction { +func (b *HBDM) orderDirection(order *hbdm.Order) Direction { if order.Direction == "buy" { return Buy } else if order.Direction == "sell" { @@ -404,7 +404,7 @@ func (b *HBDMBroker) orderDirection(order *hbdm.Order) Direction { return Buy } -func (b *HBDMBroker) orderType(order *hbdm.Order) OrderType { +func (b *HBDM) 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订单 */ @@ -421,7 +421,7 @@ func (b *HBDMBroker) orderType(order *hbdm.Order) OrderType { return OrderTypeLimit } -func (b *HBDMBroker) orderStatus(order *hbdm.Order) OrderStatus { +func (b *HBDM) orderStatus(order *hbdm.Order) OrderStatus { /* 订单状态 (1准备提交 2准备提交 3已提交 4部分成交 5部分成交已撤单 6全部成交 7已撤单 11撤单中) @@ -444,11 +444,11 @@ func (b *HBDMBroker) orderStatus(order *hbdm.Order) OrderStatus { } } -func (b *HBDMBroker) RunEventLoopOnce() (err error) { +func (b *HBDM) RunEventLoopOnce() (err error) { return } -func NewBroker(addr string, accessKey string, secretKey string) *HBDMBroker { +func New(addr string, accessKey string, secretKey string) *HBDM { //baseURL := "https://api.hbdm.com" apiParams := &hbdm.ApiParameter{ Debug: false, @@ -459,7 +459,7 @@ func NewBroker(addr string, accessKey string, secretKey string) *HBDMBroker { PrivateKeyPrime256: "", } client := hbdm.NewClient(apiParams) - return &HBDMBroker{ + return &HBDM{ client: client, } } diff --git a/brokers/hbdm-broker/broker_test.go b/brokers/hbdm/broker_test.go similarity index 77% rename from brokers/hbdm-broker/broker_test.go rename to brokers/hbdm/broker_test.go index 4c21dc9..cb4ec77 100644 --- a/brokers/hbdm-broker/broker_test.go +++ b/brokers/hbdm/broker_test.go @@ -1,4 +1,4 @@ -package hbdm_broker +package hbdm import ( . "github.com/coinrust/crex" @@ -8,7 +8,7 @@ import ( "time" ) -func newTestBroker() Broker { +func newForTest() Broker { viper.SetConfigName("test_config") viper.AddConfigPath(".") err := viper.ReadInConfig() @@ -19,11 +19,11 @@ func newTestBroker() Broker { accessKey := viper.GetString("access_key") secretKey := viper.GetString("secret_key") baseURL := "https://api.btcgateway.pro" - return NewBroker(baseURL, accessKey, secretKey) + return New(baseURL, accessKey, secretKey) } -func TestHBDMBroker_GetAccountSummary(t *testing.T) { - b := newTestBroker() +func TestHBDM_GetAccountSummary(t *testing.T) { + b := newForTest() accountSummary, err := b.GetAccountSummary("BTC") if err != nil { t.Error(err) @@ -32,8 +32,8 @@ func TestHBDMBroker_GetAccountSummary(t *testing.T) { t.Logf("%#v", accountSummary) } -func TestHBDMBroker_GetOrderBook(t *testing.T) { - b := newTestBroker() +func TestHBDM_GetOrderBook(t *testing.T) { + b := newForTest() b.SetContractType("BTC", "W1") ob, err := b.GetOrderBook("BTC200327", 1) if err != nil { @@ -43,8 +43,8 @@ func TestHBDMBroker_GetOrderBook(t *testing.T) { t.Logf("%#v", ob) } -func TestHBDMBroker_GetRecords(t *testing.T) { - b := newTestBroker() +func TestHBDM_GetRecords(t *testing.T) { + b := newForTest() b.SetContractType("BTC", ContractTypeW1) symbol, err := b.GetContractID() if err != nil { @@ -64,8 +64,8 @@ func TestHBDMBroker_GetRecords(t *testing.T) { } } -func TestHBDMBroker_GetContractID(t *testing.T) { - b := newTestBroker() +func TestHBDM_GetContractID(t *testing.T) { + b := newForTest() b.SetContractType("BTC", ContractTypeW1) symbol, err := b.GetContractID() if err != nil { @@ -75,8 +75,8 @@ func TestHBDMBroker_GetContractID(t *testing.T) { t.Logf("%v", symbol) } -func TestHBDMBroker_GetOpenOrders(t *testing.T) { - b := newTestBroker() +func TestHBDM_GetOpenOrders(t *testing.T) { + b := newForTest() b.SetContractType("BTC", ContractTypeW1) symbol, err := b.GetContractID() if err != nil { @@ -96,8 +96,8 @@ func TestHBDMBroker_GetOpenOrders(t *testing.T) { } } -func TestHBDMBroker_GetOrder(t *testing.T) { - b := newTestBroker() +func TestHBDM_GetOrder(t *testing.T) { + b := newForTest() b.SetContractType("BTC", ContractTypeW1) symbol, err := b.GetContractID() if err != nil { @@ -113,8 +113,8 @@ func TestHBDMBroker_GetOrder(t *testing.T) { t.Logf("%#v", order) } -func TestHBDMBroker_PlaceOrder(t *testing.T) { - b := newTestBroker() +func TestHBDM_PlaceOrder(t *testing.T) { + b := newForTest() b.SetLeverRate(10) b.SetContractType("BTC", ContractTypeW1) symbol, err := b.GetContractID() @@ -139,8 +139,8 @@ func TestHBDMBroker_PlaceOrder(t *testing.T) { t.Logf("%#v", order) } -func TestHBDMBroker_PlaceOrder2(t *testing.T) { - b := newTestBroker() +func TestHBDM_PlaceOrder2(t *testing.T) { + b := newForTest() b.SetLeverRate(10) b.SetContractType("BTC", ContractTypeW1) symbol, err := b.GetContractID() diff --git a/brokers/hbdm-swap-broker/test_config.example.yaml b/brokers/hbdm/test_config.example.yaml similarity index 100% rename from brokers/hbdm-swap-broker/test_config.example.yaml rename to brokers/hbdm/test_config.example.yaml diff --git a/brokers/hbdm-broker/ws.go b/brokers/hbdm/ws.go similarity index 99% rename from brokers/hbdm-broker/ws.go rename to brokers/hbdm/ws.go index e1422e6..b7bf1f3 100644 --- a/brokers/hbdm-broker/ws.go +++ b/brokers/hbdm/ws.go @@ -1,4 +1,4 @@ -package hbdm_broker +package hbdm import ( "fmt" diff --git a/brokers/hbdm-broker/ws_test.go b/brokers/hbdm/ws_test.go similarity index 98% rename from brokers/hbdm-broker/ws_test.go rename to brokers/hbdm/ws_test.go index f45bc1d..d35bfb5 100644 --- a/brokers/hbdm-broker/ws_test.go +++ b/brokers/hbdm/ws_test.go @@ -1,4 +1,4 @@ -package hbdm_broker +package hbdm import ( . "github.com/coinrust/crex" diff --git a/brokers/okex-futures-broker/broker.go b/brokers/okex-futures/broker.go similarity index 83% rename from brokers/okex-futures-broker/broker.go rename to brokers/okex-futures/broker.go index e25efde..265a262 100644 --- a/brokers/okex-futures-broker/broker.go +++ b/brokers/okex-futures/broker.go @@ -1,4 +1,4 @@ -package okex_futures_broker +package okex_futures import ( "fmt" @@ -11,8 +11,8 @@ import ( "github.com/frankrap/okex-api" ) -// OKEXFuturesBroker the OKEX futures broker -type OKEXFuturesBroker struct { +// OKEXFutures the OKEX futures broker +type OKEXFutures struct { client *okex.Client pair string // contract pair 合约交易对 contractType string // contract type 合约类型 @@ -20,11 +20,11 @@ type OKEXFuturesBroker struct { leverRate int // lever rate 杠杆倍数 } -func (b *OKEXFuturesBroker) GetName() (name string) { +func (b *OKEXFutures) GetName() (name string) { return "OKEXFutures" } -func (b *OKEXFuturesBroker) GetAccountSummary(currency string) (result AccountSummary, err error) { +func (b *OKEXFutures) GetAccountSummary(currency string) (result AccountSummary, err error) { var account okex.FuturesCurrencyAccount account, err = b.client.GetFuturesAccountsByCurrency(currency) if err != nil { @@ -38,7 +38,7 @@ func (b *OKEXFuturesBroker) GetAccountSummary(currency string) (result AccountSu return } -func (b *OKEXFuturesBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { +func (b *OKEXFutures) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { params := map[string]string{} params["size"] = fmt.Sprintf("%v", depth) // "10" //params["depth"] = fmt.Sprintf("%v", 0.01) // BTC: "0.1" @@ -69,7 +69,7 @@ func (b *OKEXFuturesBroker) GetOrderBook(symbol string, depth int) (result Order return } -func (b *OKEXFuturesBroker) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { +func (b *OKEXFutures) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { var granularity int64 var intervalValue string var intervalF int64 @@ -153,7 +153,7 @@ func (b *OKEXFuturesBroker) GetRecords(symbol string, period string, from int64, // 设置合约类型 // pair: BTC-USD // contractType: W1,W2,Q1,Q2,... -func (b *OKEXFuturesBroker) SetContractType(pair string, contractType string) (err error) { +func (b *OKEXFutures) SetContractType(pair string, contractType string) (err error) { b.pair = pair b.contractType = contractType var contractAlias string @@ -172,7 +172,7 @@ func (b *OKEXFuturesBroker) SetContractType(pair string, contractType string) (e return } -func (b *OKEXFuturesBroker) GetContractID() (symbol string, err error) { +func (b *OKEXFutures) GetContractID() (symbol string, err error) { var ret []okex.FuturesInstrumentsResult ret, err = b.client.GetFuturesInstruments() if err != nil { @@ -190,12 +190,12 @@ func (b *OKEXFuturesBroker) GetContractID() (symbol string, err error) { } // 设置杠杆大小 -func (b *OKEXFuturesBroker) SetLeverRate(value float64) (err error) { +func (b *OKEXFutures) SetLeverRate(value float64) (err error) { b.leverRate = int(value) return } -func (b *OKEXFuturesBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *OKEXFutures) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64, postOnly bool, reduceOnly bool, params map[string]interface{}) (result Order, err error) { var pType int if direction == Buy { @@ -250,7 +250,7 @@ func (b *OKEXFuturesBroker) PlaceOrder(symbol string, direction Direction, order return } -func (b *OKEXFuturesBroker) GetOpenOrders(symbol string) (result []Order, err error) { +func (b *OKEXFutures) GetOpenOrders(symbol string) (result []Order, err error) { // 6: 未完成(等待成交+部分成交) // 7: 已完成(撤单成功+完全成交) var ret okex.FuturesGetOrdersResult @@ -264,7 +264,7 @@ func (b *OKEXFuturesBroker) GetOpenOrders(symbol string) (result []Order, err er return } -func (b *OKEXFuturesBroker) GetOrder(symbol string, id string) (result Order, err error) { +func (b *OKEXFutures) GetOrder(symbol string, id string) (result Order, err error) { var ret okex.FuturesGetOrderResult ret, err = b.client.GetFuturesOrder(symbol, id) if err != nil { @@ -276,7 +276,7 @@ func (b *OKEXFuturesBroker) GetOrder(symbol string, id string) (result Order, er return } -func (b *OKEXFuturesBroker) CancelOrder(symbol string, id string) (result Order, err error) { +func (b *OKEXFutures) CancelOrder(symbol string, id string) (result Order, err error) { var ret okex.FuturesCancelInstrumentOrderResult var resp []byte resp, ret, err = b.client.CancelFuturesInstrumentOrder(symbol, id) @@ -295,15 +295,15 @@ func (b *OKEXFuturesBroker) CancelOrder(symbol string, id string) (result Order, return } -func (b *OKEXFuturesBroker) CancelAllOrders(symbol string) (err error) { +func (b *OKEXFutures) CancelAllOrders(symbol string) (err error) { return } -func (b *OKEXFuturesBroker) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { +func (b *OKEXFutures) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { return } -func (b *OKEXFuturesBroker) GetPosition(symbol string) (result Position, err error) { +func (b *OKEXFutures) GetPosition(symbol string) (result Position, err error) { var ret okex.FuturesPosition ret, err = b.client.GetFuturesInstrumentPosition(symbol) if err != nil { @@ -358,7 +358,7 @@ func (b *OKEXFuturesBroker) GetPosition(symbol string) (result Position, err err return } -func (b *OKEXFuturesBroker) convertOrder(symbol string, order *okex.FuturesGetOrderResult) (result Order) { +func (b *OKEXFutures) convertOrder(symbol string, order *okex.FuturesGetOrderResult) (result Order) { result.ID = order.OrderId result.Symbol = symbol result.Price = order.Price @@ -378,7 +378,7 @@ func (b *OKEXFuturesBroker) convertOrder(symbol string, order *okex.FuturesGetOr return } -func (b *OKEXFuturesBroker) orderDirection(order *okex.FuturesGetOrderResult) Direction { +func (b *OKEXFutures) orderDirection(order *okex.FuturesGetOrderResult) Direction { // 订单类型 //1:开多 //2:开空 @@ -392,11 +392,11 @@ func (b *OKEXFuturesBroker) orderDirection(order *okex.FuturesGetOrderResult) Di return Buy } -func (b *OKEXFuturesBroker) orderType(order *okex.FuturesGetOrderResult) OrderType { +func (b *OKEXFutures) orderType(order *okex.FuturesGetOrderResult) OrderType { return OrderTypeLimit } -func (b *OKEXFuturesBroker) orderStatus(order *okex.FuturesGetOrderResult) OrderStatus { +func (b *OKEXFutures) orderStatus(order *okex.FuturesGetOrderResult) OrderStatus { /* 订单状态 -2:失败 @@ -427,12 +427,12 @@ func (b *OKEXFuturesBroker) orderStatus(order *okex.FuturesGetOrderResult) Order } } -func (b *OKEXFuturesBroker) RunEventLoopOnce() (err error) { +func (b *OKEXFutures) RunEventLoopOnce() (err error) { return } // addr: https://www.okex.com/ -func NewBroker(addr string, accessKey string, secretKey string, passphrase string) *OKEXFuturesBroker { +func New(addr string, accessKey string, secretKey string, passphrase string) *OKEXFutures { config := okex.Config{ Endpoint: addr, WSEndpoint: "", @@ -445,7 +445,7 @@ func NewBroker(addr string, accessKey string, secretKey string, passphrase strin ProxyURL: "", } client := okex.NewClient(config) - return &OKEXFuturesBroker{ + return &OKEXFutures{ client: client, } } diff --git a/brokers/okex-futures-broker/broker_test.go b/brokers/okex-futures/broker_test.go similarity index 78% rename from brokers/okex-futures-broker/broker_test.go rename to brokers/okex-futures/broker_test.go index 50aad7d..5fc7f4e 100644 --- a/brokers/okex-futures-broker/broker_test.go +++ b/brokers/okex-futures/broker_test.go @@ -1,4 +1,4 @@ -package okex_futures_broker +package okex_futures import ( "log" @@ -9,7 +9,7 @@ import ( "github.com/spf13/viper" ) -func newTestBroker() Broker { +func newForTest() Broker { viper.SetConfigName("test_config") viper.AddConfigPath(".") err := viper.ReadInConfig() @@ -21,11 +21,11 @@ func newTestBroker() Broker { secretKey := viper.GetString("secret_key") passphrase := viper.GetString("passphrase") baseURL := "https://www.okex.me" // https://www.okex.com - return NewBroker(baseURL, accessKey, secretKey, passphrase) + return New(baseURL, accessKey, secretKey, passphrase) } func TestGetAccountSummary(t *testing.T) { - b := newTestBroker() + b := newForTest() accountSummary, err := b.GetAccountSummary("BTC") if err != nil { t.Error(err) @@ -35,7 +35,7 @@ func TestGetAccountSummary(t *testing.T) { } func TestGetOrderBook(t *testing.T) { - b := newTestBroker() + b := newForTest() symbol := "BTC-USD-200327" //symbol := "BTC-USD-200626" for { @@ -59,8 +59,8 @@ func TestGetOrderBook(t *testing.T) { //t.Logf("Time: %v", ob.Time) } -func TestOKEXFuturesBroker_GetRecords(t *testing.T) { - b := newTestBroker() +func TestOKEXFutures_GetRecords(t *testing.T) { + b := newForTest() symbol := "BTC-USD-200410" start := time.Now().Add(-20 * time.Hour) end := time.Now() @@ -75,8 +75,8 @@ func TestOKEXFuturesBroker_GetRecords(t *testing.T) { } } -func TestOKEXBroker_GetContractID(t *testing.T) { - b := newTestBroker() +func TestOKEXFutures_GetContractID(t *testing.T) { + b := newForTest() b.SetContractType("BTC-USD", ContractTypeW1) symbol, err := b.GetContractID() if err != nil { @@ -86,8 +86,8 @@ func TestOKEXBroker_GetContractID(t *testing.T) { t.Logf("%v", symbol) } -func TestOKEXBroker_PlaceOrder(t *testing.T) { - b := newTestBroker() +func TestOKEXFutures_PlaceOrder(t *testing.T) { + b := newForTest() symbol := "BTC-USD-200327" order, err := b.PlaceOrder( symbol, @@ -106,8 +106,8 @@ func TestOKEXBroker_PlaceOrder(t *testing.T) { t.Logf("%#v", order) } -func TestOKEXBroker_GetOpenOrders(t *testing.T) { - b := newTestBroker() +func TestOKEXFutures_GetOpenOrders(t *testing.T) { + b := newForTest() symbol := "BTC-USD-200327" orders, err := b.GetOpenOrders(symbol) if err != nil { @@ -120,8 +120,8 @@ func TestOKEXBroker_GetOpenOrders(t *testing.T) { } } -func TestOKEXBroker_GetOrder(t *testing.T) { - b := newTestBroker() +func TestOKEXFutures_GetOrder(t *testing.T) { + b := newForTest() symbol := "BTC-USD-200327" id := "4605829824487425" order, err := b.GetOrder(symbol, id) @@ -132,8 +132,8 @@ func TestOKEXBroker_GetOrder(t *testing.T) { t.Logf("%#v", order) } -func TestOKEXBroker_CancelOrder(t *testing.T) { - b := newTestBroker() +func TestOKEXFutures_CancelOrder(t *testing.T) { + b := newForTest() symbol := "BTC-USD-200327" id := "4605829824487425" ret, err := b.CancelOrder(symbol, id) @@ -144,8 +144,8 @@ func TestOKEXBroker_CancelOrder(t *testing.T) { t.Logf("%#v", ret) } -func TestOKEXBroker_GetPosition(t *testing.T) { - b := newTestBroker() +func TestOKEXFutures_GetPosition(t *testing.T) { + b := newForTest() symbol := "BTC-USD-200327" position, err := b.GetPosition(symbol) if err != nil { diff --git a/brokers/okex-futures-broker/test_config.example.yaml b/brokers/okex-futures/test_config.example.yaml similarity index 100% rename from brokers/okex-futures-broker/test_config.example.yaml rename to brokers/okex-futures/test_config.example.yaml diff --git a/brokers/okex-swap-broker/broker.go b/brokers/okex-swap/broker.go similarity index 82% rename from brokers/okex-swap-broker/broker.go rename to brokers/okex-swap/broker.go index 4270c7b..ee385ec 100644 --- a/brokers/okex-swap-broker/broker.go +++ b/brokers/okex-swap/broker.go @@ -1,4 +1,4 @@ -package okex_futures_broker +package okex_swap import ( "fmt" @@ -11,16 +11,8 @@ import ( "github.com/frankrap/okex-api" ) -var ( - loc *time.Location -) - -func init() { - loc = time.Now().Location() -} - -// OKEXSwapBroker the OKEX swap broker -type OKEXSwapBroker struct { +// OKEXSwap the OKEX swap broker +type OKEXSwap struct { client *okex.Client pair string // contract pair 合约交易对 contractType string // contract type 合约类型 @@ -28,11 +20,11 @@ type OKEXSwapBroker struct { leverRate int // lever rate 杠杆倍数 } -func (b *OKEXSwapBroker) GetName() (name string) { +func (b *OKEXSwap) GetName() (name string) { return "OKEXSwap" } -func (b *OKEXSwapBroker) GetAccountSummary(currency string) (result AccountSummary, err error) { +func (b *OKEXSwap) GetAccountSummary(currency string) (result AccountSummary, err error) { var account okex.SwapAccount account, err = b.client.GetSwapAccount(currency) if err != nil { @@ -46,7 +38,7 @@ func (b *OKEXSwapBroker) GetAccountSummary(currency string) (result AccountSumma return } -func (b *OKEXSwapBroker) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { +func (b *OKEXSwap) GetOrderBook(symbol string, depth int) (result OrderBook, err error) { params := map[string]string{} params["size"] = fmt.Sprintf("%v", depth) // "10" //params["depth"] = fmt.Sprintf("%v", 0.01) // BTC: "0.1" @@ -77,7 +69,7 @@ func (b *OKEXSwapBroker) GetOrderBook(symbol string, depth int) (result OrderBoo return } -func (b *OKEXSwapBroker) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { +func (b *OKEXSwap) GetRecords(symbol string, period string, from int64, end int64, limit int) (records []Record, err error) { var granularity int64 var intervalValue string var intervalF int64 @@ -159,21 +151,21 @@ func (b *OKEXSwapBroker) GetRecords(symbol string, period string, from int64, en } // 设置合约类型 -func (b *OKEXSwapBroker) SetContractType(pair string, contractType string) (err error) { +func (b *OKEXSwap) SetContractType(pair string, contractType string) (err error) { return } -func (b *OKEXSwapBroker) GetContractID() (symbol string, err error) { +func (b *OKEXSwap) GetContractID() (symbol string, err error) { return } // 设置杠杆大小 -func (b *OKEXSwapBroker) SetLeverRate(value float64) (err error) { +func (b *OKEXSwap) SetLeverRate(value float64) (err error) { b.leverRate = int(value) return } -func (b *OKEXSwapBroker) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, +func (b *OKEXSwap) PlaceOrder(symbol string, direction Direction, orderType OrderType, price float64, stopPx float64, size float64, postOnly bool, reduceOnly bool, params map[string]interface{}) (result Order, err error) { var pType int if direction == Buy { @@ -225,7 +217,7 @@ func (b *OKEXSwapBroker) PlaceOrder(symbol string, direction Direction, orderTyp return } -func (b *OKEXSwapBroker) GetOpenOrders(symbol string) (result []Order, err error) { +func (b *OKEXSwap) GetOpenOrders(symbol string) (result []Order, err error) { // 6: 未完成(等待成交+部分成交) // 7: 已完成(撤单成功+完全成交) var ret *okex.SwapOrdersInfo @@ -242,7 +234,7 @@ func (b *OKEXSwapBroker) GetOpenOrders(symbol string) (result []Order, err error return } -func (b *OKEXSwapBroker) GetOrder(symbol string, id string) (result Order, err error) { +func (b *OKEXSwap) GetOrder(symbol string, id string) (result Order, err error) { var ret okex.BaseOrderInfo ret, err = b.client.GetSwapOrderById(symbol, id) if err != nil { @@ -252,7 +244,7 @@ func (b *OKEXSwapBroker) GetOrder(symbol string, id string) (result Order, err e return } -func (b *OKEXSwapBroker) CancelOrder(symbol string, id string) (result Order, err error) { +func (b *OKEXSwap) CancelOrder(symbol string, id string) (result Order, err error) { var ret okex.SwapCancelOrderResult var resp []byte resp, ret, err = b.client.PostSwapCancelOrder(symbol, id) @@ -271,15 +263,15 @@ func (b *OKEXSwapBroker) CancelOrder(symbol string, id string) (result Order, er return } -func (b *OKEXSwapBroker) CancelAllOrders(symbol string) (err error) { +func (b *OKEXSwap) CancelAllOrders(symbol string) (err error) { return } -func (b *OKEXSwapBroker) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { +func (b *OKEXSwap) AmendOrder(symbol string, id string, price float64, size float64) (result Order, err error) { return } -func (b *OKEXSwapBroker) GetPosition(symbol string) (result Position, err error) { +func (b *OKEXSwap) GetPosition(symbol string) (result Position, err error) { var ret okex.SwapPosition ret, err = b.client.GetSwapPositionByInstrument(symbol) if err != nil { @@ -298,7 +290,7 @@ func (b *OKEXSwapBroker) GetPosition(symbol string) (result Position, err error) // 2019-10-08T11:56:07.922Z timestamp, _ := time.ParseInLocation(v.Timestamp, "2006-01-02T15:04:05.000Z", - loc) + time.Local) if v.Side == "long" { result.Size, _ = strconv.ParseFloat(v.Position, 64) result.AvgPrice, _ = strconv.ParseFloat(v.AvgCost, 64) @@ -315,7 +307,7 @@ func (b *OKEXSwapBroker) GetPosition(symbol string) (result Position, err error) return } -func (b *OKEXSwapBroker) convertOrder(symbol string, order *okex.BaseOrderInfo) (result Order) { +func (b *OKEXSwap) convertOrder(symbol string, order *okex.BaseOrderInfo) (result Order) { result.ID = order.OrderId result.Symbol = symbol result.Price = order.Price @@ -335,7 +327,7 @@ func (b *OKEXSwapBroker) convertOrder(symbol string, order *okex.BaseOrderInfo) return } -func (b *OKEXSwapBroker) orderDirection(order *okex.BaseOrderInfo) Direction { +func (b *OKEXSwap) orderDirection(order *okex.BaseOrderInfo) Direction { // 订单类型 //1:开多 //2:开空 @@ -349,14 +341,14 @@ func (b *OKEXSwapBroker) orderDirection(order *okex.BaseOrderInfo) Direction { return Buy } -func (b *OKEXSwapBroker) orderType(order *okex.BaseOrderInfo) OrderType { +func (b *OKEXSwap) orderType(order *okex.BaseOrderInfo) OrderType { if order.OrderType == "4" { return OrderTypeMarket } return OrderTypeLimit } -func (b *OKEXSwapBroker) orderStatus(order *okex.BaseOrderInfo) OrderStatus { +func (b *OKEXSwap) orderStatus(order *okex.BaseOrderInfo) OrderStatus { /* 订单状态 -2:失败 @@ -387,12 +379,12 @@ func (b *OKEXSwapBroker) orderStatus(order *okex.BaseOrderInfo) OrderStatus { } } -func (b *OKEXSwapBroker) RunEventLoopOnce() (err error) { +func (b *OKEXSwap) RunEventLoopOnce() (err error) { return } // addr: https://www.okex.com/ -func NewBroker(addr string, accessKey string, secretKey string, passphrase string) *OKEXSwapBroker { +func New(addr string, accessKey string, secretKey string, passphrase string) *OKEXSwap { config := okex.Config{ Endpoint: addr, WSEndpoint: "", @@ -405,7 +397,7 @@ func NewBroker(addr string, accessKey string, secretKey string, passphrase strin ProxyURL: "", } client := okex.NewClient(config) - return &OKEXSwapBroker{ + return &OKEXSwap{ client: client, } } diff --git a/brokers/okex-swap-broker/broker_test.go b/brokers/okex-swap/broker_test.go similarity index 96% rename from brokers/okex-swap-broker/broker_test.go rename to brokers/okex-swap/broker_test.go index 1a00d5b..497df01 100644 --- a/brokers/okex-swap-broker/broker_test.go +++ b/brokers/okex-swap/broker_test.go @@ -1,4 +1,4 @@ -package okex_futures_broker +package okex_swap import ( . "github.com/coinrust/crex" @@ -20,7 +20,7 @@ func newTestBroker() Broker { secretKey := viper.GetString("secret_key") passphrase := viper.GetString("passphrase") baseURL := "https://www.okex.me" // https://www.okex.com - return NewBroker(baseURL, accessKey, secretKey, passphrase) + return New(baseURL, accessKey, secretKey, passphrase) } func TestGetAccountSummary(t *testing.T) { diff --git a/brokers/okex-swap-broker/test_config.example.yaml b/brokers/okex-swap/test_config.example.yaml similarity index 100% rename from brokers/okex-swap-broker/test_config.example.yaml rename to brokers/okex-swap/test_config.example.yaml diff --git a/examples/backtest/main.go b/examples/backtest/main.go index 79901c2..89f02e1 100644 --- a/examples/backtest/main.go +++ b/examples/backtest/main.go @@ -4,7 +4,7 @@ import ( "fmt" . "github.com/coinrust/crex" "github.com/coinrust/crex/backtest" - "github.com/coinrust/crex/brokers/deribit-sim-broker" + "github.com/coinrust/crex/brokers/deribit-sim" "github.com/coinrust/crex/data" ) @@ -40,7 +40,7 @@ func main() { data := data.NewCsvData("../../data-samples/deribit/deribit_BTC-PERPETUAL_and_futures_tick_by_tick_book_snapshots_10_levels_2019-10-01_2019-11-01.csv") var brokers []Broker for i := 0; i < 2; i++ { - broker := deribit_sim_broker.NewBroker(data, 5.0, -0.00025, 0.00075) + broker := deribit_sim.New(data, 5.0, -0.00025, 0.00075) brokers = append(brokers, broker) } s := &BasicStrategy{}