Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions plugins/tradeMetricsHandler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package plugins

import (
"github.com/stellar/kelp/model"
)

// TradeMetricsHandler tracks the number of trades
type TradeMetricsHandler struct {
trades []model.Trade
}

// MakeTradeMetricsHandler is a factory method for the TradeMetricsHandler
func MakeTradeMetricsHandler() *TradeMetricsHandler {
return &TradeMetricsHandler{
trades: []model.Trade{},
}
}

// Reset sets the handler's trades to empty.
func (h *TradeMetricsHandler) Reset() {
h.trades = []model.Trade{}
}

// GetTrades returns all stored trades.
func (h *TradeMetricsHandler) GetTrades() []model.Trade {
return h.trades
}

// HandleFill handles a new trade
// Implements FillHandler interface
func (h *TradeMetricsHandler) HandleFill(trade model.Trade) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to test this tradeMetricsHandler. For now let's have one simple test case, where we add 3 trades:

  • buy XLM/USDT
  • buy XLM/USDT
  • sell XLM/USDT

then we assert that the computed values on the struct returned from ComputeTradeMetrics is correct.

this TestComputeTradeMetrics would be very simple and would ensure our calculations are correct.

h.trades = append(h.trades, trade)
return nil
}
53 changes: 53 additions & 0 deletions support/metrics/metricsTracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"runtime/debug"
"time"

"github.com/stellar/kelp/plugins"
"github.com/stellar/kelp/support/networking"
)

Expand All @@ -31,6 +32,7 @@ type MetricsTracker struct {
botStartTime time.Time
isDisabled bool
updateEventSentTime *time.Time
handler *plugins.TradeMetricsHandler
}

// TODO DS Investigate other fields to add to this top-level event.
Expand Down Expand Up @@ -205,6 +207,7 @@ func MakeMetricsTracker(
botStartTime: botStartTime,
isDisabled: isDisabled,
updateEventSentTime: nil,
handler: plugins.MakeTradeMetricsHandler(),
}, nil
}

Expand Down Expand Up @@ -304,3 +307,53 @@ func (mt *MetricsTracker) sendEvent(eventType string, eventProps interface{}) er
}
return nil
}

// ComputeTradeMetrics computes various trade metrics and outputs a map that can be combined with other metrics.
func (mt *MetricsTracker) ComputeTradeMetrics() map[string]interface{} {
// TODO NS Ensure proper locking around trades on metrics handler
trades := mt.handler.GetTrades()
mt.handler.Reset()

totalBaseVolume := 0.0
totalQuoteVolume := 0.0
totalPrice := 0.0
netBaseVolume := 0.0
netQuoteVolume := 0.0

numTrades := float64(len(trades))
for _, t := range trades {
base := t.Volume.AsFloat()
price := t.Price.AsFloat()
quote := base * price

totalBaseVolume += base
totalPrice += price
totalQuoteVolume += quote

if t.OrderAction.IsBuy() {
netBaseVolume += base
netQuoteVolume -= quote
} else {
netBaseVolume -= base
netQuoteVolume -= quote
}
}

avgTradeSizeBase := totalBaseVolume / numTrades
avgTradeSizeQuote := totalQuoteVolume / numTrades
avgTradePrice := totalPrice / numTrades

tradeMetrics := map[string]interface{}{
"total_base_volume": totalBaseVolume,
"total_quote_volume": totalQuoteVolume,
"net_base_volume": netBaseVolume,
"net_quote_volume": netQuoteVolume,
"num_trades": numTrades,
"avg_trade_size_base": avgTradeSizeBase,
"avg_trade_size_quote": avgTradeSizeQuote,
"avg_trade_price": avgTradePrice,
"vwap": totalQuoteVolume / totalBaseVolume,
}

return tradeMetrics
}
2 changes: 2 additions & 0 deletions trader/trader.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ func (t *Trader) synchronizeFetchBalancesOffersTrades() error {
buyingAOffers2,
) {
// this is the only success case
t.metricsTracker.ResetHandlers()
t.metricsTracker.ReadIntoHandlers(trades)
t.setBalances(baseBalance1, quoteBalance1)
t.setExistingOffers(sellingAOffers1, buyingAOffers1)
return nil
Expand Down