Skip to content

feat: add allow_short_selling parameter to StockTradingEnv#1407

Open
ujjwaldhaka71 wants to merge 2 commits into
AI4Finance-Foundation:masterfrom
ujjwaldhaka71:fix/allow-short-selling-toggle
Open

feat: add allow_short_selling parameter to StockTradingEnv#1407
ujjwaldhaka71 wants to merge 2 commits into
AI4Finance-Foundation:masterfrom
ujjwaldhaka71:fix/allow-short-selling-toggle

Conversation

@ujjwaldhaka71
Copy link
Copy Markdown

Summary

Adds an allow_short_selling parameter to StockTradingEnv that prevents the RL agent from taking short positions when set to False.

Fixes #1255

Changes

  • Added allow_short_selling: bool = True parameter to StockTradingEnv.__init__()
  • When False, action space is restricted from [-1, 1] to [0, 1]
  • Negative actions are clipped to 0 as a safety net in step()
  • Default is True — fully backward compatible, no existing behavior changes
  • Updated class docstring with new parameter documentation

How to Use

env = StockTradingEnv(
    ...,
    allow_short_selling=False,  # Prevents short positions
)

Testing

  • Verified action space bounds change correctly ([0, 1] when disabled, [-1, 1] when enabled)
  • Verified negative actions are clipped when short selling is disabled
  • Verified backward compatibility with default True — no behavior changes for existing users

ujjwaldhaka71 and others added 2 commits March 28, 2026 01:21
Adds a boolean parameter allow_short_selling (default True) to
StockTradingEnv that restricts the action space to [0, 1] and clips
negative actions when set to False. This prevents the RL agent from
taking short positions, which is required for many real-world
trading scenarios.

Fixes AI4Finance-Foundation#1255
Copy link
Copy Markdown

@atharvajoshi01 atharvajoshi01 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple things I noticed while reading through this:

  1. self.allow_short_selling is referenced in the __init__ before it gets assigned. The action space setup uses it around line 72, but the actual assignment self.allow_short_selling = allow_short_selling happens later around line 90. This will raise an AttributeError on instantiation. Moving the assignment above the action space block should fix it.

  2. The clipping logic in step() clips all negative actions to 0 when short selling is disabled. But negative actions are what trigger _sell_stock — so this prevents the agent from selling any position at all, not just short selling. The agent can only buy and hold, which isn't the intended behavior. "No short selling" should mean the agent can't go below 0 shares, not that it can't sell. _sell_stock already has a guard that clips to available shares, so it might be enough to just remove the action space change and the clip, and instead add a check in _sell_stock that prevents holdings from going negative.

  3. No tests included for the new parameter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Is there a way to prevent the FinRL model from doing any Short selling

2 participants