-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (62 loc) · 1.73 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
package main
import (
"fmt"
"marketracker/ingester"
"marketracker/models"
"marketracker/parser"
"sync"
)
func main() {
jsonChn := make(chan string)
tradesChn := make(chan models.Trade)
var parserGroup sync.WaitGroup
var processThread sync.WaitGroup
numParserThread := 8
marketDict := map[int]*models.MarketStat{}
//process the trades
processThread.Add(1)
go func() {
for {
trade, ok := <-tradesChn
if ok == false {
processThread.Done()
break
} else {
buy := 0
if trade.IsBuy {
buy = 1
}
if _, ok := marketDict[trade.Market]; ok {
marketDict[trade.Market].NumTrades++
marketDict[trade.Market].TotalPrice += trade.Price
marketDict[trade.Market].TotalVolume += trade.Volume
marketDict[trade.Market].VolumeTimesPrice += trade.Volume * trade.Price
marketDict[trade.Market].NumBuys += buy
} else {
marketDict[trade.Market] = &models.MarketStat{
NumTrades: 1,
NumBuys: buy,
TotalVolume: trade.Volume,
TotalPrice: trade.Price,
VolumeTimesPrice: trade.Volume * trade.Price,
}
}
}
}
}()
go func() {
for i := 0; i < numParserThread; i++ {
parserGroup.Add(1)
go parser.DecodeTradeJSON(jsonChn, tradesChn, &parserGroup)
}
parserGroup.Wait()
close(tradesChn)
}()
go ingester.IngestFromStdin(jsonChn)
processThread.Wait()
for key, val := range marketDict {
fmt.Printf("{\"market\":%d,\"total_volume\":%f,\"mean_price\":%f,\"mean_volume\":%f,\"volume_weighted_average_price\":%f,\"percentage_buy\":%f}\n",
key, val.TotalVolume, val.TotalPrice/float64(val.NumTrades),
val.TotalVolume/float64(val.NumTrades), val.VolumeTimesPrice/val.TotalVolume, float64(val.NumBuys)/float64(val.NumTrades))
}
}