Advertisement IC Markets

Backtest costs on position changes: a long to short flip is two trades, a 6 unit example

Share, develop, and backtest custom MQL4/MQL5 Expert Advisors, Python data-scraping scripts, trading bots, and automated market alert systems.
Post Reply
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Backtest costs on position changes: a long to short flip is two trades, a 6 unit example

Post by Fairman »

Backtest costs on position changes: a long to short flip is two trades, a 6 unit example

A vectorised backtest usually holds a position series: 1 for long, minus 1 for short, 0 for flat. The cost of trading comes from the changes in that series, and the way you count them matters.

Take eight bars of positions: 0, 1, 1, 1, -1, -1, 1, 0.

Code: Select all

pos = pd.Series([0, 1, 1, 1, -1, -1, 1, 0])
units = pos.diff().abs().fillna(0)      # 0,1,0,0,2,0,2,1
cost = units * 0.5                      # 0.5 pip per unit traded
print(units.sum(), cost.sum())          # 6.0  3.0
The flip from 1 to minus 1 is a change of 2, so it's two trades: one to close the long and one to open the short. The second flip, from minus 1 to 1, is also 2. The entry from 0 to 1 and the exit from 1 to 0 are one unit each. Total 6 units, and at 0.5 pip per unit the costs are 3.0 pips.

A common version of the bug is cost = (pos != pos.shift()).sum() * c. That counts changes, not units, so each flip counts as one. It reports 4 changes after dropping the first row, which is a 33% undercount of the cost, and it makes strategies that flip often look better than they are.

The size matters most for systems that are always in the market. A reversal system flipping 15 times a day pays 30 units a day, which at 0.5 pip is 15 pips, before a single stop has been hit.

Cost per unit should come from the spread you really pay, and it's best to use an hourly figure instead of one number.
It’s Fairman :geek:
Recommended broker for automated trading & scalping IC Markets
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Backtest costs on position changes: a long to short flip is two trades, a 6 unit example

Post by Fairman »

The unit cost has to include more than the spread. For a 0.5 pip assumption, I split it into half the spread, 0.3 pips for a 0.6 pip spread, plus an allowance for slippage of 0.1, plus the commission converted into pips. A $7 round turn commission per lot is 0.7 pips for the round trip, so 0.35 per unit on a EURUSD lot where a pip is $10. The sum is then 0.3 + 0.1 + 0.35 = 0.75 pips per unit, not 0.5. Writing the components down also tells you which one to attack first.
It’s Fairman :geek:
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Backtest costs on position changes: a long to short flip is two trades, a 6 unit example

Post by Fairman »

With position sizes, the number of units changes. If the position series holds lot sizes, say 0, 0.5, 0.5, 0.5, minus 0.3, then the diff gives 0.5, 0, 0 and 0.8, and the cost has to be multiplied by lots. The cost in pips per unit of 1 lot translates to dollars as units x cost x pip value. So the same code works with lots as long as you multiply the pip cost by the pip value afterwards, and you don't count the number of changes. Check with an easy case: a 2 lot trade in and out is 4 lot-units, and at 0.5 pip each that is 2.0 lot-pips, or $20.
It’s Fairman :geek:
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Backtest costs on position changes: a long to short flip is two trades, a 6 unit example

Post by Fairman »

To see how much costs matter in your own system, run the backtest at zero cost, at 0.5, and at 1.0 pip per unit, and plot net pips against cost. If the line drops through zero at 0.7 pips, you know the exact break-even cost for your signals, and you can compare it with what you pay. A system that dies at 0.7 when you pay 0.75 isn't a system, and finding that on a chart is far cheaper than finding it with real money. It's one of the cheapest plots to make for strategy testing.
It’s Fairman :geek:
Fairman
Posts: 6757
Joined: Tue Jul 21, 2026 7:11 am
Location: Abuja

Re: Backtest costs on position changes: a long to short flip is two trades, a 6 unit example

Post by Fairman »

There's one more cost that vectorised code tends to miss: holding costs. A position held over the daily rollover pays or earns a swap, and that's charged per night per lot. If the backtest holds positions across the rollover time, add the swap for each crossing, multiplied by the number of nights, with the triple charge on Wednesday for most pairs. For intraday scalping that stays flat at night the term is zero, and you should confirm that in the data, not assume it, because a position that quietly carries over once in a while adds a cost the backtest never counted.
It’s Fairman :geek:
Post Reply