TradingAgentBase<T>

Base Classes ยท AiDotNet.Finance.Trading.Agents

Base class for financial trading agents using reinforcement learning.

Inheritance: ReinforcementLearningAgentBase<T> โ†’ TradingAgentBase<T>

Implements: ITradingAgent<T>

Known derived types: FinRLAgent<T>, FinancialA2CAgent<T>, FinancialDQNAgent<T>, FinancialPPOAgent<T>, FinancialSACAgent<T>, MarketMakingAgent<T>

For Beginners

This is the foundation for AI trading agents:

The Key Insight: Trading is a sequential decision-making problem perfectly suited for RL. The agent observes market conditions, makes trading decisions, and learns from the resulting profits or losses. Over many training episodes, it learns strategies that maximize risk-adjusted returns.

Architecture:

  • Inherits from ReinforcementLearningAgentBase (standard RL functionality)
  • Implements ITradingAgent (financial-specific methods)
  • Adds portfolio tracking (values, returns, drawdown)
  • Adds financial metrics (Sharpe ratio, win rate)
  • Integrates risk management (position limits, VaR constraints)

How Trading Agents Learn:

  1. Observe market state (prices, volumes, indicators)
  2. Take action (buy, sell, adjust positions)
  3. Receive reward (returns, risk-adjusted returns)
  4. Store experience in replay buffer
  5. Sample experiences and update neural network
  6. Repeat thousands of times until convergence

How It Works

TradingAgentBase extends the standard RL agent infrastructure with financial-specific functionality. It provides common implementation for portfolio tracking, risk management, and financial metrics that all trading agents share.

Properties

Property Summary
IOptionPricer<T> OptionPricer { get; set; } Option-pricing strategy for options-aware agents (valuation / Greeks / implied vol).
IPositionSizer<T> PositionSizer { get; set; } Position-sizing strategy used to translate a signal/edge into a capital fraction.
T CumulativeReturn { get; }
T InitialCapital { get; }
T MaxDrawdown { get; }
T PortfolioValue { get; }
T SharpeRatio { get; }
T WinRate { get; }
int TotalTrades { get; }

Methods

Method Summary
Dictionary<string, T> GetMetrics()
Dictionary<string, T> GetTradingMetrics()
T TrainOnExperiences()
Vector<T> ExecuteTradeWithRiskManagement(Vector<T> marketState, T riskBudget)
Vector<T> SelectTradingAction(Vector<T> marketState, bool training)
void Dispose() Disposes the agent and every network it owns.
void ResetEpisode()
void ResetTrading(T initialCapital)
void StoreTradingExperience(Vector<T> marketState, Vector<T> action, T reward, Vector<T> nextMarketState, bool done, T pnl)
void StoreTradingExperience(Vector<T> marketState, Vector<T> action, T reward, Vector<T> nextMarketState, bool done, T pnl, bool[] nextLegalActions) Stores a trade with a snapshot of the legal actions at its resulting state.