An automated strategy can behave perfectly throughout a backtest and still open an unintended second position when a connection drops at the wrong moment. The dangerous assumption is that an interrupted response means the original order never reached the broker. In cTrader Algo, the safer design is to separate a trading signal, an order request and confirmed account exposure. Treat reconnection as a reconciliation problem, not a reason to replay the last entry.
1. What the cBot must recover
A cBot's in-memory variables can disappear when the instance stops or restarts. An open position or a working pending order may remain on the trading account. A local Boolean such as hasEntered is therefore not a reliable account record. In OnStart(), initialize the strategy in a blocked state, inspect current positions and pending orders, and restore any durable record of unresolved entry intentions. Do not interpret an empty collection during an uncertain connection as proof that the account is flat.
Use a stable label that identifies a particular strategy instance across restarts, for example PS-A. Combine that label with the exact trading symbol. A shared label across independent bots can create ownership conflicts; a newly randomized label on every restart can hide an existing position from the restarted bot. The following approach assumes one strategy owns the relevant symbol exposure and uses a single-entry policy. A netting account can combine trades into one position, so mixed ownership requires explicit account-level reconciliation rather than reliance on position labels alone.
2. A hypothetical interrupted EURUSD entry
At 09:30, a long signal triggers a request for 10,000 EURUSD units. The connection is interrupted before the strategy can establish the outcome. At 09:35, a restored account snapshot shows a matching long position of 10,000 units. A second identical buy on a hedging account could increase gross long exposure to 20,000 units. The correct action for this example is to block another entry and resume management of the existing position after checking its protective orders.

| Recovered state | Entry policy |
|---|---|
| Matching position: 10,000 units | Block; reconcile protective stops and target. |
| Matching pending order; no position | Block; monitor the working order. |
| No position or pending order; unresolved request | Block; check available trade history and order outcome. |
| Confirmed flat; no unresolved request | Evaluate a fresh eligible signal. |
3. Implement the exposure guard in C#
The cTrader Positions.FindAll overload accepts a label and a symbol name. The pending-order collection can be filtered using the same identifiers. This helper deliberately checks both directions, because the example does not permit simultaneous long and short positions for the same strategy.
Illustrative excerpt, not a complete executable strategy:
private const string Label = "PS-A";
private bool HasExposure()
{
if (Positions.FindAll(Label, SymbolName).Length > 0)
return true;
foreach (var order in PendingOrders)
if (order.Label == Label &&
order.SymbolName == SymbolName)
return true;
return false;
}
Place this helper inside the cBot class. Call it during startup reconciliation and immediately before any new order request from the strategy's signal handler, such as OnBarClosed(). It is only one guard: a false result must not authorize a trade until account state is trustworthy and outstanding requests have been resolved. This excerpt has not been compiled or live-tested.
4. Track the request separately from the position
Before calling ExecuteMarketOrder(), persist an entry-intent identifier containing the strategy identity, symbol, signal bar time and intended volume. Prevent another submission while that intent remains unresolved. Inspect the returned TradeResult.IsSuccessful, and log any error. A successful response should be reconciled with the resulting position; a failure or missing response should not automatically trigger another market order when the execution outcome is uncertain. If the original position was opened and then closed during the interruption, an empty current-position collection alone cannot distinguish this from a request that never executed.
Use Positions.Opened, Positions.Closed and relevant pending-order events for updates, subscribing during OnStart(). Events complement, rather than replace, account snapshots and durable records. For asynchronous operations, keep an in-flight guard until the callback and account reconciliation establish the outcome. For a cBot that stays running during reconnection, apply the same reconciliation gate before resuming entries; OnStart() is not a universal reconnection callback. If state cannot be established, fail closed and alert the operator.
5. Verify recovery before live deployment
- Restart a demo-account instance with an existing matching position and confirm no second entry is submitted.
- Repeat with a matching pending order and with a different strategy's label.
- Inject an unresolved entry intent, then test both an eventual confirmed fill and a confirmed rejection. Do not clear ambiguity merely because a timer expires.
- Test an original position that opens and closes while the strategy is unavailable. Confirm the same signal is not replayed.
- Check stop-loss and take-profit state, netting versus hedging behavior, and concurrent instances. Use independent symbols or a single coordinated owner where appropriate.
Ordinary historical backtests may not reproduce network failures, delayed acknowledgments or broker-side timing. Use deterministic fault injection for logic tests and controlled demo-account restart tests for operational validation. Slippage, spread changes, rejected protective modifications and outages can still produce losses. This is an execution-safety technique, not a guarantee of profitability or exactly-once broker execution.
Official references: Positions API, TradeResult, and cBot trading operations.
For help integrating durable strategy state and account reconciliation into an existing cBot, explore our Custom Trading Software Development Services.