Replies: 1 comment
|
Yes, but the implementation depends on how simple your position model is. For a simple long-only strategy with one position at a time, you can create a time-based exit signal by counting bars since the latest entry. Conceptually: max_bars = 10
entry_index = pd.Series(np.where(entries, np.arange(len(entries)), np.nan), index=close.index)
last_entry_index = entry_index.ffill()
bars_since_entry = pd.Series(np.arange(len(close)), index=close.index) - last_entry_index
time_exit = bars_since_entry >= max_bars
exits = take_profit_exit | time_exitThen pass The important caveat is that this is only a clean approximation when you do not pyramid and do not have multiple overlapping lots. If you can add to a position, partially exit, reverse, or hold multiple lots, “number of bars in trade” becomes path-dependent. In that case the safer approach is a custom signal/order function that tracks the actual entry bar of the open trade and emits an exit once the age reaches your limit. Also decide whether the forced time exit should use the same execution assumptions as your normal exits. If signals are evaluated at close but executed next bar, shift the time-exit signal consistently so you do not accidentally exit on information from the same bar. |
Uh oh!
There was an error while loading. Please reload this page.
Is it possible to limit the trade duration to a number of bars if take profit has not been reached? If yes, how to do it? In my strategy, this would be my stoploss.
All reactions