Skip to content

Commit

Permalink
create BookEntries type and add methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jsgoyette committed Jan 2, 2017
1 parent cd7c6ee commit 9e9e9e9
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 10 deletions.
10 changes: 0 additions & 10 deletions gemini.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,16 +166,6 @@ type TradeVolume struct {
SellTakerCount float64 `json:"sell_taker_count"`
}

type Book struct {
Bids []BookEntry
Asks []BookEntry
}

type BookEntry struct {
Price float64 `json:",string"`
Amount float64 `json:",string"`
}

type CurrentAuction struct {
ClosedUntil int64 `json:"closed_until_ms"`
LastAuctionEid Id `json:"last_auction_eid"`
Expand Down
85 changes: 85 additions & 0 deletions order-book.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package gemini

type Book struct {
Bids BookEntries
Asks BookEntries
}

type BookEntries []BookEntry

type BookEntry struct {
Price float64 `json:",string"`
Amount float64 `json:",string"`
}

func (b *BookEntries) Set(price, amount float64) {
pos := b.findByPrice(price)

if pos == -1 {
*b = append(*b, BookEntry{
Price: price,
Amount: amount,
})
} else {
if amount == 0 {
*b = append((*b)[:pos], (*b)[pos+1:]...)
} else {
(*b)[pos].Amount = amount
}
}
}

func (b BookEntries) Lowest() BookEntry {

var lowest float64
var index int

if len(b) == 0 {
return BookEntry{}
}

for idx, entry := range b {
if idx == 0 {
lowest = entry.Price
continue
}
if entry.Price < lowest {
lowest = entry.Price
index = idx
}
}

return b[index]
}

func (b BookEntries) Highest() BookEntry {

var highest float64
var index int

if len(b) == 0 {
return BookEntry{}
}

for idx, entry := range b {
if idx == 0 {
highest = entry.Price
continue
}
if entry.Price > highest {
highest = entry.Price
index = idx
}
}

return b[index]
}

func (b BookEntries) findByPrice(price float64) int {
for idx, entry := range b {
if entry.Price == price {
return idx
}
}
return -1
}

0 comments on commit 9e9e9e9

Please sign in to comment.