Add Ticker storage functionality for Exchanges (HTTP REST only)

This commit is contained in:
Adrian Gallagher
2017-02-20 10:38:04 +11:00
parent 3de08b76e4
commit e7148f174b
28 changed files with 456 additions and 326 deletions

View File

@@ -169,37 +169,50 @@ func (a *ANX) Run() {
for _, x := range a.EnabledPairs {
currency := x
go func() {
ticker := a.GetTicker(currency)
log.Printf("ANX %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Data.Last.Value, ticker.Data.High.Value, ticker.Data.Low.Value, ticker.Data.Vol.Value)
AddExchangeInfo(a.GetName(), currency[0:3], currency[3:], ticker.Data.Last.Value, ticker.Data.Vol.Value)
ticker, err := a.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
}
log.Printf("ANX %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
AddExchangeInfo(a.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume)
}()
}
time.Sleep(time.Second * a.RESTPollingDelay)
}
}
func (a *ANX) GetTicker(currency string) ANXTicker {
func (a *ANX) GetTicker(currency string) (ANXTicker, error) {
var ticker ANXTicker
err := SendHTTPGetRequest(fmt.Sprintf("%sapi/2/%s/%s", ANX_API_URL, currency, ANX_TICKER), true, &ticker)
if err != nil {
log.Println(err)
return ANXTicker{}
return ANXTicker{}, err
}
return ticker
return ticker, nil
}
func (a *ANX) GetTickerPrice(currency string) TickerPrice {
func (a *ANX) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(a.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker := a.GetTicker(currency)
ticker, err := a.GetTicker(currency)
if err != nil {
return tickerPrice, err
}
tickerPrice.Ask = ticker.Data.Buy.Value
tickerPrice.Bid = ticker.Data.Sell.Value
tickerPrice.CryptoCurrency = currency
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.Low = ticker.Data.Low.Value
tickerPrice.Last = ticker.Data.Last.Value
tickerPrice.Volume = ticker.Data.Vol.Value
tickerPrice.High = ticker.Data.High.Value
return tickerPrice
ProcessTicker(a.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (a *ANX) GetEnabledCurrencies() []string {

View File

@@ -266,9 +266,8 @@ func (b *Bitfinex) Run() {
for _, x := range b.EnabledPairs {
currency := x
go func() {
ticker, err := b.GetTicker(currency, nil)
ticker, err := b.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
}
log.Printf("Bitfinex %s Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
@@ -289,22 +288,27 @@ 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
func (b *Bitfinex) GetTickerPrice(currency string) (TickerPrice, error) {
ticker, err := GetTicker(b.GetName(), currency[0:3], currency[3:])
if err == nil {
return ticker, nil
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
tickerPrice.CryptoCurrency = currency
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Volume
tickerPrice.High = ticker.High
return tickerPrice
var tickerPrice TickerPrice
tickerNew, err := b.GetTicker(currency, nil)
if err != nil {
return tickerPrice, err
}
tickerPrice.Ask = tickerNew.Ask
tickerPrice.Bid = tickerNew.Bid
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.Low = tickerNew.Low
tickerPrice.Last = tickerNew.Last
tickerPrice.Volume = tickerNew.Volume
tickerPrice.High = tickerNew.High
ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
type BitfinexLendbookBidAsk struct {

View File

@@ -206,7 +206,7 @@ func (b *Bitstamp) Run() {
for _, x := range b.EnabledPairs {
currency := x
go func() {
ticker, err := b.GetTicker(true)
ticker, err := b.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
@@ -238,22 +238,28 @@ func (b *Bitstamp) GetTicker(hourly bool) (BitstampTicker, error) {
return ticker, nil
}
func (b *Bitstamp) GetTickerPrice(currency string) TickerPrice {
func (b *Bitstamp) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(b.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker, err := b.GetTicker(true)
if err != nil {
log.Println(err)
return tickerPrice
return tickerPrice, err
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
tickerPrice.CryptoCurrency = currency
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[0:3]
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Volume
tickerPrice.High = ticker.High
return tickerPrice
ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (b *Bitstamp) GetOrderbook() (BitstampOrderbook, error) {

View File

@@ -121,7 +121,7 @@ func (b *BrightonPeak) Run() {
for b.Enabled {
for _, x := range b.EnabledPairs {
ticker, err := b.GetTicker(x)
ticker, err := b.GetTickerPrice(x)
if err != nil {
log.Println(err)
@@ -138,22 +138,27 @@ func (b *BrightonPeak) GetTicker(symbol string) (AlphapointTicker, error) {
return b.API.GetTicker(symbol)
}
func (b *BrightonPeak) GetTickerPrice(currency string) TickerPrice {
func (b *BrightonPeak) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(b.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker, err := b.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
return tickerPrice, err
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
tickerPrice.CryptoCurrency = currency
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Volume
tickerPrice.High = ticker.High
return tickerPrice
ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (b *BrightonPeak) GetTrades(symbol string, startIndex, count int) (AlphapointTrades, error) {

View File

@@ -241,27 +241,21 @@ func (b *BTCC) Run() {
for b.Enabled {
for _, x := range b.EnabledPairs {
currency := StringToLower(x)
go func() {
ticker := b.GetTicker(currency)
if currency != "ltcbtc" {
tickerLastUSD, _ := ConvertCurrency(ticker.Last, "CNY", "USD")
tickerHighUSD, _ := ConvertCurrency(ticker.High, "CNY", "USD")
tickerLowUSD, _ := ConvertCurrency(ticker.Low, "CNY", "USD")
log.Printf("BTCC %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", currency, tickerLastUSD, ticker.Last, tickerHighUSD, ticker.High, tickerLowUSD, ticker.Low, ticker.Vol)
AddExchangeInfo(b.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[3:]), ticker.Last, ticker.Vol)
AddExchangeInfo(b.GetName(), StringToUpper(currency[0:3]), "USD", tickerLastUSD, ticker.Vol)
} else {
log.Printf("BTCC %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Vol)
AddExchangeInfo(b.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[3:]), ticker.Last, ticker.Vol)
currency := x
ticker, err := b.GetTickerPrice(StringToLower(currency))
if err != nil {
return
}
log.Printf("BTCC %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume)
}()
}
time.Sleep(time.Second * b.RESTPollingDelay)
}
}
func (b *BTCC) GetTicker(symbol string) BTCCTicker {
func (b *BTCC) GetTicker(symbol string) (BTCCTicker, error) {
type Response struct {
Ticker BTCCTicker
}
@@ -270,24 +264,29 @@ func (b *BTCC) GetTicker(symbol string) BTCCTicker {
req := fmt.Sprintf("%sdata/ticker?market=%s", BTCC_API_URL, symbol)
err := SendHTTPGetRequest(req, true, &resp)
if err != nil {
log.Println(err)
return BTCCTicker{}
return BTCCTicker{}, err
}
return resp.Ticker
return resp.Ticker, nil
}
func (b *BTCC) GetTickerPrice(currency string) TickerPrice {
func (b *BTCC) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(b.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker := b.GetTicker(currency)
ticker, err := b.GetTicker(currency)
tickerPrice.Ask = ticker.Sell
tickerPrice.Bid = ticker.Buy
tickerPrice.CryptoCurrency = currency
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Vol
tickerPrice.High = ticker.High
return tickerPrice
ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (b *BTCC) GetTradesLast24h(symbol string) bool {

View File

@@ -189,17 +189,22 @@ func (b *BTCE) GetTicker(symbol string) (map[string]BTCeTicker, error) {
return response.Data, nil
}
func (b *BTCE) GetTickerPrice(currency string) TickerPrice {
func (b *BTCE) GetTickerPrice(currency string) (TickerPrice, error) {
var tickerPrice TickerPrice
ticker := b.Ticker[currency]
ticker, ok := b.Ticker[currency]
if !ok {
return tickerPrice, errors.New("Unable to get currency.")
}
tickerPrice.Ask = ticker.Buy
tickerPrice.Bid = ticker.Sell
tickerPrice.CryptoCurrency = currency
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Vol_cur
tickerPrice.High = ticker.High
return tickerPrice
ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (b *BTCE) GetDepth(symbol string) {

View File

@@ -164,17 +164,15 @@ func (b *BTCMarkets) Run() {
for _, x := range b.EnabledPairs {
currency := x
go func() {
ticker, err := b.GetTicker(currency)
ticker, err := b.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
}
b.Ticker[currency] = ticker
BTCMarketsLastUSD, _ := ConvertCurrency(ticker.LastPrice, "AUD", "USD")
BTCMarketsBestBidUSD, _ := ConvertCurrency(ticker.BestBID, "AUD", "USD")
BTCMarketsBestAskUSD, _ := ConvertCurrency(ticker.BestAsk, "AUD", "USD")
log.Printf("BTC Markets %s: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", currency, BTCMarketsLastUSD, ticker.LastPrice, BTCMarketsBestBidUSD, ticker.BestBID, BTCMarketsBestAskUSD, ticker.BestAsk)
AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.LastPrice, 0)
BTCMarketsLastUSD, _ := ConvertCurrency(ticker.Last, "AUD", "USD")
BTCMarketsBestBidUSD, _ := ConvertCurrency(ticker.Bid, "AUD", "USD")
BTCMarketsBestAskUSD, _ := ConvertCurrency(ticker.Ask, "AUD", "USD")
log.Printf("BTC Markets %s: Last %f (%f) Bid %f (%f) Ask %f (%f)\n", currency, BTCMarketsLastUSD, ticker.Last, BTCMarketsBestBidUSD, ticker.Bid, BTCMarketsBestAskUSD, ticker.Ask)
AddExchangeInfo(b.GetName(), currency[0:3], currency[3:], ticker.Last, 0)
AddExchangeInfo(b.GetName(), currency[0:3], "USD", BTCMarketsLastUSD, 0)
}()
}
@@ -192,19 +190,24 @@ func (b *BTCMarkets) GetTicker(symbol string) (BTCMarketsTicker, error) {
return ticker, nil
}
func (b *BTCMarkets) GetTickerPrice(currency string) TickerPrice {
func (b *BTCMarkets) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(b.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker, err := b.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
return tickerPrice, err
}
tickerPrice.Ask = ticker.BestAsk
tickerPrice.Bid = ticker.BestBID
tickerPrice.CryptoCurrency = currency
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.Last = ticker.LastPrice
return tickerPrice
ProcessTicker(b.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (b *BTCMarkets) GetOrderbook(symbol string) (BTCMarketsOrderbook, error) {

119
events.go
View File

@@ -31,16 +31,16 @@ type Event struct {
Exchange string
Item string
Condition string
CryptoCurrency string
FiatCurrency string
FirstCurrency string
SecondCurrency string
Action string
Executed bool
}
var Events []*Event
func AddEvent(Exchange, Item, Condition, CryptoCurrency, FiatCurrency, Action string) (int, error) {
err := IsValidEvent(Exchange, Item, Condition, CryptoCurrency, FiatCurrency, Action)
func AddEvent(Exchange, Item, Condition, FirstCurrency, SecondCurrency, Action string) (int, error) {
err := IsValidEvent(Exchange, Item, Condition, Action)
if err != nil {
return 0, err
@@ -57,8 +57,8 @@ func AddEvent(Exchange, Item, Condition, CryptoCurrency, FiatCurrency, Action st
Event.Exchange = Exchange
Event.Item = Item
Event.Condition = Condition
Event.CryptoCurrency = CryptoCurrency
Event.FiatCurrency = FiatCurrency
Event.FirstCurrency = FirstCurrency
Event.SecondCurrency = SecondCurrency
Event.Action = Action
Event.Executed = false
Events = append(Events, Event)
@@ -106,7 +106,7 @@ func (e *Event) ExecuteAction() bool {
func (e *Event) EventToString() string {
condition := SplitStrings(e.Condition, ",")
return fmt.Sprintf("If the %s%s %s on %s is %s then %s.", e.CryptoCurrency, e.FiatCurrency, e.Item, e.Exchange, condition[0]+" "+condition[1], e.Action)
return fmt.Sprintf("If the %s%s %s on %s is %s then %s.", e.FirstCurrency, e.SecondCurrency, e.Item, e.Exchange, condition[0]+" "+condition[1], e.Action)
}
func (e *Event) CheckCondition() bool {
@@ -114,81 +114,13 @@ func (e *Event) CheckCondition() bool {
condition := SplitStrings(e.Condition, ",")
targetPrice, _ := strconv.ParseFloat(condition[1], 64)
/* to-do: add event handling for all currencies and fiat currencies */
if bot.exchange.bitfinex.GetName() == e.Exchange {
result, err := bot.exchange.bitfinex.GetTicker("btcusd", nil)
if err != nil {
lastPrice = 0
} else {
lastPrice = result.Last
}
} else if bot.exchange.bitstamp.GetName() == e.Exchange {
result, err := bot.exchange.bitstamp.GetTicker(false)
if err != nil {
lastPrice = 0
} else {
lastPrice = result.Last
}
} else if bot.exchange.brightonpeak.GetName() == e.Exchange {
result, err := bot.exchange.brightonpeak.GetTicker("BTCUSD")
if err != nil {
lastPrice = 0
} else {
lastPrice = result.Last
}
} else if bot.exchange.gdax.GetName() == e.Exchange {
result, err := bot.exchange.gdax.GetTicker("BTC-USD")
if err != nil {
lastPrice = 0
} else {
lastPrice = result.Price
}
} else if bot.exchange.lakebtc.GetName() == e.Exchange {
lastPrice = bot.exchange.lakebtc.GetTicker().CNY.Last
} else if bot.exchange.localbitcoins.GetName() == e.Exchange {
result, err := bot.exchange.localbitcoins.GetTicker()
if err != nil {
lastPrice = 0
} else {
lastPrice = result["USD"].Rates.Last
}
} else if bot.exchange.btcc.GetName() == e.Exchange {
lastPrice = bot.exchange.btcc.GetTicker("btccny").Last
} else if bot.exchange.huobi.GetName() == e.Exchange {
lastPrice = bot.exchange.huobi.GetTicker("btc").Last
} else if bot.exchange.itbit.GetName() == e.Exchange {
lastPrice = bot.exchange.itbit.GetTicker("XBTUSD").LastPrice
} else if bot.exchange.btce.GetName() == e.Exchange {
lastPrice = bot.exchange.btce.Ticker["btc_usd"].Last
} else if bot.exchange.btcmarkets.GetName() == e.Exchange {
lastPrice = bot.exchange.btcmarkets.Ticker["BTC"].LastPrice
} else if bot.exchange.okcoinChina.GetName() == e.Exchange {
result, err := bot.exchange.okcoinChina.GetTicker("btc_cny")
if err != nil {
lastPrice = 0
} else {
lastPrice = result.Last
}
} else if bot.exchange.okcoinIntl.GetName() == e.Exchange {
result, err := bot.exchange.okcoinIntl.GetTicker("btc_usd")
if err != nil {
lastPrice = 0
} else {
lastPrice = result.Last
}
} else if bot.exchange.anx.GetName() == e.Exchange {
lastPrice = bot.exchange.anx.GetTicker("BTCUSD").Data.Last.Value
} else if bot.exchange.kraken.GetName() == e.Exchange {
lastPrice = bot.exchange.kraken.Ticker["XBTUSD"].Last
} else if bot.exchange.poloniex.GetName() == e.Exchange {
result, err := bot.exchange.poloniex.GetTicker()
if err != nil {
lastPrice = 0
} else {
lastPrice = result["BTC_LTC"].Last
}
ticker, err := GetTickerByExchange(e.Exchange)
if err != nil {
return false
}
lastPrice = ticker.Price[e.FirstCurrency][e.SecondCurrency].Last
if lastPrice == 0 {
return false
}
@@ -228,7 +160,7 @@ func (e *Event) CheckCondition() bool {
return false
}
func IsValidEvent(Exchange, Item, Condition, CryptoCurrency, FiatCurrency, Action string) error {
func IsValidEvent(Exchange, Item, Condition, Action string) error {
if !IsValidExchange(Exchange) {
return ErrExchangeDisabled
}
@@ -237,10 +169,6 @@ func IsValidEvent(Exchange, Item, Condition, CryptoCurrency, FiatCurrency, Actio
return ErrInvalidItem
}
if !IsFiatCurrency(FiatCurrency) {
return ErrFiatCurrencyInvalid
}
if !StringContains(Condition, ",") {
return ErrInvalidCondition
}
@@ -287,23 +215,10 @@ func CheckEvents() {
}
func IsValidExchange(Exchange string) bool {
if bot.exchange.bitfinex.GetName() == Exchange && bot.exchange.bitfinex.IsEnabled() ||
bot.exchange.bitstamp.GetName() == Exchange && bot.exchange.bitstamp.IsEnabled() ||
bot.exchange.brightonpeak.GetName() == Exchange && bot.exchange.brightonpeak.IsEnabled() ||
bot.exchange.btcc.GetName() == Exchange && bot.exchange.btcc.IsEnabled() ||
bot.exchange.btce.GetName() == Exchange && bot.exchange.btce.IsEnabled() ||
bot.exchange.btcmarkets.GetName() == Exchange && bot.exchange.btcmarkets.IsEnabled() ||
bot.exchange.gdax.GetName() == Exchange && bot.exchange.gdax.IsEnabled() ||
bot.exchange.huobi.GetName() == Exchange && bot.exchange.huobi.IsEnabled() ||
bot.exchange.itbit.GetName() == Exchange && bot.exchange.itbit.IsEnabled() ||
bot.exchange.kraken.GetName() == Exchange && bot.exchange.kraken.IsEnabled() ||
bot.exchange.lakebtc.GetName() == Exchange && bot.exchange.lakebtc.IsEnabled() ||
bot.exchange.localbitcoins.GetName() == Exchange && bot.exchange.localbitcoins.IsEnabled() ||
bot.exchange.okcoinChina.GetName() == Exchange && bot.exchange.okcoinChina.IsEnabled() ||
bot.exchange.okcoinIntl.GetName() == Exchange && bot.exchange.okcoinIntl.IsEnabled() ||
bot.exchange.poloniex.GetName() == Exchange && bot.exchange.poloniex.IsEnabled() ||
bot.exchange.anx.GetName() == Exchange && bot.exchange.anx.IsEnabled() {
return true
for x, _ := range bot.exchanges {
if bot.exchanges[x].GetName() == Exchange && bot.exchanges[x].IsEnabled() {
return true
}
}
return false
}

View File

@@ -213,20 +213,14 @@ func (g *GDAX) Run() {
for _, x := range g.EnabledPairs {
currency := x[0:3] + "-" + x[3:]
go func() {
stats, err := g.GetStats(currency)
ticker, err := g.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
}
ticker, err := g.GetTicker(currency)
if err != nil {
log.Println(err)
return
}
log.Printf("GDAX %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Price, stats.High, stats.Low, stats.Volume)
AddExchangeInfo(g.GetName(), currency[0:3], currency[4:], ticker.Price, stats.Volume)
log.Printf("GDAX %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
AddExchangeInfo(g.GetName(), currency[0:3], currency[4:], ticker.Last, ticker.Volume)
}()
}
time.Sleep(time.Second * g.RESTPollingDelay)
@@ -363,18 +357,32 @@ func (g *GDAX) GetTicker(symbol string) (GDAXTicker, error) {
return ticker, nil
}
func (g *GDAX) GetTickerPrice(currency string) TickerPrice {
func (g *GDAX) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(g.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker, err := g.GetTicker(currency)
if err != nil {
log.Println(err)
return TickerPrice{}
return TickerPrice{}, err
}
tickerPrice.Ask = ticker.Price
tickerPrice.CryptoCurrency = currency
tickerPrice.Volume = ticker.Size
return tickerPrice
stats, err := g.GetStats(currency)
if err != nil {
return TickerPrice{}, err
}
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[4:]
tickerPrice.Volume = stats.Volume
tickerPrice.Last = ticker.Price
tickerPrice.High = stats.High
tickerPrice.Low = stats.Low
ProcessTicker(g.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (g *GDAX) GetTrades(symbol string) ([]GDAXTrade, error) {

View File

@@ -180,15 +180,14 @@ func (g *Gemini) Run() {
for g.Enabled {
for _, x := range g.EnabledPairs {
currency := x
log.Println(currency)
go func() {
ticker, err := g.GetTicker(currency)
ticker, err := g.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
}
log.Printf("Gemini %s Last %f Bid %f Ask %f Volume %f\n", currency, ticker.Last, ticker.Bid, ticker.Ask, ticker.Volume.Currency)
AddExchangeInfo(g.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume.Currency)
log.Printf("Gemini %s Last %f Bid %f Ask %f Volume %f\n", currency, ticker.Last, ticker.Bid, ticker.Ask, ticker.Volume)
AddExchangeInfo(g.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume)
}()
}
time.Sleep(time.Second * g.RESTPollingDelay)
@@ -237,19 +236,25 @@ func (g *Gemini) GetTicker(currency string) (GeminiTicker, error) {
return ticker, nil
}
func (g *Gemini) GetTickerPrice(currency string) TickerPrice {
func (g *Gemini) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(g.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker, err := g.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
return tickerPrice, err
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
tickerPrice.CryptoCurrency = currency
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Volume.USD
return tickerPrice
ProcessTicker(g.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (g *Gemini) GetSymbols() ([]string, error) {

View File

@@ -111,43 +111,55 @@ func (h *HUOBI) Run() {
for _, x := range h.EnabledPairs {
currency := StringToLower(x[0:3])
go func() {
ticker := h.GetTicker(currency)
ticker, err := h.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
}
HuobiLastUSD, _ := ConvertCurrency(ticker.Last, "CNY", "USD")
HuobiHighUSD, _ := ConvertCurrency(ticker.High, "CNY", "USD")
HuobiLowUSD, _ := ConvertCurrency(ticker.Low, "CNY", "USD")
log.Printf("Huobi %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", currency, HuobiLastUSD, ticker.Last, HuobiHighUSD, ticker.High, HuobiLowUSD, ticker.Low, ticker.Vol)
AddExchangeInfo(h.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[3:]), ticker.Last, ticker.Vol)
AddExchangeInfo(h.GetName(), StringToUpper(currency[0:3]), "USD", HuobiLastUSD, ticker.Vol)
log.Printf("Huobi %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", currency, HuobiLastUSD, ticker.Last, HuobiHighUSD, ticker.High, HuobiLowUSD, ticker.Low, ticker.Volume)
AddExchangeInfo(h.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[3:]), ticker.Last, ticker.Volume)
AddExchangeInfo(h.GetName(), StringToUpper(currency[0:3]), "USD", HuobiLastUSD, ticker.Volume)
}()
}
time.Sleep(time.Second * h.RESTPollingDelay)
}
}
func (h *HUOBI) GetTicker(symbol string) HuobiTicker {
func (h *HUOBI) GetTicker(symbol string) (HuobiTicker, error) {
resp := HuobiTickerResponse{}
path := fmt.Sprintf("http://api.huobi.com/staticmarket/ticker_%s_json.js", symbol)
err := SendHTTPGetRequest(path, true, &resp)
if err != nil {
log.Println(err)
return HuobiTicker{}
return HuobiTicker{}, err
}
return resp.Ticker
return resp.Ticker, nil
}
func (h *HUOBI) GetTickerPrice(currency string) TickerPrice {
func (h *HUOBI) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(h.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[3:]))
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker := h.GetTicker(currency)
ticker, err := h.GetTicker(currency)
if err != nil {
return tickerPrice, err
}
tickerPrice.Ask = ticker.Sell
tickerPrice.Bid = ticker.Buy
tickerPrice.CryptoCurrency = currency
tickerPrice.FirstCurrency = StringToUpper(currency[0:3])
tickerPrice.SecondCurrency = StringToUpper(currency[3:])
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Vol
tickerPrice.High = ticker.High
return tickerPrice
ProcessTicker(h.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (h *HUOBI) GetOrderBook(symbol string) bool {

View File

@@ -7,7 +7,7 @@ type IBotExchange interface {
SetDefaults()
GetName() string
IsEnabled() bool
GetTickerPrice(currency string) TickerPrice
GetTickerPrice(currency string) (TickerPrice, error)
GetEnabledCurrencies() []string
GetExchangeAccountInfo() (ExchangeAccountInfo, error)
}

View File

@@ -118,34 +118,50 @@ func (i *ItBit) Run() {
for _, x := range i.EnabledPairs {
currency := x
go func() {
ticker := i.GetTicker(currency)
log.Printf("ItBit %s: Last %f High %f Low %f Volume %f\n", currency, ticker.LastPrice, ticker.High24h, ticker.Low24h, ticker.Volume24h)
AddExchangeInfo(i.GetName(), currency[0:3], currency[3:], ticker.LastPrice, ticker.Volume24h)
ticker, err := i.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
}
log.Printf("ItBit %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
AddExchangeInfo(i.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume)
}()
}
time.Sleep(time.Second * i.RESTPollingDelay)
}
}
func (i *ItBit) GetTicker(currency string) ItBitTicker {
func (i *ItBit) GetTicker(currency string) (ItBitTicker, error) {
path := ITBIT_API_URL + "/markets/" + currency + "/ticker"
var itbitTicker ItBitTicker
err := SendHTTPGetRequest(path, true, &itbitTicker)
if err != nil {
log.Println(err)
return ItBitTicker{}
return ItBitTicker{}, err
}
return itbitTicker
return itbitTicker, nil
}
func (i *ItBit) GetTickerPrice(currency string) TickerPrice {
func (i *ItBit) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(i.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker := i.GetTicker(currency)
ticker, err := i.GetTicker(currency)
if err != nil {
return tickerPrice, err
}
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
return tickerPrice
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.High = ticker.High24h
tickerPrice.Low = ticker.Low24h
tickerPrice.Volume = ticker.Volume24h
ProcessTicker(i.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
type ItbitOrderbookEntry struct {

View File

@@ -234,7 +234,7 @@ func (k *Kraken) GetTicker(symbol string) error {
}
//This will return the TickerPrice struct when tickers are completed here..
func (k *Kraken) GetTickerPrice(currency string) TickerPrice {
func (k *Kraken) GetTickerPrice(currency string) (TickerPrice, error) {
var tickerPrice TickerPrice
/*
ticker, err := i.GetTicker(currency)
@@ -245,7 +245,7 @@ func (k *Kraken) GetTickerPrice(currency string) TickerPrice {
tickerPrice.Ask = ticker.Ask
tickerPrice.Bid = ticker.Bid
*/
return tickerPrice
return tickerPrice, nil
}
func (k *Kraken) GetOHLC(symbol string) error {

View File

@@ -128,45 +128,70 @@ func (l *LakeBTC) Run() {
}
for l.Enabled {
ticker := l.GetTicker()
ticker, err := l.GetTickerPrice("BTCUSD")
if err != nil {
log.Println(err)
return
}
ticker, err = l.GetTickerPrice("BTCCNY")
if err != nil {
log.Println(err)
return
}
for _, x := range l.EnabledPairs {
if x == "BTCUSD" {
log.Printf("LakeBTC BTC USD: Last %f High %f Low %f Volume %f\n", ticker.USD.Last, ticker.USD.High, ticker.USD.Low, ticker.USD.Volume)
AddExchangeInfo(l.GetName(), x[0:3], x[3:], ticker.USD.Last, ticker.USD.Volume)
log.Printf("LakeBTC BTC USD: Last %f High %f Low %f Volume %f\n", ticker.Last, ticker.High, ticker.Low, ticker.Volume)
AddExchangeInfo(l.GetName(), x[0:3], x[3:], ticker.Last, ticker.Volume)
} else if x == "BTCCNY" {
log.Printf("LakeBTC BTC CNY: Last %f High %f Low %f Volume %f\n", ticker.CNY.Last, ticker.CNY.High, ticker.CNY.Low, ticker.CNY.Volume)
AddExchangeInfo(l.GetName(), x[0:3], x[3:], ticker.CNY.Last, ticker.CNY.Volume)
log.Printf("LakeBTC BTC CNY: Last %f High %f Low %f Volume %f\n", ticker.Last, ticker.High, ticker.Low, ticker.Volume)
AddExchangeInfo(l.GetName(), x[0:3], x[3:], ticker.Last, ticker.Volume)
}
}
time.Sleep(time.Second * l.RESTPollingDelay)
}
}
func (l *LakeBTC) GetTicker() LakeBTCTickerResponse {
func (l *LakeBTC) GetTicker() (LakeBTCTickerResponse, error) {
response := LakeBTCTickerResponse{}
err := SendHTTPGetRequest(LAKEBTC_API_URL+LAKEBTC_TICKER, true, &response)
if err != nil {
log.Println(err)
return response
return response, err
}
return response
return response, nil
}
func (l *LakeBTC) GetTickerPrice(currency string) TickerPrice {
func (l *LakeBTC) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(l.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker := l.GetTicker()
ticker, err := l.GetTicker()
if err != nil {
return tickerPrice, err
}
if currency == "USD" {
tickerPrice.Ask = ticker.USD.Ask
tickerPrice.Bid = ticker.USD.Bid
tickerPrice.Volume = ticker.USD.Volume
tickerPrice.High = ticker.USD.High
tickerPrice.Low = ticker.USD.Low
tickerPrice.Last = ticker.USD.Last
} else if currency == "CNY" {
tickerPrice.Ask = ticker.CNY.Ask
tickerPrice.Bid = ticker.CNY.Bid
tickerPrice.Volume = ticker.CNY.Volume
tickerPrice.High = ticker.CNY.High
tickerPrice.Low = ticker.CNY.Low
tickerPrice.Last = ticker.CNY.Last
}
tickerPrice.CryptoCurrency = currency
return tickerPrice
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
ProcessTicker(l.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (l *LakeBTC) GetOrderBook(currency string) bool {

View File

@@ -97,7 +97,7 @@ func (l *LocalBitcoins) Run() {
}
for l.Enabled {
ticker, err := l.GetTicker()
ticker, err := l.GetTickerPrice("BTCUSD")
if err != nil {
log.Println(err)
@@ -105,9 +105,8 @@ func (l *LocalBitcoins) Run() {
}
for _, x := range l.EnabledPairs {
currency := x[3:]
log.Printf("LocalBitcoins BTC %s: Last %f Average 1h %f Average 24h %f Volume %f\n", currency, ticker[currency].Rates.Last,
ticker[currency].Avg1h, ticker[currency].Avg24h, ticker[currency].VolumeBTC)
AddExchangeInfo(l.GetName(), x[0:3], x[3:], ticker[currency].Rates.Last, ticker[currency].VolumeBTC)
log.Printf("LocalBitcoins BTC %s: Last %f Volume %f\n", currency, ticker.Last, ticker.Volume)
AddExchangeInfo(l.GetName(), x[0:3], x[3:], ticker.Last, ticker.Volume)
}
sleep:
time.Sleep(time.Second * l.RESTPollingDelay)
@@ -140,17 +139,23 @@ func (l *LocalBitcoins) GetTicker() (map[string]LocalBitcoinsTicker, error) {
return result, nil
}
func (l *LocalBitcoins) GetTickerPrice(currency string) TickerPrice {
func (l *LocalBitcoins) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(l.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker, err := l.GetTicker()
if err != nil {
log.Println(err)
return tickerPrice
return tickerPrice, err
}
tickerPrice.Ask = ticker[currency].Rates.Last
tickerPrice.CryptoCurrency = currency
return tickerPrice
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.Volume = ticker[currency].VolumeBTC
ProcessTicker(l.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
type LocalBitcoinsTrade struct {

10
main.go
View File

@@ -32,10 +32,12 @@ type Exchange struct {
}
type Bot struct {
config Config
exchange Exchange
exchanges []IBotExchange
shutdown chan bool
config Config
exchange Exchange
exchanges []IBotExchange
tickers []Ticker
tickerChan chan Ticker
shutdown chan bool
}
var bot Bot

View File

@@ -294,17 +294,17 @@ func (o *OKCoin) Run() {
}()
}
go func() {
ticker, err := o.GetTicker(currency)
ticker, err := o.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
}
log.Printf("OKCoin Intl Spot %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Vol)
AddExchangeInfo(o.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[4:]), ticker.Last, ticker.Vol)
log.Printf("OKCoin Intl Spot %s: Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
AddExchangeInfo(o.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[4:]), ticker.Last, ticker.Volume)
}()
} else {
go func() {
ticker, err := o.GetTicker(currency)
ticker, err := o.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
@@ -312,9 +312,9 @@ func (o *OKCoin) Run() {
tickerLastUSD, _ := ConvertCurrency(ticker.Last, "CNY", "USD")
tickerHighUSD, _ := ConvertCurrency(ticker.High, "CNY", "USD")
tickerLowUSD, _ := ConvertCurrency(ticker.Low, "CNY", "USD")
log.Printf("OKCoin China %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", currency, tickerLastUSD, ticker.Last, tickerHighUSD, ticker.High, tickerLowUSD, ticker.Low, ticker.Vol)
AddExchangeInfo(o.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[4:]), ticker.Last, ticker.Vol)
AddExchangeInfo(o.GetName(), StringToUpper(currency[0:3]), "USD", tickerLastUSD, ticker.Vol)
log.Printf("OKCoin China %s: Last %f (%f) High %f (%f) Low %f (%f) Volume %f\n", currency, tickerLastUSD, ticker.Last, tickerHighUSD, ticker.High, tickerLowUSD, ticker.Low, ticker.Volume)
AddExchangeInfo(o.GetName(), StringToUpper(currency[0:3]), StringToUpper(currency[4:]), ticker.Last, ticker.Volume)
AddExchangeInfo(o.GetName(), StringToUpper(currency[0:3]), "USD", tickerLastUSD, ticker.Volume)
}()
}
}
@@ -334,22 +334,27 @@ func (o *OKCoin) GetTicker(symbol string) (OKCoinTicker, error) {
return resp.Ticker, nil
}
func (o *OKCoin) GetTickerPrice(currency string) TickerPrice {
func (o *OKCoin) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(o.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker, err := o.GetTicker(currency)
if err != nil {
log.Println(err)
return tickerPrice
return tickerPrice, err
}
tickerPrice.Ask = ticker.Sell
tickerPrice.Bid = ticker.Buy
tickerPrice.CryptoCurrency = currency
tickerPrice.FirstCurrency = StringToUpper(currency[0:3])
tickerPrice.SecondCurrency = StringToUpper(currency[4:])
tickerPrice.Low = ticker.Low
tickerPrice.Last = ticker.Last
tickerPrice.Volume = ticker.Vol
tickerPrice.High = ticker.High
return tickerPrice
ProcessTicker(o.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (o *OKCoin) GetOrderBook(symbol string, size int64, merge bool) (OKCoinOrderbook, error) {

View File

@@ -137,12 +137,12 @@ func (p *Poloniex) Run() {
for _, x := range p.EnabledPairs {
currency := x
go func() {
ticker, err := p.GetTicker()
ticker, err := p.GetTickerPrice(currency)
if err != nil {
log.Println(err)
return
}
log.Printf("Poloniex %s Last %f High %f Low %f Volume %f\n", currency, ticker[currency].Last, ticker[currency].High24Hr, ticker[currency].Low24Hr, ticker[currency].QuoteVolume)
log.Printf("Poloniex %s Last %f High %f Low %f Volume %f\n", currency, ticker.Last, ticker.High, ticker.Low, ticker.Volume)
//AddExchangeInfo(p.GetName(), currency[0:3], currency[3:], ticker.Last, ticker.Volume)
}()
}
@@ -165,22 +165,28 @@ func (p *Poloniex) GetTicker() (map[string]PoloniexTicker, error) {
return resp.Data, nil
}
func (p *Poloniex) GetTickerPrice(currency string) TickerPrice {
func (p *Poloniex) GetTickerPrice(currency string) (TickerPrice, error) {
tickerNew, err := GetTicker(p.GetName(), currency[0:3], currency[3:])
if err == nil {
return tickerNew, nil
}
var tickerPrice TickerPrice
ticker, err := p.GetTicker()
if err != nil {
log.Println(err)
return tickerPrice
return tickerPrice, err
}
tickerPrice.CryptoCurrency = currency
tickerPrice.FirstCurrency = currency[0:3]
tickerPrice.SecondCurrency = currency[3:]
tickerPrice.Ask = ticker[currency].Last
tickerPrice.Bid = ticker[currency].HighestBid
tickerPrice.High = ticker[currency].HighestBid
tickerPrice.Last = ticker[currency].Last
tickerPrice.Low = ticker[currency].LowestAsk
tickerPrice.Volume = ticker[currency].BaseVolume
return tickerPrice
ProcessTicker(p.GetName(), tickerPrice.FirstCurrency, tickerPrice.SecondCurrency, tickerPrice)
return tickerPrice, nil
}
func (p *Poloniex) GetVolume() (interface{}, error) {

View File

@@ -5,11 +5,11 @@ import (
)
type ExchangeInfo struct {
Exchange string
CryptoCurrency string
FiatCurrency string
Price float64
Volume float64
Exchange string
FirstCurrency string
FiatCurrency string
Price float64
Volume float64
}
var ExchInfo []ExchangeInfo
@@ -60,7 +60,7 @@ func AddExchangeInfo(exchange, crypto, fiat string, price, volume float64) {
func AppendExchangeInfo(exchange, crypto, fiat string, price, volume float64) {
exch := ExchangeInfo{}
exch.Exchange = exchange
exch.CryptoCurrency = crypto
exch.FirstCurrency = crypto
exch.FiatCurrency = fiat
exch.Price = price
exch.Volume = volume
@@ -69,7 +69,7 @@ func AppendExchangeInfo(exchange, crypto, fiat string, price, volume float64) {
func ExchangeInfoAlreadyExists(exchange, crypto, fiat string, price, volume float64) bool {
for i, _ := range ExchInfo {
if ExchInfo[i].Exchange == exchange && ExchInfo[i].CryptoCurrency == crypto && ExchInfo[i].FiatCurrency == fiat {
if ExchInfo[i].Exchange == exchange && ExchInfo[i].FirstCurrency == crypto && ExchInfo[i].FiatCurrency == fiat {
ExchInfo[i].Price, ExchInfo[i].Volume = price, volume
return true
}
@@ -81,7 +81,7 @@ func SortExchangesByVolume(crypto, fiat string, reverse bool) []ExchangeInfo {
info := []ExchangeInfo{}
for _, x := range ExchInfo {
if x.CryptoCurrency == crypto && x.FiatCurrency == fiat {
if x.FirstCurrency == crypto && x.FiatCurrency == fiat {
info = append(info, x)
}
}
@@ -98,7 +98,7 @@ func SortExchangesByPrice(crypto, fiat string, reverse bool) []ExchangeInfo {
info := []ExchangeInfo{}
for _, x := range ExchInfo {
if x.CryptoCurrency == crypto && x.FiatCurrency == fiat {
if x.FirstCurrency == crypto && x.FiatCurrency == fiat {
info = append(info, x)
}
}

136
ticker.go
View File

@@ -1,18 +1,28 @@
package main
import (
"errors"
"log"
"strconv"
)
var (
ErrTickerForExchangeNotFound = "Ticker for exchange does not exist."
ErrPrimaryCurrencyNotFound = "Error primary currency for ticker not found."
ErrSecondaryCurrencyNotFound = "Error secondary currency for ticker not found."
)
type TickerPrice struct {
CryptoCurrency string `json:"CryptoCurrency"`
FiatCurrency string `json:"FiatCurrency"`
FirstCurrency string `json:"FirstCurrency"`
SecondCurrency string `json:"SecondCurrency"`
CurrencyPair string `json:"CurrencyPair"`
Last float64 `json:"Last"`
High float64 `json:"High"`
Low float64 `json:"Low"`
Bid float64 `json:"Bid"`
Ask float64 `json:"Ask"`
Volume float64 `json:"Volume"`
PriceATH float64 `json:"PriceATH"`
}
type Ticker struct {
@@ -20,42 +30,120 @@ type Ticker struct {
ExchangeName string
}
func (t *Ticker) PriceToString(cryptoCurrency, fiatCurrency, priceType string) string {
func (t *Ticker) PriceToString(firstCurrency, secondCurrency, priceType string) string {
switch priceType {
case "last":
return strconv.FormatFloat(t.Price[cryptoCurrency][fiatCurrency].Last, 'f', -1, 64)
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Last, 'f', -1, 64)
case "high":
return strconv.FormatFloat(t.Price[cryptoCurrency][fiatCurrency].High, 'f', -1, 64)
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].High, 'f', -1, 64)
case "low":
return strconv.FormatFloat(t.Price[cryptoCurrency][fiatCurrency].Low, 'f', -1, 64)
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Low, 'f', -1, 64)
case "bid":
return strconv.FormatFloat(t.Price[cryptoCurrency][fiatCurrency].Bid, 'f', -1, 64)
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Bid, 'f', -1, 64)
case "ask":
return strconv.FormatFloat(t.Price[cryptoCurrency][fiatCurrency].Ask, 'f', -1, 64)
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Ask, 'f', -1, 64)
case "volume":
return strconv.FormatFloat(t.Price[cryptoCurrency][fiatCurrency].Volume, 'f', -1, 64)
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].Volume, 'f', -1, 64)
case "ath":
return strconv.FormatFloat(t.Price[firstCurrency][secondCurrency].PriceATH, 'f', -1, 64)
default:
return ""
}
}
func AddTickerPrice(m map[string]map[string]TickerPrice, cryptocurrency, fiatcurrency string, price TickerPrice) {
mm, ok := m[cryptocurrency]
if !ok {
mm = make(map[string]TickerPrice)
m[cryptocurrency] = mm
func GetTicker(exchange, firstCurrency, secondCurrency string) (TickerPrice, error) {
ticker, err := GetTickerByExchange(exchange)
if err != nil {
return TickerPrice{}, err
}
mm[fiatcurrency] = price
if !FirstCurrencyExists(exchange, firstCurrency) {
return TickerPrice{}, errors.New(ErrPrimaryCurrencyNotFound)
}
if !SecondCurrencyExists(exchange, firstCurrency, secondCurrency) {
return TickerPrice{}, errors.New(ErrSecondaryCurrencyNotFound)
}
return ticker.Price[firstCurrency][secondCurrency], nil
}
func NewTicker(exchangeName string, prices []TickerPrice) *Ticker {
ticker := &Ticker{}
func GetTickerByExchange(exchange string) (*Ticker, error) {
for _, y := range bot.tickers {
if y.ExchangeName == exchange {
return &y, nil
}
}
return nil, errors.New(ErrTickerForExchangeNotFound)
}
func FirstCurrencyExists(exchange, currency string) bool {
for _, y := range bot.tickers {
if y.ExchangeName == exchange {
if _, ok := y.Price[currency]; ok {
return true
}
}
}
return false
}
func SecondCurrencyExists(exchange, primary, secondary string) bool {
for _, y := range bot.tickers {
if y.ExchangeName == exchange {
if _, ok := y.Price[primary]; ok {
if _, ok := y.Price[primary][secondary]; ok {
return true
}
}
}
}
return false
}
func CreateNewTicker(exchangeName string, firstCurrency, secondCurrency string, tickerNew TickerPrice) {
ticker := Ticker{}
ticker.ExchangeName = exchangeName
ticker.Price = make(map[string]map[string]TickerPrice, 0)
for x, _ := range prices {
AddTickerPrice(ticker.Price, prices[x].CryptoCurrency, prices[x].FiatCurrency, prices[x])
}
return ticker
ticker.Price = make(map[string]map[string]TickerPrice)
sMap := make(map[string]TickerPrice)
sMap[secondCurrency] = tickerNew
ticker.Price[firstCurrency] = sMap
bot.tickers = append(bot.tickers, ticker)
}
func ProcessTicker(exchangeName string, firstCurrency, secondCurrency string, tickerNew TickerPrice) {
tickerNew.CurrencyPair = tickerNew.FirstCurrency + tickerNew.SecondCurrency
if len(bot.tickers) == 0 {
CreateNewTicker(exchangeName, firstCurrency, secondCurrency, tickerNew)
return
} else {
ticker, err := GetTickerByExchange(exchangeName)
if err != nil {
CreateNewTicker(exchangeName, firstCurrency, secondCurrency, tickerNew)
return
}
if FirstCurrencyExists(exchangeName, firstCurrency) {
if !SecondCurrencyExists(exchangeName, firstCurrency, secondCurrency) {
second := ticker.Price[firstCurrency]
second[secondCurrency] = tickerNew
ticker.Price[firstCurrency] = second
return
}
}
sMap := make(map[string]TickerPrice)
sMap[secondCurrency] = tickerNew
ticker.Price[firstCurrency] = sMap
}
}
func HandleTickerEvents() {
bot.tickerChan = make(chan Ticker)
for {
select {
case ticker := <-bot.tickerChan:
log.Printf("Ticker update recv %v..\n", ticker)
}
}
}

View File

@@ -13,10 +13,15 @@ func jsonTickerResponse(w http.ResponseWriter, r *http.Request) {
currency := vars["currency"]
exchangeName := vars["exchangeName"]
var response TickerPrice
var err error
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)
response, err = bot.exchanges[i].GetTickerPrice(currency)
if err != nil {
log.Println(err)
continue
}
}
}
}
@@ -25,7 +30,7 @@ func jsonTickerResponse(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
encoder := json.NewEncoder(w)
if err := encoder.Encode(response); err != nil {
if err = encoder.Encode(response); err != nil {
panic(err)
}
}
@@ -50,7 +55,10 @@ func getAllActiveTickersResponse(w http.ResponseWriter, r *http.Request) {
currencies := individualBot.GetEnabledCurrencies()
log.Println(currencies)
for _, currency := range currencies {
tickerPrice := individualBot.GetTickerPrice(currency)
tickerPrice, err := individualBot.GetTickerPrice(currency)
if err != nil {
continue
}
log.Println(tickerPrice)
individualExchange.ExchangeValues = append(individualExchange.ExchangeValues, tickerPrice)

View File

@@ -4,7 +4,7 @@
<label>Exchange: {{exchange.exchangeName}}
</div>
<div class="row">
<label>Currency: </label>{{currency.CryptoCurrency}}
<label>Currency: </label>{{currency.CurrencyPair}}
</div>
<div class="row">
<label>Lowest Ask: </label><label>{{currency.Ask}}</label>

View File

@@ -15,7 +15,7 @@ angular.module('myApp.buy',[]).component('buy', {
});
$scope.GetLatestDataFromExchangeCurrency = function () {
$http.get('/GetLatestDataFromExchangeCurrency?exhange=' + $scope.exchange.exchangeName + '&currency='+ $scope.currency.CryptoCurrency).success(function (data) {
$http.get('/GetLatestDataFromExchangeCurrency?exhange=' + $scope.exchange.exchangeName + '&currency='+ $scope.currency.CurrencyPair).success(function (data) {
$scope.currency.Last = data.Last;
$scope.currency.Volume = data.Volume;
$scope.currency.Ask = data.Ask;

View File

@@ -18,7 +18,7 @@
<th>Volume</th>
</tr>
<tr ng-repeat="value in exchange.exchangeValues">
<td><a href="" ng-click="reloadDashboardWithExchangeCurrency(exchange,value)">{{value.CryptoCurrency}}</a></td>
<td><a href="" ng-click="reloadDashboardWithExchangeCurrency(exchange,value)">{{value.CurrencyPair}}</a></td>
<td>{{value.Last | number: 2}}</td>
<td>{{value.Volume | number: 2}}</td>
</tr>

View File

@@ -4,7 +4,7 @@
<label>Exchange: {{exchange.exchangeName}}
</div>
<div class="row">
<label>Currency: </label>{{currency.CryptoCurrency}}
<label>Currency: </label>{{currency.CurrencyPair}}
</div>
<div class="row">
<label>Bid: </label><label>{{currency.Bid}}</label>

View File

@@ -15,7 +15,7 @@ angular.module('myApp.sell',[]).component('sell', {
});
$scope.GetLatestDataFromExchangeCurrency = function () {
$http.get('/GetLatestDataFromExchangeCurrency?exhange=' + $scope.exchange.exchangeName + '&currency='+ $scope.currency.CryptoCurrency).success(function (data) {
$http.get('/GetLatestDataFromExchangeCurrency?exhange=' + $scope.exchange.exchangeName + '&currency='+ $scope.currency.CurrencyPair).success(function (data) {
$scope.currency.Last = data.Last;
$scope.currency.Volume = data.Volume;
$scope.currency.Bid = data.Bid;

View File

@@ -2,7 +2,7 @@
<div ng-show="loaded">
<div class="col-md-11">
<h2>{{exchange.exchangeName}} Exchange</h2>
<h4>{{currency.CryptoCurrency}}</h4>
<h4>{{currency.CurrencyPair}}</h4>
</div>
<div class="col-md-8">
<div class="col-md-12">