Vega

Rolling win rate over 20 trades

A window mean turns the binary win/loss series into a rolling win rate: the curve only starts once the window is full (filter) and is compared against the 50% rule.

Prerequisites

Vega-Lite v5

Vega-Lite
{
  "$schema": "https://vega.github.io/schema/vega-lite/v5.json",
  "data": {
    "values": [
      { "n": 1, "win": 1 }, { "n": 2, "win": 1 }, { "n": 3, "win": 0 }, { "n": 4, "win": 1 }, { "n": 5, "win": 0 },
      { "n": 6, "win": 1 }, { "n": 7, "win": 1 }, { "n": 8, "win": 1 }, { "n": 9, "win": 0 }, { "n": 10, "win": 1 },
      { "n": 11, "win": 0 }, { "n": 12, "win": 1 }, { "n": 13, "win": 1 }, { "n": 14, "win": 0 }, { "n": 15, "win": 1 },
      { "n": 16, "win": 1 }, { "n": 17, "win": 0 }, { "n": 18, "win": 0 }, { "n": 19, "win": 1 }, { "n": 20, "win": 1 },
      { "n": 21, "win": 1 }, { "n": 22, "win": 0 }, { "n": 23, "win": 1 }, { "n": 24, "win": 1 }, { "n": 25, "win": 1 },
      { "n": 26, "win": 0 }, { "n": 27, "win": 1 }, { "n": 28, "win": 1 }, { "n": 29, "win": 0 }, { "n": 30, "win": 1 }
    ]
  },
  "transform": [
    { "window": [{ "op": "mean", "field": "win", "as": "wr" }], "frame": [-19, 0], "sort": [{ "field": "n" }] },
    { "calculate": "datum.wr * 100", "as": "wr_pct" },
    { "filter": "datum.n >= 20" }
  ],
  "height": 250,
  "layer": [
    {
      "mark": { "type": "line", "color": "#00f3ff", "strokeWidth": 2.5, "point": { "filled": true, "size": 45 }, "tooltip": true },
      "encoding": {
        "x": { "field": "n", "type": "quantitative", "title": "Numéro de trade", "axis": { "tickMinStep": 1 } },
        "y": { "field": "wr_pct", "type": "quantitative", "title": "Win rate glissant (%)", "scale": { "domain": [0, 100] } },
        "tooltip": [
          { "field": "n", "title": "Trade" },
          { "field": "wr_pct", "type": "quantitative", "format": ".0f", "title": "Win rate 20 trades (%)" }
        ]
      }
    },
    {
      "mark": { "type": "rule", "color": "#9aa4b2", "strokeDash": [5, 4] },
      "encoding": { "y": { "datum": 50 } }
    },
    {
      "mark": { "type": "text", "align": "left", "dx": 4, "dy": -7, "color": "#9aa4b2", "text": "50 % : pile ou face" },
      "encoding": { "y": { "datum": 50 }, "x": { "value": 0 } }
    }
  ]
}
TradingWin ratewindowMoyenne glissante

Related snippets

Back to the Data Lab