Markets — Replay Feature

Steps through historical OHLC candles one at a time on marketsReplayPage.tsx. Detects patterns on the growing visible window, filters them by type, and simulates trades in real time as the playhead advances.

Page layout:

<MarketsReplayPage>
  <ReplayControls />       ← symbol, timeframe, date, playback, pattern filters
  <ReplayChart />          ← lightweight-charts canvas
  <ReplayActiveTradeBox /> ← live P&L (null-renders when no active trade)
  <ReplayTradeLog />       ← completed trades table (null-renders when empty)
</MarketsReplayPage>

Redux State

All replay state lives in the replay slice (replaySlice.ts). It is isolated from the global ohlc slice — components must not cross-read between them.

FieldTypeDefaultDescription
symbolstring'EUR/USD'Active symbol
timeframeTimeframe'1h'Active timeframe
allCandlesOhlcCandle[][]Full loaded candle set
currentIndexnumber0Replay playhead position
isPlayingbooleanfalseTick timer running
speedMsnumber500Ms per tick
detectedPatternsFrontendPattern[][]Patterns on visible window
emergingPatternsEmergingPattern[][]Emerging patterns on visible window
replaySwingFilterHarmonicPatternType[] | 'all''all'Swing / harmonic pattern filter
replayCandlestickFilterHarmonicPatternType[] | 'all''all'Candlestick pattern filter
activeTradeReplayActiveTrade | nullnullCurrently open simulated trade
tradeLogReplayTradeLogEntry[][]Completed simulated trades

setReplayCandles resets currentIndex, detectedPatterns, emergingPatterns, replaySwingFilter, replayCandlestickFilter, activeTrade, and tradeLog.


Pattern Filter Multiselects

ReplayControls renders two PatternMultiSelect components — one for swing/harmonic patterns, one for candlestick patterns. These bind to replay-specific state, not the global ohlc filter state.

ActionSelector
setReplaySwingFilterselectReplaySwingFilter
setReplayCandlestickFilterselectReplayCandlestickFilter

Both useReplayChartPatternSync (chart overlays) and useReplayTradeSimulation (trade entry) read these selectors independently, so filtering affects both chart rendering and which patterns can trigger a simulated trade.


Simulated Trade Overlay

useReplayTradeSimulation is a side-effect hook called inside useReplayChart. It runs on every currentIndex or detectedPatterns change.

No active trade — scan for entry:

  1. Iterates detectedPatterns; skips IDs already in processedIdsRef
  2. Applies replaySwingFilter / replayCandlestickFilter
  3. Enters the first pattern whose dTime matches the current candle’s Unix timestamp
  4. Dispatches setReplayActiveTrade; records the pattern ID so it is never re-entered
  5. SL / TP levels come from the pattern’s pre-calculated sl, tp1, tp2, tp3

Active trade — check for exit:

  1. Tests candle high / low against SL and all TP levels
  2. If SL and a TP both trigger on the same candle, SL wins
  3. On exit: addReplayTradeLogEntryclearReplayActiveTrade
  4. If no exit: updates currentPips from the current close price

useReplayChart syncs activeTrade to markersPlugin.updateActiveTrades() after every change, keeping entry / SL / TP lines on the chart in sync.

ReplayActiveTrade type

FieldTypeDescription
patternTypeHarmonicPatternTypePattern that triggered entry
direction'bullish' | 'bearish'Trade direction
entryPricenumberD-point close price
entryTimenumberD-point Unix timestamp
sl / tp1numberStop-loss / first take-profit
tp2 / tp3number | nullOptional take-profit levels
currentPipsnumberLive P&L in pips (signed, 1 d.p.)

ReplayActiveTradeBox component

Renders a horizontal bar below the chart. Hidden when activeTrade is null. Shows pattern label, direction chip (green = bullish / red = bearish), Entry / SL / TP1–3 prices, and live pips coloured green (positive) or red (negative).


Trade Log

Completed trades accumulate in tradeLog. The ReplayTradeLog component renders a compact MUI sticky-header table (newest first, max-height 200 px). Hidden when the log is empty.

ColumnNotes
PatternPATTERN_TYPE_LABELS[patternType]
Directioncolour chip
EntryentryPrice to 5 d.p.
ExitexitPrice to 5 d.p.
OutcomeTP1 / TP2 / TP3 chip (success) or SL chip (error)
Pipssigned, coloured

ReplayTradeLogEntry type

FieldTypeDescription
idstring{patternType}-{entryTime}-{exitTime}
patternTypeHarmonicPatternType
direction'bullish' | 'bearish'
entryPrice / exitPricenumber
entryTime / exitTimenumberUnix timestamps
outcome'tp1' | 'tp2' | 'tp3' | 'sl'
pipsnumberSigned, rounded to 1 d.p.