-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
133 lines (113 loc) · 2.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"os"
"time"
)
type Trade struct {
ID int `json:"id"`
Market int `json:"market"`
Price float64 `json:"price"`
Volume float64 `json:"volume"`
IsBuy bool `json:"is_buy"`
}
type Markets struct {
ID int `json:"market"`
Trades int `json:"total_trades"`
Buys int `json:"total_buys"`
BuysPercent float64 `json:"percentage_buy"`
Volume float64 `json:"total_volume"`
VolumeMean float64 `json:"mean_volume"`
Price float64 `json:"total_price"`
PriceMean float64 `json:"mean_price"`
VWAP float64 `json:"volume_weighted_average_price"`
}
type M map[int]Markets
func main() {
//Start a timer!
start := time.Now()
var trade Trade
markets := make(M)
//Initialize readers
reader := bufio.NewReader(os.Stdin)
//We know that stdin will start with a string -- BEGIN
passReader := parseStrings(*reader, "BEGIN\n")
//Decode JSON
decoder := json.NewDecoder(&passReader)
for {
err := decoder.Decode(&trade)
if err != nil {
//TODO -- Once we run out of JSON the decoder barfs when reading END
//Find a way to stop reading json, consume remaining statistical messages and exit
break
}
//Compute trade
computeTrade(trade, markets)
}
//Print results to stdout
printMarketData(markets)
//Report execution time
duration := time.Since(start)
fmt.Println(duration)
}
func computeTrade(trade Trade, markets M) {
isBuy := 0
if trade.IsBuy {
isBuy = 1
}
if market, ok := markets[trade.Market]; ok {
market.Trades += 1
market.Buys += isBuy
market.BuysPercent = float64(market.Buys) / float64(market.Trades)
market.Volume += trade.Volume
market.VolumeMean = float64(market.Volume) / float64(market.Trades)
market.Price += trade.Price
market.PriceMean = float64(market.Price) / float64(market.Trades)
market.VWAP = (float64(market.Price) * float64(market.Volume)) / float64(market.Trades)
markets[trade.Market] = market
} else {
market.ID = trade.Market
market.Trades = 1
market.Buys = isBuy
market.BuysPercent = float64(isBuy)
market.Volume = trade.Volume
market.VolumeMean = trade.Volume
market.Price = trade.Price
market.PriceMean = trade.Price
market.VWAP = (float64(market.Price) * float64(market.Volume)) / float64(market.Trades)
markets[trade.Market] = market
}
}
func printMarketData(markets M) {
for _, market := range markets {
b, err := json.Marshal(market)
if err != nil {
panic(err)
}
os.Stdout.Write(b)
}
}
//Rudimentary way to try and catch BEGIN and END
func parseStrings(reader bufio.Reader, match string) bufio.Reader {
for {
line, err := reader.ReadString('\n')
if err == io.EOF {
fmt.Println("Reached end of file")
break
}
if err != nil {
fmt.Println("caught error")
fmt.Println(err)
}
if line == match {
fmt.Println("Matched line:")
fmt.Println(line)
break
}
fmt.Println(line)
}
return reader
}