EMA Crossover with Limit Entry and TP/SL via [[exit]]

This example extends EMA Crossover with Market Entry and TP/SL via [[exit]] with a pending limit entry. It shows the full lifecycle of a pending limit entry: the order is placed, may be filled if price reaches it, and can then be canceled if the strategy requires it, before bracket-style exits are managed.

Because the entry can remain pending or be canceled before execution, this example does not produce exactly the same results as the simpler versions.

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
[[moving_average]]
id           = "fast"
type         = "ema"
length.start = 2
length.stop  = 50

# Slow EMA
[[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 place the limit exit above the
# average entry price.
[[constant]]
id    = "take_profit_pct"
start = 1
stop  = 10

# Stop-loss threshold:
# This percentage range is tested to place the protective stop 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 the limit entry block.
[[crossover]]
id            = "golden_cross"
reference     = "fast"
comparison    = "slow"
next_block_id = "place_limit_entry"

# Place a long limit entry on order "main".
# Using "close" keeps the rule simple while still creating
# a genuine pending limit order.
[[entry]]
id            = "place_limit_entry"
order_id      = "main"
direction     = "long"
limit         = "close"
next_block_id = "wait_entry_fill"

# While entry is pending, either wait for the fill or invalidate the setup.
[[or]]
id            = "wait_entry_fill"
conditions    = ["entry_filled", "entry_invalidated"]
next_block_id = "wait_entry_fill_route"

# Entry fill detection: once at least one trade is open,
# place the TP/SL exit orders.
[[condition]]
id        = "entry_filled"
condition = "opentrades > 0"

# If the bullish setup fails before the limit fill, cancel the pending entry.
[[crossunder]]
id         = "entry_invalidated"
reference  = "fast"
comparison = "slow"

[[if]]
id            = "wait_entry_fill_route"
condition     = "opentrades > 0"
then_block_id = "set_exits"
else_block_id = "cancel_entry"

# Cancel the pending long limit entry and restart the signal wait loop.
[[cancel]]
id            = "cancel_entry"
order_id      = "main"
next_block_id = "golden_cross"

# Place TP/SL exit orders for the filled position.
# TP is a limit order and SL is a stop order.
[[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"

# Monitor exits on each candle:
# 1) TP/SL fill (position is closed)
# 2) Technical close on death cross
[[or]]
id            = "monitor_exits"
conditions    = ["position_closed", "death_cross"]
next_block_id = "monitor_exits_route"

# Exit fill detection: once no open trade remains,
# restart the bullish crossover wait state.
[[condition]]
id        = "position_closed"
condition = "opentrades == 0"

# Technical exit: if fast EMA crosses back below slow EMA, close immediately.
[[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"

# Close the position on order "main" and return directly
# to the next bullish crossover wait state.
[[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-limit-entry.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 318 ms with 117 600 backtests, 1 333 500 positions, and 270.7M 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.025067728612 | G1C123589 | BINANCE:BTCUSDT | D
[info] ========================================================================
[info] 
[info] Backtest Settings
[info] --------------------------------------------------------------
[info] Strategy                        | crossover-tp-sl-limit-entry 
[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.86 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.01 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.76 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.09 USDT |  948.82 USDT |             |            |             |                
[info]         |       |                     |           |                |    0.02206   |             |            |             |                
[info] --------------------------------------------------------------------------------------------------------------------------------------------