tenth version
Deploy / Test, build and deploy (push) Successful in 1m46s

This commit is contained in:
2026-06-08 14:58:56 +00:00
parent 20cc8506ad
commit 7626c1b831
17 changed files with 917 additions and 27 deletions
+35
View File
@@ -135,12 +135,47 @@ func orderAdverseSlippageBps(order domain.Order) (decimal.Decimal, bool) {
}
func orderReferencePrice(order domain.Order) decimal.Decimal {
if mid := rawFillMidPrice(order.RawStateJSON); mid.IsPositive() {
return mid
}
if mid := rawMidPrice(order.RawStateJSON); mid.IsPositive() {
return mid
}
return order.LimitPrice
}
func rawFillMidPrice(raw string) decimal.Decimal {
var root map[string]any
if err := json.Unmarshal([]byte(raw), &root); err != nil {
return decimal.Zero
}
if mid := fillMidFromContainer(root); mid.IsPositive() {
return mid
}
if local, ok := root["local"].(map[string]any); ok {
return fillMidFromContainer(local)
}
return decimal.Zero
}
func fillMidFromContainer(container map[string]any) decimal.Decimal {
quotes, ok := container["fill_quotes"].([]any)
if !ok || len(quotes) == 0 {
return decimal.Zero
}
for i := len(quotes) - 1; i >= 0; i-- {
quote, ok := quotes[i].(map[string]any)
if !ok {
continue
}
mid, ok := decimalFromAny(quote["mid"])
if ok && mid.IsPositive() {
return mid
}
}
return decimal.Zero
}
func rawMidPrice(raw string) decimal.Decimal {
var root map[string]any
if err := json.Unmarshal([]byte(raw), &root); err != nil {
+17
View File
@@ -26,6 +26,23 @@ func TestAverageAdverseSlippageBpsUsesLocalQuoteMid(t *testing.T) {
}
}
func TestAverageAdverseSlippageBpsPrefersFillQuoteMid(t *testing.T) {
orders := []domain.Order{{
InstrumentUID: "uid",
Side: domain.SideBuy,
LimitPrice: decimal.NewFromInt(100),
FilledLots: 1,
AvgFillPrice: decimal.NewFromFloat(102),
RawStateJSON: `{"local":{"local_quote":{"mid":"100"},"fill_quotes":[{"mid":"101"}]}}`,
UpdatedAt: time.Now().UTC(),
}}
got := AverageAdverseSlippageBps(orders, 0)
want := decimal.NewFromInt(10_000).Div(decimal.NewFromInt(101))
if got.Sub(want).Abs().GreaterThan(decimal.NewFromFloat(0.000001)) {
t.Fatalf("slippage=%s, want fill-mid based slippage", got)
}
}
func TestAverageAdverseSlippageBpsFallsBackToLimit(t *testing.T) {
orders := []domain.Order{{
InstrumentUID: "uid",