Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nntaoli committed Feb 27, 2021
2 parents 50b8d06 + da4136c commit 466bffc
Show file tree
Hide file tree
Showing 59 changed files with 1,825 additions and 870 deletions.
4 changes: 2 additions & 2 deletions API.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ type API interface {
CancelOrder(orderId string, currency CurrencyPair) (bool, error)
GetOneOrder(orderId string, currency CurrencyPair) (*Order, error)
GetUnfinishOrders(currency CurrencyPair) ([]Order, error)
GetOrderHistorys(currency CurrencyPair, currentPage, pageSize int) ([]Order, error)
GetOrderHistorys(currency CurrencyPair, opt ...OptionalParameter) ([]Order, error)
GetAccount() (*Account, error)

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 KlinePeriod, size int, optional ...OptionalParameter) ([]Kline, error)
//非个人,整个交易所的交易记录
GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, error)

Expand Down
67 changes: 41 additions & 26 deletions CurrencyPair.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ var (
TRX = Currency{"TRX", ""}
GBP = Currency{"GBP", ""}
XLM = Currency{"XLM", ""}
DOT = Currency{"DOT", ""}
DASH = Currency{"DASH", ""}
CRV = Currency{"CRV", ""}
ALGO = Currency{"ALGO", ""}

//currency pair
BTC_KRW = CurrencyPair{CurrencyA: BTC, CurrencyB: KRW, AmountTickSize: 2, PriceTickSize: 1}
Expand All @@ -68,33 +72,41 @@ var (
LTC_KRW = CurrencyPair{CurrencyA: LTC, CurrencyB: KRW, AmountTickSize: 2, PriceTickSize: 2}
BCH_KRW = CurrencyPair{CurrencyA: BCH, CurrencyB: KRW, AmountTickSize: 2, PriceTickSize: 2}

BTC_USD = CurrencyPair{CurrencyA: BTC, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 1}
LTC_USD = CurrencyPair{CurrencyA: LTC, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
ETH_USD = CurrencyPair{CurrencyA: ETH, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
ETC_USD = CurrencyPair{CurrencyA: ETC, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
BCH_USD = CurrencyPair{CurrencyA: BCH, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
XRP_USD = CurrencyPair{CurrencyA: XRP, CurrencyB: USD, AmountTickSize: 3, PriceTickSize: 3}
BCD_USD = CurrencyPair{CurrencyA: BCD, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 3}
EOS_USD = CurrencyPair{CurrencyA: EOS, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
BTG_USD = CurrencyPair{CurrencyA: BTG, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
BSV_USD = CurrencyPair{CurrencyA: BSV, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
BTC_USD = CurrencyPair{CurrencyA: BTC, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 1}
LTC_USD = CurrencyPair{CurrencyA: LTC, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
ETH_USD = CurrencyPair{CurrencyA: ETH, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
ETC_USD = CurrencyPair{CurrencyA: ETC, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
BCH_USD = CurrencyPair{CurrencyA: BCH, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
XRP_USD = CurrencyPair{CurrencyA: XRP, CurrencyB: USD, AmountTickSize: 3, PriceTickSize: 3}
BCD_USD = CurrencyPair{CurrencyA: BCD, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 3}
EOS_USD = CurrencyPair{CurrencyA: EOS, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
BTG_USD = CurrencyPair{CurrencyA: BTG, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
BSV_USD = CurrencyPair{CurrencyA: BSV, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
DOT_USD = CurrencyPair{CurrencyA: DOT, CurrencyB: USD, AmountTickSize: 3, PriceTickSize: 2}
DASH_USD = CurrencyPair{CurrencyA: DASH, CurrencyB: USD, AmountTickSize: 2, PriceTickSize: 2}
CRV_USD = CurrencyPair{CurrencyA: CRV, CurrencyB: USD, AmountTickSize: 4, PriceTickSize: 3}
ALGO_USD = CurrencyPair{CurrencyA: ALGO, CurrencyB: USD, AmountTickSize: 4, PriceTickSize: 4}

BTC_USDT = CurrencyPair{CurrencyA: BTC, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 1}
LTC_USDT = CurrencyPair{CurrencyA: LTC, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
BCH_USDT = CurrencyPair{CurrencyA: BCH, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
ETC_USDT = CurrencyPair{CurrencyA: ETC, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 3}
ETH_USDT = CurrencyPair{CurrencyA: ETH, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
BCD_USDT = CurrencyPair{CurrencyA: BCD, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
NEO_USDT = CurrencyPair{CurrencyA: NEO, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
EOS_USDT = CurrencyPair{CurrencyA: EOS, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
XRP_USDT = CurrencyPair{CurrencyA: XRP, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
HSR_USDT = CurrencyPair{CurrencyA: HSR, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
BSV_USDT = CurrencyPair{CurrencyA: BSV, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
OKB_USDT = CurrencyPair{CurrencyA: OKB, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
HT_USDT = CurrencyPair{CurrencyA: HT, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 4}
BNB_USDT = CurrencyPair{CurrencyA: BNB, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
PAX_USDT = CurrencyPair{CurrencyA: PAX, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 3}
TRX_USDT = CurrencyPair{CurrencyA: TRX, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 3}
BTC_USDT = CurrencyPair{CurrencyA: BTC, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 1}
LTC_USDT = CurrencyPair{CurrencyA: LTC, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
BCH_USDT = CurrencyPair{CurrencyA: BCH, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
ETC_USDT = CurrencyPair{CurrencyA: ETC, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 3}
ETH_USDT = CurrencyPair{CurrencyA: ETH, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
BCD_USDT = CurrencyPair{CurrencyA: BCD, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
NEO_USDT = CurrencyPair{CurrencyA: NEO, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
EOS_USDT = CurrencyPair{CurrencyA: EOS, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
XRP_USDT = CurrencyPair{CurrencyA: XRP, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
HSR_USDT = CurrencyPair{CurrencyA: HSR, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
BSV_USDT = CurrencyPair{CurrencyA: BSV, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
OKB_USDT = CurrencyPair{CurrencyA: OKB, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
HT_USDT = CurrencyPair{CurrencyA: HT, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 4}
BNB_USDT = CurrencyPair{CurrencyA: BNB, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
PAX_USDT = CurrencyPair{CurrencyA: PAX, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 3}
TRX_USDT = CurrencyPair{CurrencyA: TRX, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 3}
DOT_USDT = CurrencyPair{CurrencyA: DOT, CurrencyB: USDT, AmountTickSize: 3, PriceTickSize: 2}
DASH_USDT = CurrencyPair{CurrencyA: DASH, CurrencyB: USDT, AmountTickSize: 2, PriceTickSize: 2}
CRV_USDT = CurrencyPair{CurrencyA: CRV, CurrencyB: USDT, AmountTickSize: 3, PriceTickSize: 3}
ALGO_USDT = CurrencyPair{CurrencyA: ALGO, CurrencyB: USDT, AmountTickSize: 3, PriceTickSize: 4}

XRP_EUR = CurrencyPair{CurrencyA: XRP, CurrencyB: EUR, AmountTickSize: 2, PriceTickSize: 4}

Expand All @@ -121,6 +133,7 @@ var (
HT_BTC = CurrencyPair{CurrencyA: HT, CurrencyB: BTC, AmountTickSize: 2, PriceTickSize: 7}
BNB_BTC = CurrencyPair{CurrencyA: BNB, CurrencyB: BTC, AmountTickSize: 2, PriceTickSize: 6}
TRX_BTC = CurrencyPair{CurrencyA: TRX, CurrencyB: BTC, AmountTickSize: 2, PriceTickSize: 7}
DOT_BTC = CurrencyPair{CurrencyA: DOT, CurrencyB: BTC, AmountTickSize: 3, PriceTickSize: 6}

ETC_ETH = CurrencyPair{CurrencyA: ETC, CurrencyB: ETH, AmountTickSize: 2, PriceTickSize: 4}
EOS_ETH = CurrencyPair{CurrencyA: EOS, CurrencyB: ETH, AmountTickSize: 2, PriceTickSize: 4}
Expand Down Expand Up @@ -196,6 +209,8 @@ func NewCurrency(symbol, desc string) Currency {
return BNB
case "trx", "TRX":
return TRX
case "dot", "DOT":
return DOT
default:
return Currency{strings.ToUpper(symbol), desc}
}
Expand Down
7 changes: 6 additions & 1 deletion FutureRestAPI.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ type FutureRestAPI interface {
*/
GetUnfinishFutureOrders(currencyPair CurrencyPair, contractType string) ([]FutureOrder, error)

/**
* 获取个人订单历史,默认获取最近的订单历史列表,返回多少条订单数据,需根据平台接口定义而定
*/
GetFutureOrderHistory(pair CurrencyPair, contractType string, optional ...OptionalParameter) ([]FutureOrder, error)

/**
*获取交易费
*/
Expand All @@ -111,7 +116,7 @@ type FutureRestAPI interface {
/**
* 获取K线数据
*/
GetKlineRecords(contractType string, currency CurrencyPair, period, size, since int) ([]FutureKline, error)
GetKlineRecords(contractType string, currency CurrencyPair, period KlinePeriod, size int, optional ...OptionalParameter) ([]FutureKline, error)

/**
* 获取Trade数据
Expand Down
7 changes: 4 additions & 3 deletions HttpUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/nntaoli-project/goex/internal/logger"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/nntaoli-project/goex/internal/logger"
"github.com/valyala/fasthttp"
"github.com/valyala/fasthttp/fasthttpproxy"
)

var (
Expand Down
35 changes: 35 additions & 0 deletions Models.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package goex

import (
"fmt"
"net/http"
"time"
)
Expand Down Expand Up @@ -261,3 +262,37 @@ type DepositWithdrawHistory struct {
Status int `json:"status,string"`
Timestamp time.Time `json:"timestamp"`
}

type OptionalParameter map[string]interface{}

func (optional OptionalParameter) Optional(name string, value interface{}) OptionalParameter {
optional[name] = value
return optional
}

func (optional OptionalParameter) GetString(name string) string {
return fmt.Sprint(optional[name])
}

func (optional OptionalParameter) GetInt(name string) int {
return ToInt(optional[name])
}

func (optional OptionalParameter) GetInt64(name string) int64 {
return ToInt64(optional[name])
}

func (optional OptionalParameter) GetFloat64(name string) float64 {
return ToFloat64(optional[name])
}

func (optional OptionalParameter) GetTime(name string) *time.Time {
val := optional["name"]
if val != nil {
t, ok := val.(time.Time)
if ok {
return &t
}
}
return nil
}
9 changes: 9 additions & 0 deletions Utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ func ValuesToJson(v url.Values) ([]byte, error) {
return json.Marshal(parammap)
}

func MergeOptionalParameter(values *url.Values, opts ...OptionalParameter) url.Values {
for _, opt := range opts {
for k, v := range opt {
values.Set(k, fmt.Sprint(v))
}
}
return *values
}

func GzipDecompress(data []byte) ([]byte, error) {
r, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion allcoin/allcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ func (ac *Allcoin) GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, e
panic("not implements")
}

func (ac *Allcoin) GetOrderHistorys(currency CurrencyPair, currentPage, pageSize int) ([]Order, error) {
func (ac *Allcoin) GetOrderHistorys(currency CurrencyPair, opt ...OptionalParameter) ([]Order, error) {
panic("not implements")
}
func (ba *Allcoin) adaptCurrencyPair(pair CurrencyPair) CurrencyPair {
Expand Down
15 changes: 7 additions & 8 deletions atop/atop.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const (
Withdrawal = "/trade/api/v1/withdraw"
)

var KlinePeriodConverter = map[int]string{
var KlinePeriodConverter = map[KlinePeriod]string{
KLINE_PERIOD_1MIN: "1min",
KLINE_PERIOD_3MIN: "3min",
KLINE_PERIOD_5MIN: "5min",
Expand Down Expand Up @@ -472,13 +472,13 @@ func (at *Atop) GetUnfinishOrders(currencyPair CurrencyPair) ([]Order, error) {
}

//hao
func (at *Atop) GetKlineRecords(currency CurrencyPair, period, size, since int) ([]Kline, error) {
func (at *Atop) GetKlineRecords(currency CurrencyPair, period KlinePeriod, size int, opt ...OptionalParameter) ([]Kline, error) {
pair := at.adaptCurrencyPair(currency)
params := url.Values{}
params.Set("market", pair.ToLower().String())
//params.Set("type", "1min") //1min,5min,15min,30min,1hour,6hour,1day,7day,30day
params.Set("type", KlinePeriodConverter[period]) //1min,5min,15min,30min,1hour,6hour,1day,7day,30day
params.Set("since", fmt.Sprintf("%d", size)) //The first time is 0, followed by the value of the response since
MergeOptionalParameter(&params, opt...)

klineUrl := ApiBaseUrl + GetKLine + "?" + params.Encode()
kLines, err := HttpGet(at.httpClient, klineUrl)
Expand Down Expand Up @@ -544,18 +544,17 @@ func (at *Atop) GetTrades(currencyPair CurrencyPair, since int64) ([]Trade, erro
return trades, nil
}

func (at *Atop) GetOrderHistorys(currency CurrencyPair, currentPage, pageSize int) ([]Order, error) {
func (at *Atop) GetOrderHistorys(currency CurrencyPair, optional ...OptionalParameter) ([]Order, error) {
//panic("not support")
pair := at.adaptCurrencyPair(currency)
path := ApiBaseUrl + GetHistorys
params := url.Values{}
params.Set("market", pair.ToLower().String())
//params.Set("type", "1")
//params.Set("status", "0")
params.Set("page", fmt.Sprint(currentPage))
params.Set("pageSize", fmt.Sprint(pageSize))

MergeOptionalParameter(&params, optional...)

at.buildPostForm(&params)

resp, err := HttpPostForm(at.httpClient, path, params)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions bigone/Bigone.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ func (bo *Bigone) GetOneOrder(orderId string, currencyPair goex.CurrencyPair) (*
func (bo *Bigone) GetUnfinishOrders(currencyPair goex.CurrencyPair) ([]goex.Order, error) {
return bo.getOrdersList(currencyPair, -1, goex.ORDER_UNFINISH)
}
func (bo *Bigone) GetOrderHistorys(currencyPair goex.CurrencyPair, currentPage, pageSize int) ([]goex.Order, error) {
func (bo *Bigone) GetOrderHistorys(currencyPair goex.CurrencyPair, opt ...goex.OptionalParameter) ([]goex.Order, error) {
return bo.getOrdersList(currencyPair, -1, goex.ORDER_FINISH)
}

Expand Down Expand Up @@ -471,7 +471,7 @@ func (bo *Bigone) GetDepth(size int, currencyPair goex.CurrencyPair) (*goex.Dept
return depth, nil
}

func (bo *Bigone) GetKlineRecords(currency goex.CurrencyPair, period, size, since int) ([]goex.Kline, error) {
func (bo *Bigone) GetKlineRecords(currency goex.CurrencyPair, period goex.KlinePeriod, size int, opt ...goex.OptionalParameter) ([]goex.Kline, error) {
panic("not implements")
}

Expand Down
2 changes: 1 addition & 1 deletion bigone/BigoneV3.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (bo *BigoneV3) GetOneOrder(orderId string, currencyPair goex.CurrencyPair)
func (bo *BigoneV3) GetUnfinishOrders(currencyPair goex.CurrencyPair) ([]goex.Order, error) {
return bo.getOrdersList(currencyPair, 200, goex.ORDER_UNFINISH)
}
func (bo *BigoneV3) GetOrderHistorys(currencyPair goex.CurrencyPair, currentPage, pageSize int) ([]goex.Order, error) {
func (bo *BigoneV3) GetOrderHistorys(currencyPair goex.CurrencyPair, opt goex.OptionalParameter) ([]goex.Order, error) {
return bo.getOrdersList(currencyPair, 200, goex.ORDER_FINISH)
}

Expand Down
2 changes: 1 addition & 1 deletion bigone/Bigone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestBigone_GetUnfinishOrders(t *testing.T) {
func TestBigone_GetOrderHistorys(t *testing.T) {
return
TCT_BTC := NewCurrencyPair2("TCT_BTC")
t.Log(bo.GetOrderHistorys(TCT_BTC, 1, 1))
t.Log(bo.GetOrderHistorys(TCT_BTC))
}
func TestBigone_LimitSell(t *testing.T) {
return
Expand Down
66 changes: 66 additions & 0 deletions binance/Adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package binance

import (
"fmt"
"github.com/nntaoli-project/goex"
"strings"
)

func adaptStreamToCurrencyPair(stream string) goex.CurrencyPair {
symbol := strings.Split(stream, "@")[0]

if strings.HasSuffix(symbol, "usdt") {
return goex.NewCurrencyPair2(fmt.Sprintf("%s_usdt", strings.TrimSuffix(symbol, "usdt")))
}

if strings.HasSuffix(symbol, "usd") {
return goex.NewCurrencyPair2(fmt.Sprintf("%s_usd", strings.TrimSuffix(symbol, "usd")))
}

if strings.HasSuffix(symbol, "btc") {
return goex.NewCurrencyPair2(fmt.Sprintf("%s_btc", strings.TrimSuffix(symbol, "btc")))
}

return goex.UNKNOWN_PAIR
}

func adaptSymbolToCurrencyPair(symbol string) goex.CurrencyPair {
symbol = strings.ToUpper(symbol)

if strings.HasSuffix(symbol, "USD") {
return goex.NewCurrencyPair2(fmt.Sprintf("%s_USD", strings.TrimSuffix(symbol, "USD")))
}

if strings.HasSuffix(symbol, "USDT") {
return goex.NewCurrencyPair2(fmt.Sprintf("%s_USDT", strings.TrimSuffix(symbol, "USDT")))
}

if strings.HasSuffix(symbol, "PAX") {
return goex.NewCurrencyPair2(fmt.Sprintf("%s_PAX", strings.TrimSuffix(symbol, "PAX")))
}

if strings.HasSuffix(symbol, "BTC") {
return goex.NewCurrencyPair2(fmt.Sprintf("%s_BTC", strings.TrimSuffix(symbol, "BTC")))
}

return goex.UNKNOWN_PAIR
}

func adaptOrderStatus(status string) goex.TradeStatus {
var tradeStatus goex.TradeStatus
switch status {
case "NEW":
tradeStatus = goex.ORDER_UNFINISH
case "FILLED":
tradeStatus = goex.ORDER_FINISH
case "PARTIALLY_FILLED":
tradeStatus = goex.ORDER_PART_FINISH
case "CANCELED":
tradeStatus = goex.ORDER_CANCEL
case "PENDING_CANCEL":
tradeStatus = goex.ORDER_CANCEL_ING
case "REJECTED":
tradeStatus = goex.ORDER_REJECT
}
return tradeStatus
}
Loading

0 comments on commit 466bffc

Please sign in to comment.