Separate the breakout signal from execution cost

A valid breakout does not necessarily justify submitting an order when the bid-ask spread is unusually wide. This cTrader cBot example checks a completed-bar breakout above the previous 20 highs and applies a separate quote-quality gate immediately before a market buy. The filter is a cost constraint, not a forecast of future returns.

Example: the same signal, two quotes

For EUR/USD with a pip size of 0.0001, bid 1.10000 and ask 1.10008 imply a spread of 0.8 pips. An ask of 1.10025 instead implies 2.5 pips. With a maximum of 1.5 pips, the first quote passes and the second fails. A rejected signal is discarded rather than queued for a later tick.

Spread-Gated Breakout Entries in a cBot

Hypothetical quotes; both cases assume the same qualifying breakout.

cBot implementation

// Inside a cTrader Robot class
protected override void OnBarClosed()
{
    if (Bars.Count < 21) return;
    double level = Bars.HighPrices.Last(1);
    for (int i = 2; i <= 20; i++)
        level = Math.Max(level, Bars.HighPrices.Last(i));
    if (Bars.ClosePrices.Last(0) <= level) return;
    if (Positions.Find("SpreadGate", SymbolName) != null)
        return;
    double spreadPips = (Symbol.Ask - Symbol.Bid)
        / Symbol.PipSize;
    if (spreadPips > 1.5) return;
    double volume = Symbol.VolumeInUnitsMin;
    var result = ExecuteMarketOrder(TradeType.Buy,
        SymbolName, volume, "SpreadGate", 15, 30);
    if (!result.IsSuccessful)
        Print("Entry failed: {0}", result.Error);
}

Insert the method into a Robot template with System and cAlgo.API namespaces. In OnBarClosed, the Bars collection excludes the live bar, so Last(0) refers to the completed signal bar. The reference highs exclude that bar. The current Symbol bid and ask are execution-time quotes, not historical bar prices. See the official bar events guide and Robot API reference.

Order management and practical limits

The example uses the symbol minimum volume in units and a 15-pip stop and 30-pip target. These are illustrative, not recommended settings. Validate broker stop-distance restrictions, available margin and volume limits. Use a unique label for each strategy instance if independent position tracking is required. The position check prevents a new entry while a matching position exists; it does not enforce account-wide exposure limits. A new qualifying bar after an exit can trigger another order.

A spread check cannot guarantee the eventual execution spread or fill price. OnBarClosed runs when a new tick opens the next bar, which may be delayed over market closures. Stops can slip during gaps. In production, add a session filter, maximum exposure controls and persistent error logging.

Testing

Compare the same breakout and exit rules with no spread gate and with 1.0, 1.5 and 2.0-pip limits. Use historical bid/ask or realistic variable spread assumptions, commission and slippage. Record blocked signals, fill costs, trade count and drawdown; check whether the filter merely removes trades during particular sessions. Validate on a demo account before considering live deployment. This example has not been compiled or backtested here. Leveraged trading can cause substantial losses.

For custom execution filters and risk controls, see Custom cTrader 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.