feat: add allow_short_selling parameter to StockTradingEnv#1407
feat: add allow_short_selling parameter to StockTradingEnv#1407ujjwaldhaka71 wants to merge 2 commits into
Conversation
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
for more information, see https://pre-commit.ci
atharvajoshi01
left a comment
There was a problem hiding this comment.
A couple things I noticed while reading through this:
-
self.allow_short_sellingis referenced in the__init__before it gets assigned. The action space setup uses it around line 72, but the actual assignmentself.allow_short_selling = allow_short_sellinghappens later around line 90. This will raise an AttributeError on instantiation. Moving the assignment above the action space block should fix it. -
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_stockalready 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_stockthat prevents holdings from going negative. -
No tests included for the new parameter.
Summary
Adds an
allow_short_sellingparameter toStockTradingEnvthat prevents the RL agent from taking short positions when set toFalse.Fixes #1255
Changes
allow_short_selling: bool = Trueparameter toStockTradingEnv.__init__()False, action space is restricted from[-1, 1]to[0, 1]step()True— fully backward compatible, no existing behavior changesHow to Use
Testing
[0, 1]when disabled,[-1, 1]when enabled)True— no behavior changes for existing users