Skip to content

State Machine Logic

How the RQF indicator tracks setup progression through states.

Overview

The indicator uses a state machine to track where we are in the RQF sequence:

stateDiagram-v2
    [*] --> State0: Start
    State0 --> State1: BoS Detected
    State1 --> State2: Sweep Detected
    State1 --> State0: Timeout
    State2 --> State3: MSS Confirmed
    State2 --> State1: New Sweep (failed MSS)
    State2 --> State0: Timeout
    State3 --> State0: Entry/Invalidation

State Definitions

State Name Looking For Next Event
0 Idle BoS BoS detected → State 1
1 BoS Sweep Sweep detected → State 2
2 Sweep MSS MSS confirmed → State 3
3 Ready Entry Entry/invalidation → State 0

State 0: Looking for BoS

What happens:

  • Monitoring for structure breaks
  • No active setup

Transition to State 1:

  • Bearish BoS detected (for bullish setup)
  • Bullish BoS detected (for bearish setup)
if rqfBullState == 0
    if bearishBosDetected and barstate.isconfirmed
        rqfBullState := 1
        bosLevel := structureLow
        bosBar := bar_index

State 1: Looking for Sweep

What happens:

  • BoS has occurred
  • Watching for price to sweep the BoS level
  • Tracking Trigger High (TH) / Trigger Low (TL)

Transition to State 2:

  • Sweep detected (wick beyond, close inside)

Reset to State 0:

  • Timeout (100 bars default)
if rqfBullState == 1
    // Timeout check
    if bar_index - bosBar > sweepTimeout
        rqfBullState := 0  // Reset

    // Sweep check
    else if low < bosLevel and close > bosLevel
        rqfBullState := 2
        sweepLevel := bosLevel
        triggerHigh := ta.highest(high, bar_index - bosBar)

State 2: Looking for MSS

What happens:

  • Sweep has occurred (S1)
  • Watching for price to break TH/TL
  • May see additional sweeps (S2, S3...)

Transition to State 3:

  • MSS confirmed (close beyond TH/TL)

Stay in State 2:

  • New sweep occurs (current becomes failed: f1, f2...)

Reset to State 0:

  • Timeout
if rqfBullState == 2
    // New sweep (failed MSS)
    if low < bosLevel and close > bosLevel and bar_index > sweepBar
        // Mark current as failed, track new sweep
        failedMssCount += 1
        sweepCount += 1
        // Recalculate TH

    // MSS check
    else if close > triggerHigh and barstate.isconfirmed
        rqfBullState := 3
        // Draw Fib, find OB, etc.

State 3: Ready for Entry

What happens:

  • MSS confirmed
  • Fibonacci drawn
  • Order Block identified
  • Watching for entry in discount zone

Transition to State 0:

  • Entry signal fired
  • Setup invalidated (price closes beyond 0.0)
  • Setup completed (price reaches 1.0 target)
if rqfBullState == 3
    // Entry signal
    if inDiscountZone and hasConfluence
        // Fire entry signal

    // Invalidation
    if close < fib_0
        rqfBullState := 0
        // Cleanup drawings

    // Completion
    if close > fib_1
        rqfBullState := 0
        // Cleanup drawings

Parallel State Machines

The indicator runs two parallel state machines:

Machine Direction Trigger Entry
rqfBullState Looking for LONG Bearish BoS Discount zone
rqfBearState Looking for SHORT Bullish BoS Premium zone

Both can be active simultaneously (looking for setups in both directions).

Timeouts

Transition Timeout Reason
State 1 → 0 100 bars No sweep found
State 2 → 0 100 bars No MSS found

Timeouts prevent getting stuck in intermediate states indefinitely.

Failed MSS Tracking

When in State 2, if a new sweep occurs before MSS:

  1. Current sweep labeled as f1 (failed)
  2. New sweep becomes S2
  3. Trigger level recalculated
  4. Still in State 2
    ════════════════════════════ BoS Level
              │         │
          ┌───┴───┐ ┌───┴───┐
          │  f1   │ │  S2   │
          └───────┘ └───────┘
              ▲         ▲
              │         │
        Failed MSS   Current sweep

Display in Info Table

┌─────────────────┐
│ Long Setup  S2  │ ← State 2, second sweep
│ Short Setup --- │ ← State 0
│ Failed MSS  1   │ ← One failed sweep
└─────────────────┘

Code Structure

// State variables
var int rqfBullState = 0
var int rqfBearState = 0

// State 1 data
var float bosLevel = na
var int bosBar = na

// State 2 data
var float sweepLevel = na
var float triggerHigh = na
var int sweepCount = 0
var int failedMssCount = 0

// State 3 data
var float fib_0 = na
var float fib_1 = na

Best Practices

  1. Always check barstate.isconfirmed for state transitions
  2. Store all relevant data at transition time
  3. Include timeout handling for every intermediate state
  4. Clean up drawings on reset
  5. Reset all variables when returning to State 0