flowchart TB
A([Start]) --> TG[Time gate]
TG -->|After market close| OUT[Stop: after market close]
TG -->|Pass| SEL{Positions ready to roll?}
SEL -->|No| OUT
SEL -->|Yes| FP[First position]
FP --> INC[Increment roll attempts; set trade ready]
INC --> TPL[Build roll template]
TPL --> TPLV{Template valid}
TPLV -->|No| OUT
TPLV --> TRY[Try roll candidates in order]
TRY --> CAND{Candidate viable}
CAND -->|No| NEXT([Outcome: no viable candidate this pass])
CAND -->|Yes| ORD[Build roll order]
ORD --> NRR[Not-open-ready checks]
NRR -->|Fail| OUT
NRR --> ORC[Final order readiness checks]
ORC -->|Fail| OUT
ORC --> LRC[Leg readiness]
LRC -->|Fail| OUT
LRC --> PLACE[Place order]
PLACE --> SUC{Placed successfully}
SUC -->|Yes| DONE([Outcome: roll order placed; flags cleared])
SUC -->|No| OUT
classDef step fill:#111827,stroke:#94a3b8,color:#e5e7eb,stroke-width:1px;
classDef gate fill:#0e1729,stroke:#22d3ee,color:#e5e7eb,stroke-width:1px;
classDef out fill:transparent,stroke:#94a3b8,color:#cbd5e1,stroke-dasharray:5 3;
class A,TG,SEL,FP,INC,TPL,TPLV,TRY,CAND,ORD,NRR,ORC,LRC,PLACE,SUC,DONE,NEXT step;
class SEL,TPLV,CAND,SUC gate;
class OUT out;