Skip to content

Commit

Permalink
Merge pull request #2 from nntaoli-project/dev
Browse files Browse the repository at this point in the history
Merge from org source
  • Loading branch information
huzhihao2002 authored Aug 10, 2017
2 parents 51754ba + 1dfd20b commit 5c9841b
Show file tree
Hide file tree
Showing 18 changed files with 283 additions and 734 deletions.
613 changes: 9 additions & 604 deletions Const.go

Large diffs are not rendered by default.

82 changes: 82 additions & 0 deletions CurrencyPair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package goex

import "strings"

type Currency struct {
Symbol string
Desc string
}

func (c Currency) String() string {
return c.Symbol
}

// A->B(A兑换为B)
type CurrencyPair struct {
CurrencyA Currency
CurrencyB Currency
}

var (
UNKNOWN = Currency{"UNKNOWN", ""}
CNY = Currency{"CNY", "rmb (China Yuan)"}
USD = Currency{"USD", "USA dollar"}
BTC = Currency{"BTC", "bitcoin.org"}
BCC = Currency{"BCC", "bitcoin-abc"}
LTC = Currency{"LTC", "litecoin.org"}
ETH = Currency{"ETH", ""}
ETC = Currency{"ETC", ""}
EOS = Currency{"EOS", ""}
BTS = Currency{"BTS", ""}
QTUM = Currency{"QTUM", ""}
SC = Currency{"SC", "sia.tech"}
ANS = Currency{"ANS", "www.antshares.org"}
ZEC = Currency{"ZEC", ""}

//currency pair

BTC_CNY = CurrencyPair{BTC, CNY}
LTC_CNY = CurrencyPair{LTC, CNY}
BCC_CNY = CurrencyPair{BCC, CNY}
ETH_CNY = CurrencyPair{ETH, CNY}
ETC_CNY = CurrencyPair{ETC, CNY}
EOS_CNY = CurrencyPair{EOS, CNY}
BTS_CNY = CurrencyPair{BTS, CNY}
QTUM_CNY = CurrencyPair{QTUM, CNY}
SC_CNY = CurrencyPair{SC, CNY}
ANS_CNY = CurrencyPair{ANS, CNY}
ZEC_CNY = CurrencyPair{ZEC, CNY}

BTC_USD = CurrencyPair{BTC, USD}
LTC_USD = CurrencyPair{LTC, USD}
ETH_USD = CurrencyPair{ETH, USD}
ETC_USD = CurrencyPair{ETC, USD}

LTC_BTC = CurrencyPair{LTC, BTC}
ETH_BTC = CurrencyPair{ETH, BTC}
ETC_BTC = CurrencyPair{ETC, BTC}
BCC_BTC = CurrencyPair{BCC, BTC}

ETC_ETH = CurrencyPair{ETC, ETH}
EOS_ETH = CurrencyPair{EOS, ETH}
)

func (c CurrencyPair) String() string {
return c.ToSymbol("_")
}

func NewCurrency(symbol, desc string) Currency {
return Currency{symbol, desc}
}

func NewCurrencyPair(currencyA Currency, currencyB Currency) CurrencyPair {
return CurrencyPair{currencyA, currencyB}
}

func (pair CurrencyPair) ToSymbol(joinChar string) string {
return strings.Join([]string{pair.CurrencyA.Symbol, pair.CurrencyB.Symbol}, joinChar)
}

func (pair CurrencyPair) ToSymbol2(joinChar string) string {
return strings.Join([]string{pair.CurrencyB.Symbol, pair.CurrencyA.Symbol}, joinChar)
}
19 changes: 19 additions & 0 deletions CurrencyPair_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package goex

import (
"strings"
"testing"
)

func TestCurrency2_String(t *testing.T) {
btc := NewCurrency("btc", "bitcoin")
ltc := NewCurrency("ltc", "litecoin")
t.Log(btc)
t.Log(ltc)
}

func TestCurrencyPair2_String(t *testing.T) {
btccny := NewCurrencyPair(NewCurrency("btc", ""), NewCurrency("cny", ""))
t.Log(strings.ToUpper(btccny.String()))
t.Log(BTC_CNY)
}
16 changes: 16 additions & 0 deletions Utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package goex

import "strconv"

func ToFloat64(v interface{}) float64 {
switch v.(type) {
case float64:
return v.(float64)
case string:
vStr, _ := v.(string)
vF, _ := strconv.ParseFloat(vStr, 64)
return vF
default:
panic("to float64 error.")
}
}
85 changes: 35 additions & 50 deletions bitfinex/bitfinex.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package bitfinex

import (
"errors"
"fmt"
. "github.com/nntaoli-project/GoEx"
"net/http"
"strconv"
// "strings"
"log"
"errors"
"encoding/json"
"strings"
)

type Bitfinex struct {
Expand All @@ -19,19 +16,10 @@ type Bitfinex struct {

const (
EXCHANGE_NAME = "bitfinex.com"

BASE_URL = "https://api.bitfinex.com/v1"
)

var CURRENCYPAIR_TO_SYMBOL = map[CurrencyPair]string{
BTC_USD: "btcusd",
LTC_USD: "ltcusd",
LTC_BTC: "ltcbtc",
ETH_BTC: "ethbtc",
ETC_BTC: "etcbtc",
ETC_USD: "etcusd",
}

func New(client *http.Client, accessKey, secretKey string) *Bitfinex {
return &Bitfinex{client, accessKey, secretKey}
}
Expand All @@ -40,42 +28,35 @@ func (bfx *Bitfinex) GetExchangeName() string {
return EXCHANGE_NAME
}

func (bfx *Bitfinex) GetTicker(currency CurrencyPair) (*Ticker, error) {
func (bfx *Bitfinex) GetTicker(currencyPair CurrencyPair) (*Ticker, error) {
//pubticker
cur := currency.DeleteUnderLineString()
if cur == "nil" {
log.Println("Unsupport The CurrencyPair")
return nil, errors.New("Unsupport The CurrencyPair")
}
apiUrl := fmt.Sprintf("%s/pubticker/%s", BASE_URL, cur)
apiUrl := fmt.Sprintf("%s/pubticker/%s", BASE_URL, bfx.currencyPairToSymbol(currencyPair))
resp, err := HttpGet(bfx.httpClient, apiUrl)
if err != nil {
return nil, err
}
str, _ := json.Marshal(resp)
// fmt.Println("str:", string(str))
if string(str) == "{\"error\":\"ERR_RATE_LIMIT\"}" {
fmt.Println("err", "{\"error\":\"ERR_RATE_LIMIT\"}")
return nil, errors.New("ERR_RATE_LIMIT")

if resp["error"] != nil {
return nil, errors.New(resp["error"].(string))
}

//fmt.Println(resp)
ticker := new(Ticker)
ticker.Last, _ = strconv.ParseFloat(resp["last_price"].(string), 64)
ticker.Vol, _ = strconv.ParseFloat(resp["volume"].(string), 64)
ticker.High, _ = strconv.ParseFloat(resp["high"].(string), 64)
ticker.Low, _ = strconv.ParseFloat(resp["low"].(string), 64)
ticker.Sell, _ = strconv.ParseFloat(resp["ask"].(string), 64)
ticker.Buy, _ = strconv.ParseFloat(resp["bid"].(string), 64)
date, _ := strconv.ParseFloat(resp["timestamp"].(string), 64)
ticker.Date = uint64(date)
ticker.Last = ToFloat64(resp["last_price"])
ticker.Vol = ToFloat64(resp["volume"])
ticker.High = ToFloat64(resp["high"])
ticker.Low = ToFloat64(resp["low"])
ticker.Sell = ToFloat64(resp["ask"])
ticker.Buy = ToFloat64(resp["bid"])
ticker.Date = uint64(ToFloat64(resp["timestamp"]))
//dateStr := resp["timestamp"].(string)
//dataMeta := strings.Split(dateStr, ".")
//ticker.Date, _ = strconv.ParseUint(dataMeta[0], 10, 64)
return ticker, nil
}

func (bfx *Bitfinex) GetDepth(size int, currency CurrencyPair) (*Depth, error) {
apiUrl := fmt.Sprintf("%s/book/%s?limit_bids=%d&limit_asks=%d", BASE_URL, currency.DeleteUnderLineString(), size, size)
func (bfx *Bitfinex) GetDepth(size int, currencyPair CurrencyPair) (*Depth, error) {
apiUrl := fmt.Sprintf("%s/book/%s?limit_bids=%d&limit_asks=%d", BASE_URL, bfx.currencyPairToSymbol(currencyPair), size, size)
resp, err := HttpGet(bfx.httpClient, apiUrl)
if err != nil {
return nil, err
Expand All @@ -88,24 +69,24 @@ func (bfx *Bitfinex) GetDepth(size int, currency CurrencyPair) (*Depth, error) {

for _, bid := range bids {
_bid := bid.(map[string]interface{})
amount, _ := strconv.ParseFloat(_bid["amount"].(string), 64)
price, _ := strconv.ParseFloat(_bid["price"].(string), 64)
amount := ToFloat64(_bid["amount"])
price := ToFloat64(_bid["price"])
dr := DepthRecord{Amount: amount, Price: price}
depth.BidList = append(depth.BidList, dr)
}

for _, ask := range asks {
_ask := ask.(map[string]interface{})
amount, _ := strconv.ParseFloat(_ask["amount"].(string), 64)
price, _ := strconv.ParseFloat(_ask["price"].(string), 64)
amount := ToFloat64(_ask["amount"])
price := ToFloat64(_ask["price"])
dr := DepthRecord{Amount: amount, Price: price}
depth.AskList = append(depth.AskList, dr)
}

return depth, nil
}

func (bfx *Bitfinex) GetKlineRecords(currency CurrencyPair, period string, size, since int) ([]Kline, error) {
func (bfx *Bitfinex) GetKlineRecords(currencyPair CurrencyPair, period string, size, since int) ([]Kline, error) {
panic("not implement")
}

Expand All @@ -119,34 +100,38 @@ func (bfx *Bitfinex) GetAccount() (*Account, error) {
return nil, nil
}

func (bfx *Bitfinex) LimitBuy(amount, price string, currency CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) LimitBuy(amount, price string, currencyPair CurrencyPair) (*Order, error) {
return nil, nil
}

func (bfx *Bitfinex) LimitSell(amount, price string, currency CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) LimitSell(amount, price string, currencyPair CurrencyPair) (*Order, error) {
return nil, nil
}

func (bfx *Bitfinex) MarketBuy(amount, price string, currency CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) MarketBuy(amount, price string, currencyPair CurrencyPair) (*Order, error) {
panic("not implement.")
}

func (bfx *Bitfinex) MarketSell(amount, price string, currency CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) MarketSell(amount, price string, currencyPair CurrencyPair) (*Order, error) {
panic("not implement.")
}

func (bfx *Bitfinex) CancelOrder(orderId string, currency CurrencyPair) (bool, error) {
func (bfx *Bitfinex) CancelOrder(orderId string, currencyPair CurrencyPair) (bool, error) {
return false, nil
}

func (bfx *Bitfinex) GetOneOrder(orderId string, currency CurrencyPair) (*Order, error) {
func (bfx *Bitfinex) GetOneOrder(orderId string, currencyPair CurrencyPair) (*Order, error) {
return nil, nil
}

func (bfx *Bitfinex) GetUnfinishOrders(currency CurrencyPair) ([]Order, error) {
func (bfx *Bitfinex) GetUnfinishOrders(currencyPair CurrencyPair) ([]Order, error) {
return nil, nil
}

func (bfx *Bitfinex) GetOrderHistorys(currency CurrencyPair, currentPage, pageSize int) ([]Order, error) {
func (bfx *Bitfinex) GetOrderHistorys(currencyPair CurrencyPair, currentPage, pageSize int) ([]Order, error) {
return nil, nil
}

func (bfx *Bitfinex) currencyPairToSymbol(currencyPair CurrencyPair) string {
return strings.ToLower(currencyPair.ToSymbol(""))
}
Loading

0 comments on commit 5c9841b

Please sign in to comment.