Advertisement IC Markets

Daily loss limit that I actually respect and how

Discuss 1-minute to 15-minute price action setups, fading intraday momentum, key support/resistance zones, and proven short-term trading methodologies.
FTtrader
Posts: 954
Joined: Mon Aug 03, 2026 2:43 pm

Re: Daily loss limit that I actually respect and how

Post by FTtrader »

To push outbound HTTP requests from a cAlgo Watchdog script, you must first grant the cBot permission to step outside the sandbox by setting AccessRights = AccessRights.FullAccess in the robot's attribute.

Since you are running C# under the hood, you can instantiate a standard HttpClient to fire off an asynchronous POST request. This ensures the webhook payload is dispatched without blocking the main trading thread, which is critical when the script's primary job is to ruthlessly close unauthorized positions in milliseconds.
Recommended broker for automated trading & scalping IC Markets
FTtrader
Posts: 954
Joined: Mon Aug 03, 2026 2:43 pm

Re: Daily loss limit that I actually respect and how

Post by FTtrader »

cAlgo (C#) Implementation

Here is the code to wire a Discord webhook directly into the rejection logic. Discord expects a simple JSON payload with a content field.

Code: Select all

using cAlgo.API;
using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace cAlgo.Robots
{
    // 1. FullAccess is mandatory for outbound web requests
    [Robot(AccessRights = AccessRights.FullAccess)]
    public class WatchdogEnforcer : Robot
    {
        // Reusing a single HttpClient instance is C# best practice to avoid socket exhaustion
        private static readonly HttpClient _httpClient = new HttpClient();
        
        [Parameter("Webhook URL", DefaultValue = "https://discord.com/api/webhooks/...")]
        public string WebhookUrl { get; set; }

        private bool _isLockedOut;

        protected override void OnStart()
        {
            Positions.Opened += OnPositionOpened;
        }

        private void OnPositionOpened(PositionOpenedEventArgs args)
        {
            if (_isLockedOut)
            {
                // 1. Kill the trade immediately
                ClosePositionAsync(args.Position);

                // 2. Fire the webhook asynchronously 
                string msg = $"🚨 **Trade Rejected:** Attempted to open {args.Position.TradeType} on {args.Position.SymbolName}. Daily lockout is active.";
                _ = SendWebhookAsync(msg); // Discard the task so we don't block the thread
            }
        }

        private async Task SendWebhookAsync(string message)
        {
            try
            {
                // Hardcoding the JSON string avoids needing external NuGet packages 
                // for Newtonsoft or System.Text.Json inside the cTrader IDE.
                string jsonPayload = $"{{\"content\": \"{message}\"}}";
                
                var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
                
                HttpResponseMessage response = await _httpClient.PostAsync(WebhookUrl, content);
                
                if (!response.IsSuccessStatusCode)
                {
                    Print($"Webhook failed: {response.StatusCode}");
                }
            }
            catch (Exception ex)
            {
                Print($"Webhook exception: {ex.Message}");
            }
        }
    }
}
FTtrader
Posts: 954
Joined: Mon Aug 03, 2026 2:43 pm

Re: Daily loss limit that I actually respect and how

Post by FTtrader »

MQL5 Equivalent

If you deploy this in MetaTrader 5, the logic relies on the native WebRequest function.

The MT5 Gotcha: MT5 inherently blocks all outbound HTTP traffic. You must manually whitelist your endpoint URL (e.g., [https://discord.com](https://discord.com)) in the terminal settings under Tools > Options > Expert Advisors > "Allow WebRequest for listed URL".

The Function Call:

Code: Select all

void SendDiscordWebhook(string message)
{
    string cookie = NULL, headers = "Content-Type: application/json\r\n";
    string url = "https://discord.com/api/webhooks/...";
    
    // Construct the JSON string
    string json = "{\"content\": \"" + message + "\"}";
    
    char data[];
    StringToCharArray(json, data, 0, WHOLE_ARRAY, CP_UTF8);
    
    char result[];
    string result_headers;
    
    // Fire the POST request (5000ms timeout)
    int res = WebRequest("POST", url, headers, 5000, data, result, result_headers);
    
    if(res != 200 && res != 204) {
        Print("Webhook error: ", GetLastError());
    }
}
FTtrader
Posts: 954
Joined: Mon Aug 03, 2026 2:43 pm

Re: Daily loss limit that I actually respect and how

Post by FTtrader »

Securing the Endpoint

If you push this to a custom web endpoint rather than Discord (for example, logging the event into a PHP/MySQL backend on a server you control), ensure your endpoint validates the payload. Because cAlgo bots run locally on your machine, passing a simple static Authorization: Bearer <token> header in your HttpClient setup will prevent random bots from spamming your API if they discover the URL.
LondonScalper
Posts: 770
Joined: Sat Sep 05, 2026 7:54 am

Re: Daily loss limit that I actually respect and how

Post by LondonScalper »

-2R is where my limit ended up too, but I got there from the other direction. I started at -4R because it felt professional, and my records showed that nearly everything after the second loss was worse than the first two. The limit came down because the third and fourth tickets had negative expectancy on their own, not because of the comeback maths.

One thing I'd add to the mechanics: the limit should include open risk. If I'm at -1.5R and put on a trade that risks another full R, I've already committed to -2.5R, and the rule is broken before any stop is hit. So the check happens at entry: realised loss plus the new trade's risk has to stay inside the line.

The accountability message is the easiest part to skip, and the one that stops the day quietly reopening in the evening. Mine is a single line with the time on it.
Post Reply