Skip to content

Commit f6a0858

Browse files
exchanges: fix volume calculation bug in *ExchangeBot.processState
1 parent a15db1f commit f6a0858

File tree

4 files changed

+21
-25
lines changed

4 files changed

+21
-25
lines changed

cmd/dcrdata/public/js/controllers/market_controller.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@ const prettyDurations = {
3333
}
3434
const exchangeLinks = {
3535
CurrencyPairDCRBTC: {
36-
dcrdex: 'https://dex.decred.org'
36+
dcrdex: 'https://dex.decred.org',
37+
coinex: 'https://www.coinex.com/en/exchange/DCR-BTC'
3738
},
3839
CurrencyPairDCRUSDT: {
3940
binance: 'https://www.binance.com/en/trade/DCR_USDT',
4041
dcrdex: 'https://dex.decred.org',
41-
mexc: 'https://www.mexc.com/exchange/DCR_USDT'
42+
mexc: 'https://www.mexc.com/exchange/DCR_USDT',
43+
kucoin: 'https://www.kucoin.com/trade/DCR-USDT',
44+
coinex: 'https://www.coinex.com/en/exchange/DCR-USDT'
4245
}
4346
}
4447
const CurrencyPairDCRUSDT = 'DCR-USDT'

cmd/dcrdata/views/extras.tmpl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
<link rel="preload" href="/images/connected.svg?x=65tv3" as="image" type="image/svg+xml" />
6868
<link rel="preload" href="/images/disconnected.svg?x=65tv3" as="image" type="image/svg+xml" />
6969

70-
<link href="/dist/css/style.16ae332755b66248.css" rel="stylesheet">
70+
<link href="/dist/css/style.0fa8da7a4d68be40.css" rel="stylesheet">
7171

7272
<script src="/js/vendor/turbolinks.min.js?v=65DCG"></script>
7373
</head>
@@ -188,12 +188,12 @@
188188
</span>
189189
</div>
190190
<script
191-
src="/dist/js/4.9eec6eb13cc00a3d.bundle.js"
191+
src="/dist/js/4.06b06a4759f2b4ad.bundle.js"
192192
data-turbolinks-eval="false"
193193
data-turbolinks-suppress-warning
194194
></script>
195195
<script
196-
src="/dist/js/app.a882ac4b6d92599f.bundle.js"
196+
src="/dist/js/app.fbea774a52e72f1a.bundle.js"
197197
data-turbolinks-eval="false"
198198
data-turbolinks-suppress-warning
199199
></script>

exchanges/bot.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -819,22 +819,15 @@ func (bot *ExchangeBot) cachedChartVersion(chartId string) int {
819819

820820
// processState is a helper function to process a slice of ExchangeState into a
821821
// price, and optionally a volume sum, and perform some cleanup along the way.
822-
// If volumeAveraged is false, all exchanges are given equal weight in the avg.
823822
// If exchange is invalid, a bool false is returned as a last return value.
824-
func (bot *ExchangeBot) processState(token, code string, states map[CurrencyPair]*ExchangeState, volumeAveraged bool) (float64, float64, bool) {
823+
func (bot *ExchangeBot) processState(token, code string, states map[CurrencyPair]*ExchangeState) (float64, float64, bool) {
825824
oldestValid := time.Now().Add(-bot.RequestExpiry)
826825
if bot.Exchanges[token].LastUpdate().Before(oldestValid) {
827826
return 0, 0, false
828827
}
829828

830829
var priceAccumulator, volSum float64
831830
for currencyPair, state := range states {
832-
volume := 1.0
833-
if volumeAveraged {
834-
volume = state.Volume
835-
}
836-
volSum += volume
837-
838831
// Convert price to bot.Index.
839832
price := state.Price
840833
switch currencyPair {
@@ -843,13 +836,13 @@ func (bot *ExchangeBot) processState(token, code string, states map[CurrencyPair
843836
case CurrencyPairDCRUSDT:
844837
price = bot.indexPrice(USDTIndex, code) * price
845838
}
846-
if price == 0 { // missing index price for currencyPair.
847-
return 0, 0, false
839+
if price == 0 {
840+
continue // missing index price for currencyPair so let's skip this one.
848841
}
849842

850-
priceAccumulator += volume * price
843+
volSum += state.Volume
844+
priceAccumulator += state.Volume * price
851845
}
852-
853846
if volSum == 0 {
854847
return 0, 0, true
855848
}
@@ -927,7 +920,7 @@ func (bot *ExchangeBot) updateIndices(update *IndexUpdate) error {
927920
func (bot *ExchangeBot) dcrPriceAndVolume(code string) (float64, float64) {
928921
var dcrPrice, volume, nSources float64
929922
for token, xcStates := range bot.currentState.DCRExchanges {
930-
processedDcrPrice, processedVolume, ok := bot.processState(token, code, xcStates, true)
923+
processedDcrPrice, processedVolume, ok := bot.processState(token, code, xcStates)
931924
if !ok {
932925
continue
933926
}

exchanges/exchanges.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ func (sticks Candlesticks) needsUpdate(bin candlestickKey) bool {
340340
type BaseState struct {
341341
Price float64 `json:"price"`
342342
// BaseVolume is poorly named. This is the volume in terms of (usually) BTC
343-
// or USDT, not the base asset of any particular market.
343+
// or USDT, not the base asset of any particular market. TODO: Rename.
344344
BaseVolume float64 `json:"base_volume,omitempty"`
345345
Volume float64 `json:"volume,omitempty"`
346346
Change float64 `json:"change,omitempty"`
@@ -1985,7 +1985,7 @@ type KucoinPriceResponse struct {
19851985
VolValue string `json:"volValue"`
19861986
Last string `json:"last"`
19871987

1988-
// These are unsed fields, but left commented since they are part of the original schema.
1988+
// These are unused fields, but left commented since they are part of the original schema.
19891989
// Sequence string `json:"sequence"`
19901990
// Buy string `json:"buy"`
19911991
// Sell string `json:"sell"`
@@ -2098,7 +2098,7 @@ func (r KucoinCandlestickResponse) translate() Candlesticks {
20982098
type KucoinDepthResponse struct {
20992099
// Code string `json:"code"`
21002100
Data struct {
2101-
// These are unsed fields, but left commented since they are part of the original schema.
2101+
// These are unused fields, but left commented since they are part of the original schema.
21022102
// Time int64 `json:"time"` used
21032103
// Sequence string `json:"sequence"` used
21042104
Bids [][2]string
@@ -2235,7 +2235,7 @@ type CoinExPriceResponse struct {
22352235
Value string `json:"value"`
22362236
Volume string `json:"volume"`
22372237

2238-
// These are unsed fields, but left commented since they are part of the
2238+
// These are unused fields, but left commented since they are part of the
22392239
// original schema.
22402240
// Market string `json:"market"`
22412241
// Period int64 `json:"period"`
@@ -2273,7 +2273,7 @@ type CoinExCandlestickResponse struct {
22732273
Open string `json:"open"`
22742274
Volume string `json:"volume"`
22752275

2276-
// These are unsed fields, but left commented since they are part of the
2276+
// These are unused fields, but left commented since they are part of the
22772277
// original schema.
22782278
// Market string `json:"market"`
22792279
// Value string `json:"value"`
@@ -2331,14 +2331,14 @@ type CoinExDepthResponse struct {
23312331
Asks [][2]string `json:"asks"`
23322332
} `json:"depth"`
23332333

2334-
// These are unsed fields, but left commented since they are part of the
2334+
// These are unused fields, but left commented since they are part of the
23352335
// original schema.
23362336
// Last string `json:"last"`
23372337
// UpdatedAt int64 `json:"updated_at"`
23382338
// Checksum string `json:"checksum"`
23392339
} `json:"data"`
23402340

2341-
// These are unsed fields, but left commented since they are part of the
2341+
// These are unused fields, but left commented since they are part of the
23422342
// original schema.
23432343
// IsFull bool `json:"is_full"`
23442344
// Market string `json:"market"`

0 commit comments

Comments
 (0)