-
Notifications
You must be signed in to change notification settings - Fork 261
Add metrics for trade tracking. #574
base: master
Are you sure you want to change the base?
Changes from 5 commits
51b70ac
2d78a35
92018eb
7a113bb
69bc642
40c5d20
79364e7
6876075
ebdc97a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| 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.
|
||
| return &TradeMetricsHandler{ | ||
| trades: []model.Trade{}, | ||
| } | ||
| } | ||
|
|
||
| // Reset sets the handler's trade counter to zero. | ||
| func (h *TradeMetricsHandler) Reset() { | ||
| h.trades = []model.Trade{} | ||
| } | ||
|
|
||
| // Read stores new trades internally. | ||
| func (h *TradeMetricsHandler) Read(newTrades []model.Trade) { | ||
|
debnil marked this conversation as resolved.
Outdated
|
||
| for _, nt := range newTrades { | ||
| h.trades = append(h.trades, nt) | ||
| } | ||
| } | ||
|
|
||
| // NumTrades returns the number of trades. | ||
| func (h *TradeMetricsHandler) NumTrades() int { | ||
|
debnil marked this conversation as resolved.
Outdated
|
||
| return len(h.trades) | ||
| } | ||
|
|
||
| // TotalBaseVolume returns the total base volume. | ||
| func (h *TradeMetricsHandler) TotalBaseVolume() (total float64) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should add additional metrics here -- maybe can return a map instead of individual metrics and call the function (Note: base units is
anything else you can think of that would be important to capture for trades?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PS: we will need to combine this with
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is everything (including that VWAP formula) -- super helpful, thank you!! I've made these changes. |
||
| for _, t := range h.trades { | ||
| total += t.Volume.AsFloat() | ||
| } | ||
| return | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,8 @@ import ( | |
| "runtime/debug" | ||
| "time" | ||
|
|
||
| "github.com/stellar/kelp/api" | ||
| "github.com/stellar/kelp/model" | ||
| "github.com/stellar/kelp/support/networking" | ||
| ) | ||
|
|
||
|
|
@@ -31,6 +33,9 @@ type MetricsTracker struct { | |
| botStartTime time.Time | ||
| isDisabled bool | ||
| updateEventSentTime *time.Time | ||
|
|
||
| // uninitialized | ||
| handlers []api.TradeMetricsHandler | ||
|
debnil marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| // TODO DS Investigate other fields to add to this top-level event. | ||
|
|
@@ -81,6 +86,8 @@ type commonProps struct { | |
| OperationalBufferNonNativePct float64 `json:"operational_buffer_non_native_pct"` | ||
| SimMode bool `json:"sim_mode"` | ||
| FixedIterations uint64 `json:"fixed_iterations"` | ||
| NumTradesSinceLastUpdate int `json:"num_trades_since_last_update"` | ||
|
debnil marked this conversation as resolved.
Outdated
|
||
| BaseVolumeTradesSinceLastUpdate float64 `json:"base_volume_trades_since_last_update"` | ||
| } | ||
|
|
||
| // updateProps holds the properties for the update Amplitude event. | ||
|
|
@@ -304,3 +311,41 @@ func (mt *MetricsTracker) sendEvent(eventType string, eventProps interface{}) er | |
| } | ||
| return nil | ||
| } | ||
|
|
||
| // RegisterHandler adds an internal handler. | ||
| func (mt *MetricsTracker) RegisterHandler(handler api.TradeMetricsHandler) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we don't need any of these functions here. instead inside sendUpdate we can call |
||
| mt.handlers = append(mt.handlers, handler) | ||
| } | ||
|
|
||
| // GetHandlers returns the handlers | ||
| func (mt *MetricsTracker) GetHandlers() []api.TradeMetricsHandler { | ||
| return mt.handlers | ||
| } | ||
|
|
||
| // ResetHandlers resets all handlers | ||
| func (mt *MetricsTracker) ResetHandlers() { | ||
| for _, h := range mt.handlers { | ||
| h.Reset() | ||
| } | ||
| } | ||
|
|
||
| // ReadIntoHandlers reads to all handlers | ||
| func (mt *MetricsTracker) ReadIntoHandlers(trades []model.Trade) { | ||
| for _, h := range mt.handlers { | ||
| h.Read(trades) | ||
| } | ||
| } | ||
|
|
||
| func (mt *MetricsTracker) getTotalVolume() (total float64) { | ||
| for _, h := range mt.handlers { | ||
| total += h.TotalBaseVolume() | ||
| } | ||
| return | ||
| } | ||
|
|
||
| func (mt *MetricsTracker) getTotalNumTrades() (total int) { | ||
| for _, h := range mt.handlers { | ||
| total += h.NumTrades() | ||
| } | ||
| return | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.