Exit Strategies
Exit Strategies control how trades are closed in Algo Trading Stack.
They define stop-loss and take-profit logic, and are completely pluggable — meaning any bot can use any exit strategy.
This separation makes bots modular: you can keep your entry logic unchanged while experimenting with different exit rules.
1. Role of Exit Strategies
- Manage risk by defining stop-loss levels
- Optionally define take-profit levels
- Support trailing logic to move stops as trades become profitable
- Allow interchangeability: the same bot can run with different exits
- Support ML-driven and bandit-driven adaptive exits
2. ExitStrategy Interface
All exits implement a common interface:
-
initial_levels(side, entry_price, atr, tick_size, ...) -> (stop_loss, take_profit|None)
Defines the initial stop-loss and optional take-profit. -
update_stop(side, stop_loss, entry_price, current_price, atr, tick_size, ...) -> new_stop|None
Called on each bar to trail or adjust stops. Returns a new stop orNone
to keep unchanged.
This unified interface allows exits to be swapped without changing bot code.
3. Built-in Exit Strategies
3.1 TrailingATRExit
- Type: Trailing stop
- Logic: Stop = entry ± ATR × multiple (e.g., 3 × ATR)
- Behavior: Stop trails only after price moves favorably past entry
- Take-profit: None
from bots.exit_strategies import TrailingATRExit
exit = TrailingATRExit(atr_multiple=3.0)
3.2 FixedRatioExit
- Type: Fixed stop + fixed target
- Logic: Stop =
entry ± (ATR × multiple)
; Take-profit =entry ± (R × risk distance)
- Use case: Traditional fixed risk/reward systems (e.g., 1:3 R:R)
from bots.exit_strategies import FixedRatioExit
exit = FixedRatioExit(atr_multiple=3.0, rr_multiple=3.0)
3.3 RLTrailingATRExit (PPO)
- Type: RL-driven trailing stop
- Logic: ATR multiple chosen dynamically by a PPO-trained model
- Fallback: Uses a fixed multiple if the model is unavailable
- Use case: Adaptive stop selection based on market context
from bots.exit_strategies import RLTrailingATRExit
exit = RLTrailingATRExit(model_dir="bots/models/PPO_Trailing_Stop_Loss")
3.4 SequenceRLATRExit (LSTM)
- Type: Sequence model exit
- Logic: TorchScript LSTM classifier selects ATR multiple from a recent window of features
[ATR, RSI, EMA, Close, PositionType]
- Use case: Research-driven, offline-trained exits
from bots.sequence_rl_atr_exit import SequenceRLATRExit
exit = SequenceRLATRExit(model_dir="bots/models/LSTM_Trailing_Stop_Loss")
3.5 Bandit Hybrid Exits
- Type: ML prior + online LinUCB bandit
- Logic: Prior from PPO/LSTM model; bandit refines ATR multiple online based on rewards
- Files:
ppo_bandit_atr_exit.py
sequence_bandit_atr_exit.py
- Use case: Combines offline training with online learning for adaptability
from bots.ppo_bandit_atr_exit import PPOLinUCBATRExit
exit = PPOLinUCBATRExit(model_dir="bots/models/PPO_Trailing_Stop_Loss")
4. Research & Training
Exit strategies can be trained and improved using the research tooling:
- Generate labeled data
PYTHONPATH=. python3 bots/generate_ML_SL_Training_data.py
- Train PPO exits
PYTHONPATH=. python3 bots/train_ppo_stop_selector.py
- Train LSTM exits
PYTHONPATH=. python3 bots/train_lstm_sl_model.py
5. Using Exit Strategies with Bots
Attach an exit when constructing a bot:
from bots.coin_flip_bot.coin_flip_bot import CoinFlipBot
from bots.exit_strategies import TrailingATRExit
bot = CoinFlipBot(exit_strategy=TrailingATRExit(atr_multiple=3.0))
env.set_bot(bot)
6. Summary
- Exit strategies are modular and interchangeable across all bots
- Core exits: ATR trailing, Fixed ratio, PPO/LSTM RL, hybrid bandit
- Unified interface:
initial_levels()
andupdate_stop()
- Research workflow: generate labeled data → train models → plug into bots
- Makes Algo Trading Stack a powerful sandbox for experimenting with risk management ideas