2017-12-26 22:19:42 -07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-28 20:26:25 -07:00
|
|
|
"git.cotugno.family/kevin/spectator/exhibit"
|
2017-12-26 22:19:42 -07:00
|
|
|
"github.com/shopspring/decimal"
|
|
|
|
|
2017-12-30 03:23:20 -07:00
|
|
|
"image"
|
2017-12-26 22:19:42 -07:00
|
|
|
"log"
|
2018-01-01 18:25:23 -07:00
|
|
|
"sync"
|
2018-01-02 00:26:31 -07:00
|
|
|
"time"
|
2017-12-30 23:55:36 -07:00
|
|
|
"unicode/utf8"
|
2017-12-26 22:19:42 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-12-27 16:49:20 -07:00
|
|
|
coin = "ETH-USD"
|
2017-12-26 22:19:42 -07:00
|
|
|
timeFormat = "15:04:05"
|
|
|
|
)
|
|
|
|
|
|
|
|
var trades = NewQueue()
|
|
|
|
|
|
|
|
var terminal *exhibit.Terminal
|
2018-01-01 18:25:23 -07:00
|
|
|
var ob *OrderBook
|
2017-12-26 22:19:42 -07:00
|
|
|
|
|
|
|
var window *exhibit.WindowWidget
|
|
|
|
var topAsks *exhibit.ListWidget
|
|
|
|
var topBids *exhibit.ListWidget
|
|
|
|
var midPrice *exhibit.ListWidget
|
2017-12-30 23:55:36 -07:00
|
|
|
var history *exhibit.ListWidget
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2018-01-01 18:25:23 -07:00
|
|
|
var numLock sync.Mutex
|
2018-01-02 00:26:31 -07:00
|
|
|
var num int
|
2018-01-01 18:25:23 -07:00
|
|
|
|
2018-01-02 00:26:31 -07:00
|
|
|
var sizeLock sync.Mutex
|
2018-01-02 00:11:56 -07:00
|
|
|
var sizeChanged bool
|
|
|
|
|
2018-01-01 18:25:23 -07:00
|
|
|
var low, high decimal.Decimal
|
|
|
|
|
2017-12-26 22:19:42 -07:00
|
|
|
func main() {
|
2018-01-01 18:25:23 -07:00
|
|
|
var err error
|
|
|
|
|
2017-12-26 22:19:42 -07:00
|
|
|
terminal = exhibit.Init()
|
|
|
|
defer terminal.Shutdown()
|
|
|
|
terminal.HideCursor()
|
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
window = &exhibit.WindowWidget{}
|
2017-12-30 23:55:36 -07:00
|
|
|
window.SetBorder(exhibit.Border{Visible: true, Attributes: exhibit.Attributes{ForegroundColor: exhibit.FGYellow}})
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2017-12-30 23:55:36 -07:00
|
|
|
topAsks = &exhibit.ListWidget{}
|
2017-12-26 22:19:42 -07:00
|
|
|
topBids = &exhibit.ListWidget{}
|
|
|
|
|
|
|
|
midPrice = &exhibit.ListWidget{}
|
2017-12-30 23:55:36 -07:00
|
|
|
midPrice.SetSize(image.Pt(24, 1))
|
|
|
|
|
|
|
|
history = &exhibit.ListWidget{}
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
window.AddWidget(topAsks)
|
2017-12-30 23:55:36 -07:00
|
|
|
window.AddWidget(midPrice)
|
|
|
|
window.AddWidget(topBids)
|
|
|
|
window.AddWidget(history)
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2017-12-30 14:42:13 -07:00
|
|
|
scene := exhibit.Scene{terminal, window}
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2018-01-02 00:11:56 -07:00
|
|
|
watchSize(terminal)
|
2018-01-01 18:25:23 -07:00
|
|
|
|
|
|
|
ob, err = NewOrderBook(coin)
|
2017-12-26 22:19:42 -07:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2018-01-02 00:26:31 -07:00
|
|
|
Loop:
|
2017-12-26 22:19:42 -07:00
|
|
|
for e := range terminal.Event {
|
2017-12-30 03:23:20 -07:00
|
|
|
switch e {
|
|
|
|
case exhibit.Eventq:
|
|
|
|
fallthrough
|
|
|
|
case exhibit.EventCtrC:
|
2018-01-01 18:25:23 -07:00
|
|
|
ob.Shutdown()
|
2017-12-30 03:23:20 -07:00
|
|
|
break Loop
|
2017-12-26 22:19:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
go renderLoop(&scene, 100*time.Millisecond)
|
|
|
|
|
2018-01-01 18:25:23 -07:00
|
|
|
updateOrders("sell")
|
|
|
|
updateOrders("buy")
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2018-01-01 18:25:23 -07:00
|
|
|
for msg := range ob.Msg {
|
|
|
|
updateOrders(msg.Side)
|
2018-01-01 19:30:27 -07:00
|
|
|
|
|
|
|
if msg.Type == "match" {
|
|
|
|
addTrade(msg)
|
|
|
|
}
|
2017-12-26 22:19:42 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 18:25:23 -07:00
|
|
|
func numPerSide() int {
|
|
|
|
numLock.Lock()
|
|
|
|
defer numLock.Unlock()
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2018-01-01 18:25:23 -07:00
|
|
|
return num
|
2017-12-26 22:19:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func numOfOrderPerSide(y int) int {
|
2017-12-30 23:55:36 -07:00
|
|
|
total := y - 3 - 2
|
2017-12-26 22:19:42 -07:00
|
|
|
|
|
|
|
return (total / 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func flatten(entries Entries) (decimal.Decimal, decimal.Decimal) {
|
|
|
|
var price, size decimal.Decimal
|
|
|
|
|
|
|
|
for _, v := range entries {
|
|
|
|
price = v.Price
|
|
|
|
size = size.Add(v.Size)
|
|
|
|
}
|
|
|
|
|
|
|
|
return price, size
|
|
|
|
}
|
|
|
|
|
|
|
|
func renderLoop(scene *exhibit.Scene, interval time.Duration) {
|
|
|
|
timer := time.NewTicker(interval)
|
2018-01-02 00:11:56 -07:00
|
|
|
changed := time.NewTicker(2 * time.Second)
|
2017-12-26 22:19:42 -07:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2018-01-02 00:11:56 -07:00
|
|
|
case <-changed.C:
|
|
|
|
if didsizeChanged() {
|
|
|
|
terminal.Clear()
|
|
|
|
setSizeChanged(false)
|
|
|
|
}
|
2017-12-26 22:19:42 -07:00
|
|
|
case <-timer.C:
|
|
|
|
scene.Render()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-01 18:25:23 -07:00
|
|
|
func recalcSizes(sz image.Point) {
|
2018-01-02 00:11:56 -07:00
|
|
|
numLock.Lock()
|
|
|
|
defer numLock.Unlock()
|
2017-12-26 22:19:42 -07:00
|
|
|
|
2018-01-02 00:11:56 -07:00
|
|
|
sizeLock.Lock()
|
|
|
|
defer sizeLock.Unlock()
|
2018-01-01 18:25:23 -07:00
|
|
|
|
2018-01-02 00:11:56 -07:00
|
|
|
num = numOfOrderPerSide(sz.Y)
|
2018-01-01 18:25:23 -07:00
|
|
|
|
2018-01-02 00:11:56 -07:00
|
|
|
if history.Size() != image.Pt(33, sz.Y) {
|
|
|
|
history.SetSize(image.Pt(33, sz.Y))
|
|
|
|
}
|
2018-01-01 18:25:23 -07:00
|
|
|
|
2018-01-02 00:11:56 -07:00
|
|
|
hOr := image.Pt(sz.X-35, 0)
|
|
|
|
if history.Origin() != hOr {
|
|
|
|
history.SetOrigin(hOr)
|
|
|
|
}
|
2018-01-01 18:25:23 -07:00
|
|
|
|
2018-01-02 00:11:56 -07:00
|
|
|
num := numOfOrderPerSide(sz.Y)
|
|
|
|
size := image.Point{23, num}
|
|
|
|
if topAsks.Size() != size {
|
|
|
|
topAsks.SetSize(size)
|
|
|
|
}
|
2017-12-30 23:55:36 -07:00
|
|
|
|
2018-01-02 00:11:56 -07:00
|
|
|
bOrigin := image.Pt(0, size.Y+3)
|
|
|
|
if topBids.Origin() != bOrigin {
|
|
|
|
topBids.SetOrigin(bOrigin)
|
|
|
|
}
|
|
|
|
if topBids.Size() != size {
|
|
|
|
topBids.SetSize(size)
|
|
|
|
}
|
|
|
|
|
|
|
|
mOr := image.Pt(0, num+1)
|
|
|
|
if midPrice.Origin() != mOr {
|
|
|
|
midPrice.SetOrigin(mOr)
|
|
|
|
}
|
2017-12-30 23:55:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func padString(value string, length int) string {
|
|
|
|
c := utf8.RuneCountInString(value)
|
|
|
|
|
|
|
|
if c >= length {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
|
|
|
|
pad := length - c
|
|
|
|
|
|
|
|
var s string
|
|
|
|
for i := 0; i < pad; i++ {
|
|
|
|
s = s + " "
|
|
|
|
}
|
|
|
|
|
|
|
|
return s + value
|
|
|
|
}
|
|
|
|
|
|
|
|
func fmtObEntry(price, size decimal.Decimal) string {
|
|
|
|
s := padString(price.StringFixed(2), 8)
|
|
|
|
s = s + " "
|
|
|
|
s = s + padString(size.StringFixed(8), 14)
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func fmtHistoryEntry(msg Message) string {
|
|
|
|
var arrow string
|
|
|
|
switch msg.Side {
|
|
|
|
case "buy":
|
|
|
|
arrow = "↓"
|
|
|
|
case "sell":
|
|
|
|
arrow = "↑"
|
|
|
|
}
|
|
|
|
|
|
|
|
s := padString(msg.Size.StringFixed(8), 14)
|
|
|
|
s = s + " "
|
|
|
|
s = s + padString(msg.Price.StringFixed(2), 8)
|
|
|
|
s = s + arrow
|
|
|
|
s = s + " "
|
|
|
|
s = s + msg.Time.Local().Format(timeFormat)
|
|
|
|
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func fmtMid(high, low decimal.Decimal) string {
|
|
|
|
diff := low.Sub(high)
|
|
|
|
mid := high.Add(diff.Div(decimal.New(2, 0))).StringFixed(3)
|
|
|
|
|
|
|
|
return padString(mid, 9) + padString(diff.StringFixed(2), 14)
|
|
|
|
}
|
2018-01-01 18:25:23 -07:00
|
|
|
|
2018-01-02 00:11:56 -07:00
|
|
|
func watchSize(t *exhibit.Terminal) {
|
2018-01-01 18:25:23 -07:00
|
|
|
go func() {
|
2018-01-02 00:11:56 -07:00
|
|
|
for s := range t.SizeChange {
|
2018-01-01 18:25:23 -07:00
|
|
|
recalcSizes(s)
|
2018-01-02 00:11:56 -07:00
|
|
|
setSizeChanged(true)
|
2018-01-01 18:25:23 -07:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateOrders(side string) {
|
|
|
|
n := numPerSide()
|
|
|
|
entries := ob.Entries(side, n)
|
|
|
|
|
|
|
|
switch side {
|
|
|
|
case "sell":
|
|
|
|
updateAsks(entries)
|
|
|
|
case "buy":
|
|
|
|
updateBids(entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
midPrice.AddEntry(ListEntry{Value: fmtMid(high, low)})
|
|
|
|
midPrice.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateAsks(entries []Entries) {
|
|
|
|
for i := len(entries) - 1; i >= 0; i-- {
|
|
|
|
entry := entries[i]
|
|
|
|
price, size := flatten(entry)
|
|
|
|
|
|
|
|
topAsks.AddEntry(ListEntry{Value: fmtObEntry(price, size),
|
2018-01-02 00:26:31 -07:00
|
|
|
Attrs: exhibit.Attributes{ForegroundColor: exhibit.FGRed}})
|
2018-01-01 18:25:23 -07:00
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
low = price
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
topAsks.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateBids(entries []Entries) {
|
|
|
|
for i := 0; i < len(entries); i++ {
|
|
|
|
entry := entries[i]
|
|
|
|
price, size := flatten(entry)
|
|
|
|
|
|
|
|
topBids.AddEntry(ListEntry{Value: fmtObEntry(price, size),
|
|
|
|
Attrs: exhibit.Attributes{ForegroundColor: exhibit.FGGreen}})
|
|
|
|
|
|
|
|
if i == 0 {
|
|
|
|
high = price
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
topBids.Commit()
|
|
|
|
}
|
2018-01-01 19:30:27 -07:00
|
|
|
|
|
|
|
func addTrade(msg Message) {
|
|
|
|
if trades.Length() == 256 {
|
|
|
|
trades.Dequeue()
|
|
|
|
}
|
|
|
|
|
|
|
|
trades.Enqueue(msg)
|
|
|
|
|
|
|
|
max := history.Size().Y
|
|
|
|
length := trades.Length()
|
|
|
|
var num int
|
|
|
|
if length > max {
|
|
|
|
num = max
|
|
|
|
} else {
|
|
|
|
num = length
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
var index int
|
|
|
|
|
|
|
|
adj := trades.Length() - i - 1
|
|
|
|
|
|
|
|
if adj < 0 {
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
index = adj
|
|
|
|
}
|
|
|
|
|
|
|
|
e := trades.Element(index)
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
msg := e.(Message)
|
|
|
|
|
|
|
|
var attrs exhibit.Attributes
|
|
|
|
|
|
|
|
switch msg.Side {
|
|
|
|
case "buy":
|
|
|
|
attrs.ForegroundColor = exhibit.FGRed
|
|
|
|
case "sell":
|
|
|
|
attrs.ForegroundColor = exhibit.FGGreen
|
|
|
|
}
|
|
|
|
|
|
|
|
le := ListEntry{fmtHistoryEntry(msg), attrs}
|
|
|
|
history.AddEntry(le)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
history.Commit()
|
|
|
|
}
|
2018-01-02 00:11:56 -07:00
|
|
|
|
|
|
|
func didsizeChanged() bool {
|
|
|
|
sizeLock.Lock()
|
|
|
|
defer sizeLock.Unlock()
|
|
|
|
|
|
|
|
return sizeChanged
|
|
|
|
}
|
|
|
|
|
|
|
|
func setSizeChanged(c bool) {
|
|
|
|
sizeLock.Lock()
|
|
|
|
defer sizeLock.Unlock()
|
|
|
|
|
|
|
|
sizeChanged = c
|
|
|
|
}
|