EMA Crossover with Market Entry and Close-Based TP/SL

This example adds close-based TP and SL exits to an EMA crossover.

Exits are handled by an [[or]] block checked on each candle: as soon as a TP, SL, or death-cross signal becomes true, the strategy moves to a shared [[close]] block. Results are ranked by Calmar ratio.

See the console output.

# Illustrative example only, do not use for live trading. Public domain (CC0 1.0).

# Backtest configuration
[backtest]
symbol            = ["BINANCE:BTCUSDT"]
timeframe         = ["D"]
start_date        = 2024-01-01
end_date          = 2026-01-01
initial_capital   = 1000
# 95% keeps position size below 1x to avoid an immediate 1x margin call
# caused by trading fees and the gap between signal and execution price.
default_qty_value = 95

# Rank the tested combinations by Calmar ratio so the best return-over-drawdown
# EMA crossover configurations appear first in the results.
[[objective]]
id        = "calmar"
formula   = "calmar_ratio"
ascending = false

# Enable detailed trade output in the results.
[logging]
include_trades    = true
timestamp_enabled = false

# Fast EMA:
# this is the reactive line used to detect bullish and bearish crossovers.
[[moving_average]]
id           = "fast"
type         = "ema"
length.start = 2
length.stop  = 50

# Slow EMA:
# this is the reference line used to confirm the broader trend direction.
[[moving_average]]
id           = "slow"
type         = "ema"
length.start = 2
length.stop  = 50

# Hyperparameter constraint:
# keep the fast EMA shorter than the slow EMA so only coherent crossover
# combinations are tested.
[constraints]
condition = "fast.length < slow.length"

# Take-profit threshold:
# This percentage range is tested to validate a close-based exit once the
# candle close rises enough above the average entry price.
[[constant]]
id    = "take_profit_pct"
start = 1
stop  = 10

# Stop-loss threshold:
# This percentage range is tested to validate a close-based exit once the
# candle close falls enough below the average entry price.
[[constant]]
id    = "stop_loss_pct"
start = 1
stop  = 10

# Entry point of the state machine.
# Sequential execution starts here and first jumps to "golden_cross".
[start]
id            = "origin"
next_block_id = "golden_cross"

# Wait for the fast EMA to cross above the slow EMA.
# Once validated, execution moves to "enter_long".
[[crossover]]
id            = "golden_cross"
reference     = "fast"
comparison    = "slow"
next_block_id = "enter_long"

# Open a long position on order "main",
# then move to the exit-monitoring block.
[[entry]]
id            = "enter_long"
order_id      = "main"
direction     = "long"
next_block_id = "monitor_exits"

# Monitor exits on each candle.
# Child blocks are checked in order: TP, then SL, then death cross.
# As soon as one child validates, the OR block jumps to "close_position".
[[or]]
id            = "monitor_exits"
conditions    = ["take_profit_hit", "stop_loss_hit", "death_cross"]
next_block_id = "close_position"

# TP: validate exit when close reaches the threshold computed from
# position_avg_price (the average entry price of the open position).
[[condition]]
id        = "take_profit_hit"
condition = "close >= position_avg_price * (1 + take_profit_pct / 100)"

# SL: validate exit when close reaches the protective threshold
# computed from position_avg_price.
[[condition]]
id        = "stop_loss_hit"
condition = "close <= position_avg_price * (1 - stop_loss_pct / 100)"

# Technical exit: if the fast EMA crosses back below the slow EMA,
# the strategy also closes the position.
[[crossunder]]
id         = "death_cross"
reference  = "fast"
comparison = "slow"

# Close the position on order "main" and jump directly
# back to waiting for the next bullish crossover.
[[close]]
id            = "close_position"
order_id      = "main"
next_block_id = "golden_cross"

Console output

[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.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 6 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 222 ms with 117 600 backtests, 1 333 500 positions, and 387.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.912543536018 | G1C97857 | BINANCE:BTCUSDT | D
[info] ========================================================================
[info] 
[info] Backtest Settings
[info] ------------------------------------------------------
[info] Strategy                        | crossover-tp-sl     
[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     |  6              
[info] slow.length     | 39              
[info] stop_loss_pct   |  1.000000000000 
[info] take_profit_pct |  5.000000000000 
[info] ----------------------------------
[info] 
[info] Overview
[info] -------------------------------------------------------------------------------
[info] Total P&L   | Max equity drawdown | Trades | Profitable trades | Profit factor 
[info] -------------------------------------------------------------------------------
[info] 735.95 USDT |          60.71 USDT |     12 |           83.33 % |        10.642 
[info]  73.60 %    |           3.55 %    |        | 10/12             |               
[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          |  735.95 USDT | 735.95 USDT | 0.00 USDT 
[info]                     |   73.60 %    |  73.60 %    | 0.00 %    
[info] -------------------------------------------------------------
[info] Gross profit        |  812.29 USDT | 812.29 USDT | 0.00 USDT 
[info]                     |   81.23 %    |  81.23 %    | 0.00 %    
[info] -------------------------------------------------------------
[info] Gross loss          |   76.33 USDT |  76.33 USDT | 0.00 USDT 
[info]                     |    7.63 %    |   7.63 %    | 0.00 %    
[info] -------------------------------------------------------------
[info] Commission paid     |   30.92 USDT |  30.92 USDT | 0.00 USDT 
[info] -------------------------------------------------------------
[info] Buy & hold return   | 1010.86 USDT |             |           
[info]                     |  101.09 %    |             |           
[info] -------------------------------------------------------------
[info] Max equity run-up   |  759.01 USDT |             |           
[info]                     |   43.17 %    |             |           
[info] -------------------------------------------------------------
[info] Max equity drawdown |   60.71 USDT |             |           
[info]                     |    3.55 %    |             |           
[info] -------------------------------------------------------------
[info] 
[info] Trade Analysis
[info] ----------------------------------------------------------------------
[info] Metric                        | All         | Long        | Short     
[info] ----------------------------------------------------------------------
[info] Total trades                  |  12         |  12         | 0         
[info] ----------------------------------------------------------------------
[info] Total open trades             |   0         |             |           
[info] ----------------------------------------------------------------------
[info] Winning trades                |  10         |  10         | 0         
[info] ----------------------------------------------------------------------
[info] Losing trades                 |   2         |   2         | 0         
[info] ----------------------------------------------------------------------
[info] Percent profitable            |  83.33 %    |  83.33 %    | 0.00 %    
[info] ----------------------------------------------------------------------
[info] Avg P&L                       |  61.33 USDT |  61.33 USDT | 0.00 USDT 
[info]                               |   5.02 %    |   5.02 %    | 0.00 %    
[info] ----------------------------------------------------------------------
[info] Avg winning trade             |  81.23 USDT |  81.23 USDT | 0.00 USDT 
[info]                               |   6.56 %    |   6.56 %    | 0.00 %    
[info] ----------------------------------------------------------------------
[info] Avg losing trade              |  38.17 USDT |  38.17 USDT | 0.00 USDT 
[info]                               |   2.68 %    |   2.68 %    | 0.00 %    
[info] ----------------------------------------------------------------------
[info] Largest winning trade         | 136.45 USDT | 136.45 USDT | 0.00 USDT 
[info] ----------------------------------------------------------------------
[info] Largest winning trade percent |   9.48 %    |   9.48 %    | 0.00 %    
[info] ----------------------------------------------------------------------
[info] Largest losing trade          |  50.39 USDT |  50.39 USDT | 0.00 USDT 
[info] ----------------------------------------------------------------------
[info] Largest losing trade percent  |   3.11 %    |   3.11 %    | 0.00 %    
[info] ----------------------------------------------------------------------
[info] 
[info] Risk and Performance Ratios
[info] ---------------------------------------------
[info] Metric          | All     | Long    | Short  
[info] ---------------------------------------------
[info] Sharpe ratio    |  2.343  |         |        
[info] ---------------------------------------------
[info] Sortino ratio   |  7.170  |         |        
[info] ---------------------------------------------
[info] Calmar ratio    |  8.913  |         |        
[info] ---------------------------------------------
[info] Profit factor   | 10.642  |         |        
[info] ---------------------------------------------
[info] Market Exposure | 11.61 % | 11.61 % | 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] 12 Long | Exit  | 2025-10-07 00:00:00 | main   | 124658.54 USDT | 1654.22 USDT |  77.24 USDT |  99.34 USDT |  -5.76 USDT |    735.95 USDT 
[info]         |       |                     |        |                |              |   4.90 %    |   6.31 %    |  -0.37 %    |     73.60 %    
[info]         | Entry | 2025-10-02 00:00:00 | main   | 118594.99 USDT | 1573.76 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01327   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 11 Long | Exit  | 2025-09-23 00:00:00 | main   | 112650.99 USDT | 1573.73 USDT | -50.39 USDT |  24.51 USDT | -60.71 USDT |    658.72 USDT 
[info]         |       |                     |        |                |              |  -3.11 %    |   1.51 %    |  -3.74 %    |     65.87 %    
[info]         | Entry | 2025-09-13 00:00:00 | main   | 116029.41 USDT | 1620.93 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01397   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 10 Long | Exit  | 2025-07-11 00:00:00 | main   | 116010.01 USDT | 1631.10 USDT | 136.45 USDT | 150.15 USDT | -15.31 USDT |    709.11 USDT 
[info]         |       |                     |        |                |              |   9.14 %    |  10.06 %    |  -1.03 %    |     70.91 %    
[info]         | Entry | 2025-06-25 00:00:00 | main   | 106083.00 USDT | 1491.53 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01406   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 9 Long  | Exit  | 2025-04-23 00:00:00 | main   |  93442.99 USDT | 1501.63 USDT | 129.93 USDT | 138.58 USDT |  -1.92 USDT |    572.66 USDT 
[info]         |       |                     |        |                |              |   9.48 %    |  10.11 %    |  -0.14 %    |     57.27 %    
[info]         | Entry | 2025-04-21 00:00:00 | main   |  85179.24 USDT | 1368.83 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01607   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 8 Long  | Exit  | 2025-01-22 00:00:00 | main   | 106143.82 USDT | 1374.56 USDT |  70.45 USDT | 116.42 USDT | -42.25 USDT |    442.73 USDT 
[info]         |       |                     |        |                |              |   5.41 %    |   8.94 %    |  -3.24 %    |     44.27 %    
[info]         | Entry | 2025-01-16 00:00:00 | main   | 100497.35 USDT | 1301.44 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01295   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 7 Long  | Exit  | 2025-01-07 00:00:00 | main   | 102235.60 USDT | 1307.59 USDT |  64.61 USDT |  69.04 USDT | -12.56 USDT |    372.29 USDT 
[info]         |       |                     |        |                |              |   5.20 %    |   5.56 %    |  -1.01 %    |     37.23 %    
[info]         | Entry | 2025-01-03 00:00:00 | main   |  96984.79 USDT | 1240.44 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01279   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 6 Long  | Exit  | 2024-10-15 00:00:00 | main   |  66084.00 USDT | 1246.34 USDT |  64.41 USDT |  73.51 USDT | -10.42 USDT |    307.68 USDT 
[info]         |       |                     |        |                |              |   5.46 %    |   6.23 %    |  -0.88 %    |     30.77 %    
[info]         | Entry | 2024-10-12 00:00:00 | main   |  62539.99 USDT | 1179.50 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01886   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 5 Long  | Exit  | 2024-09-27 00:00:00 | main   |  65173.99 USDT | 1184.86 USDT |  59.76 USDT |  73.03 USDT |  -4.85 USDT |    243.26 USDT 
[info]         |       |                     |        |                |              |   5.32 %    |   6.50 %    |  -0.43 %    |     24.33 %    
[info]         | Entry | 2024-09-19 00:00:00 | main   |  61759.98 USDT | 1122.80 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01818   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 4 Long  | Exit  | 2024-08-27 00:00:00 | main   |  62834.00 USDT | 1124.10 USDT | -25.94 USDT |  13.93 USDT | -25.42 USDT |    183.50 USDT 
[info]         |       |                     |        |                |              |  -2.26 %    |   1.21 %    |  -2.21 %    |     18.35 %    
[info]         | Entry | 2024-08-25 00:00:00 | main   |  64157.02 USDT | 1147.77 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01789   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 3 Long  | Exit  | 2024-07-22 00:00:00 | main   |  68165.35 USDT | 1153.36 USDT |  66.75 USDT |  71.31 USDT | -15.46 USDT |    209.44 USDT 
[info]         |       |                     |        |                |              |   6.15 %    |   6.57 %    |  -1.42 %    |     20.94 %    
[info]         | Entry | 2024-07-18 00:00:00 | main   |  64087.99 USDT | 1084.37 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01692   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 2 Long  | Exit  | 2024-05-21 00:00:00 | main   |  71446.62 USDT | 1090.99 USDT |  92.76 USDT |  94.90 USDT |  -2.96 USDT |    142.69 USDT 
[info]         |       |                     |        |                |              |   9.30 %    |   9.52 %    |  -0.30 %    |     14.27 %    
[info]         | Entry | 2024-05-17 00:00:00 | main   |  65235.21 USDT |  996.14 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.01527   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------
[info] 1 Long  | Exit  | 2024-02-09 00:00:00 | main   |  45288.66 USDT | 1000.88 USDT |  49.93 USDT |  58.13 USDT | -24.30 USDT |     49.93 USDT 
[info]         |       |                     |        |                |              |   5.26 %    |   6.12 %    |  -2.56 %    |      4.99 %    
[info]         | Entry | 2024-01-31 00:00:00 | main   |  42941.10 USDT |  949.00 USDT |             |             |             |                
[info]         |       |                     |        |                |    0.02210   |             |             |             |                
[info] ------------------------------------------------------------------------------------------------------------------------------------------