40 Commits
Author SHA1 Message Date
Khizar Imran 872c8a30c8 perf: reduce Python callback overhead and verify matched benchmarks
Reuse the Python callback method-name string and expose constant-time position count and presence. Preserve dynamic callbacks, Bar identity, and snapshot behavior with regression tests.

Add isolated callback measurements and replace approximate cross-library comparisons with matched trades and equity assertions, separate setup/run timings, and reproducible reports. Track the remaining ingestion bottleneck in #53.

Refs #48, #38, #39, #53

Codex 🤖, On behalf of Khizar
2026-10-31 22:45:13 +00:00
Khizar Imran b82a09ff71 fix: validate broker settings, orders, and optimization signals
Reject invalid numeric inputs at Python boundaries before account mutation or grid workers start. Preserve partial-close no-ops and profit-locking stops, and reject invalid native SMA windows without panicking.

Refs #16

Codex 🤖, On behalf of Khizar
2026-10-31 00:47:50 +00:00
Khizar Imran 44c36b2563 fix: use opening prices for gap-crossed stops and targets
Process opening SL/TP triggers before intrabar range touches, preserving midpoint triggers and exit costs. Add regression coverage and document execution and migration behavior.

Refs #8

Codex 🤖, On behalf of Khizar
2026-10-31 00:34:15 +00:00
Khizar Imran c4281671a0 perf: process batch SL/TP exits in linear time
Codex 🤖, On behalf of Khizar
2026-10-20 22:16:46 +00:00
Khizar ImranandGitHub c490f274e6 Document and test stop-first intrabar execution policy (#49) 2026-10-18 15:26:34 +01:00
Khizar Imran 623a0c6976 Reset broker state before independent native engine runs 2026-10-08 20:08:05 +01:00
Khizar Imran d77ceeeb1a Fix full bid-ask spread semantics and equity marking
Codex 🤖, On behalf of Khizar
2026-10-06 20:56:06 +00:00
KhizarImran 7627094c63 feat: partial close and in-place stop loss updates
Adds two Broker methods for path-dependent strategies:

- close_partial(id, lot_size) scales out of a position and leaves the
  remainder open under the same id, so it can keep being managed.
- update_sl(id, stop_loss) moves the stop in place. A trailing stop no
  longer has to close and reopen, which paid spread and commission on
  every step.

Both are reachable from Python as Strategy.close_partial and
Strategy.update_sl.

Closing a position, part of one, or the whole book previously repeated
the same PnL and commission arithmetic in four places. That is now one
settle() helper taking the number of lots to close, so a partial close
is the same code with a smaller number and the feature made broker.rs
shorter rather than longer.

Commission splits across the legs: the entry was charged on the full
size when the position opened, so settle only takes the exit leg for
the lots being closed. Two 0.5 closes land on the same cash as one 1.0
close, which is what closing_partially_twice_matches_closing_once pins.

update_sl deliberately does not check which way the stop moves, since
widening a stop is a legitimate thing for a strategy to do.
2026-08-30 11:16:37 +01:00
KhizarImran d54c3827b8 chore: fix clippy warnings and enforce clippy in CI
Replace four map_or(false, ..) calls in check_sl_tp with is_some_and, which
reads closer to the intent ("there is a stop and price hit it").

CI now runs `cargo clippy --all-targets -- -D warnings` in place of
`cargo check` — clippy is a superset, so this is more coverage in one
fewer step, and -D warnings stops the lint debt building back up.
2026-08-02 22:28:59 +01:00
KhizarImran 4b4d53dd26 new: optimise.rs which handles optimisation. also linked into the python 2026-08-02 22:25:03 +01:00
KhizarImran 494f157c13 bench: add three-way speed benchmark (backtesting.py vs py-next vs native rust)
Adds a native-Rust SMA strategy path (Engine.run_native_sma) so we can time
the pure-Rust engine against the Python-callback path and backtesting.py.
2026-07-18 00:08:49 +01:00
Khizar c31e0088db feat: add standalone HTML reports 2026-07-17 14:54:59 +00:00
Khizar 8275c809e5 fix: include final liquidation in equity curve 2026-07-17 14:24:38 +00:00
Khizar 2c07815aba fix: reconcile trade pnl with commissions 2026-07-17 14:20:28 +00:00
KhizarImran 6aee7006a5 test: cover close_position and short-side SL/TP; add CI
Adds the two remaining broker test gaps (close_position by id,
short position stop-loss fill). CI runs cargo check + cargo test
on push/PR to main.
2026-07-12 21:22:47 +01:00
KhizarImran b8c0cd1d57 test: add unit tests for broker PnL, commission, and SL/TP fills
Covers buy/sell close PnL, commission charged on open and close,
long SL fills at the stop price (not bar.close), positions staying
open when neither SL nor TP is hit, and unrealized equity.
2026-07-12 21:20:41 +01:00
KhizarImran cefa7c8ee1 feat: Position __repr__ and DataFrame column validation 2026-07-05 14:58:58 +01:00
KhizarImran c22153f02f feat: strategy data access, sharpe ratio, and correctness fixes
- Trade.pnl now stores net pnl (after exit commission) so per-trade stats are accurate
- Position pyclass uses from_py_object to fix deprecation warning
- Removed dead AttributeError swallow in engine.rs
- Strategy gains self.data, self.index, self.cash, self.equity properties
- Broker.cash exposed to Python via pyo3(get)
- Sharpe ratio added to Stats (unannualized)
- Added examples/sma_cross.py and examples/compare_bt.py
- Logic verified against backtesting.py: 34 trades, 29.4% win rate match
2026-07-05 14:42:10 +01:00
KhizarImran 2af19afafa positions are now visible in python. can be check by if not sel.position 2026-07-02 22:12:20 +01:00
KhizarImran 53fe8e0e9d fix: commision bug fix 2026-06-25 21:48:49 +01:00
KhizarImranandClaude Sonnet 4.6 e79070539d feat: column-aware CSV loading and changelog
- Replaced manual CSV parsing with csv crate, columns now looked up by name
- Fixed Rust example path and contract_size
- Added CHANGELOG.md for release tracking

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-21 18:54:14 +01:00
KhizarImran 62131a2918 getting it ready for publishing 2026-06-21 17:56:41 +01:00
KhizarImran cb9bf41ff3 added lot size 2026-06-21 00:18:44 +01:00
KhizarImran 706be103f0 bug fixes: types.rs 2026-06-20 17:52:19 +01:00
KhizarImran 5609249180 bug: backtest.py bug fixes 2026-06-20 17:43:19 +01:00
KhizarImran b0559ecf79 core: fully restructured code to support pyo3 2026-06-14 02:13:28 +01:00
KhizarImran 322ff54d63 made pyclass work for stats 2026-06-14 01:29:20 +01:00
KhizarImran 6f39fc2d34 adding python wrapping 2026-06-14 01:21:15 +01:00
KhizarImran a039484b08 core: when backtest finishes, any open positions should now close at bar.last 2026-06-13 23:28:49 +01:00
KhizarImran 9847a99561 feat: stoploss and take profit implementation on the broker.rs 2026-06-13 12:01:44 +01:00
Khizar Imran 8da311cc3f bug fix: broker.rs (close position & close all) 2026-06-09 00:10:09 +01:00
Khizar Imran 9081e2b7ab feat: spread calculated on exit (close position & all) 2026-06-09 00:01:46 +01:00
Khizar Imran 26e625d6f8 new: better stats 2026-06-08 23:55:42 +01:00
Khizar Imran bbfe87edb7 checkpoint: spread/commisions working 2026-06-06 17:16:32 +01:00
Khizar Imran 1dc728078f new: added a working example 2026-06-06 16:18:45 +01:00
Khizar Imran 04b5f405e0 minor commenting 2026-06-04 21:34:26 +01:00
Khizar Imran 73a144c4df added engine rs & data rs 2026-06-04 21:25:52 +01:00
Khizar Imran 7d19b1d519 fn: added close_position to broker.rs 2026-06-04 00:25:08 +01:00
Khizar Imran 35a6f8dc00 build: fixed error relates to exit_price & exit_timestamp 2026-06-04 00:06:43 +01:00
Khizar Imran 48586de436 Initial commit: project strcture & initialisation 2026-06-03 23:39:39 +01:00