The Midnight Gap Edge Case
This logic elegantly handles the most dangerous VPS crash scenario: a crash at 23:45 and a reboot at 00:15.
Because the EA pulls the current server time on initialization and compares it to the saved g_currentDay, it instantly recognizes that the day has rolled over during the downtime. It will ignore the cached balance, pull the new midnight balance, and overwrite the storage files automatically, keeping your drawdown limits mathematically accurate for the new session.
Scaling rules after payout — keeping challenge habits on funded
Re: Scaling rules after payout — keeping challenge habits on funded
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Re: Scaling rules after payout — keeping challenge habits on funded
To implement a webhook notification, the most critical architectural requirement is spam prevention (a state latch). Because OnTick fires hundreds of times per second during high-volume sessions, triggering an HTTP request as soon as equity drops below the threshold will instantly spam your API and get your webhook IP-banned.
We need to add a boolean flag (_alertSentToday) that resets at the start of the daily rollover.
Here is how to implement the Telegram Bot API natively in both MT5 and cTrader, including the state latches.
1. MetaTrader 5 (MQL5): Telegram WebRequest
In MT5, you use the native WebRequest() function.
Requirement: You must explicitly allow [https://api.telegram.org](https://api.telegram.org) in the MT5 Terminal options (Tools > Options > Expert Advisors > Allow WebRequests for listed URL).
We need to add a boolean flag (_alertSentToday) that resets at the start of the daily rollover.
Here is how to implement the Telegram Bot API natively in both MT5 and cTrader, including the state latches.
1. MetaTrader 5 (MQL5): Telegram WebRequest
In MT5, you use the native WebRequest() function.
Requirement: You must explicitly allow [https://api.telegram.org](https://api.telegram.org) in the MT5 Terminal options (Tools > Options > Expert Advisors > Allow WebRequests for listed URL).
Code: Select all
//+------------------------------------------------------------------+
//| Inputs |
//+------------------------------------------------------------------+
input string InpTelegramBotToken = ""; // Telegram Bot Token
input string InpTelegramChatID = ""; // Telegram Chat ID
bool g_alertSentToday = false;
//+------------------------------------------------------------------+
//| Telegram HTTP Post Function |
//+------------------------------------------------------------------+
bool SendTelegramMessage(string message)
{
if(InpTelegramBotToken == "" || InpTelegramChatID == "") return false;
string url = "https://api.telegram.org/bot" + InpTelegramBotToken + "/sendMessage";
string headers = "Content-Type: application/json\r\n";
// Construct JSON Payload
string payload = "{\"chat_id\":\"" + InpTelegramChatID + "\",\"text\":\"" + message + "\"}";
char data[], result[];
string result_headers;
StringToCharArray(payload, data, 0, WHOLE_ARRAY, CP_UTF8);
// Fire WebRequest (timeout 1000ms)
int res = WebRequest("POST", url, headers, 1000, data, result, result_headers);
if(res != 200) Print("Telegram Error Code: ", res);
return (res == 200);
}
//+------------------------------------------------------------------+
//| Integration into Gatekeeper |
//+------------------------------------------------------------------+
bool IsDrawdownCompliant()
{
if(g_startOfDayBalance <= 0) return false;
double currentEquity = AccountInfoDouble(ACCOUNT_EQUITY);
double maxAllowedLoss = g_startOfDayBalance * (InpDailyDDLimitPct / 100.0);
double currentLoss = g_startOfDayBalance - currentEquity;
if(currentLoss >= maxAllowedLoss)
{
// 1. Trigger the Alert Latch
if(!g_alertSentToday)
{
string alertMsg = "🚨 ALGO LOCKOUT 🚨\n" +
"Account: " + IntegerToString(AccountInfoInteger(ACCOUNT_LOGIN)) + "\n" +
"Daily DD Breached. Max allowed loss: $" + DoubleToString(maxAllowedLoss, 2);
SendTelegramMessage(alertMsg);
// Native MT5 Push to phone app (optional fallback)
SendNotification(alertMsg);
g_alertSentToday = true;
}
return false;
}
return true;
}
// Inside your OnTick() Rollover check, add:
// if(timeStruct.day_of_year != g_currentDay) { ... g_alertSentToday = false; ... }Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Re: Scaling rules after payout — keeping challenge habits on funded
2. cTrader (C#): Native Http Request
In cTrader, you can use the built-in Http service.
Requirement: You must elevate the robot's access rights to AccessRights = AccessRights.Internet in the header, otherwise the cBot will sandbox the HTTP request and throw an exception.
In cTrader, you can use the built-in Http service.
Requirement: You must elevate the robot's access rights to AccessRights = AccessRights.Internet in the header, otherwise the cBot will sandbox the HTTP request and throw an exception.
Code: Select all
using System;
using cAlgo.API;
namespace cAlgo.Robots
{
// CRITICAL: Must include AccessRights.Internet to allow webhooks
[Robot(TimeZone = TimeZones.UTC, AccessRights = AccessRights.Internet)]
public class PostPayoutRiskManager : Robot
{
[Parameter("Telegram Bot Token", DefaultValue = "", Group = "Alerts")]
public string TelegramBotToken { get; set; }
[Parameter("Telegram Chat ID", DefaultValue = "", Group = "Alerts")]
public string TelegramChatId { get; set; }
private bool _alertSentToday = false;
protected override void OnTick()
{
int serverDay = Server.Time.DayOfYear;
// Session Rollover
if (serverDay != _currentDay)
{
// ... Balance anchoring logic here ... //
// Reset the alert latch for the new day
_alertSentToday = false;
}
// ... Active DD flat-account trigger here ... //
}
private void SendTelegramAlert(string message)
{
if (string.IsNullOrWhiteSpace(TelegramBotToken) || string.IsNullOrWhiteSpace(TelegramChatId))
return;
try
{
// URL Encode the message to handle spaces and newlines safely
string url = $"https://api.telegram.org/bot{TelegramBotToken}/sendMessage?chat_id={TelegramChatId}&text={Uri.EscapeDataString(message)}";
// Execute HTTP GET (synchronous, but fast enough for a single daily trigger)
var response = Http.Get(url);
if (response.IsSuccessful)
Print("Telegram lockout alert sent successfully.");
else
Print($"Telegram HTTP Error: {response.StatusCode}");
}
catch (Exception ex)
{
Print($"Webhook Exception: {ex.Message}");
}
}
private bool IsDrawdownCompliant()
{
double maxAllowedLoss = _startOfDayBalance * (DailyDdLimitPct / 100.0);
double currentLoss = _startOfDayBalance - Account.Equity;
if (currentLoss >= maxAllowedLoss)
{
if (!_alertSentToday)
{
string alertMsg = $"🚨 ALGO LOCKOUT 🚨\nAccount: {Account.Number}\nLimit of ${Math.Round(maxAllowedLoss, 2)} breached.";
SendTelegramAlert(alertMsg);
_alertSentToday = true;
}
return false;
}
return true;
}
}
}Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.
Re: Scaling rules after payout — keeping challenge habits on funded
Setup Steps for the Telegram Bot
If you haven't set up the bot infrastructure yet:
Message @BotFather on Telegram and send /newbot.
Copy the HTTP API Token he provides (this is your TelegramBotToken).
Send a message to your new bot, then go to [https://api.telegram.org/bot](https://api.telegram.org/bot)<YOUR_TOKEN>/getUpdates in your browser. Look for the "id" under the "chat" object. This is your TelegramChatId.
If you haven't set up the bot infrastructure yet:
Message @BotFather on Telegram and send /newbot.
Copy the HTTP API Token he provides (this is your TelegramBotToken).
Send a message to your new bot, then go to [https://api.telegram.org/bot](https://api.telegram.org/bot)<YOUR_TOKEN>/getUpdates in your browser. Look for the "id" under the "chat" object. This is your TelegramChatId.
Preserve your own money. Scale with the market's money. Exponential growth is the ultimate key.