FAQ · Getting systematic research right

How do I backtest an FX carry strategy in Python?

Hold the currency with the higher interest rate against the one with the lower rate, and charge or credit the swap every night the position is open, with the weekend's three nights on the day your broker books them. Carry earns slowly and loses quickly when risk appetite turns, so a backtest needs the nightly swap, a realistic spread and a period that includes a carry unwind such as 2008.

Hold the currency with the higher interest rate against the one with the lower rate, and charge or credit the swap every night the position is open, with the weekend's three nights on the day your broker books them. Carry earns slowly and loses quickly when risk appetite turns, so a backtest needs the nightly swap, a realistic spread and a period that includes a carry unwind such as 2008.

How carry earns

A long AUD/JPY position borrows yen and holds Australian dollars. When Australian rates are higher, the account is credited the difference each night, less the broker's markup. That nightly swap is the strategy's income; the exchange rate is its risk.

  • Income. Roughly the rate differential, accrued daily. A 4% gap on a position worth $100,000 is about $11 a night.
  • Risk. High-yield currencies tend to fall sharply in sell-offs, as AUD/JPY did by over 40% in 2008. Years of swap can go in weeks.

What the backtest must get right

  • Swap per night, by direction. Long and short get different rates, and with a markup both sides can pay. Use your broker's swap table, not the central bank rates.
  • The triple night. FX settles two days forward, so the weekend's interest is booked on one weekday, usually Wednesday, at three times the normal rate.
  • Rates change. Swap follows central bank rates. A carry backtest over years needs the swap history, or at least the rate differential by month.
  • Spread. Exotic high-yield pairs carry wide spreads, which a carry trade pays on every rebalance.

How Reamer Research handles it

Reamer Research charges swap once each time the bars cross into a new date, from a rate per unit for each weekday, Sunday to Saturday, set per pair through ticker_config. A long position pays the rate and a short receives it, so a negative rate credits a long carry position. The night is keyed by the day the position is held over, so a Wednesday rate three times the others books the weekend there. This class holds long carry while a trend filter agrees:

import reamer_research as rr

class CarryWithTrend:
    lookback = 100
    units = 100_000                                     # one standard lot
    def on_bar(self, d):
        if d.close[0] == 0:
            return None
        uptrend = d.close[-1] > d.close.mean()
        if d.position() == 0 and uptrend:
            return rr.buy_market(self.units)
        if d.position() > 0 and not uptrend:
            return rr.close_position()

credit = -0.011                     # yen per unit per night: about ¥1,100 a lot, long receives
AUDJPY = dict(swap_per_unit=[0, credit, credit, 3 * credit, credit, credit, 0],
              spread=0.02)
result = rr.backtest(CarryWithTrend, {'AUDJPY': 'data/AUDJPY_daily.bin'},
                     ticker_config={'AUDJPY': AUDJPY},
                     initial_capital=2_000_000, leverage=20)     # a yen account
print(f'net {result.net_pnl:.0f}, of which swap {-result.total_swap_cost:.0f}')

The list runs Sunday to Saturday. With bars only on weekdays, the Friday-to-Monday crossing is booked under Friday at the normal rate, and Wednesday's triple rate covers the weekend, as brokers book it. Prices, capital and swap here are in yen, so the account is a yen account; for another account currency, convert them, or take swap_per_unit from the broker's table in account terms. A 4% rate gap on one lot worth about ¥10 million is about ¥1,100 a night, the credit above. total_swap_cost is the run's total, negative when swap was earned. The rule works as an on_bar(data) function or as an object configured with the pair's rates.

For rates that change over time, run the history in pieces with start and end, one per rate regime, or carry the differential as exogenous data and size the position from it.

Limits to know

  • One swap table per pair per run. Rate changes over a long history go in as separate runs or as exogenous data.
  • A long pays exactly what a short receives. A broker's markup on both sides is set by giving each direction its own run, or by using the rate for the side you trade.
  • Evaluation Mode runs 30 days or 50,000 bars of one ticker. Enough to check a month of swap bookings; a basket of pairs over years needs a licence.

A $20 month includes the full kit, so you can test carry on your own FX data and swap tables and cancel if it does not fit.

Next questions

Can I backtest futures with roll handling, or FX with swap costs?Yes, but only if the backtest handles two things a plain price series hides. In a continuous futures series, the price jump at each contract roll must not be counted as profit or loss. An FX position held overnight must be charged or credited swap every night it is held. A backtest that skips either can report a result the account would never have produced.How do I backtest a momentum or trend following strategy in Python?Measure trend from past closes only, such as the return over the last 12 months or price above a long average, hold long while it is positive and exit when it turns, and size each position by its recent volatility. Test across many years and several markets, because trend following earns most of its return in a few large moves.How realistic do slippage and spread need to be in a backtest?Realistic enough that the result survives them. Set spread, slippage and commission per instrument from what your broker actually charges, let spread and slippage widen when the market moves fast, and then rerun at one and a half and two times those costs. A strategy whose edge disappears under slightly higher costs was never a reliable edge.How do I size positions in a backtest?Choose a sizing rule and compute the quantity at each entry. Fixed fractional risks a set share of equity per trade, volatility targeting scales size so each position carries similar risk, and ATR sizing sets quantity so a stop placed a multiple of ATR away loses a fixed amount. Then check the size against your buying power.

Every answer here restates the documents that ship in the kit, which are authoritative. All questions.

Reamer Research

Your first backtest in a minute

Backtesting and live trading in Python and C++. Free to install in Evaluation Mode, with no key or sign-up.

pip install reamer-research

A licence at $20 a month removes every limit. The same code runs unchanged.

Reamer Server

The order-management core for your firm

Your venues, your risk rules on every order, one strategy per CPU core, on your own Linux servers.

$24,000 / year

Or start with a 30-day trial at $2,000.