Conversion example · Strategy

Order fill policy: drop, fill at close, or shrink to fit

The same RSI mean-reversion strategy as the commission-fees example and the trailing-stop-distance example — same entries, same 2×ATR trailing exit and 2×ATR hard stop, same 4,089 bars of CSI 300 daily data, same $10,000, same 0.1% round-trip fee — with one question: when an order doesn't fit the account, what should the backtest do? At the 2015-08-27 signal the engine sized 3 contracts on the close ($9,616.92, inside the account), but at the next bar's open the same 3 contracts cost $9,738.72 against $9,687.31 of equity — $51.41 short — and the order was dropped silently. This page runs the three answers to that question, each with the fee on and off: drop it, fill it at the close, or shrink it to what fits.

The Python below is the actual output of the Pine2Py converter, run on this exact Pine source with the local compiler (node bin/pine2pyne.mjs), with zero diagnostics. The other two policies differ from it in exactly one line each, shown verbatim below.

The Pine original (policy A: drop)

rsi_drop_fee.pinePine Script v6
//@version=6
// #22 policy A — DROP (baseline, identical to #20): a market entry sized at the
// signal close is rejected at the fill bar's open when the same quantity costs
// more than the account holds. Nothing here changes the fill rule.
strategy("RSI Mean Reversion (drop, fee 0.1% round-trip)", overlay=true, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.05,
     default_qty_type=strategy.percent_of_equity, default_qty_value=100)

rsiLen     = input.int(14, "RSI length", minval=1)
overbought = input.int(70, "Overbought level", minval=51, maxval=100)
oversold   = input.int(30, "Oversold level", minval=0, maxval=49)

rsi = ta.rsi(close, rsiLen)
atrVal = ta.atr(14)

var float trailLevel = na
inTrade = strategy.position_size != 0
if inTrade
    trailLevel := na(trailLevel) ? close - atrVal * 2 : math.max(trailLevel, close - atrVal * 2)
else
    trailLevel := na

longSignal = ta.crossover(rsi, oversold)

if longSignal and not inTrade
    strategy.entry("Long", strategy.long)

if inTrade
    strategy.exit("Exit Long", "Long", stop=strategy.position_avg_price - atrVal * 2)
    if close < trailLevel
        strategy.close("Long", comment="Trail exit")

plot(rsi, "RSI", color=color.new(color.purple, 0))
hline(overbought, "Overbought", color=color.new(color.red, 0))
hline(oversold, "Oversold", color=color.new(color.green, 0))

The Python output (policy A: drop)

rsi_drop_fee.pyPython · PyneCore
"""
@pyne
"""

from pynecore import Persistent, Series
from pynecore.lib import close, color, hline, input, math, na, plot, script
from pynecore.lib import strategy, ta

_pyne_in_rsiLen: int = input.int(14, "RSI length", minval=1)
_pyne_in_overbought: int = input.int(70, "Overbought level", minval=51, maxval=100)
_pyne_in_oversold: int = input.int(30, "Oversold level", minval=0, maxval=49)

@script.strategy(title="RSI Mean Reversion (drop, fee 0.1% round-trip)", overlay=True, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.05, default_qty_type=strategy.percent_of_equity, default_qty_value=100)
def main():
    rsi: Series[float] = ta.rsi(close, _pyne_in_rsiLen)
    atrVal: Series[float] = ta.atr(14)
    trailLevel: Persistent[float] = na
    inTrade: Series[bool] = (strategy.position_size != 0)
    if inTrade:
        trailLevel = ((close - (atrVal * 2)) if na(trailLevel) else math.max(trailLevel, (close - (atrVal * 2))))
    else:
        trailLevel = na
    longSignal: Series[float] = ta.crossover(rsi, _pyne_in_oversold)
    if (longSignal and (not inTrade)):
        strategy.entry("Long", strategy.long)
    if inTrade:
        strategy.exit("Exit Long", "Long", stop=(strategy.position_avg_price - (atrVal * 2)))
        if (close < trailLevel):
            strategy.close("Long", comment="Trail exit")
    plot(rsi, "RSI", color=color.new(color.purple, 0))
    hline(_pyne_in_overbought, "Overbought", color=color.new(color.red, 0))
    hline(_pyne_in_oversold, "Oversold", color=color.new(color.green, 0))

Policy B: fill at close — one line in strategy()

rsi_close_fee.pine (changed lines only)Pine Script v6
strategy("RSI Mean Reversion (fill at close, fee 0.1% round-trip)", overlay=true, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.05,
     default_qty_type=strategy.percent_of_equity, default_qty_value=100, process_orders_on_close=true)

The compiled Python differs in exactly the same place:

rsi_close_fee.py (changed line only)Python · PyneCore
@script.strategy(title="RSI Mean Reversion (fill at close, fee 0.1% round-trip)", overlay=True, initial_capital=10000, commission_type=strategy.commission.percent, commission_value=0.05, default_qty_type=strategy.percent_of_equity, default_qty_value=100, process_orders_on_close=True)

Every market order now fills at the bar's close — the price the signal was computed on. The unaffordable order is sized on the signal close and filled at that same close, so it always fits. This is the convention the naive vectorized backtest assumes without saying so.

Policy C: size on the fill price — one line in the entry

rsi_stop_fee.pine (changed line only)Pine Script v6
strategy.entry("Long", strategy.long, stop=close)

The compiled Python differs in exactly the same place:

rsi_stop_fee.py (changed line only)Python · PyneCore
        strategy.entry("Long", strategy.long, stop=close)

The market entry becomes a buy stop at the signal close. A price-based order resolves its quantity at the actual fill price, so when the fill bar gaps up, the order shrinks to what fits instead of being dropped. The converter emits the stop=close argument verbatim — the sizing-at-fill behaviour lives in the runtime, not in the script, and the script says exactly which one it asked for.

What translates 1:1

All three policies are one-line changes in Pine and one-line diffs in the Python: process_orders_on_close=true rides the strategy() call, stop=close rides the strategy.entry() call, and the no-fee baseline is commission_value=0 in the same call. Nothing else in the script moves — the var float trail, the na reset, the strategy.exit stop and the strategy.close trail exit are byte-identical across all six runs. A converter that silently dropped process_orders_on_close or the stop argument would have run a different backtest while showing you the same script.

What else changes meaning

The gate is judged at the fill price, not the sizing price. The order is sized on the signal close (3 × $3,205.64 = $9,616.92, inside the account) and rejected at the fill bar's open (3 × $3,246.24 = $9,738.72, $51.41 over it). The drop is not a property of the policy alone: in the no-fee run the equity path runs $156.72 higher by the same signal ($9,844.03), the 3 contracts fit, and the trade exists — worth +$925.95. Same policy, same sizing rule, different equity path, different trade list.

The fee costs 4.4× what it charges — under the drop policy. Fee-on net minus fee-off net: −$1,592.79 under drop (fees paid $359.12), −$366.56 under fill-at-close (paid $372.55), −$310.18 under size-on-fill (paid $385.72). Under drop, $925.95 of the $1,592.79 is the rescued trade existing in the no-fee run and not in the fee run, and $666.84 is the same 47 signals performing differently on different equity paths. Under the other two policies the fee costs about what it charges. A split test without a no-fee baseline measures the fee's shadow, not the policy.

The ranking flips without the fee. With the fee on, fill-at-close beats drop by +$361.92; without it, it loses by −$864.31. The whole flip is one trade (signal 488, +$894.54 in the drop run, skipped by the close run because it was still in its earlier trade). Size-on-fill beats drop in both worlds, but the margin collapses from +$2,001.53 to +$718.92. If you report a fill-policy comparison from fee-on runs only, you are reporting the fee's opinion, not the policy's.

The rescued trade is not the story. It is worth +$915.75 to +$1,043.91 across the three saves, while the headline deltas are +$361.92 and +$2,001.53. Fill-at-close's rescue (+$1,043.91) is bigger than its whole delta, because the lost signal-488 trade cancels 86% of it. Size-on-fill's gain is dominated by seven trades it never took (net −$1,429.45 in the drop run) — its resting orders delay entries, the delays overlap later signals, and the skipped signals happen to be the bad ones. On this sample.

The shrink never fired once. Across all 44 fills under size-on-fill, the quantity was never smaller than the market policy's on the same signal. The mechanism exists in the code path and never engaged on this data, because whenever the fill price was above the signal close, the equity path had room. A policy that never fires is not a tested policy.

Slippage has a sign, and the sign depends on the policy. Fill price minus signal-bar close, per closed entry: drop −$973.91 (32 of 47 fills below the signal close — the gap pays this mean-reversion script), fill-at-close $0.00 exactly (the fill is the signal close), size-on-fill +$1,122.30 (27 fills above the close, 17 exactly at it, none below — the stop only triggers when price comes back up). Anyone who assumes slippage is always a cost has not looked at their own fills.

The runs: three policies × fee on / off

We ran all six variants in the PyneCore patched runtime on 4,089 bars of CSI 300 daily data (2010-01-04 to 2026-11-06), $10,000, 100% of equity per trade, 0.1% round-trip fee or zero commission, no slippage — in our testing:

A drop, feeB close, feeC stop, feeA drop, no feeB close, no feeC stop, no fee
Closed trades474744484744
Net profit+$937.45+$1,299.37+$2,938.98+$2,530.24+$1,665.93+$3,249.16
Profit Factor1.1171.1571.3851.3021.2081.435
Win rate18 / 47 (38.3%)15 / 47 (31.9%)16 / 44 (36.4%)19 / 48 (39.6%)18 / 47 (38.3%)16 / 44 (36.4%)
Gross profit / gross loss+$8,951.70 / −$8,014.25+$9,552.61 / −$8,253.24+$10,572.36 / −$7,633.38+$10,908.26 / −$8,378.02+$9,688.12 / −$8,022.19+$10,718.87 / −$7,469.71
Avg win / avg loss$497.32 / −$276.35$636.84 / −$257.91$660.77 / −$272.62$574.12 / −$288.90$538.23 / −$276.63$669.93 / −$266.78
Avg hold16.15 bars17.83 bars18.09 bars17.04 bars17.83 bars18.09 bars
Median trade−$172.61−$152.24−$150.47−$161.62−$142.62−$152.62
Fees paid$359.12$372.55$385.72$0.00$0.00$0.00
Max equity drawdown$2,231.62 (21.01%)$2,048.16$1,623.61$1,881.50$1,948.16$1,581.10
Top-5 winners' share of gross profit67.6%63.8%65.1%63.7%63.3%64.7%
Net minus the biggest trade−$936.86−$574.69+$599.03+$168.70−$217.63+$898.45

No policy wins on this evidence: each is a different backtest, and the ranking between them depends on the fee. What the six runs establish is that the fill policy is a first-order choice ($2,001.53 of headline between the best and the worst), that the rescued trade is a minority of every delta, and that the fee's real cost under the drop policy is 4.4× the fees paid. The discipline is the same as the trailing-distance test: a choice you never tested is a choice you made by feel. The fill policy belongs in the methods section of every backtest — which policy, and what it cost — next to the fee and the data. One symbol, one period, one fee rate, no slippage — run it on your own symbol before believing any of it.

Try it on your own script

Paste this Pine script — or your own — into the converter on the home page and compare the output. Every account gets 3 free conversions, no card required; Pro is $12/month via Paddle for unlimited conversions.

The output runs on the open-source PyneCore runtime. We ship a patched runtime build for the two constructs stock PyneCore doesn't handle yet — details are on the home page. Pine2Py translates code; it does not provide investment advice, and no backtest result on this page is a promise about any strategy's future performance.