Separate signal timing from execution timing

A moving-average crossover is easy to express in Pine Script, but its reported performance depends on when orders become eligible to fill. A confirmed close is information available at the end of a bar, not a guarantee of execution at that price. This long-only example uses a 10-period EMA crossing above a 30-period EMA to enter and the reverse crossover to exit.

A concrete gap example

Assume the crossover is confirmed at a close of 104. The next bar opens at 107. With default next-tick market-order processing, the historical broker emulator normally fills at that next open, before configured slippage. The difference is three price points. If a later exit fills at 102, the gross result is minus five points rather than minus two points calculated from the signal close. These are hypothetical prices, not measured performance.

Confirmed EMA Crossovers and Next-Bar Execution

Illustration: six hypothetical candles showing the gap between a confirmed signal and the next eligible market fill.

Pine Script v6 implementation

//@version=6
strategy("Confirmed EMA crossover", overlay=true,
    pyramiding=0, process_orders_on_close=false,
    calc_on_every_tick=false, default_qty_type=strategy.fixed,
    default_qty_value=1)
fast = ta.ema(close, 10)
slow = ta.ema(close, 30)
up = ta.crossover(fast, slow)
down = ta.crossunder(fast, slow)
if barstate.isconfirmed and up and strategy.position_size == 0
    strategy.entry("Long", strategy.long)
if barstate.isconfirmed and down and strategy.position_size > 0
    strategy.close("Long")
plot(fast, color=color.blue)
plot(slow, color=color.orange)

The strategy uses one unit, no pyramiding, confirmed bar signals and next-tick processing. It deliberately omits protective orders to isolate the timing experiment; it is not a production-ready risk-managed system. Add instrument-specific position sizing and protective exits before considering deployment. The crossover functions are evaluated on every bar, outside conditional branches.

Testing and execution limitations

Compare the same strategy with process_orders_on_close set to true and false, keeping data, position size and costs fixed. Record signal prices, actual simulated entry prices, gap distributions, net returns and drawdown. Configure realistic commissions and slippage in strategy properties. Use standard candles rather than synthetic chart prices, and reserve an out-of-sample period. Closing-tick simulation can be optimistic, particularly near session boundaries. TradingView broker-emulator fills differ from live execution; alerts, network latency, broker acceptance and liquidity introduce further differences. A live integration needs duplicate-order protection and position reconciliation. The code has not been compiled or backtested here. Trading involves risk of loss, and gaps can exceed intended risk.

For custom execution timing, risk controls or alert integration, see Custom TradingView strategy Development Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.