/
ticker_manager.go
99 lines (89 loc) · 2.67 KB
/
ticker_manager.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
package core
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const MinuteNumInDay = 24 * 60
type TickerManager struct {
// The prices in last 24 hours
PriceList [MinuteNumInDay]sdk.Dec `json:"price_list"`
// The following 4 members act like a small fifo
// new Price goes first into Price1st, then goes into Price2nd,
// finally Price2nd goes into PriceList
Price1st sdk.Dec `json:"price_1st"`
Minute1st int `json:"minute_1st"`
Price2nd sdk.Dec `json:"price_2nd"`
Minute2nd int `json:"minute_2nd"`
Market string `json:"market"`
Initialized bool `json:"initialized"`
}
func NewTickerManager(Market string) *TickerManager {
return &TickerManager{
Market: Market,
}
}
// Flush the cached NewestPrice and NewestMinute to PriceList,
// and assign currPrice to NewestPrice, currMinute to NewestMinute
func (tm *TickerManager) UpdateNewestPrice(currPrice sdk.Dec, currMinute int) {
//fmt.Printf("@@ %s: %s %d\n", tm.Market, currPrice, currMinute)
if currMinute >= MinuteNumInDay || currMinute < 0 {
panic(fmt.Sprintf("Minute not Valid: %d", currMinute))
}
if !tm.Initialized {
// Initialize all the members with currPrice
tm.Initialized = true
for i := 0; i < MinuteNumInDay; i++ {
tm.PriceList[i] = currPrice
}
tm.Price1st = currPrice
tm.Minute1st = currMinute
tm.Price2nd = currPrice
tm.Minute2nd = currMinute
return
}
tm.fillHistory(tm.Minute2nd, tm.Minute1st, tm.Price2nd)
tm.Price2nd = tm.Price1st
tm.Minute2nd = tm.Minute1st
tm.Price1st = currPrice
tm.Minute1st = currMinute
}
func (tm *TickerManager) fillHistory(beginTime, endTime int, price sdk.Dec) {
for {
tm.PriceList[beginTime] = price
beginTime++
if beginTime >= MinuteNumInDay {
beginTime = 0
}
if beginTime == endTime {
break
}
}
}
// Return a Ticker if NewPrice or OldPriceOneDayAgo is different from its previous minute
func (tm *TickerManager) GetTicker(currMinute int) *Ticker {
if !tm.Initialized {
return nil
}
if currMinute >= MinuteNumInDay || currMinute < 0 {
panic(fmt.Sprintf("Minute not Valid: %d", currMinute))
}
lastMinute := currMinute - 1
if lastMinute < 0 {
lastMinute = MinuteNumInDay - 1
}
// If the price changes at currMinute of today
if (tm.Minute1st == currMinute && !tm.Price1st.Equal(tm.Price2nd)) ||
// Or If price changes at currMinute of yesterday
!tm.PriceList[currMinute].Equal(tm.PriceList[lastMinute]) {
return &Ticker{
NewPrice: tm.Price1st,
OldPriceOneDayAgo: tm.PriceList[currMinute],
Market: tm.Market,
MinuteInDay: currMinute,
}
}
if tm.Minute1st != currMinute { //flush the price into the small fifo
tm.UpdateNewestPrice(tm.Price1st, currMinute)
}
return nil
}