torchtrade icon indicating copy to clipboard operation
torchtrade copied to clipboard

Live envs: surface exchange-status staleness to the policy and truncate on a prolonged outage

Open BY571 opened this issue 3 months ago • 2 comments

Follow-up to #270, which makes an unknown exchange status stop reading as a flat account. This covers what the policy sees during an outage, deliberately split out because it changes the observation contract.

Why separate

#270 is pure safety: no observation-spec change, no effect on trained checkpoints. Everything below alters what the env emits, so it invalidates policies trained before it and needs its own review.

1. The observation cannot say "I don't know"

account_state is a fixed 6-element vector shared with the offline envs. During an outage the honest answer is neither "flat" nor a confirmed open position. #270 falls back to the last known cached state, which is the best available truth but is silently stale.

There is no room for a staleness flag inside the 6 elements without breaking offline/live parity — the offline envs have no analogous concept, so a 7th slot would desync them. The right shape is a separate additive observation key (e.g. status_unknown) alongside account_state.

2. A prolonged outage should truncate, not terminate

All five live envs currently hardcode truncated=torch.tensor([False]), so the channel is dead weight. A long outage should end the episode via truncated=True, never terminated=True: value estimators read terminated as "true return-to-go is 0" and truncated as "bootstrap from the final observation". Terminating would teach the critic that an unreachable API and a blown account carry the same value target.

Suggested shape: a consecutive-unknown counter mirroring the existing hold_counter in PositionState, with a config threshold mirroring bankrupt_threshold/done_on_bankruptcy in core/live.py.

3. Unverified, needs checking first

Whether SyncDataCollector's auto_reset handles truncated-without-terminated correctly for these stateful, non-vectorized live envs. Confirm against how examples/ actually drives them before relying on the semantics.

BY571 avatar Aug 07 '26 22:08 BY571

Part 3 checked — and the premise needs updating before parts 1 and 2 are designed

The issue asks whether SyncDataCollector's auto_reset handles truncated-without-terminated for these envs. Verified against the pinned torchrl (0.13.2), and three things are off:

1. There is no auto_reset parameter. Collector.__init__ exposes reset_at_each_iter, reset_when_done and set_truncated — no auto_reset. So the specific mechanism the issue is worried about does not exist in the version this repo pins.

2. SyncDataCollector is not what the live examples use. torchrl.collectors in 0.13.2 has no SyncDataCollector attribute at all (AttributeError), and every live driver — examples/online_rl/ppo/live.py:191, dqn/live.py, rule_based/live.py, llm/local/live.py — constructs Collector(env, policy, frames_per_batch=1, ...). The only auto_reset=True in the tree is examples/online_rl/dqn/utils.py:456, on the offline path.

3. set_truncated is the parameter that actually matters, and it cuts against part 2. Its documented behaviour:

if True, the truncated signals (and corresponding "done" but not "terminated") will be set to True when the last frame of a rollout is reached. If no "truncated" key is found, an exception is raised.

Two consequences for the design:

  • With frames_per_batch=1 — what every live driver uses — a collector-set truncation would fire on every step, so an outage-driven truncated=True must be distinguishable from a batch-boundary one. The counter in PositionState that the issue proposes gives you that, but the observation consumer needs to know which it is looking at.
  • The "if no truncated key is found, an exception is raised" clause is why the key cannot simply be dropped from the five live envs' specs. Note that today only polymarket/env.py:257 declares truncated in its full_done_spec; I could not find the hardcoded truncated=torch.tensor([False]) the issue describes in the five futures/alpaca envs, so that part of the premise is worth re-confirming against current main before building on it.

On the split: part 2 (truncate on outage) does not obviously invalidate checkpoints the way part 1 does — truncated is already part of the done contract, and emitting True sometimes changes episode boundaries rather than the observation shape. Part 1 (the status_unknown key) is the one that changes what the policy sees. If you want, those could ship separately, with part 2 first.

I have not implemented any of it: part 1 is explicitly a checkpoint-invalidating decision, and part 2's semantics depend on the set_truncated/frames_per_batch=1 interaction above, which is a design call rather than a patch.

BY571 avatar Aug 18 '26 04:08 BY571

Section 3 is answered, and section 1's premise does not match what shipped. Both verified by execution.

§3: the collector handles truncated-without-terminated correctly — with one trap

Drove a real live env (alpaca, repo mocks) through both env.rollout and torchrl.collectors.Collector, emitting truncated=True with terminated=False every third step:

frame 0: done=0 term=0 trunc=0
frame 1: done=0 term=0 trunc=0
frame 2: done=1 term=0 trunc=1     <- collector resets, terminated stays 0

terminated survives as 0, which is the whole point — the critic bootstraps from the final observation instead of being taught that an unreachable API and a blown account have the same value target. So the semantics this issue wants are safe to rely on.

The trap: setting only truncated=True and letting TorchRL fill the rest does not work:

frame 2: done=0 term=0 trunc=1     <- episode never ends, collector never resets

TorchRL does not derive done from truncated in _step output. _step must write done = terminated | truncated itself. A naive implementation would produce exactly the dead channel this issue is about — truncation set, silently ignored — and no existing test would catch it, since truncated has been write-only since #313.

§1: there is no cached-state fallback to flag as stale

The issue says "#270 falls back to the last known cached state, which is the best available truth but is silently stale." That is not what #270 shipped. It closed via #296, which added ObservationFailurePolicy with exactly two values:

  • HALT — raise LiveObservationHalt
  • FLATTEN — attempt close_position(), then raise

There is no third path that continues with stale state, and nothing in torchtrade/ catches LiveObservationHalt (grep -rn "except LiveObservationHalt" torchtrade/ → nothing). So today an outage does not produce a stale observation — it produces an uncaught exception that kills the process.

That means §1 and §2 are coupled, and the issue is really asking for a behaviour that does not exist yet: a grace period during which the env keeps stepping on stale state (emitting status_unknown=1) before truncating. You cannot have "the observation says I don't know" without first deciding the env may continue at all.

That is a risk-posture decision, not an implementation detail — it is the difference between "refuse to act when we cannot see the account" (today, and the explicit point of #296) and "keep trading for N bars on last-known state." I am not going to pick it silently. Asking before building.

BY571 avatar Aug 24 '26 20:08 BY571