Skip to content
This repository has been archived by the owner on Feb 1, 2024. It is now read-only.

Add metrics for trade tracking. #574

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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 {
debnil marked this conversation as resolved.
Show resolved Hide resolved
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 {
debnil marked this conversation as resolved.
Show resolved Hide resolved
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"
debnil marked this conversation as resolved.
Show resolved Hide resolved
"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(),
debnil marked this conversation as resolved.
Show resolved Hide resolved
}, 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{} {
debnil marked this conversation as resolved.
Show resolved Hide resolved
// TODO NS Ensure proper locking around trades on metrics handler
debnil marked this conversation as resolved.
Show resolved Hide resolved
trades := mt.handler.GetTrades()
mt.handler.Reset()

totalBaseVolume := 0.0
totalQuoteVolume := 0.0
totalPrice := 0.0
netBaseVolume := 0.0
netQuoteVolume := 0.0
debnil marked this conversation as resolved.
Show resolved Hide resolved

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
debnil marked this conversation as resolved.
Show resolved Hide resolved

tradeMetrics := map[string]interface{}{
debnil marked this conversation as resolved.
Show resolved Hide resolved
"total_base_volume": totalBaseVolume,
debnil marked this conversation as resolved.
Show resolved Hide resolved
"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
}