Crossover EMA avec entrée market et TP/SL via [[exit]]

Cet exemple reste proche de Crossover EMA avec entrée market et TP/SL sur le close, mais délègue la gestion du TP/SL à un unique bloc [[exit]] utilisant des ordres limit et stop.

Comme le TP/SL est géré par des ordres de sortie, cet exemple ne produit pas exactement les mêmes résultats que la version basée sur le close.

Voir la sortie console.

# Exemple illustratif uniquement, ne pas utiliser en trading réel. Domaine public (CC0 1.0).

# Configuration du backtest
[backtest]
symbol            = ["BINANCE:BTCUSDT"]
timeframe         = ["D"]
start_date        = 2024-01-01
end_date          = 2026-01-01
initial_capital   = 1000
# 95 % garde la position sous 1x pour éviter un appel de marge immédiat à 1x
# lié aux frais de trading et à l'écart entre le signal et le prix d'exécution.
default_qty_value = 95

# Classe les combinaisons testées par ratio de Calmar pour faire remonter
# en tête les configurations EMA qui maximisent le rendement par drawdown.
[[objective]]
id        = "calmar"
formula   = "calmar_ratio"
ascending = false

# Active l'affichage détaillé des trades dans les résultats.
[logging]
include_trades    = true
timestamp_enabled = false

# EMA rapide
[[moving_average]]
id           = "fast"
type         = "ema"
length.start = 2
length.stop  = 50

# EMA lente
[[moving_average]]
id           = "slow"
type         = "ema"
length.start = 2
length.stop  = 50

# Contrainte sur les hyperparamètres :
# l'EMA rapide doit rester plus courte que l'EMA lente pour ne tester
# que des combinaisons de crossover cohérentes.
[constraints]
condition = "fast.length < slow.length"

# Seuil de take profit :
# Cette plage de pourcentages est testée pour placer la sortie `limit`
# au-dessus du prix moyen d'entrée.
[[constant]]
id    = "take_profit_pct"
start = 1
stop  = 10

# Seuil de stop loss :
# Cette plage de pourcentages est testée pour placer l'ordre de protection
# sous le prix moyen d'entrée.
[[constant]]
id    = "stop_loss_pct"
start = 1
stop  = 10

# Point d'entrée de la machine d'états.
# L'exécution séquentielle commence ici et passe d'abord au bloc "golden_cross".
[start]
id            = "origin"
next_block_id = "golden_cross"

# Attend que l'EMA rapide croise à la hausse l'EMA lente.
# Une fois validé, l'exécution passe au bloc d'entrée long.
[[crossover]]
id            = "golden_cross"
reference     = "fast"
comparison    = "slow"
next_block_id = "enter_long"

# Ouvre une position longue sur l'ordre "main", puis passe au bloc d'ordres de sortie TP/SL.
[[entry]]
id            = "enter_long"
order_id      = "main"
direction     = "long"
next_block_id = "set_exits"

# Place les ordres de sortie TP/SL pour la position ouverte.
# Le TP est soumis comme un ordre limit et le SL comme un ordre stop.
[[exit]]
id            = "set_exits"
order_id      = "main_exit"
from_entry    = "main"
limit         = "position_avg_price * (1 + take_profit_pct / 100)"
stop          = "position_avg_price * (1 - stop_loss_pct / 100)"
next_block_id = "monitor_exits"

# Surveille les sorties à chaque bougie :
# 1) Fill TP/SL (position fermée)
# 2) Sortie technique sur death cross
[[or]]
id            = "monitor_exits"
conditions    = ["position_closed", "death_cross"]
next_block_id = "monitor_exits_route"

# Détection du fill des sorties : dès qu'il ne reste plus de trade ouvert,
# on repart sur l'attente du prochain crossover haussier.
[[condition]]
id        = "position_closed"
condition = "opentrades == 0"

# Sortie technique : si l'EMA rapide recroise sous l'EMA lente, fermeture immédiate.
[[crossunder]]
id         = "death_cross"
reference  = "fast"
comparison = "slow"

[[if]]
id            = "monitor_exits_route"
condition     = "opentrades == 0"
then_block_id = "golden_cross"
else_block_id = "close_position"

# Ferme la position sur l'ordre "main" et repart directement
# sur l'attente du prochain croisement haussier.
[[close]]
id            = "close_position"
order_id      = "main"
next_block_id = "golden_cross"

Sortie console

[info] 
[info] ================================================================
[info]                     Whale Engine 1.0.1044
[info]           Backtesting & Strategy Optimization Software
[info]             (c) 2025 Almageste - https://Whale-E.com
[info] ================================================================
[info] Strategy : C:\Whale-E_Project\dragon\doc\examples\crossover-tp-sl-exit-orders.toml
[info] Using 31 threads for parallel computation.
[info] Using database cache directory: C:\Users\Tony\AppData\Local\Almageste\Whale-E
[info] Detected 1 grid in the strategy.
[info] Analyzing hyperparameter grids.
[info] Grid 1/1 has 117 600 valid combinations out of 240 100 total combinations.
[info] Hyperparameter grid analysis completed in 7 ms.
[info] Starting exploration of 1 grid.
[info] [Run 1/1] Grid 1 BINANCE:BTCUSDT (D): Price series ready.
[info] [Run 1/1] Grid 1 BINANCE:BTCUSDT (D): Exploring 117 600 valid combinations.
[info] [Run 1/1] Grid 1 BINANCE:BTCUSDT (D): Completed in 291 ms with 117 600 backtests, 1 333 500 positions, and 295.8M candles/s.
[info] Completed 117 600 backtests in total.
[info] 
[info] +======================================================================+
[info] | OBJECTIVE RANKING                                                    |
[info] | Name: calmar                                                         |
[info] | Sort order: descending                                               |
[info] +======================================================================+
[info] 
[info] ========================================================================
[info] Result 1 | score 8.025067616190 | G1C123589 | BINANCE:BTCUSDT | D
[info] ========================================================================
[info] 
[info] Backtest Settings
[info] --------------------------------------------------------------
[info] Strategy                        | crossover-tp-sl-exit-orders 
[info] Timezone                        | UTC                         
[info] Price history start date        |                             
[info] Backtest start                  | 2024-01-01 00:00:00         
[info] Backtest end                    | 2026-01-01 23:59:59         
[info] Initial capital                 |               1000.00 USDT  
[info] Quantity type                   | percent_of_equity           
[info] Default quantity value          |                 95.00 %     
[info] Long size multiplier            |                 x1.00       
[info] Short size multiplier           |                 x1.00       
[info] Pyramiding                      |                  1          
[info] Long margin                     |                100.00 %     
[info] Short margin                    |                100.00 %     
[info] Process orders on close         | No                          
[info] Bar Magnifier                   | No                          
[info] Close open position at end      | Yes                         
[info] Close entries rule              | fifo                        
[info] Backtest fill limits assumption |                  0          
[info] Automatic slippage              | No                          
[info] Automatic slippage ratio        |                  0.000000   
[info] Slippage                        |                  0          
[info] Commission type                 | percent                     
[info] Commission value                |                  0.100000 % 
[info] --------------------------------------------------------------
[info] 
[info] Hyperparameters
[info] ----------------------------------
[info] fast.length     | 13              
[info] slow.length     | 25              
[info] stop_loss_pct   |  2.000000000000 
[info] take_profit_pct |  6.000000000000 
[info] ----------------------------------
[info] 
[info] Overview
[info] -------------------------------------------------------------------------------
[info] Total P&L   | Max equity drawdown | Trades | Profitable trades | Profit factor 
[info] -------------------------------------------------------------------------------
[info] 440.29 USDT |          33.81 USDT |     11 |           72.73 % |         6.584 
[info]  44.03 %    |           2.49 %    |        | 8/11              |               
[info] -------------------------------------------------------------------------------
[info] 
[info] Performance
[info] -------------------------------------------------------------
[info] Metric              | All          | Long        | Short     
[info] -------------------------------------------------------------
[info] Open P&L            |    0.00 USDT |             |           
[info]                     |    0.00 %    |             |           
[info] -------------------------------------------------------------
[info] Net profit          |  440.29 USDT | 440.29 USDT | 0.00 USDT 
[info]                     |   44.03 %    |  44.03 %    | 0.00 %    
[info] -------------------------------------------------------------
[info] Gross profit        |  519.13 USDT | 519.13 USDT | 0.00 USDT 
[info]                     |   51.91 %    |  51.91 %    | 0.00 %    
[info] -------------------------------------------------------------
[info] Gross loss          |   78.85 USDT |  78.85 USDT | 0.00 USDT 
[info]                     |    7.88 %    |   7.88 %    | 0.00 %    
[info] -------------------------------------------------------------
[info] Commission paid     |   25.56 USDT |  25.56 USDT | 0.00 USDT 
[info] -------------------------------------------------------------
[info] Buy & hold return   | 1010.86 USDT |             |           
[info]                     |  101.09 %    |             |           
[info] -------------------------------------------------------------
[info] Max equity run-up   |  442.61 USDT |             |           
[info]                     |   30.70 %    |             |           
[info] -------------------------------------------------------------
[info] Max equity drawdown |   33.81 USDT |             |           
[info]                     |    2.49 %    |             |           
[info] -------------------------------------------------------------
[info] 
[info] Trade Analysis
[info] --------------------------------------------------------------------
[info] Metric                        | All        | Long       | Short     
[info] --------------------------------------------------------------------
[info] Total trades                  | 11         | 11         | 0         
[info] --------------------------------------------------------------------
[info] Total open trades             |  0         |            |           
[info] --------------------------------------------------------------------
[info] Winning trades                |  8         |  8         | 0         
[info] --------------------------------------------------------------------
[info] Losing trades                 |  3         |  3         | 0         
[info] --------------------------------------------------------------------
[info] Percent profitable            | 72.73 %    | 72.73 %    | 0.00 %    
[info] --------------------------------------------------------------------
[info] Avg P&L                       | 40.03 USDT | 40.03 USDT | 0.00 USDT 
[info]                               |  3.61 %    |  3.61 %    | 0.00 %    
[info] --------------------------------------------------------------------
[info] Avg winning trade             | 64.89 USDT | 64.89 USDT | 0.00 USDT 
[info]                               |  5.79 %    |  5.79 %    | 0.00 %    
[info] --------------------------------------------------------------------
[info] Avg losing trade              | 26.28 USDT | 26.28 USDT | 0.00 USDT 
[info]                               |  2.20 %    |  2.20 %    | 0.00 %    
[info] --------------------------------------------------------------------
[info] Largest winning trade         | 75.04 USDT | 75.04 USDT | 0.00 USDT 
[info] --------------------------------------------------------------------
[info] Largest winning trade percent |  5.79 %    |  5.79 %    | 0.00 %    
[info] --------------------------------------------------------------------
[info] Largest losing trade          | 29.07 USDT | 29.07 USDT | 0.00 USDT 
[info] --------------------------------------------------------------------
[info] Largest losing trade percent  |  2.20 %    |  2.20 %    | 0.00 %    
[info] --------------------------------------------------------------------
[info] 
[info] Risk and Performance Ratios
[info] -------------------------------------------
[info] Metric          | All    | Long   | Short  
[info] -------------------------------------------
[info] Sharpe ratio    | 1.824  |        |        
[info] -------------------------------------------
[info] Sortino ratio   | 3.782  |        |        
[info] -------------------------------------------
[info] Calmar ratio    | 8.025  |        |        
[info] -------------------------------------------
[info] Profit factor   | 6.584  |        |        
[info] -------------------------------------------
[info] Market Exposure | 7.79 % | 7.79 % | 0.00 % 
[info] -------------------------------------------
[info] Margin call     | 0      | 0      | 0      
[info] -------------------------------------------
[info] 
[info] Trade List
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] Trade # | Type  | Date                | Signal    | Price          | Qty          | P&L         | Run-up     | Drawdown    | Cumulative P&L 
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 11 Long | Exit  | 2025-10-06 00:00:00 | main_exit | 125710.69 USDT | 1372.76 USDT |  75.04 USDT | 76.41 USDT |  -4.74 USDT |    440.29 USDT 
[info]         |       |                     |           |                |              |   5.79 %    |  5.89 %    |  -0.37 %    |     44.03 %    
[info]         | Entry | 2025-10-02 00:00:00 | main      | 118594.99 USDT | 1295.06 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.01092   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 10 Long | Exit  | 2025-09-22 00:00:00 | main_exit | 113599.92 USDT | 1296.18 USDT | -29.07 USDT | 21.29 USDT | -27.78 USDT |    365.25 USDT 
[info]         |       |                     |           |                |              |  -2.20 %    |  1.61 %    |  -2.10 %    |     36.52 %    
[info]         | Entry | 2025-09-14 00:00:00 | main      | 115918.29 USDT | 1322.63 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.01141   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 9 Long  | Exit  | 2025-07-10 00:00:00 | main_exit | 113363.89 USDT | 1328.62 USDT |  72.62 USDT | 73.95 USDT | -22.90 USDT |    394.32 USDT 
[info]         |       |                     |           |                |              |   5.79 %    |  5.89 %    |  -1.83 %    |     39.43 %    
[info]         | Entry | 2025-06-27 00:00:00 | main      | 106947.06 USDT | 1253.42 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.01172   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 8 Long  | Exit  | 2025-04-22 00:00:00 | main_exit |  90181.62 USDT | 1259.84 USDT |  68.86 USDT | 70.12 USDT | -16.94 USDT |    321.70 USDT 
[info]         |       |                     |           |                |              |   5.79 %    |  5.89 %    |  -1.42 %    |     32.17 %    
[info]         | Entry | 2025-04-20 00:00:00 | main      |  85077.00 USDT | 1188.53 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.01397   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 7 Long  | Exit  | 2025-01-19 00:00:00 | main_exit | 105986.54 USDT | 1194.47 USDT |  65.29 USDT | 66.48 USDT |  -1.54 USDT |    252.83 USDT 
[info]         |       |                     |           |                |              |   5.79 %    |  5.89 %    |  -0.14 %    |     25.28 %    
[info]         | Entry | 2025-01-17 00:00:00 | main      |  99987.30 USDT | 1126.86 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.01127   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 6 Long  | Exit  | 2025-01-07 00:00:00 | main_exit |  96396.33 USDT | 1127.84 USDT | -25.30 USDT | 49.87 USDT | -24.17 USDT |    187.54 USDT 
[info]         |       |                     |           |                |              |  -2.20 %    |  4.33 %    |  -2.10 %    |     18.75 %    
[info]         | Entry | 2025-01-06 00:00:00 | main      |  98363.61 USDT | 1150.85 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.01170   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 5 Long  | Exit  | 2024-09-26 00:00:00 | main_exit |  65465.58 USDT | 1156.12 USDT |  63.19 USDT | 64.35 USDT |  -4.71 USDT |    212.84 USDT 
[info]         |       |                     |           |                |              |   5.79 %    |  5.89 %    |  -0.43 %    |     21.28 %    
[info]         | Entry | 2024-09-19 00:00:00 | main      |  61759.98 USDT | 1090.68 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.01766   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 4 Long  | Exit  | 2024-08-26 00:00:00 | main_exit |  62873.87 USDT | 1091.49 USDT | -24.48 USDT | 13.52 USDT | -23.39 USDT |    149.65 USDT 
[info]         |       |                     |           |                |              |  -2.20 %    |  1.21 %    |  -2.10 %    |     14.96 %    
[info]         | Entry | 2024-08-25 00:00:00 | main      |  64157.02 USDT | 1113.77 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.01736   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 3 Long  | Exit  | 2024-07-21 00:00:00 | main_exit |  67827.20 USDT | 1119.15 USDT |  61.17 USDT | 62.29 USDT | -12.40 USDT |    174.13 USDT 
[info]         |       |                     |           |                |              |   5.79 %    |  5.89 %    |  -1.17 %    |     17.41 %    
[info]         | Entry | 2024-07-19 00:00:00 | main      |  63987.92 USDT | 1055.80 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.01650   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 2 Long  | Exit  | 2024-05-20 00:00:00 | main_exit |  71045.44 USDT | 1060.71 USDT |  57.98 USDT | 59.04 USDT | -18.42 USDT |    112.95 USDT 
[info]         |       |                     |           |                |              |   5.79 %    |  5.89 %    |  -1.84 %    |     11.30 %    
[info]         | Entry | 2024-05-18 00:00:00 | main      |  67024.00 USDT | 1000.67 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.01493   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------
[info] 1 Long  | Exit  | 2024-02-08 00:00:00 | main_exit |  45591.77 USDT | 1005.75 USDT |  54.98 USDT | 55.98 USDT | -18.36 USDT |     54.98 USDT 
[info]         |       |                     |           |                |              |   5.79 %    |  5.89 %    |  -1.93 %    |      5.50 %    
[info]         | Entry | 2024-02-04 00:00:00 | main      |  43011.10 USDT |  948.82 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.02206   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------