Add PPO agent to trading pipeline using stable-baselines3 for adaptive position sizing and prediction trust - #162
Merged
Merged
Conversation
Closed
Agent-Logs-Url: https://github.com/ch55secake/hyperion/sessions/512a6a90-c566-4215-a4ff-3077a163a91f Co-authored-by: ch55secake <87881861+ch55secake@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add PPO to the pipeline for position sizing and training
Add PPO agent to trading pipeline for adaptive position sizing and prediction trust
Apr 19, 2026
ch55secake
reviewed
Apr 20, 2026
ch55secake
marked this pull request as ready for review
April 20, 2026 09:13
Agent-Logs-Url: https://github.com/ch55secake/hyperion/sessions/2acc396e-da29-4e8b-8657-fe6273e5da50 Co-authored-by: ch55secake <87881861+ch55secake@users.noreply.github.com>
Copilot
AI
changed the title
Add PPO agent to trading pipeline for adaptive position sizing and prediction trust
Add PPO agent to trading pipeline using stable-baselines3 for adaptive position sizing and prediction trust
Apr 20, 2026
Owner
|
@copilot Resolve the conflicts |
…lict Co-authored-by: ch55secake <87881861+ch55secake@users.noreply.github.com>
Contributor
Author
Resolved in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Adds a
stable-baselines3-backed PPO (Proximal Policy Optimisation) agent to the simulation pipeline. The agent observes ML model predictions alongside market state and learns online whether to trust those predictions before executing trades — addressing position sizing and stock selection via reinforcement learning rather than fixed heuristics.The implementation uses
stable_baselines3.PPOfor all neural-network and optimisation logic, with a minimalgymnasium.Envwrapper for SB3 model initialisation. Online learning is preserved by manually populating SB3'sRolloutBufferstep-by-step and callingmodel.train()after each rollout window.Changes in this pull request
src/simulation/strategy/ppo.py— New module containing:_TradingEnv: Minimalgymnasium.Envwith a 5-dim continuous observation space and 3-action discrete action space, used to initialise the SB3 PPO modelPPOAgent: Wrapsstable_baselines3.PPO; collects transitions viastore(), populates SB3'sRolloutBuffer, and triggersmodel.train()updates; exposesselect_action,value, andbuffer_sizefor use by the strategyPPOStrategy: WrapsPPOAgentin theStrategyinterface, registered as"ppo"inStrategyRegistry; state vector includes raw MLpred_returnso the agent learns signal reliability over timesrc/simulation/strategy/__init__.py— Import bothppoandregime_awaremodules (conflict with main resolved; both imports are now present)pyproject.toml/uv.lock— Addedstable-baselines3==2.8.0dependency (transitively addstorch==2.11.0andgymnasium==1.2.3)tests/simulation/test_strategies.py— 15 new tests (TestPPOAgent,TestPPOStrategy) covering action sampling, buffer lifecycle, gradient update stability, state construction, registry integration, and an end-to-endTradingSimulatorsmoke test;TestStrategyRegistryupdated to include"ppo"in expected strategy setUsage via existing registry:
The agent's observation vector
[pred_return, position_flag, portfolio_ratio, days_held_norm, abs_pred_return]means it learns not just when to trade, but when the upstream ML signal is worth acting on.