Advertisement IC Markets

Broker apps: usable for emergency flat only?

Discuss 1-minute to 15-minute price action setups, fading intraday momentum, key support/resistance zones, and proven short-term trading methodologies.
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: Broker apps: usable for emergency flat only?

Post by PTScalper »

1.Open cTrader Automate:

Switch to the Automate tab in the left-hand navigation menu of cTrader.

2.Create a new cBot: Right-click on the cBots folder and select New cBot. Name it EmergencyFlatten.

3.Paste and Build: Replace the default template code with the C# code above. Press Ctrl + B (or click the Build icon) to compile it.

4.Execute the Flat: Add it to a chart to keep it ready.

To use it, add the cBot to any chart. When an emergency happens, simply click the Play (Start) button on the cBot instance. It will run instantly, close everything, and then turn itself back off.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Recommended broker for automated trading & scalping IC Markets
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: Broker apps: usable for emergency flat only?

Post by PTScalper »

To elevate this to an enterprise-grade execution tool, we need to account for real-world market microstructure problems. During high-volatility events—exactly when you need an emergency flatten—broker APIs frequently reject requests, connections drop, or liquidity gaps cause partial fills.

A professional "kill switch" requires retry logic, execution telemetry, and defensive array handling to prevent collection-modified exceptions as trades close out.

Here is the refactored, robust C# implementation for cTrader:

Code: Select all

using System;
using System.Linq;
using System.Diagnostics;
using cAlgo.API;

namespace cAlgo.Robots
{
    [Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.None)]
    public class ProEmergencyFlatten : Robot
    {
        [Parameter("Scope: All Symbols", DefaultValue = true, Group = "Targeting")]
        public bool CloseAllSymbols { get; set; }

        [Parameter("Max Retries", DefaultValue = 3, MinValue = 1, Group = "Execution")]
        public int MaxRetries { get; set; }

        [Parameter("Log Level", DefaultValue = LogLevel.Detailed, Group = "Diagnostics")]
        public LogLevel LoggingLevel { get; set; }

        public enum LogLevel { Standard, Detailed }

        protected override void OnStart()
        {
            Print("[Emergency Flatten] Initiating protocol...");
            var stopwatch = Stopwatch.StartNew();

            // Execute closures using a generalized retry wrapper
            int positionsClosed = ExecuteWithRetry(CloseTargetPositions, "Positions");
            int ordersCancelled = ExecuteWithRetry(CancelTargetOrders, "Pending Orders");

            stopwatch.Stop();
            
            Print($"[Emergency Flatten] Execution completed in {stopwatch.ElapsedMilliseconds}ms.");
            Print($"[Emergency Flatten] Summary: {positionsClosed} Positions Closed | {ordersCancelled} Orders Cancelled.");
            
            // Terminate instantly after execution
            Stop(); 
        }

        /// <summary>
        /// Wraps execution logic in a retry loop to handle broker rejections during high volatility.
        /// </summary>
        private int ExecuteWithRetry(Func<int> executionFunc, string operationName)
        {
            int successCount = 0;
            
            for (int attempt = 1; attempt <= MaxRetries; attempt++)
            {
                successCount += executionFunc();

                // Re-evaluate remaining targets dynamically
                int remaining = operationName == "Positions" ? GetTargetPositions().Length : GetTargetOrders().Length;
                
                if (remaining == 0)
                {
                    if (attempt > 1 && LoggingLevel == LogLevel.Detailed)
                        Print($"[{operationName}] Successfully cleared all targets on attempt {attempt}.");
                    break;
                }

                Print($"[{operationName}] Attempt {attempt} finished with {remaining} remaining targets. Retrying...");
                
                // 200ms backoff to allow the broker's order queue to process state
                System.Threading.Thread.Sleep(200);
            }

            return successCount;
        }

        private int CloseTargetPositions()
        {
            var targets = GetTargetPositions();
            int closed = 0;

            foreach (var position in targets)
            {
                var result = ClosePosition(position);
                
                if (result.IsSuccessful)
                {
                    closed++;
                    if (LoggingLevel == LogLevel.Detailed)
                        Print($"[Success] Closed Position {position.Id} ({position.SymbolName})");
                }
                else
                {
                    Print($"[Error] Failed to close Position {position.Id}: {result.Error}");
                }
            }
            return closed;
        }

        private int CancelTargetOrders()
        {
            var targets = GetTargetOrders();
            int cancelled = 0;

            foreach (var order in targets)
            {
                var result = CancelPendingOrder(order);
                
                if (result.IsSuccessful)
                {
                    cancelled++;
                    if (LoggingLevel == LogLevel.Detailed)
                        Print($"[Success] Cancelled Order {order.Id} ({order.SymbolName})");
                }
                else
                {
                    Print($"[Error] Failed to cancel Order {order.Id}: {result.Error}");
                }
            }
            return cancelled;
        }

        // Materializing queries to arrays prevents InvalidOperationException 
        // if the collection changes while we are iterating over it.
        private Position[] GetTargetPositions() => 
            CloseAllSymbols ? Positions.ToArray() : Positions.Where(p => p.SymbolName == SymbolName).ToArray();

        private PendingOrder[] GetTargetOrders() => 
            CloseAllSymbols ? PendingOrders.ToArray() : PendingOrders.Where(o => o.SymbolName == SymbolName).ToArray();
    }
}
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
PTScalper
Site Admin
Posts: 7821
Joined: Mon Jul 20, 2026 1:28 pm

Re: Broker apps: usable for emergency flat only?

Post by PTScalper »

Key Engineering Upgrades

Idempotent Retry Wrapper: High-volatility moments often trigger "Server Busy" or "Off Quotes" errors. The ExecuteWithRetry method accepts a delegate (Func<int>), wrapping the closure logic in a loop with a hard limit and a 200ms thread block to respect API limits before hammering the broker again.

State Freezing: Standard LINQ queries on live collections (Positions and PendingOrders) will throw an exception if a position closes mid-iteration. GetTargetPositions() materializes the query to an array (.ToArray()) first, ensuring memory safety.

Execution Telemetry: Implements Stopwatch to log exact execution latency. This gives you tangible data on how fast your broker is actually processing API requests during chaotic market conditions.

Defensive Error Handling: Surfaces exact error codes from the cTrader API rather than silently failing, controlled by a customizable logging enum to keep the terminal clean unless you need granular debugging.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
LondonScalper
Posts: 965
Joined: Sat Sep 05, 2026 7:54 am

Re: Broker apps: usable for emergency flat only?

Post by LondonScalper »

That is a cleaner line than mine: analysis at the desk, execution anywhere. With M15 triggers and zones fixed during prep, the phone is a remote rather than a trading screen, and I can see why it works for you.

For my style it breaks down on the length of the decision. An M1 entry at a London level is a few seconds of reading the tape, and on a phone I lose what surrounds it — the live spread, the correlated pair, the level on M5. I tried it for a month once. Fills were fine, but my skip rate dropped. I was taking marginal ones because the small screen hid whatever would normally have put me off.

Where we clearly overlap is testing the flatten route. One addition from experience: make sure the app is logged into the account that actually holds the risk. During an outage a few years back I opened the wrong login and spent a very long minute wondering where my position had gone.
LondonNewsTrader
Posts: 421
Joined: Mon Sep 21, 2026 9:30 am

Re: Broker apps: usable for emergency flat only?

Post by LondonNewsTrader »

PTScalper wrote: Because cTrader uses standard C#, we can utilize LINQ to cleanly filter the orders.
Using OnStart and then Stop() is a neat way to get script behaviour. Two changes I'd make before trusting it in a real emergency.

First, cancel the pending orders before closing the positions. In a fast market a stop entry can trigger while the loop is still closing the open trades, and you finish with a new position the flatten never saw.

Second, when CloseAllSymbols is true, targetPositions is the live Positions collection itself, and each successful close removes an item from it while the foreach is still iterating. Taking a snapshot with Positions.ToArray() first is the safe pattern, the same thing you already do for the filtered pending orders.

It also closes everything on the account, including positions opened by other cBots. That's probably what you want in a panic, but it's worth printing a summary of what was closed so the other bots' state can be checked afterwards.

On the original question, I still keep the broker's own mobile app as the last resort, because a cBot needs a running platform somewhere, and the occasions when I've needed an emergency exit were usually the ones when my desktop wasn't available.
FXS Prop Desk
Posts: 1689
Joined: Wed Oct 07, 2026 9:52 am

Re: Broker apps: usable for emergency flat only?

Post by FXS Prop Desk »

Separating analysis from execution is a clear way to draw the line. For phone execution, one practical check is the order confirmation setting: some apps place orders with a single tap from the chart, others require confirmation. For structured trading based on prepared levels, using pending orders set in advance from the phone reduces the chance of tapping at a bad moment.

Another practical check is to test the app's behaviour when the connection drops in the middle of an order, to see whether it shows a clear status afterwards.
FXS Official Team | The Idea Lab & Strategy
Post Reply