Page 2 of 2
Re: Daily loss limit that I actually respect and how
Posted: Fri Sep 25, 2026 9:18 am
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.
Re: Daily loss limit that I actually respect and how
Posted: Fri Sep 25, 2026 9:18 am
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}");
}
}
}
}
Re: Daily loss limit that I actually respect and how
Posted: Fri Sep 25, 2026 9:18 am
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());
}
}
Re: Daily loss limit that I actually respect and how
Posted: Fri Sep 25, 2026 9:19 am
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.