Backtesting Ml Models Against Transaction Costs logo

Backtesting Ml Models Against Transaction Costs

Community
HimanshuJ16
backtesting-ml-models-against-transaction-costs

Use when an ML model flips its prediction on nearly every bar and its Sharpe depends on that turnover; applies per-turnover cost charges and confidence thresholding before the strategy is judged. Order-level cost attribution is transaction-cost-analysis-tca-integration.

Overview

PublisherHimanshuJ16
RepositoryAlgo-Trading-Skills
Skill namebacktesting-ml-models-against-transaction-costs
Stars
50
Forks
7
Bundled files
5
LicenseApache-2.0
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • 5 bundled files

    Scripts, templates, and references the model can read while it works. Files are read-only and never executed.

  • Open source

    Published by HimanshuJ16 on GitHub. Read the source before you install it.

Installation

Install the Backtesting Ml Models Against Transaction Costs AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/HimanshuJ16/Algo-Trading-Skills/tree/main/skills/backtesting-ml-models-against-transaction-costs
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/HimanshuJ16/Algo-Trading-Skills.git /tmp/Algo-Trading-Skills
mkdir -p .claude/skills
cp -r /tmp/Algo-Trading-Skills/skills/backtesting-ml-models-against-transaction-costs .claude/skills/backtesting-ml-models-against-transaction-costs
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Backtesting Ml Models Against Transaction Costs in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Backtesting Ml Models Against Transaction Costs on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Backtesting Ml Models Against Transaction Costs is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

When to Use

Machine learning models inherently generate highly active signals (e.g., changing predictions from slightly positive to slightly negative on every bar). Without strict Transaction Cost Analysis (TCA) and confidence thresholding, these models achieve massive theoretical Sharpe ratios that instantly collapse in live trading due to Turnover Drag. Invoke this skill to evaluate ML model predictions against realistic slippage and commission models, filtering out low-confidence trades that do not cover their own execution costs.

When NOT to Use

  • Order-level cost attribution. This skill models cost as a flat bps charge per unit of turnover. To decompose realised shortfall into delay, spread, and market impact, use transaction-cost-analysis-tca-integration.
  • Size-dependent impact. The flat-rate model has no ADV or participation term, so it will understate cost for orders that are large relative to available liquidity. Pair it with liquidity-adjusted-position-sizing and strategy-capacity-estimation-before-scaling-capital before scaling capital.
  • Continuous or leveraged target weights. Positions here are discrete {-1, 0, +1}. A continuous-weight portfolio needs turnover computed on the weight vector — see portfolio-construction-with-transaction-cost-awareness.

Prerequisites

  • An array of numerical ML predictions (e.g., predicted forward returns or continuous signal strength).
  • An array of actual underlying returns, already aligned so that element i is the return realised after predictions[i] was observable. The backtester cannot detect misalignment.
  • Estimated transaction costs in basis points (bps) per half-turn (e.g., 5 bps for slippage + fee), calibrated against your own execution data rather than a folklore constant.

Workflow

  1. Align predictions to forward returns: pair predictions[t] with the return earned by acting on it (t → t+1, or t+1 → t+2 if your execution lags the signal by a bar). Getting this wrong is the single most common way to produce a spectacular, meaningless backtest.
  2. Clean the inputs: drop or impute NaN/Inf predictions before the backtest. A NaN prediction silently classified as "flat" is a hidden regime change in the strategy, not a neutral default — MlTcaBacktester rejects non-finite input rather than absorbing it.
  3. Set the hurdle: the entry threshold must clear the round-trip cost. A position that is entered and later exited pays 2 units of turnover, so require signal_threshold >= 2 * bps_cost_half_turn / 10_000 in decimal-return units. Below this line, a correct prediction is still a losing trade.
  4. Generate positions: map predictions to {+1, -1, 0} against the threshold.
  5. Decide whether to apply a buy/hold spread: if the model churns around the threshold boundary, set exit_threshold < signal_threshold so an open position is held until conviction genuinely decays, rather than being closed and re-entered on noise.
  6. Calculate turnover: costs are charged on position changes only. Flat→long is 1 unit; long→short is 2 units; a position still open on the final bar owes its exit half-turn (liquidate_at_end, on by default).
  7. Apply cost drag and compute net returns: subtract turnover × bps_cost_half_turn / 10_000 from gross returns each period.
  8. Sweep the threshold out-of-sample: pick signal_threshold/exit_threshold on training folds and confirm them on held-out data. Choosing the threshold that maximises net return on the same sample you report is parameter mining, not cost control — see walk-forward-optimization-window-management.
  9. Stress the cost assumption: re-run at 2–3× your estimated bps. A strategy whose net edge disappears when costs double has no margin for a bad execution day.

Full procedure: see references/workflows.md. Standards reference: see references/standards.md. Printable pre-flight checklist: see assets/checklist.md.

Common Pitfalls

  • Ignoring State Persistence: Charging a transaction cost on every bar simply because the model predicted "Buy", even though the portfolio is already in a "Buy" state. Costs are only incurred on position changes (Turnover).
  • Zero Thresholding: Taking trades on microscopic predictions (e.g., model predicts +0.0001% return) which guarantees a net loss when the transaction cost is 0.05%. A threshold of exactly 0 is worse than useless — it makes the long and short conditions overlap, so a neutral prediction can be classified as a short.
  • Symmetric Costs: Forgetting that crossing the spread (market order) costs money on both entry and exit (half-turns).
  • The Free Exit: Ending the backtest with an open position and never charging the closing half-turn. On a low-trade-count strategy this can hide a meaningful slice of total cost.
  • Contemporaneous Returns: Multiplying predictions[t] by the return of the bar the prediction was made on. This is look-ahead bias, and the resulting equity curve survives any cost assumption you throw at it — see lookahead-bias-elimination.
  • NaN as Neutral: Letting missing predictions default to "flat". The backtest then reports a strategy nobody can trade, and a NaN in the return series silently poisons every compounded metric.
  • Threshold Mined In-Sample: Sweeping signal_threshold across the full history and reporting the best net result. The threshold is a free parameter, and cost-aware backtests overfit it as readily as any other.

Verification

  • Simulate an ML model that perfectly predicts 10% returns but flips direction every day. Apply a 6% (600 bps) per-half-turn cost. The gross return will be massive, but the net return must be decisively negative — covered by test_perfect_but_churning_model_is_net_negative.
  • Confirm a held position is charged once, not once per bar: two consecutive identical long signals must produce 1 unit of turnover, not 2.
  • Confirm a neutral (0.0) prediction maps to flat, never to short.
  • Run python -m unittest discover -s skills/backtesting-ml-models-against-transaction-costs/scripts and confirm a 100% pass rate.

Related Skills

  • transaction-cost-analysis-tca-integration
  • execution-cost-model-recalibration-cadence
  • lookahead-bias-elimination
  • execution-realistic-simulation
  • walk-forward-optimization-window-management
  • portfolio-construction-with-transaction-cost-awareness

Bundled files

The model reads these on demand while the skill is loaded. They are exposed as readable files and are never executed.

Frequently asked questions

What does the Backtesting Ml Models Against Transaction Costs AI skill do?

Use when an ML model flips its prediction on nearly every bar and its Sharpe depends on that turnover; applies per-turnover cost charges and confidence thresholding before the strategy is judged. Order-level cost attribution is transaction-cost-analysis-tca-integration.

Why use Backtesting Ml Models Against Transaction Costs on TypingMind?

Because you install it once and use it with any model. Backtesting Ml Models Against Transaction Costs is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Backtesting Ml Models Against Transaction Costs in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/HimanshuJ16/Algo-Trading-Skills/tree/main/skills/backtesting-ml-models-against-transaction-costs. TypingMind reads its SKILL.md and bundles its files and installs it as a skill you can enable per chat.

Which AI models can use Backtesting Ml Models Against Transaction Costs?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Backtesting Ml Models Against Transaction Costs?

As many as you like. As long as a model supports skills, you can use Backtesting Ml Models Against Transaction Costs with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Backtesting Ml Models Against Transaction Costs AI skill free?

Yes. It is published on GitHub by HimanshuJ16 under the Apache-2.0 license. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇