$npx -y skills add agiprolabs/claude-trading-skills --skill rl-executionReinforcement learning for trade execution optimization including order splitting, adaptive timing, and impact minimization
| 1 | # RL Execution Optimization |
| 2 | |
| 3 | Reinforcement learning (RL) for trade execution teaches an agent to split and |
| 4 | time large orders so that total market impact is minimized. Instead of following |
| 5 | a fixed schedule (TWAP, VWAP), an RL agent observes real-time market state and |
| 6 | adapts its trading rate on the fly. |
| 7 | |
| 8 | ## Why Execution Optimization Matters |
| 9 | |
| 10 | Every trade has a cost beyond the quoted spread: |
| 11 | |
| 12 | | Cost Component | Cause | Typical Magnitude | |
| 13 | |---|---|---| |
| 14 | | Spread cost | Crossing the bid-ask | 5-50 bps on DEXs | |
| 15 | | Temporary impact | Consuming liquidity | Scales with trade rate | |
| 16 | | Permanent impact | Information leakage | Scales with total size | |
| 17 | | Timing risk | Price drifts while waiting | Scales with volatility and time | |
| 18 | |
| 19 | A 100 SOL market buy on a thin pool can move the price 2-5%. Splitting it into |
| 20 | ten 10 SOL slices over a few minutes can cut that cost by 30-60%. The question |
| 21 | is **how** to split optimally — and that is where execution algorithms and RL |
| 22 | come in. |
| 23 | |
| 24 | ## The RL Framework for Execution |
| 25 | |
| 26 | ### State Space |
| 27 | |
| 28 | The agent observes at each decision step: |
| 29 | |
| 30 | ``` |
| 31 | state = [ |
| 32 | remaining_qty, # How much is left to trade (0-1 normalized) |
| 33 | time_remaining, # Fraction of allowed horizon remaining |
| 34 | current_price, # Current mid-price (normalized to arrival price) |
| 35 | spread, # Current bid-ask spread |
| 36 | volatility, # Recent realized volatility |
| 37 | volume, # Recent trading volume (normalized) |
| 38 | ] |
| 39 | ``` |
| 40 | |
| 41 | ### Action Space |
| 42 | |
| 43 | Discrete actions controlling how much to trade this step: |
| 44 | |
| 45 | ``` |
| 46 | actions = [0%, 10%, 25%, 50%, 100%] # of remaining quantity |
| 47 | ``` |
| 48 | |
| 49 | A small action space keeps the problem tractable. Each action represents the |
| 50 | fraction of the remaining order to execute in the current time step. |
| 51 | |
| 52 | ### Reward Function |
| 53 | |
| 54 | The reward penalizes execution cost relative to a benchmark: |
| 55 | |
| 56 | ``` |
| 57 | reward = -(execution_price - arrival_price) * quantity_traded |
| 58 | ``` |
| 59 | |
| 60 | Summed over all steps, the total reward equals the negative implementation |
| 61 | shortfall. The agent learns to minimize total cost. |
| 62 | |
| 63 | ### Episode Structure |
| 64 | |
| 65 | One episode = one order from placement to completion: |
| 66 | |
| 67 | 1. Agent receives order: buy/sell Q units within T time steps |
| 68 | 2. At each step, agent picks an action (trade amount) |
| 69 | 3. Market simulator applies price impact and updates state |
| 70 | 4. Episode ends when quantity is fully executed or time expires |
| 71 | 5. Any remaining quantity at expiry is executed at market (penalty) |
| 72 | |
| 73 | ## Standard Execution Algorithms |
| 74 | |
| 75 | ### TWAP (Time-Weighted Average Price) |
| 76 | |
| 77 | The simplest baseline — split the order equally across all time steps: |
| 78 | |
| 79 | ```python |
| 80 | trade_per_step = total_quantity / num_steps |
| 81 | ``` |
| 82 | |
| 83 | **Pros**: Simple, deterministic, easy to implement. |
| 84 | **Cons**: Ignores market conditions entirely. |
| 85 | |
| 86 | ### VWAP (Volume-Weighted Average Price) |
| 87 | |
| 88 | Split proportional to expected volume in each period: |
| 89 | |
| 90 | ```python |
| 91 | trade_at_step_t = total_quantity * (expected_volume[t] / total_expected_volume) |
| 92 | ``` |
| 93 | |
| 94 | **Pros**: Trades more when liquidity is available. |
| 95 | **Cons**: Requires accurate volume forecasts; still non-adaptive. |
| 96 | |
| 97 | ### Almgren-Chriss Optimal Execution |
| 98 | |
| 99 | The foundational analytical model. Minimizes a combination of execution cost |
| 100 | and timing risk: |
| 101 | |
| 102 | ``` |
| 103 | minimize: E[cost] + λ * Var[cost] |
| 104 | ``` |
| 105 | |
| 106 | With linear impact assumptions, this yields a closed-form optimal trajectory. |
| 107 | See `references/execution_algorithms.md` for the full derivation. |
| 108 | |
| 109 | ### RL-Based Adaptive Execution |
| 110 | |
| 111 | An RL agent (DQN, PPO, or similar) that learns the execution policy from |
| 112 | simulated experience: |
| 113 | |
| 114 | ```python |
| 115 | # Pseudocode training loop |
| 116 | for episode in range(num_episodes): |
| 117 | state = env.reset(order_qty=Q, horizon=T) |
| 118 | done = False |
| 119 | while not done: |
| 120 | action = agent.select_action(state) |
| 121 | next_state, reward, done, info = env.step(action) |
| 122 | agent.store_transition(state, action, reward, next_state, done) |
| 123 | agent.update() |
| 124 | state = next_state |
| 125 | ``` |
| 126 | |
| 127 | **Pros**: Adapts to current market conditions, can learn non-linear patterns. |
| 128 | **Cons**: Requires realistic simulator, sim-to-real gap, training instability. |
| 129 | |
| 130 | ## Price Impact Model |
| 131 | |
| 132 | The simulator uses a standard two-component impact model: |
| 133 | |
| 134 | ``` |
| 135 | temporary_impact = η * (trade_rate / avg_volume) |
| 136 | permanent_impact = γ * (trade_rate / avg_volume) |
| 137 | ``` |
| 138 | |
| 139 | - **Temporary impact** decays after the trade (liquidity replenishes) |
| 140 | - **Permanent impact** shifts the equilibrium price (information effect) |
| 141 | |
| 142 | The execution price for a trade of size `q` at time `t`: |
| 143 | |
| 144 | ``` |
| 145 | exec_price = mid_price + permanent_impact + temporary_impact |
| 146 | mid_price_next = mid_price + permanent_impact + noise |
| 147 | ``` |
| 148 | |
| 149 | ## When to Use This Skill |
| 150 | |
| 151 | This skill is most valuable when: |
| 152 | |
| 153 | - **Order size is large relative to available liquidity** (>1% of daily volume) |
| 154 | - **Market impact is significant** (thin DEX pools, low-cap tokens) |
| 155 | - **Execution window is flexible** (minutes to hours, not milliseconds) |
| 156 | - **Cost savings just |