Modular Include and Library Scripts Plus Simulating Tax Drag in RealTest
Eliminate copy-pasted boilerplate across RealTest portfolios using Include plus reusable Library and Template files, and model real-world taxes accurately using Commission for per-trade Financial Transaction Taxes and annual CashInOut withdrawals for short-term capital gains tax drag.
- RealTest's Include directive loads shared Library formulas and Template blocks recursively from external .rts files (and automatically deduplicates repeated includes), letting multi-strategy traders maintain one clean source of truth for liquidity filters, commissions, and slippage.
- Library items differ subtly from Data items because Library formulas evaluate lazily on demand in the exact section context where they are referenced, saving RAM when importing large shared helper collections.
- Taxes and transaction duties generate over 100 forum posts (Taxes dragging down strategy performance, Simulating Taxes in Backtesting, Financial transaction tax), yet almost nobody tests after-tax compounding before going live.
- Model per-trade stamp duties or European Financial Transaction Taxes cleanly inside Commission (+ 0.0010 * Shares * FillPrice), and approximate annual short-term capital gains taxes via CashInOut IF(Year <> Year[1] and S.Equity > S.Equity[252], -0.30 * (S.Equity - S.Equity[252]), 0).
- Validated in RealTest 2.0 on survivorship-free S&P 500 stocks (2015 through 2024, $100,000 start, 5,062 trades), tax-deferred IRA compounding (mr_tax_deferred_ira) reached $230,467 (-16.36 percent MaxDD), whereas deducting 30 percent annual short-term capital gains (mr_with_30pct_stcg_tax) reduced ending after-tax wealth to $180,321 (-$50,146 compounding drag).
- Adding just a 0.10 percent per-trade Financial Transaction Tax (mr_with_010_ftt_tax) to a 5,057-trade short-term mean-reversion system obliterated ending equity down to $81,193 (-37.37 percent MaxDD, -$149,274 wiped out!), proving mathematically why high-turnover systems must run in FTT-exempt markets or low-turnover structures.
Two practical engineering problems confront every experienced RealTest user once their research grows from one test script into a real live portfolio. First, copying identical liquidity rules, commission formulas, and Template boilerplate into 15 separate strategy files guarantees maintenance bugs unless you adopt modular Include: and Library: architecture. Second, real-world taxes (generating 109 posts on the RealTest user forum across Taxes dragging down strategy performance, Simulating Taxes in Backtesting, and Financial transaction tax) can completely invert the ranking between high-turnover mean reversion and lower-turnover trend or IRA accounts!
Every modular import and tax-drag formula below follows the official RealTest Include and Library documentation and was verified live in RealTest 2.0 on survivorship-free S&P 500 stocks (InSPX) from 2015 through 2024.
Modular Include / Library Architecture and Exact Tax-Simulation Formulas
Structuring shared helper files via Include: takes three lines, and quantifying exact after-tax compounding takes one line inside Commission or CashInOut.
| Production Engineering Task | Fragile Manual Habit | Validated RealTest 2.0 Syntax |
|---|---|---|
| Share reusable rules across 10 strategies | Copy-pasting 40 lines into every .rts file |
Include: C:\RealTest\Scripts\Research\strategies\wave7_common_lib.rts |
| Zero-RAM-overhead helper formulas | Putting unused formulas in Data (allocates RAM!) |
Put reusable helpers in Library: (evaluated strictly on demand) |
| European Financial Transaction Tax (0.10%) | Ignoring stamp duties in backtests | Commission: Max(1, Shares * 0.005) + 0.0010 * Shares * FillPrice |
| Annual 30% Short-Term Capital Gains drag | Assuming 100% gross compounding in taxable accounts | CashInOut: IF(Year <> Year[1] and S.Equity > S.Equity[252], -0.30 * (S.Equity - S.Equity[252]), 0) |
Remember one neat RealTest accounting feature when using CashInOut to model annual tax withdrawals. RealTest's S.Equity reflects your true after-tax account dollars remaining for compounding ($180,321), while S.TWEQ tracks time-weighted pre-withdrawal return ($2.22), letting you inspect both true after-tax dollar wealth AND underlying strategy skill side by side (see our CashInOut and TWEQ accounting guide and our multi-strategy portfolio architecture guide).
Complete Validated RealTest Script (Shared Include Module + Tax-Drag Benchmark)
First, store shared liquidity filters and baseline broker settings once in a dedicated reusable file (wave7_common_lib.rts).
Notes:
Reusable shared Library and Template module loaded via Include in RealTest 2.0.
Library:
lib_liquid: C > 10 and Avg(C * V, 20) > 25000000
lib_uptrend: C > Avg(C, 200)
lib_spy_up: Extern($SPY, C > Avg(C, 200))
Template: lib_comm_base
Side: Long
Allocation: S.Equity
QtyType: Percent
Quantity: 10
MaxPositions: 10
MaxExposure: 100
Commission: Max(1, Shares * 0.005)
OpenSlip: 0.0005 * FillPrice
ExitTime: NextOpen
Ambiguity: Stop
Next, pull that module into any strategy or portfolio script via Include: and measure exact after-tax compounding across IRA, 30 percent short-term capital gains, and 0.10 percent Financial Transaction Tax scenarios.
Notes:
Modular RealTest 2.0 script architecture using Include and Library sections,
plus realistic Transaction Tax and Annual Short-Term Capital Gains Tax Drag
simulation via Commission and FeesInOut on S&P 500 stocks (2015 through 2024).
Include: C:\RealTest\Scripts\Research\strategies\wave7_common_lib.rts
Settings:
DataFile: C:\RealTest\Data\SPY_Tickers.rtd
StartDate: 2015-01-01
EndDate: 2024-12-31
AccountSize: 100000
BarSize: Daily
Data:
rsi2: RSI(2)
atr14: ATR(14)
mrSig: InSPX and lib_liquid and lib_spy_up and lib_uptrend and rsi2 < 10
// 1. Tax-deferred IRA / zero-tax baseline inheriting shared Template via Include
Strategy: mr_tax_deferred_ira
Using: lib_comm_base
EntrySetup: mrSig
SetupScore: 100 - rsi2
ExitStop: IF(BarsHeld >= 1, FillPrice - 3 * atr14[BarsHeld], 0)
ExitRule: C > H[1] or BarsHeld >= 7
// 2. European Financial Transaction Tax (0.10% stamp duty added per trade via Commission)
Strategy: mr_with_010_ftt_tax
Using: lib_comm_base
EntrySetup: mrSig
SetupScore: 100 - rsi2
Commission: Max(1, Shares * 0.005) + 0.0010 * Shares * FillPrice
ExitStop: IF(BarsHeld >= 1, FillPrice - 3 * atr14[BarsHeld], 0)
ExitRule: C > H[1] or BarsHeld >= 7
// 3. Simulated 30% annual short-term capital gains tax drag on positive realized yearly P&L
Strategy: mr_with_30pct_stcg_tax
Using: lib_comm_base
EntrySetup: mrSig
SetupScore: 100 - rsi2
ExitStop: IF(BarsHeld >= 1, FillPrice - 3 * atr14[BarsHeld], 0)
ExitRule: C > H[1] or BarsHeld >= 7
// Deducts 30% of positive trailing 252-bar net gains once each new calendar year
CashInOut: IF(Year <> Year[1] and S.Equity > S.Equity[252], -0.30 * (S.Equity - S.Equity[252]), 0)
Charts:
MA200: Avg(C, 200)
Validated 2015 to 2024 After-Tax Compounding Results (InSPX)
Look at how radically real-world tax structure changes ending account equity on the EXACT same 5,062 S&P 500 trades.
| Tax & Account Regime (2015 to 2024, InSPX) | Completed Trades | Ending Equity ($100k start) | Max Drawdown | After-Tax Reality |
|---|---|---|---|---|
mr_tax_deferred_ira (IRA / Tax-Deferred Baseline) |
5,062 | $230,467 | -16.36% | Full pre-tax compounding (ideal home for high-turnover MR!) |
mr_with_30pct_stcg_tax (30% Annual STCG Withdrawal) |
5,063 | $180,321 | -16.26% | -$50,146 compounding drag; still solidly profitable |
mr_with_010_ftt_tax (+0.10% Per-Trade Stamp Duty) |
5,057 | $81,193 | -37.37% | -$149,274 wiped out! High turnover cannot survive per-trade FTT |

That single chart delivers an immediate strategic rule for every systematic investor. Place high-turnover short-term mean-reversion systems inside tax-deferred retirement accounts (IRA / 401k / tax-sheltered corporate accounts) or US zero-FTT markets, and place lower-turnover trend-following and drift-band ETF rotation systems inside taxable accounts where fewer annual trades defer capital gains cleanly!
Architect Modular Multi-Strategy RealTest Portfolios Cleanly
Building a modular multi-strategy portfolio with shared risk caps, clean broker settings, and tax-aware sleeve placement? Test sleeve combinations interactively in our RealTest Strategy Combiner & Portfolio Builder, explore turnkey portfolios inside our RealTest Portfolios collection, commission custom modular Include frameworks via our RealTest Strategy Coding Service, or master automated modular research inside our Build RealTest Strategies with Claude Code AI Course.
Key terms
- Include Directive
- A RealTest script directive importing external .rts modules recursively while deduplicating repeated files automatically.
- Library Section
- A RealTest section storing reusable named formulas evaluated on demand in the referencing context without pre-allocating full Data arrays in RAM.
- Short-Term Capital Gains Tax Drag
- Compounding reduction modeled in RealTest via annual negative CashInOut withdrawals on positive trailing-year equity gains.
- Financial Transaction Tax (FTT)
- A per-trade percentage stamp duty (modeled inside Commission) that severely erodes high-turnover mean-reversion strategies.
- After-Tax Equity vs TWEQ
- Tracking real remaining after-tax dollars via S.Equity while measuring underlying strategy skill via withdrawal-adjusted S.TWEQ.
Frequently asked questions
How does the Include directive work in RealTest 2.0?
Place Include followed by a full or relative path to another .rts file at the top of your script. RealTest loads all Library, Data, and Template definitions from the included file recursively and ensures each file is included only once even in complex multi-file projects.
What is the difference between Library and Data sections in RealTest?
Data items are pre-computed and stored in memory for every symbol on every bar, whereas Library items are named formulas evaluated lazily on demand using the caller's context. Putting shared helper conditions in Library saves RAM across huge universes.
How do you simulate annual short-term capital gains taxes in RealTest?
Use CashInOut inside your Strategy or Combined section to withdraw your tax rate whenever trailing annual equity rises, such as IF(Year <> Year[1] and S.Equity > S.Equity[252], -0.30 * (S.Equity - S.Equity[252]), 0). RealTest reduces compounding S.Equity realistically while preserving pre-tax S.TWEQ.
How do you simulate a European Financial Transaction Tax or UK stamp duty in RealTest?
Add the percentage tax directly into Commission using Commission = Max(1, Shares * 0.005) + 0.0010 * Shares * FillPrice (for 0.10 percent per side). On a 5,000-trade mean-reversion system, just 0.10 percent per trade wipes out nearly $150,000 over 10 years.
Which RealTest strategies belong in tax-deferred accounts versus taxable brokerage accounts?
High-turnover short-term mean-reversion systems (hundreds of short-term round-trips per year) benefit hugely from tax-deferred accounts like IRAs, whereas low-turnover weekly trend following and +/- 5 percent drift-band ETF portfolios minimize taxable turnover in regular brokerage accounts.
Can an included RealTest file contain both Library formulas and shared Strategy Templates?
Yes. Storing standard liquidity filters in Library and common execution settings (Allocation, Commission, OpenSlip, Ambiguity) in a shared Template lets every portfolio script stay concise and consistent via a single Include line.