Time in Position Trigger
The time_in_position trigger fires when a position has been open for a specified duration. Use it as a safety net to free stuck capital when other exit triggers have not fired.
TOML Syntax
[[actions.triggers]]
type = "time_in_position"
value = "7d" # Required: duration string
Parameters
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
type | string | Yes | — | Must be "time_in_position" |
value | string | Yes | — | Duration: "24h", "7d", or minutes as a number |
max_count | integer | No | unlimited | Maximum times this trigger can fire per position |
time_in_position does not support the timeframe field. Duration is absolute, not candle-dependent.
Duration Format
| Value | Duration |
|---|---|
"1h" | 1 hour (60 minutes) |
"24h" | 24 hours |
"7d" | 7 days |
"30d" | 30 days |
"60" | 60 minutes (numeric = minutes) |
The engine converts the duration to minutes internally and checks if the position has been open for at least that many 1-minute candles.
How It Works
- When a position opens, the engine records the entry tick (candle index)
- On every candle, it calculates:
minutes_open = current_tick - entry_tick - When
minutes_open >= duration_in_minutes, the trigger fires
graph TD
A["Position opens<br/>at tick N"] --> B["Each candle:<br/>elapsed = tick - N"]
B --> C{"elapsed >=<br/>duration?"}
C -->|Yes| D["Trigger fires"]
C -->|No| E["Continue waiting"]
E --> B
Examples
Close After 7 Days
A safety exit that frees capital stuck in a position that is not moving:
[[actions]]
type = "sell"
amount = "100%"
[[actions.triggers]]
type = "time_in_position"
value = "7d"
Close After 24 Hours
For shorter-term strategies or scalping:
[[actions]]
type = "sell"
amount = "100%"
[[actions.triggers]]
type = "time_in_position"
value = "24h"
Complete Strategy with Time Exit
Time exits pair well with other exit triggers. The first one to fire wins:
[meta]
name = "Time-Protected DCA"
max_open_positions = 1
# Entry
[[actions]]
type = "open_long"
amount = "100 USDC"
[[actions.triggers]]
indicator = "rsi_14"
operator = "<"
target = "30"
timeframe = "1h"
# Take profit at +3%
[[actions]]
type = "sell"
amount = "100%"
[[actions.triggers]]
type = "pos_price_change"
value = "3%"
# Trailing stop at -4% from peak
[[actions]]
type = "sell"
amount = "100%"
[[actions.triggers]]
type = "trailing_stop"
value = "-4%"
# Time exit: close after 7 days no matter what
[[actions]]
type = "sell"
amount = "100%"
[[actions.triggers]]
type = "time_in_position"
value = "7d"
Tips
A time_in_position exit is your safety net. It prevents capital from being locked in a position that never hits its take-profit or stop-loss. A 7-day time exit is a good default for swing strategies.
In backtesting, time_in_position helps ensure positions close before the end of your data range. Without it, positions open near the end of your backtest data may never close, skewing results.
time_in_position fires regardless of the position's profitability. If your position is up 10% after 7 days, the time exit still sells at 100%. If you want to only force-close losing positions, combine it with a pos_price_change trigger in the same action (AND logic).
Related Triggers
| Trigger | What It Measures |
|---|---|
| Position Price Change | Fixed % from entry price |
| Trailing Stop | % drop from peak price |
| Next Candle | Candle-close scheduling |