kernc / kernc/backtesting.py

Add Time-in-Force (TIF) for limit orders

Open
#1,285 2 comments 3 reactions 0 assignees View on GitHub
enhancement
Dominant language
Python
Stars
9k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

### Enhancement description

Currently, backtesting.py allows the placement of limit orders. However, these orders appear to remain "Good-Til-Cancelled" (GTC) indefinitely if not immediately triggered. There is no built-in mechanism to define an expiry date or a time-in-force (TIF) for these pending orders.

This limitation significantly impacts the realism of backtesting, as many real-world trading strategies and brokers require or benefit from orders that automatically expire after a certain period (e.g., end of day, after X bars, after Y days).

Without TIF/expiry options, users are forced to manually implement order cancellation logic within their strategy code (next() method), which can be complex, error-prone, and less efficient. This also adds unnecessary overhead and reduces the clarity of the trading logic.

I would appreciate extending the buy() and sell() methods to allow specifying a time-in-force or expiry for limit orders.

Contributor guide

Open the contributing guide

Research direction

Start by tracing the buy() and sell() methods and how pending limit orders are represented and processed. Determine how an expiry or time-in-force option should apply to orders that are not immediately triggered, including examples such as end of day or a number of bars. Done means the public methods support the chosen option and pending orders expire accordingly, with relevant tests added.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend-api-design, fintech-quant
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.