torchtrade icon indicating copy to clipboard operation
torchtrade copied to clipboard

AlpacaSLTP: include_close_action produces an action slot identical to HOLD

Open BY571 opened this issue 3 months ago • 0 comments

Found while fixing the same bug class on binance/bitget in #417 (part of #288).

The defect

AlpacaSLTPConfig.include_close_action exists (alpaca/env_sltp.py:40) and is passed to the map builder (:127). Setting it True produces:

>>> create_alpaca_sltp_action_map([-0.02], [0.03], include_close_action=True)
{0: (None, None), 1: (None, None), 2: (-0.02, 0.03)}

Index 1 is the "close" slot. It is byte-identical to index 0 (HOLD).

create_alpaca_sltp_action_map returns (sl, tp) 2-tuples rather than the futures (side, sl, tp) 3-tuples, so the "close" marker has nowhere to live and is dropped. Alpaca's executor then returns at its first guard:

# alpaca/env_sltp.py:234
if action_tuple == (None, None) or self.position.current_position == 1:
    return trade_info

So enabling the flag widens the Categorical action space by one and hands the policy a slot that provably cannot do anything. The comment in alpaca's _step claiming "the close action's (None, None) targets flat" overclaims — it can never target flat.

Why it is filed separately from #417

Same family as the binance/bitget bug that #417 fixes (include_close_action on the shared futures config emitted ("close", None, None) that only bybit/okx handled), but a different mechanism. The futures venues had the marker and ignored it; alpaca loses the marker in the tuple format.

Fixing it is a design call, not a guard:

  1. Widen alpaca's map to 3-tuples and give its executor a "close" branch — consistent with the four futures venues, but changes alpaca's action-map contract and every test that reads it.
  2. Reject the flag at the boundary. Per CLAUDE.md invariant 4 ("a guard that absorbs a nonsense config is worse than no guard: it makes the nonsense silent"), raise in __post_init__ if include_close_action=True on a config whose map cannot express it.
  3. Sell-to-flat via the existing path — alpaca is long-only, so "close" means sell. There may be a cheaper encoding than a full 3-tuple migration.

(2) is the smallest correct step and could ship immediately; (1) is the consistent one.

Reproduce

from torchtrade.envs.utils.action_maps import create_alpaca_sltp_action_map as f
m = f([-0.02], [0.03], include_close_action=True)
assert m[0] == m[1]     # HOLD and CLOSE are the same action

Scope note

SLTP_FUTURES_ENVS excludes spot by construction, so #417's new cross-venue close tests are structurally blind to this — alpaca cannot be added to them without the format decision above.

BY571 avatar Aug 27 '26 12:08 BY571