Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nntaoli committed Jul 22, 2020
2 parents 5430118 + ed525ea commit d1e2535
Show file tree
Hide file tree
Showing 37 changed files with 478 additions and 271 deletions.
6 changes: 3 additions & 3 deletions API.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package goex
// api interface

type API interface {
LimitBuy(amount, price string, currency CurrencyPair) (*Order, error)
LimitSell(amount, price string, currency CurrencyPair) (*Order, error)
LimitBuy(amount, price string, currency CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error)
LimitSell(amount, price string, currency CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error)
MarketBuy(amount, price string, currency CurrencyPair) (*Order, error)
MarketSell(amount, price string, currency CurrencyPair) (*Order, error)
CancelOrder(orderId string, currency CurrencyPair) (bool, error)
Expand All @@ -15,7 +15,7 @@ type API interface {

GetTicker(currency CurrencyPair) (*Ticker, error)
GetDepth(size int, currency CurrencyPair) (*Depth, error)
GetKlineRecords(currency CurrencyPair, period , size, since int) ([]Kline, error)
GetKlineRecords(currency CurrencyPair, period, size, since int) ([]Kline, error)
//非个人,整个交易所的交易记录
GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, error)

Expand Down
21 changes: 21 additions & 0 deletions Const.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,24 @@ const (
TIPS //余币宝
SWAP //永续合约
)

type LimitOrderOptionalParameter int

func (opt LimitOrderOptionalParameter) String() string {
switch opt {
case PostOnly:
return "post_only"
case Fok:
return "fok"
case Ioc:
return "ioc"
default:
return "error-order-optional-parameter"
}
}

const (
PostOnly LimitOrderOptionalParameter = iota + 1
Ioc
Fok
)
11 changes: 7 additions & 4 deletions FutureRestAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ type FutureRestAPI interface {
* @param openType 1:开多 2:开空 3:平多 4:平空
* @param matchPrice 是否为对手价 0:不是 1:是 ,当取值为1时,price无效
*/
PlaceFutureOrder(currencyPair CurrencyPair, contractType, price, amount string, openType, matchPrice, leverRate int) (string, error)
PlaceFutureOrder(currencyPair CurrencyPair, contractType, price, amount string, openType, matchPrice int, leverRate float64) (string, error)

LimitFuturesOrder(currencyPair CurrencyPair, contractType, price, amount string, openType int) (*FutureOrder, error)
LimitFuturesOrder(currencyPair CurrencyPair, contractType, price, amount string, openType int, opt ...LimitOrderOptionalParameter) (*FutureOrder, error)

//对手价下单
MarketFuturesOrder(currencyPair CurrencyPair, contractType, amount string, openType int) (*FutureOrder, error)

/**
* 取消订单
Expand Down Expand Up @@ -108,10 +111,10 @@ type FutureRestAPI interface {
/**
* 获取K线数据
*/
GetKlineRecords(contract_type string, currency CurrencyPair, period, size, since int) ([]FutureKline, error)
GetKlineRecords(contractType string, currency CurrencyPair, period, size, since int) ([]FutureKline, error)

/**
* 获取Trade数据
*/
GetTrades(contract_type string, currencyPair CurrencyPair, since int64) ([]Trade, error)
GetTrades(contractType string, currencyPair CurrencyPair, since int64) ([]Trade, error)
}
14 changes: 9 additions & 5 deletions Models.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ type APIConfig struct {
ApiPassphrase string //for okex.com v3 api
ClientId string //for bitstamp.net , huobi.pro

Lever int //杠杆倍数 , for future
Lever float64 //杠杆倍数 , for future
}

type Kline struct {
Expand Down Expand Up @@ -163,10 +163,14 @@ type FutureOrder struct {
Currency CurrencyPair
OrderType int //ORDINARY=0 POST_ONLY=1 FOK= 2 IOC= 3
OType int //1:开多 2:开空 3:平多 4: 平空
LeverRate int //倍数
LeverRate float64 //倍数
Fee float64 //手续费
ContractName string
FinishedTime int64 // finished timestamp

//策略委托单
TriggerPrice float64
AlgoType int //1:限价 2:市场价;触发价格类型,默认是限价;为市场价时,委托价格不必填;
}

type FuturePosition struct {
Expand All @@ -176,7 +180,7 @@ type FuturePosition struct {
BuyPriceCost float64
BuyProfitReal float64
CreateDate int64
LeverRate int
LeverRate float64
SellAmount float64
SellAvailable float64
SellPriceAvg float64
Expand All @@ -186,6 +190,8 @@ type FuturePosition struct {
ContractType string
ContractId int64
ForceLiquPrice float64 //预估爆仓价
ShortPnlRatio float64 //空仓收益率
LongPnlRatio float64 //多仓收益率
}

type HistoricalFunding struct {
Expand Down Expand Up @@ -222,7 +228,6 @@ type RepaymentParameter struct {
BorrowId string
}


type TransferParameter struct {
Currency string `json:"currency"`
From int `json:"from"`
Expand All @@ -242,7 +247,6 @@ type WithdrawParameter struct {
Fee string `json:"fee"`
}


type DepositWithdrawHistory struct {
WithdrawalId string `json:"withdrawal_id,omitempty"`
Currency string `json:"currency"`
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">
<img width="409" heigth="205" src="https://upload-images.jianshu.io/upload_images/6760989-dec7dc747846880e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="goex">
<img src="https://travis-ci.org/nntaoli-project/goex.svg?branch=dev"/>
<img width="198" height="205" src="https://upload-images.jianshu.io/upload_images/6760989-81f29f7a5dbd9bb6.jpg" >
</div>

### goex目标
Expand All @@ -9,6 +9,10 @@ goex项目是为了统一并标准化各个数字资产交易平台的接口而

[English](https://github.com/nntaoli-project/goex/blob/dev/README_en.md)

### wiki文档

[文档](https://github.com/nntaoli-project/goex/wiki)

### goex已支持交易所 `23+`

| 交易所 | 行情接口 | 交易接口 | 版本号 |
Expand Down
4 changes: 2 additions & 2 deletions allcoin/allcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,11 @@ func (ac *Allcoin) GetAccount() (*Account, error) {
return &acc, nil
}

func (ac *Allcoin) LimitBuy(amount, price string, currencyPair CurrencyPair) (*Order, error) {
func (ac *Allcoin) LimitBuy(amount, price string, currencyPair CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return ac.placeOrder(amount, price, currencyPair, "LIMIT", "buy")
}

func (ac *Allcoin) LimitSell(amount, price string, currencyPair CurrencyPair) (*Order, error) {
func (ac *Allcoin) LimitSell(amount, price string, currencyPair CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return ac.placeOrder(amount, price, currencyPair, "LIMIT", "sale")
}

Expand Down
4 changes: 2 additions & 2 deletions atop/atop.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,12 +306,12 @@ func (at *Atop) GetAccount() (*Account, error) {
}

//hao
func (at *Atop) LimitBuy(amount, price string, currencyPair CurrencyPair) (*Order, error) {
func (at *Atop) LimitBuy(amount, price string, currencyPair CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return at.plateOrder(amount, price, currencyPair, "limit", "buy")
}

//hao
func (at *Atop) LimitSell(amount, price string, currencyPair CurrencyPair) (*Order, error) {
func (at *Atop) LimitSell(amount, price string, currencyPair CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return at.plateOrder(amount, price, currencyPair, "limit", "sale")
}

Expand Down
4 changes: 2 additions & 2 deletions bigone/Bigone.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,11 @@ func (bo *Bigone) placeOrder(amount, price string, pair goex.CurrencyPair, order
OrderTime: int(time.Now().Unix())}, nil
}

func (bo *Bigone) LimitBuy(amount, price string, currency goex.CurrencyPair) (*goex.Order, error) {
func (bo *Bigone) LimitBuy(amount, price string, currency goex.CurrencyPair, opt ...goex.LimitOrderOptionalParameter) (*goex.Order, error) {
return bo.placeOrder(amount, price, currency, "LIMIT", "BID")
}

func (bo *Bigone) LimitSell(amount, price string, currency goex.CurrencyPair) (*goex.Order, error) {
func (bo *Bigone) LimitSell(amount, price string, currency goex.CurrencyPair, opt ...goex.LimitOrderOptionalParameter) (*goex.Order, error) {
return bo.placeOrder(amount, price, currency, "LIMIT", "ASK")
}

Expand Down
57 changes: 20 additions & 37 deletions binance/Binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,11 @@ func (bn *Binance) GetAccount() (*Account, error) {
return &acc, nil
}

func (bn *Binance) LimitBuy(amount, price string, currencyPair CurrencyPair) (*Order, error) {
func (bn *Binance) LimitBuy(amount, price string, currencyPair CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return bn.placeOrder(amount, price, currencyPair, "LIMIT", "BUY")
}

func (bn *Binance) LimitSell(amount, price string, currencyPair CurrencyPair) (*Order, error) {
func (bn *Binance) LimitSell(amount, price string, currencyPair CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return bn.placeOrder(amount, price, currencyPair, "LIMIT", "SELL")
}

Expand Down Expand Up @@ -537,40 +537,6 @@ func (bn *Binance) GetUnfinishOrders(currencyPair CurrencyPair) ([]Order, error)
return orders, nil
}

func (bn *Binance) GetAllUnfinishOrders() ([]Order, error) {
params := url.Values{}

bn.buildParamsSigned(&params)
path := bn.apiV3 + UNFINISHED_ORDERS_INFO + params.Encode()

respmap, err := HttpGet3(bn.httpClient, path, map[string]string{"X-MBX-APIKEY": bn.accessKey})
if err != nil {
return nil, err
}

orders := make([]Order, 0)
for _, v := range respmap {
ord := v.(map[string]interface{})
side := ord["side"].(string)
orderSide := SELL
if side == "BUY" {
orderSide = BUY
}

ordId := ToInt(ord["orderId"])
orders = append(orders, Order{
OrderID: ToInt(ord["orderId"]),
OrderID2: strconv.Itoa(ordId),
Currency: bn.toCurrencyPair(ord["symbol"].(string)),
Price: ToFloat64(ord["price"]),
Amount: ToFloat64(ord["origQty"]),
Side: TradeSide(orderSide),
Status: ORDER_UNFINISH,
OrderTime: ToInt(ord["time"])})
}
return orders, nil
}

func (bn *Binance) GetKlineRecords(currency CurrencyPair, period, size, since int) ([]Kline, error) {
params := url.Values{}
params.Set("symbol", currency.ToSymbol(""))
Expand Down Expand Up @@ -662,14 +628,31 @@ func (bn *Binance) GetOrderHistorys(currency CurrencyPair, currentPage, pageSize
orderSide = BUY
}
ordId := ToInt(ord["orderId"])
status := ord["status"].(string)
var tradeStatus TradeStatus
switch status {
case "NEW":
tradeStatus = ORDER_UNFINISH
case "FILLED":
tradeStatus = ORDER_FINISH
case "PARTIALLY_FILLED":
tradeStatus = ORDER_PART_FINISH
case "CANCELED":
tradeStatus = ORDER_CANCEL
case "PENDING_CANCEL":
tradeStatus = ORDER_CANCEL_ING
case "REJECTED":
tradeStatus = ORDER_REJECT
}

orders = append(orders, Order{
OrderID: ToInt(ord["orderId"]),
OrderID2: strconv.Itoa(ordId),
Currency: currency,
Price: ToFloat64(ord["price"]),
Amount: ToFloat64(ord["origQty"]),
Side: TradeSide(orderSide),
Status: ORDER_UNFINISH,
Status: tradeStatus,
OrderTime: ToInt(ord["time"])})
}
return orders, nil
Expand Down
18 changes: 11 additions & 7 deletions binance/BinanceSwap.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ type BinanceSwap struct {
Binance
}

func (bs *BinanceSwap) LimitFuturesOrder(currencyPair CurrencyPair, contractType, price, amount string, openType int) (*FutureOrder, error) {
return bs.PlaceFutureOrder2(currencyPair, contractType, price, amount, openType, 0, 10)
}

func NewBinanceSwap(config *APIConfig) *BinanceSwap {
if config.Endpoint == "" {
config.Endpoint = baseUrl
Expand Down Expand Up @@ -293,7 +289,7 @@ func (bs *BinanceSwap) Transfer(currency Currency, transferType int, amount floa
return ToInt64(respmap["tranId"]), nil
}

func (bs *BinanceSwap) PlaceFutureOrder(currencyPair CurrencyPair, contractType, price, amount string, openType, matchPrice, leverRate int) (string, error) {
func (bs *BinanceSwap) PlaceFutureOrder(currencyPair CurrencyPair, contractType, price, amount string, openType, matchPrice int, leverRate float64) (string, error) {
fOrder, err := bs.PlaceFutureOrder2(currencyPair, contractType, price, amount, openType, matchPrice, leverRate)
return fOrder.OrderID2, err
}
Expand All @@ -308,7 +304,7 @@ func (bs *BinanceSwap) PlaceFutureOrder(currencyPair CurrencyPair, contractType,
* @param openType 1:开多 2:开空 3:平多 4:平空
* @param matchPrice 是否为对手价 0:不是 1:是 ,当取值为1时,price无效
*/
func (bs *BinanceSwap) PlaceFutureOrder2(currencyPair CurrencyPair, contractType, price, amount string, openType, matchPrice, leverRate int) (*FutureOrder, error) {
func (bs *BinanceSwap) PlaceFutureOrder2(currencyPair CurrencyPair, contractType, price, amount string, openType, matchPrice int, leverRate float64) (*FutureOrder, error) {
fOrder := &FutureOrder{
Currency: currencyPair,
ClientOid: GenerateOrderClientId(32),
Expand Down Expand Up @@ -362,6 +358,14 @@ func (bs *BinanceSwap) PlaceFutureOrder2(currencyPair CurrencyPair, contractType
return fOrder, nil
}

func (bs *BinanceSwap) LimitFuturesOrder(currencyPair CurrencyPair, contractType, price, amount string, openType int, opt ...LimitOrderOptionalParameter) (*FutureOrder, error) {
return bs.PlaceFutureOrder2(currencyPair, contractType, price, amount, openType, 0, 10)
}

func (bs *BinanceSwap) MarketFuturesOrder(currencyPair CurrencyPair, contractType, amount string, openType int) (*FutureOrder, error) {
return bs.PlaceFutureOrder2(currencyPair, contractType, "0", amount, openType, 1, 10)
}

/**
* 取消订单
* @param symbol btc_usd:比特币 ltc_usd :莱特币
Expand Down Expand Up @@ -486,7 +490,7 @@ func (bs *BinanceSwap) GetFuturePosition(currencyPair CurrencyPair, contractType
continue
}
p := FuturePosition{
LeverRate: ToInt(cont["leverage"]),
LeverRate: ToFloat64(cont["leverage"]),
Symbol: currencyPair,
ForceLiquPrice: ToFloat64(cont["liquidationPrice"]),
}
Expand Down
7 changes: 6 additions & 1 deletion binance/Binance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var ba = NewWithConfig(
},
Timeout: 10 * time.Second,
},
Endpoint: US_API_BASE_URL,
Endpoint: GLOBAL_API_BASE_URL,
ApiKey: "q6y6Gr7fF3jSJLncpfn2PmAA0xu4XRiRFHpFkyJy3d7K68WUxY0Gt8rrajCDUfbI",
ApiSecretKey: "AP8C2kh4RyISN3fpRCFMZJddf233XbPcYWQ1S7gBan3pGjCQg2JnyQFSJrIaNzRh",
})
Expand Down Expand Up @@ -70,6 +70,7 @@ func TestBinance_GetUnfinishOrders(t *testing.T) {
orders, err := ba.GetUnfinishOrders(goex.ETH_BTC)
t.Log(orders, err)
}

func TestBinance_GetKlineRecords(t *testing.T) {
before := time.Now().Add(-time.Hour).Unix() * 1000
kline, _ := ba.GetKlineRecords(goex.ETH_BTC, goex.KLINE_PERIOD_5MIN, 100, int(before))
Expand All @@ -91,3 +92,7 @@ func TestBinance_SetTimeOffset(t *testing.T) {
t.Log(ba.setTimeOffset())
t.Log(ba.timeOffset)
}

func TestBinance_GetOrderHistorys(t *testing.T) {
t.Log(ba.GetOrderHistorys(goex.BTC_USDT, 1, 1))
}
4 changes: 2 additions & 2 deletions bitfinex/bitfinex.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ func (bfx *Bitfinex) placeOrder(orderType, side, amount, price string, pair Curr
return order, nil
}

func (bfx *Bitfinex) LimitBuy(amount, price string, currencyPair CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) LimitBuy(amount, price string, currencyPair CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return bfx.placeOrder("exchange limit", "buy", amount, price, currencyPair)
}

func (bfx *Bitfinex) LimitSell(amount, price string, currencyPair CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) LimitSell(amount, price string, currencyPair CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return bfx.placeOrder("exchange limit", "sell", amount, price, currencyPair)
}

Expand Down
4 changes: 2 additions & 2 deletions bithumb/bithumb.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ func (bit *Bithumb) placeOrder(side, amount, price string, pair CurrencyPair) (*
Status: ORDER_UNFINISH}, nil
}

func (bit *Bithumb) LimitBuy(amount, price string, currency CurrencyPair) (*Order, error) {
func (bit *Bithumb) LimitBuy(amount, price string, currency CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return bit.placeOrder("bid", amount, price, currency)
}

func (bit *Bithumb) LimitSell(amount, price string, currency CurrencyPair) (*Order, error) {
func (bit *Bithumb) LimitSell(amount, price string, currency CurrencyPair, opt ...LimitOrderOptionalParameter) (*Order, error) {
return bit.placeOrder("ask", amount, price, currency)
}

Expand Down
Loading

0 comments on commit d1e2535

Please sign in to comment.