Skip to content
This repository has been archived by the owner on Oct 27, 2023. It is now read-only.

API wapper for api/trades #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
23 changes: 23 additions & 0 deletions binance.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type Binance interface {
OpenOrders(oor OpenOrdersRequest) ([]*ExecutedOrder, error)
// AllOrders returns list of all previous orders.
AllOrders(aor AllOrdersRequest) ([]*ExecutedOrder, error)
// RecentTrades returns all recent trades
RecentTrades(hr RecentTradesRequest) ([]*RecentTrades, error)

// Account returns account data.
Account(ar AccountRequest) (*Account, error)
Expand Down Expand Up @@ -487,6 +489,27 @@ func (b *binance) WithdrawHistory(hr HistoryRequest) ([]*Withdrawal, error) {
return b.Service.WithdrawHistory(hr)
}


type RecentTradesRequest struct {
Symbol string
Limit int
}

type RecentTrades struct {
ID int64
Price float64
Qty float64
Time time.Time
IsBuyerMaker bool
IsBestMatch bool
}

// Recent trades list.
func (b *binance) RecentTrades(rt RecentTradesRequest) ([]*RecentTrades, error) {
return b.Service.RecentTrades(rt)
}


// Stream represents stream information.
//
// Read web docs to get more information about using streams.
Expand Down
1 change: 1 addition & 0 deletions service_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type Service interface {
Withdraw(wr WithdrawRequest) (*WithdrawResult, error)
DepositHistory(hr HistoryRequest) ([]*Deposit, error)
WithdrawHistory(hr HistoryRequest) ([]*Withdrawal, error)
RecentTrades(hr RecentTradesRequest) ([]*RecentTrades, error)

StartUserDataStream() (*Stream, error)
KeepAliveUserDataStream(s *Stream) error
Expand Down
61 changes: 61 additions & 0 deletions service_market.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,67 @@ func (as *apiService) AggTrades(atr AggTradesRequest) ([]*AggTrade, error) {
return aggTrades, nil
}

func (as *apiService) RecentTrades(rtr RecentTradesRequest) ([]*RecentTrades, error) {
params := make(map[string]string)
params["symbol"] = rtr.Symbol
if rtr.Limit != 0 {
params["limit"] = strconv.Itoa(rtr.Limit)
}

res, err := as.request("GET", "api/v1/trades", params, false, false)
if err != nil {
return nil, err
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Defer must be here!

defer res.Body.Close()

textRes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, errors.Wrap(err, "unable to read response from RecentTrades")
}
defer res.Body.Close()

if res.StatusCode != 200 {
as.handleError(textRes)
}

var rawRecentTrades []struct {
ID int64 `json:"id"`
Price string `json:"price"`
Qty string `json:"qty"`
Time float64 `json:"time"`
IsBuyerMaker bool `json:"isBuyerMaker"`
IsBestMatch bool `json:"isBestMatch"`
}

if err := json.Unmarshal(textRes, &rawRecentTrades); err != nil {
return nil, errors.Wrap(err, "rawRecentTrades unmarshal failed")
}

var recentTrades []*RecentTrades
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recentTrades := make([]*RecentTrades, 0, len(rawRecentTrades))

for _, rrt := range rawRecentTrades {
price, err := floatFromString(rrt.Price)
if err != nil {
return nil, err
}
qty, err := floatFromString(rrt.Qty)
if err != nil {
return nil, err
}

t, err := timeFromUnixTimestampFloat(rrt.Time)
if err != nil {
return nil, err
}
recentTrades = append(recentTrades, &RecentTrades{
ID: rrt.ID,
Price: price,
Qty: qty,
Time: t,
IsBuyerMaker: rrt.IsBuyerMaker,
IsBestMatch: rrt.IsBestMatch,
})
}
return recentTrades, nil
}

func (as *apiService) Klines(kr KlinesRequest) ([]*Kline, error) {
params := make(map[string]string)
params["symbol"] = kr.Symbol
Expand Down