Skip to content

Commit

Permalink
fix 24-hour calcs
Browse files Browse the repository at this point in the history
Fixes the calculation of 24 hour volume and price change. I was using
the same cache for every market before. duh.
  • Loading branch information
buck54321 authored and chappjc committed Jul 31, 2022
1 parent fb05984 commit 268021b
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions server/apidata/apidata.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ type DataAPI struct {

cacheMtx sync.RWMutex
marketCaches map[string]map[uint64]*candles.Cache
cache5min *candles.Cache
}

// NewDataAPI is the constructor for a new DataAPI.
Expand Down Expand Up @@ -90,12 +89,6 @@ func (s *DataAPI) AddMarketSource(mkt MarketSource) error {
cache := candles.NewCache(candles.CacheSize, binSize)
cacheList = append(cacheList, cache)
binCaches[binSize] = cache
if binSize == bin5min {
s.cache5min = cache
}
}
if s.cache5min == nil {
panic("no 5-minute cache")
}
err = s.db.LoadEpochStats(mkt.Base(), mkt.Quote(), cacheList)
if err != nil {
Expand All @@ -118,17 +111,19 @@ func (s *DataAPI) ReportEpoch(base, quote uint32, epochIdx uint64, stats *matche
}

// Add the candlestick.
s.cacheMtx.Lock()
mktCaches := s.marketCaches[mktName]
if mktCaches == nil {
s.cacheMtx.Unlock()
return nil, fmt.Errorf("unknown market %q", mktName)
}
epochDur := s.epochDurations[mktName]
startStamp := epochIdx * epochDur
endStamp := startStamp + epochDur
for _, cache := range mktCaches {
cache.Add(&candles.Candle{
addCandle := func() (change24 float64, vol24 uint64, err error) {
s.cacheMtx.Lock()
defer s.cacheMtx.Unlock()
mktCaches := s.marketCaches[mktName]
if mktCaches == nil {
return 0, 0, fmt.Errorf("unknown market %q", mktName)
}
epochDur := s.epochDurations[mktName]
startStamp := epochIdx * epochDur
endStamp := startStamp + epochDur
var cache5min *candles.Cache
const fiveMins = uint64(time.Minute * 5 / time.Millisecond)
candle := &candles.Candle{
StartStamp: startStamp,
EndStamp: endStamp,
MatchVolume: stats.MatchVolume,
Expand All @@ -137,10 +132,24 @@ func (s *DataAPI) ReportEpoch(base, quote uint32, epochIdx uint64, stats *matche
LowRate: stats.LowRate,
StartRate: stats.StartRate,
EndRate: stats.EndRate,
})
}
for dur, cache := range mktCaches {
if dur == fiveMins {
cache5min = cache
}
cache.Add(candle)
}
if cache5min == nil {
return 0, 0, fmt.Errorf("no 5 minute cache")
}
change24, vol24 = cache5min.Delta(time.Now().Add(-time.Hour * 24))
return
}

change24, vol24, err := addCandle()
if err != nil {
return nil, err
}
change24, vol24 := s.cache5min.Delta(time.Now().Add(-time.Hour * 24))
s.cacheMtx.Unlock()

// Encode the spot price.
spot := &msgjson.Spot{
Expand Down

0 comments on commit 268021b

Please sign in to comment.