Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/fix from tests #85

Merged
merged 2 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions exchanges/binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"context"
"errors"
"fmt"
"time"

"github.com/adshao/go-binance"
"github.com/saniales/golang-crypto-trading-bot/environment"
Expand Down Expand Up @@ -153,25 +152,38 @@ func (wrapper *BinanceWrapper) orderbookFromREST(market *environment.Market) (*e
// BuyLimit performs a limit buy action.
func (wrapper *BinanceWrapper) BuyLimit(market *environment.Market, amount float64, limit float64) (string, error) {
orderNumber, err := wrapper.api.NewCreateOrderService().Type(binance.OrderTypeLimit).Side(binance.SideTypeBuy).Symbol(MarketNameFor(market, wrapper)).Price(fmt.Sprint(limit)).Quantity(fmt.Sprint(amount)).Do(context.Background())
return fmt.Sprint(orderNumber.ClientOrderID), err
if err != nil {
return "", err
}
return orderNumber.ClientOrderID, nil
}

// SellLimit performs a limit sell action.
func (wrapper *BinanceWrapper) SellLimit(market *environment.Market, amount float64, limit float64) (string, error) {
orderNumber, err := wrapper.api.NewCreateOrderService().Type(binance.OrderTypeLimit).Side(binance.SideTypeSell).Symbol(MarketNameFor(market, wrapper)).Price(fmt.Sprint(limit)).Quantity(fmt.Sprint(amount)).Do(context.Background())
return fmt.Sprint(orderNumber.ClientOrderID), err
if err != nil {
return "", err
}
return orderNumber.ClientOrderID, nil
}

// BuyMarket performs a market buy action.
func (wrapper *BinanceWrapper) BuyMarket(market *environment.Market, amount float64) (string, error) {
orderNumber, err := wrapper.api.NewCreateOrderService().Type(binance.OrderTypeMarket).Side(binance.SideTypeBuy).Symbol(MarketNameFor(market, wrapper)).Quantity(fmt.Sprint(amount)).Do(context.Background())
return fmt.Sprint(orderNumber.ClientOrderID), err
if err != nil {
return "", err
}

return orderNumber.ClientOrderID, nil
}

// SellMarket performs a market sell action.
func (wrapper *BinanceWrapper) SellMarket(market *environment.Market, amount float64) (string, error) {
orderNumber, err := wrapper.api.NewCreateOrderService().Type(binance.OrderTypeMarket).Side(binance.SideTypeSell).Symbol(MarketNameFor(market, wrapper)).Quantity(fmt.Sprint(amount)).Do(context.Background())
return fmt.Sprint(orderNumber.ClientOrderID), err
if err != nil {
return "", err
}
return orderNumber.ClientOrderID, nil
}

// GetTicker gets the updated ticker for a market.
Expand Down Expand Up @@ -356,14 +368,12 @@ func (wrapper *BinanceWrapper) subscribeOrderbookFeed(market *environment.Market
}
// 24 hours max
currentUpdateID := lastUpdateID
logrus.Info(time.Now())

done, _, err := binance.WsPartialDepthServe(MarketNameFor(market, wrapper), "20", func(event *binance.WsPartialDepthEvent) {
if event.LastUpdateID <= currentUpdateID { // this update is more recent than the latest fetched
return
}

logrus.Info(time.Now())

var orderbook environment.OrderBook

orderbook.Asks = make([]environment.Order, len(event.Asks))
Expand All @@ -389,7 +399,6 @@ func (wrapper *BinanceWrapper) subscribeOrderbookFeed(market *environment.Market
orderbook.Bids[i] = newOrder
}

logrus.Infof("%s : %s", MarketNameFor(market, wrapper), orderbook)
wrapper.orderbook.Set(market, &orderbook)
}, func(err error) {
logrus.Error(err)
Expand Down
16 changes: 10 additions & 6 deletions exchanges/hitbtc.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package exchanges

import (
"fmt"
"github.com/satori/go.uuid"
"sort"

"github.com/juju/errors"
Expand Down Expand Up @@ -158,7 +159,6 @@ func (wrapper *HitBtcWrapperV2) SellLimit(market *environment.Market, amount flo
requestOrder := hitbtc.Order{
Symbol: MarketNameFor(market, wrapper),
Side: "sell",
Status: "new",
Type: "limit",
Quantity: amount,
Price: limit,
Expand All @@ -173,12 +173,16 @@ func (wrapper *HitBtcWrapperV2) SellLimit(market *environment.Market, amount flo

// SellMarket performs a market sell action.
func (wrapper *HitBtcWrapperV2) SellMarket(market *environment.Market, amount float64) (string, error) {
clientOrderID, err := uuid.NewV4()
if err != nil {
return "", err
}
requestOrder := hitbtc.Order{
Symbol: MarketNameFor(market, wrapper),
Side: "sell",
Status: "new",
Type: "market",
Quantity: amount,
Symbol: MarketNameFor(market, wrapper),
Side: "sell",
Type: "market",
Quantity: amount,
ClientOrderId: clientOrderID.String()[:32], // max length is 32 characters
}

orderNumber, err := wrapper.api.PlaceOrder(requestOrder)
Expand Down