Adds GetTickerPrice(currency string) TickerPrice func to interface. Implements on all exchanges (where possible)

Attempts to make routing generic with /exchanges/{exchangeName}/latest/{currency} path and response
This commit is contained in:
Scott
2016-06-05 19:10:32 +10:00
parent 4b2a336e1b
commit 95cfb356ba
21 changed files with 244 additions and 28 deletions

View File

@@ -203,6 +203,19 @@ func (a *Alphapoint) GetTicker(symbol string) (AlphapointTicker, error) {
return response, nil
}
func (a *Alphapoint) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := a.GetTicker(currency)
if err != nil {
log.Println(err)
return TickerPrice{}
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
return tickerPrice
}
func (a *Alphapoint) GetTrades(symbol string, startIndex, count int) (AlphapointTrades, error) {
request := make(map[string]interface{})
request["ins"] = symbol

View File

@@ -188,6 +188,15 @@ func (a *ANX) GetTicker(currency string) ANXTicker {
return ticker
}
func (a *ANX) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker := a.GetTicker(currency)
tickerPrice.Ask = ticker.Data.Buy.Value
tickerPrice.Bid = ticker.Data.Sell.Value
return tickerPrice
}
func (a *ANX) GetAPIKey(username, password, otp, deviceID string) (string, string) {
request := make(map[string]interface{})
request["nonce"] = strconv.FormatInt(time.Now().UnixNano(), 10)[0:13]

View File

@@ -1,27 +0,0 @@
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
)
func getLatestAnxTicker(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
currency := vars["currency"]
response := bot.exchange.anx.GetTicker(currency)
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
panic(err)
}
}
var anxRoutes = Routes{
Route{
"Index",
"GET",
"/exchanges/anx/latest/{currency}",
getLatestAnxTicker,
},
}

View File

@@ -284,6 +284,20 @@ func (b *Bitfinex) GetTicker(symbol string, values url.Values) (BitfinexTicker,
return response, nil
}
func (b *Bitfinex) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := b.GetTicker(currency, nil)
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
return tickerPrice
}
type BitfinexLendbookBidAsk struct {
Rate float64 `json:"rate,string"`
Amount float64 `json:"amount,string"`

View File

@@ -234,6 +234,19 @@ func (b *Bitstamp) GetTicker(hourly bool) (BitstampTicker, error) {
return ticker, nil
}
func (b *Bitstamp) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := b.GetTicker(true)
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
return tickerPrice
}
func (b *Bitstamp) GetOrderbook() (BitstampOrderbook, error) {
type response struct {
Timestamp int64 `json:"timestamp,string"`

View File

@@ -134,6 +134,19 @@ func (b *BrightonPeak) GetTicker(symbol string) (AlphapointTicker, error) {
return b.API.GetTicker(symbol)
}
func (b *BrightonPeak) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := b.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
return tickerPrice
}
func (b *BrightonPeak) GetTrades(symbol string, startIndex, count int) (AlphapointTrades, error) {
return b.API.GetTrades(symbol, startIndex, count)
}

View File

@@ -272,6 +272,15 @@ func (b *BTCC) GetTicker(symbol string) BTCCTicker {
return resp.Ticker
}
func (b *BTCC) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker := b.GetTicker(currency)
tickerPrice.Ask = ticker.Sell
tickerPrice.Bid = ticker.Buy
return tickerPrice
}
func (b *BTCC) GetTradesLast24h(symbol string) bool {
req := fmt.Sprintf("%sdata/trades?market=%s", BTCC_API_URL, symbol)
err := SendHTTPGetRequest(req, true, nil)

View File

@@ -185,6 +185,21 @@ func (b *BTCE) GetTicker(symbol string) (map[string]BTCeTicker, error) {
return response.Data, nil
}
/// This getticker is different, so I'll let someone else implement....
/// Or I will, just later...
func (b *BTCE) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
/*ticker, err:= b.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker.Buy
tickerPrice.Bid = ticker.Sell
*/
return tickerPrice
}
func (b *BTCE) GetDepth(symbol string) {
type Response struct {
Data map[string]BTCEOrderbook

View File

@@ -188,6 +188,19 @@ func (b *BTCMarkets) GetTicker(symbol string) (BTCMarketsTicker, error) {
return ticker, nil
}
func (b *BTCMarkets) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := b.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker.BestAsk
tickerPrice.Bid = ticker.BestBID
return tickerPrice
}
func (b *BTCMarkets) GetOrderbook(symbol string) (BTCMarketsOrderbook, error) {
orderbook := BTCMarketsOrderbook{}
path := fmt.Sprintf("/market/%s/AUD/orderbook", symbol)

View File

@@ -359,6 +359,18 @@ func (c *Coinbase) GetTicker(symbol string) (CoinbaseTicker, error) {
return ticker, nil
}
func (c *Coinbase) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := c.GetTicker(currency)
if err != nil {
log.Println(err)
return TickerPrice{}
}
tickerPrice.Ask = ticker.Price
return tickerPrice
}
func (c *Coinbase) GetTrades(symbol string) ([]CoinbaseTrade, error) {
trades := []CoinbaseTrade{}
path := fmt.Sprintf("%s/%s/%s", COINBASE_API_URL+COINBASE_PRODUCTS, symbol, COINBASE_TRADES)

View File

@@ -186,6 +186,13 @@ func (g *Gemini) Run() {
}
}
/// Once GetTicker is created, then add in GetTickerPrice code plz - Scott
func (g *Gemini) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
return tickerPrice
}
func (g *Gemini) GetSymbols() ([]string, error) {
symbols := []string{}
path := fmt.Sprintf("%s/v%s/%s", GEMINI_API_URL, GEMINI_API_VERSION, GEMINI_SYMBOLS)

View File

@@ -132,6 +132,15 @@ func (h *HUOBI) GetTicker(symbol string) HuobiTicker {
return resp.Ticker
}
func (h *HUOBI) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker := h.GetTicker(currency)
tickerPrice.Ask = ticker.Sell
tickerPrice.Bid = ticker.Buy
return tickerPrice
}
func (h *HUOBI) GetOrderBook(symbol string) bool {
path := fmt.Sprintf("http://market.huobi.com/staticmarket/depth_%s_json.js", symbol)
err := SendHTTPGetRequest(path, true, nil)

View File

@@ -6,4 +6,6 @@ type IBotExchange interface {
SetDefaults()
GetName() string
IsEnabled() bool
GetTickerPrice(currency string) TickerPrice
}

View File

@@ -134,6 +134,16 @@ func (i *ItBit) GetTicker(currency string) ItBitTicker {
return itbitTicker
}
func (i *ItBit) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker := i.GetTicker(currency)
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
return tickerPrice
}
type ItbitOrderbookEntry struct {
Quantitiy float64 `json:"quantity,string"`
Price float64 `json:"price,string"`

View File

@@ -229,6 +229,21 @@ func (k *Kraken) GetTicker(symbol string) error {
return nil
}
//This will return the TickerPrice struct when tickers are completed here..
func (k *Kraken) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
/*
ticker, err := i.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
*/
return tickerPrice
}
func (k *Kraken) GetOHLC(symbol string) error {
values := url.Values{}
values.Set("pair", symbol)

View File

@@ -148,6 +148,21 @@ func (l *LakeBTC) GetTicker() LakeBTCTickerResponse {
return response
}
func (l *LakeBTC) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker := l.GetTicker()
if(currency == "USD") {
tickerPrice.Ask = ticker.USD.Ask
tickerPrice.Bid = ticker.USD.Bid
} else if(currency =="CNY") {
tickerPrice.Ask = ticker.CNY.Ask
tickerPrice.Bid = ticker.CNY.Bid
}
return tickerPrice
}
func (l *LakeBTC) GetOrderBook(currency string) bool {
req := LAKEBTC_ORDERBOOK
if currency == "CNY" {

View File

@@ -136,6 +136,18 @@ func (l *LocalBitcoins) GetTicker() (map[string]LocalBitcoinsTicker, error) {
return result, nil
}
func (l *LocalBitcoins) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := l.GetTicker()
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker[currency].Rates.Last
return tickerPrice
}
type LocalBitcoinsTrade struct {
TID int64 `json:"tid"`
Date int64 `json:"date"`

View File

@@ -322,6 +322,19 @@ func (o *OKCoin) GetTicker(symbol string) (OKCoinTicker, error) {
return resp.Ticker, nil
}
func (o *OKCoin) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := o.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker.Sell
tickerPrice.Bid = ticker.Buy
return tickerPrice
}
func (o *OKCoin) GetOrderBook(symbol string, size int64, merge bool) (OKCoinOrderbook, error) {
resp := OKCoinOrderbook{}
vals := url.Values{}

View File

@@ -161,6 +161,19 @@ func (p *Poloniex) GetTicker() (map[string]PoloniexTicker, error) {
return resp.Data, nil
}
func (p *Poloniex) GetTickerPrice(currency string) TickerPrice {
var tickerPrice TickerPrice
ticker, err := p.GetTicker()
if err != nil {
log.Println(err)
return tickerPrice
}
tickerPrice.Ask = ticker[currency].Last
tickerPrice.Bid = ticker[currency].HighestBid
return tickerPrice
}
func (p *Poloniex) GetVolume() (interface{}, error) {
var resp interface{}
path := fmt.Sprintf("%s/public?command=return24hVolume", POLONIEX_API_URL)

View File

@@ -8,7 +8,7 @@ import (
func NewRouter(exchanges []IBotExchange) *mux.Router {
router := mux.NewRouter().StrictSlash(true)
allRoutes := append(routes,anxRoutes...)
allRoutes := append(routes,exchangeRoutes...)
for _, route := range allRoutes {
var handler http.Handler
handler = route.HandlerFunc

36
tickerRoutes.go Normal file
View File

@@ -0,0 +1,36 @@
package main
import (
"encoding/json"
"net/http"
"github.com/gorilla/mux"
)
func jsonTickerResponse(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
currency := vars["currency"]
exchangeName := vars["exchangeName"]
var response TickerPrice;
for i := 0; i < len(bot.exchanges); i++ {
if bot.exchanges[i] != nil {
if(bot.exchanges[i].IsEnabled() && bot.exchanges[i].GetName() == exchangeName) {
response = bot.exchanges[i].GetTickerPrice(currency)
}
}
}
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(response); err != nil {
panic(err)
}
}
var exchangeRoutes = Routes{
Route{
"Index",
"GET",
"/exchanges/{exchangeName}/latest/{currency}",
jsonTickerResponse,
},
}