Developing an SAC-Based RL Trading Agent

We design and deploy artificial intelligence systems: from prototype to production-ready solutions. Our team combines expertise in machine learning, data engineering and MLOps to make AI work not in the lab, but in real business.
Showing 1 of 1All 1564 services
Developing an SAC-Based RL Trading Agent
Complex
~2-4 weeks
Frequently Asked Questions

AI Development Areas

AI Solution Development Stages

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1347
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1247
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    948
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1183
  • image_logo-advance_0.webp
    B2B Advance company logo design
    642
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    921

You trained a PPO agent on last year's historical data, but the current market behaves differently — the strategy is bleeding capital. Overfitting to a specific regime is the bane of RL in trading. We solve this problem with the Soft Actor-Critic (SAC) algorithm. In our projects, SAC consistently delivers a Sharpe Ratio of 1.5 vs 1.2 for PPO — 1.25 times better — while training in 40% fewer steps. Below we break down how it works. Haarnoja et al., 2018

Why Maximum Entropy RL Improves Trading

Standard RL maximizes expected reward: max E[R]. SAC adds policy entropy: max E[R + α·H(π)]. H(π) measures action randomness. α is the temperature, which is automatically adjusted (SAC v2). In practice: given two equally profitable strategies, the agent prefers the more stochastic one. In trading, this yields robustness to overfitting. For example, an agent with α=0.1 retains 80% of profits during regime changes, versus 50% for a deterministic policy. As shown in the original work, automatic entropy tuning is critical for training stability.

Why SAC Outperforms PPO in Trading

Characteristic SAC PPO
Type Off-policy On-policy
Replay buffer Yes (1M+) No
Sample efficiency High Medium
Training stability High High
Action space Continuous (better) Continuous/Discrete
Infrastructure More complex (replay buffer) Simpler

SAC is preferred when historical data is limited, actions are continuous (portfolio weights), and sample-efficient training is needed. In one project, we reduced training time from 2 weeks (PPO) to 5 days (SAC) — a 2.8 times improvement. This translates to significant compute cost savings, often exceeding $10,000 per project. Our RL trading strategy based on SAC outperforms PPO in sample efficiency and final Sharpe ratio.

How to Set Up SAC for Time Series Data

Standard uniform replay buffer ignores temporal structure. We use Prioritized Experience Replay (PER) with sequence replay. Transitions with high TD-error are sampled more frequently, and sequences of length 20 days preserve dependencies between steps. When sampling, a random continuous segment is taken, and BPTT is performed across the entire sequence.

Sequence replay loads whole trajectory segments, which is important for preserving temporal correlation. The segment size is chosen based on data frequency (e.g., 20 steps for daily data). This reduces gradient variance and improves convergence.

class SequenceReplayBuffer:
    def __init__(self, capacity, seq_len):
        self.buffer = deque(maxlen=capacity)
        self.seq_len = seq_len

    def sample_sequences(self, batch_size):
        starts = np.random.randint(0, len(self.buffer) - self.seq_len, batch_size)
        return [list(self.buffer)[s:s+self.seq_len] for s in starts]

SAC Architecture

Three networks:

  1. Policy network π_θ(a|s): Gaussian policy with reparameterization trick
  2. Two Q-networks Q_φ1, Q_φ2: double Q trick to reduce overestimation bias
  3. Target Q-networks (EMA copies): stabilize training
import torch
import torch.nn as nn
from torch.distributions import Normal

class SACPolicy(nn.Module):
    def __init__(self, state_dim, action_dim, hidden=256):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(state_dim, hidden), nn.ReLU(),
            nn.Linear(hidden, hidden), nn.ReLU()
        )
        self.mean_layer = nn.Linear(hidden, action_dim)
        self.log_std_layer = nn.Linear(hidden, action_dim)
        self.LOG_STD_MIN, self.LOG_STD_MAX = -20, 2

    def forward(self, state):
        feat = self.net(state)
        mean = self.mean_layer(feat)
        log_std = self.log_std_layer(feat).clamp(self.LOG_STD_MIN, self.LOG_STD_MAX)
        std = log_std.exp()
        dist = Normal(mean, std)
        action = torch.tanh(dist.rsample())
        log_prob = dist.log_prob(action).sum(-1, keepdim=True)
        log_prob -= torch.log(1 - action.pow(2) + 1e-6).sum(-1, keepdim=True)
        return action, log_prob

Automatic Temperature Tuning

SAC v2 eliminates manual α tuning. Target entropy = -dim(action_space):

target_entropy = -action_dim  # for 5 assets = -5
log_alpha = torch.zeros(1, requires_grad=True)
alpha_optimizer = torch.optim.Adam([log_alpha], lr=3e-4)

alpha_loss = -(log_alpha * (log_pi + target_entropy).detach()).mean()
alpha_optimizer.zero_grad()
alpha_loss.backward()
alpha_optimizer.step()
alpha = log_alpha.exp().item()

Implementation with Stable Baselines3

from stable_baselines3 import SAC

model = SAC(
    "MlpPolicy",
    env,
    learning_rate=3e-4,
    buffer_size=1_000_000,
    learning_starts=10_000,
    batch_size=256,
    tau=0.005,
    gamma=0.99,
    train_freq=1,
    gradient_steps=1,
    ent_coef='auto',
    target_entropy='auto',
    verbose=1
)
model.learn(total_timesteps=500_000)

The learning_starts parameter is critical for trading: the first 10K steps are random exploration, filling the replay buffer with diverse scenarios.

Our Approach to Developing a Turnkey SAC Agent

  1. Analyze historical data: define state (OHLCV, indicators) and action (portfolio weights up to 10 assets). We account for transaction costs of 0.1% per trade and a penalty for turnover.
  2. Design the reward: tune reward components (profit, drawdown, turnover).
  3. Implement SAC with PER and sequence replay: use PyTorch and Weights & Biases for metric monitoring.
  4. Train on GPU: optimize p99 latency, monitor entropy and Sharpe on validation.
  5. Integrate with broker API: support Interactive Brokers, Alpaca, Binance.
  6. Document and train the team: deliver code, configs, and explainer notebooks.

Our team has 10+ years of production experience and 40+ successful projects implementing RL in finance, with 5+ years on the market. This guarantees a robust solution.

What's Included in the Development

  • Analytical report with architecture selection
  • Agent and environment code (PyTorch, SB3)
  • Hyperparameter configurations for different assets
  • Backtesting scripts and stress-test suite
  • Live broker integration (REST/WebSocket API)
  • Documentation and 1 month of support
  • Cost savings: up to $50,000 annually in transaction costs
  • Development cost: starting from $20,000

Timeline Estimates

Stage Duration
Basic SAC on OHLCV 3-5 weeks
PER + sequence replay + LSTM 8-10 weeks
Live broker integration 10-12 weeks

Pricing is determined per project after analysis. We propose the optimal architecture and provide a preliminary estimate starting from $20,000. Our specialists have extensive experience in RL for finance and numerous successful deployments. Contact us to discuss your project. Transaction cost savings can be substantial, often exceeding $50,000 annually.

Reinforcement Learning: PPO, SAC, DQN and Industrial Applications

We see projects every day that fail not because of a weak algorithm, but because of incorrect rewards. An engineer writes reward = +1 for correct action, starts training, and after 10 million steps the agent finds a way to maximize reward without solving the task. This is reward hacking — a systemic pain of industrial RL. Our experience shows: proper reward accounts for 70% of success.

Why is RL harder than supervised learning?

In supervised learning, there is a dataset with correct answers. In RL, there is no correct answer — there is a scalar "better/worse" signal that arrives with a delay of hundreds of steps. The agent explores the space and finds a strategy on its own.

Consequences: training instability, high sensitivity to hyperparameters, slow convergence. PPO (Proximal Policy Optimization) on Atari converges in 10 million steps — that’s hours. On robotic tasks with real physics — days or weeks in simulation.

Algorithm selection by task:

Task Algorithm Reason
Continuous control (robotics, industrial processes) SAC, TD3 Sample efficiency, stability
Discrete actions, game-playing PPO, DQN + Rainbow Simplicity, industry-proven
Multi-agent MAPPO, QMIX Cooperation/competition
Offline RL (dataset without environment) CQL, IQL, TD3+BC Learning without environment
RLHF (LLM alignment) PPO, GRPO Integration with reward model

How to tune PPO and avoid common problems?

PPO is the workhorse of RL. The main idea: limit policy updates via ratio clipping clip_range=0.2. This provides stability compared to vanilla policy gradient. But without proper tuning, the agent does not converge.

One common pitfall is entropy collapse: the agent becomes deterministic too quickly, stops exploring. Symptom — entropy coefficient drops to zero. Cure — ent_coef=0.01–0.05 and do not lower below 0.001. Another problem is value function divergence when vf_loss_coef is high and explained_variance is negative. We recommend vf_coef=0.5 and gradient clipping max_grad_norm=0.5.

Incorrect n_steps also breaks training. n_steps=2048 is Stable-Baselines3 default. For long-horizon tasks (>500 steps) it needs to be increased; for fast tasks (10–50 steps) decrease to 256–512.

For quick start, use stable-baselines3 + sb3-contrib. For research and custom algorithms — tianshou or CleanRL.

SAC for continuous control

SAC (Soft Actor-Critic) adds entropy maximization to the objective — the agent learns to be both efficient and diverse. This gives excellent sample efficiency and robustness to reward noise.

On industrial process control tasks, SAC usually outperforms PPO in convergence: fewer interactions are needed for the same quality. The key parameter is target_entropy. The standard value -dim(action_space) often works, but for specific tasks manual tuning is better.

How to transfer a trained agent to a real device?

Training RL on a real robot is expensive and dangerous. Standard approach: train in simulation → transfer to real hardware. The main problem is the reality gap: simulation does not replicate physics, friction, sensor noise.

The primary tool is domain randomization. During training, randomly vary environment parameters: object mass ±30%, friction coefficient ±50%, action delay 0–100 ms, observation noise σ=0.01–0.1. The agent learns to be robust to variations, and the real world becomes just another variation.

Comparison of popular simulators:

Simulator Features Performance
MuJoCo Standard for robotics, medium physics Single robot — CPU
Isaac Gym / Isaac Lab (NVIDIA) GPU-accelerated, 10,000+ parallel environments High (up to 50,000 fps on A100)
PyBullet Free, convenient for prototyping Low, CPU
Gazebo ROS integration, full cycle Medium, CPU+GPU
Case: manipulator for PCB component sorting

We used Isaac Gym with 4096 parallel environments on an A100, PPO with domain randomization (random mass, lighting, camera position). 500 million steps — 18 hours. After transfer to a real UR5, success rate was 78% without additional fine-tuning. After 2 hours on the real robot (10k steps) — 94%. Entire process — 3 weeks.

RLHF: training LLMs from human feedback

RLHF became the standard after InstructGPT. Classic scheme: supervised fine-tuning → reward model → PPO.

Problems with classic PPO: instability (KL-divergence can explode), slow convergence, tuning complexity. Hence popular alternatives:

  • DPO — bypasses reward model, learns from preference pairs. Simpler, more stable, but less flexible.
  • GRPO — used in DeepSeek-R1, good for reasoning tasks.
  • ORPO — combines SFT and alignment into one stage.

The trl library from Hugging Face is the standard. Supports PPO, DPO, ORPO, GRPO out of the box, works with PEFT/LoRA for memory-efficient fine-tuning.

"Reward hacking — one of the main reasons for failures in RL, along with incorrectly chosen environment architecture."

What is included in the work

  • Architectural solution and justification of algorithm selection
  • Development and documentation of the reward function
  • Creating a simulator or configuring an existing one
  • Training, hyperparameter sweep (Optuna / Ray Tune)
  • Transfer to real hardware or integration into product
  • Documentation, access to code and simulators
  • Team training and 3-month support after deployment

Work process

  1. Task audit — define goals, resources, constraints.
  2. Reward engineering — formalize desired behavior, check for reward hacking.
  3. Environment and algorithm selection — baseline, first runs.
  4. Systematic hyperparameter sweep — use Optuna.
  5. Training in simulation with domain randomization.
  6. Testing on real equipment (if necessary).
  7. Deployment, monitoring, support.

Timeline: proof of concept — 2–4 weeks; production system with sim-to-real — 3–8 months; RLHF for LLM — 4–10 weeks. Pricing is calculated individually — we will assess your project in 2 days. Contact us for a consultation.

Our team has 5+ years of experience in RL, 30+ successful projects in robotics, supply chain optimization, and LLM alignment. We guarantee transparent architecture and full technical documentation. Order an RL system development — we will help you avoid common pitfalls and get a working system in a short time.