<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Spectar FX Gold</title>
    <description>The latest articles on DEV Community by Spectar FX Gold (@spectarfxgold).</description>
    <link>https://dev.to/spectarfxgold</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4274703%2Fc65d049f-38cc-49fb-8753-9be3a93baaaf.jpg</url>
      <title>DEV Community: Spectar FX Gold</title>
      <link>https://dev.to/spectarfxgold</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/spectarfxgold"/>
    <language>en</language>
    <item>
      <title>A read-only MQL5 script that measures today's drawdown from the server's midnight</title>
      <dc:creator>Spectar FX Gold</dc:creator>
      <pubDate>Mon, 30 Nov 2026 16:18:39 +0000</pubDate>
      <link>https://dev.to/spectarfxgold/a-read-only-mql5-script-that-measures-todays-drawdown-from-the-servers-midnight-3bp6</link>
      <guid>https://dev.to/spectarfxgold/a-read-only-mql5-script-that-measures-todays-drawdown-from-the-servers-midnight-3bp6</guid>
      <description>&lt;p&gt;Most prop-firm style accounts have a daily loss limit, and most traders track it in their head. The arithmetic is simple, but three details make hand counting go wrong: the day starts at the &lt;strong&gt;server's&lt;/strong&gt; midnight (not yours), commission and swap count, and open positions count too.&lt;/p&gt;

&lt;p&gt;This post walks through a small &lt;strong&gt;read-only MQL5 script&lt;/strong&gt; that does the count properly. It never opens, modifies or closes anything; it only reads history and positions and prints the result.&lt;/p&gt;

&lt;h2&gt;
  
  
  What "today" means on MetaTrader 5
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;TimeCurrent()&lt;/code&gt; is the time of the last quote, and &lt;code&gt;TimeLocal()&lt;/code&gt; is your PC clock. Neither is what a broker uses to cut the day. The day boundary is midnight on the trade server, so the script builds it from &lt;code&gt;TimeTradeServer()&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;void ServerDayStart(datetime &amp;amp;day0)
  {
   MqlDateTime t;
   TimeToStruct(TimeTradeServer(), t);
   t.hour = 0; t.min = 0; t.sec = 0;
   day0 = StructToTime(t);
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Closed P/L: sum the deals, not the positions
&lt;/h2&gt;

&lt;p&gt;A closed position's result lives in the deal history. For each deal since server midnight we add four fields: &lt;code&gt;DEAL_PROFIT&lt;/code&gt;, &lt;code&gt;DEAL_COMMISSION&lt;/code&gt;, &lt;code&gt;DEAL_SWAP&lt;/code&gt; and &lt;code&gt;DEAL_FEE&lt;/code&gt;. Balance operations (deposits, withdrawals, credits) are deals too, so we keep only &lt;code&gt;DEAL_TYPE_BUY&lt;/code&gt; and &lt;code&gt;DEAL_TYPE_SELL&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Two things are easy to miss:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Commission is often charged on the &lt;strong&gt;entry&lt;/strong&gt; deal, so filtering by &lt;code&gt;DEAL_ENTRY_OUT&lt;/code&gt; would drop it. Summing every buy/sell deal of the day keeps it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;HistorySelect()&lt;/code&gt; must be called before any &lt;code&gt;HistoryDeal*&lt;/code&gt; function, and the &lt;code&gt;to&lt;/code&gt; time should be a little in the future so the newest deal is included.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Floating P/L: open positions count too
&lt;/h2&gt;

&lt;p&gt;For open positions we add &lt;code&gt;POSITION_PROFIT&lt;/code&gt; and &lt;code&gt;POSITION_SWAP&lt;/code&gt;. Many rule sets measure the daily limit on equity, so a losing open trade uses up the limit even though nothing is closed yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Start-of-day balance without storing anything
&lt;/h2&gt;

&lt;p&gt;The script does not need to remember the balance at midnight. Current balance minus today's closed result gives the same number:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const double sodBal = balance - closed;   // balance at server midnight
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the base for a percentage limit when the rule says "x% of the start-of-day balance". If your rule says "x% of the initial account size", there is an input for that instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  The full script
&lt;/h2&gt;

&lt;p&gt;Save it as &lt;code&gt;DailyDrawdownMeter.mq5&lt;/code&gt; in &lt;code&gt;MQL5/Scripts&lt;/code&gt;, compile it in MetaEditor and drag it onto any chart.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//+------------------------------------------------------------------+
//|                                           DailyDrawdownMeter.mq5 |
//|  Shows how much of a daily loss limit is already used today,     |
//|  counted from the trading server's midnight, the way most prop   |
//|  firms count it: closed P/L today (with commission and swap)     |
//|  plus the floating P/L of open positions.                        |
//|  Read-only: this script never opens, modifies or closes orders.  |
//+------------------------------------------------------------------+
#property copyright "Spectar FX"
#property version   "1.00"
#property description "Daily drawdown meter: today's closed + floating P/L from server midnight vs your daily limit. Read-only."
#property script_show_inputs
#property strict

input double InpDailyLimitMoney = 0.0;   // Daily loss limit in account currency (0 = use percent)
input double InpDailyLimitPct   = 0.0;   // Daily loss limit in % of the base (0 = off)
enum ENUM_BASE { BASE_BALANCE_SOD = 0, BASE_INITIAL = 1 };
input ENUM_BASE InpBase         = BASE_BALANCE_SOD; // Base for the percent: start-of-day balance or initial size
input double InpInitialSize     = 0.0;   // Initial account size (only for BASE_INITIAL)

//--- start of the current day on the SERVER clock
void ServerDayStart(datetime &amp;amp;day0)
  {
   MqlDateTime t;
   TimeToStruct(TimeTradeServer(), t);
   t.hour = 0; t.min = 0; t.sec = 0;
   day0 = StructToTime(t);
  }

//--- closed P/L since server midnight: profit + commission + swap + fee of every OUT deal
void ClosedToday(const datetime from, double &amp;amp;sum, int &amp;amp;deals)
  {
   sum = 0.0;
   deals = 0;
   const int n = HistorySelect(from, TimeTradeServer() + 60) ? HistoryDealsTotal() : 0;
   for(int i = 0; i &amp;lt; n; i++)
     {
      const ulong tk = HistoryDealGetTicket(i);
      if(tk == 0) continue;
      const long type = HistoryDealGetInteger(tk, DEAL_TYPE);
      if(type != DEAL_TYPE_BUY &amp;amp;&amp;amp; type != DEAL_TYPE_SELL) continue;   // skip deposits/withdrawals
      sum += HistoryDealGetDouble(tk, DEAL_PROFIT)
           + HistoryDealGetDouble(tk, DEAL_COMMISSION)
           + HistoryDealGetDouble(tk, DEAL_SWAP)
           + HistoryDealGetDouble(tk, DEAL_FEE);
      deals++;
     }
  }

//--- floating P/L of all open positions (profit + swap)
void FloatingNow(double &amp;amp;sum, int &amp;amp;positions)
  {
   sum = 0.0;
   positions = PositionsTotal();
   for(int i = 0; i &amp;lt; positions; i++)
     {
      const ulong tk = PositionGetTicket(i);
      if(tk == 0) continue;
      sum += PositionGetDouble(POSITION_PROFIT) + PositionGetDouble(POSITION_SWAP);
     }
  }

void OnStart()
  {
   datetime day0 = 0;
   ServerDayStart(day0);
   int deals = 0, pos = 0;
   double closed = 0.0, floating = 0.0;
   ClosedToday(day0, closed, deals);
   FloatingNow(floating, pos);
   const double today    = closed + floating;
   const double balance  = AccountInfoDouble(ACCOUNT_BALANCE);
   const double sodBal   = balance - closed;              // balance at server midnight
   const string ccy      = AccountInfoString(ACCOUNT_CURRENCY);

   double limit = InpDailyLimitMoney;
   if(limit &amp;lt;= 0.0 &amp;amp;&amp;amp; InpDailyLimitPct &amp;gt; 0.0)
     {
      const double base = (InpBase == BASE_INITIAL &amp;amp;&amp;amp; InpInitialSize &amp;gt; 0.0) ? InpInitialSize : sodBal;
      limit = base * InpDailyLimitPct / 100.0;
     }

   string txt = StringFormat("Server day started: %s (server time now %s)\n",
                             TimeToString(day0, TIME_DATE | TIME_MINUTES),
                             TimeToString(TimeTradeServer(), TIME_DATE | TIME_SECONDS));
   txt += StringFormat("Closed today: %.2f %s (%d deals, incl. commission and swap)\n", closed, ccy, deals);
   txt += StringFormat("Floating now: %.2f %s (%d open positions)\n", floating, ccy, pos);
   txt += StringFormat("Day total:    %.2f %s\n", today, ccy);
   if(limit &amp;gt; 0.0)
     {
      const double used = (today &amp;lt; 0.0) ? -today : 0.0;
      txt += StringFormat("Daily limit:  %.2f %s  |  used %.1f%%  |  room left %.2f %s\n",
                          limit, ccy, 100.0 * used / limit, limit - used, ccy);
     }
   else
      txt += "Daily limit:  not set (enter it in the inputs to see the room left)\n";
   txt += "Read-only: nothing was opened, changed or closed. Always check your own firm's exact rule.";

   Print(txt);
   Comment(txt);
   MessageBox(txt, "Daily Drawdown Meter", MB_OK | MB_ICONINFORMATION);
   Comment("");
  }
//+------------------------------------------------------------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Limits of this approach
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;It is a snapshot. A script runs once; for a live number you would move the same three functions into an indicator or an Expert Advisor and call them from &lt;code&gt;OnTimer()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Firms word the rule differently: balance-based or equity-based, start-of-day or initial size, with or without a trailing element. Check the exact wording of your own rule and set the inputs to match.&lt;/li&gt;
&lt;li&gt;A gap or slippage at the broker can carry a loss past any number you watch. Measuring a limit is not the same as holding it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you only want to try numbers without MetaTrader, the same arithmetic is in a browser calculator: &lt;a href="https://spectarfx.com/daily-loss-calculator?utm_source=devto" rel="noopener noreferrer"&gt;prop firm drawdown calculator&lt;/a&gt;. And if you want the limit held on the account rather than measured, we publish a free Expert Advisor that locks the day at your limit: &lt;a href="https://spectarfx.com/free-mt5-daily-loss-lock?utm_source=devto" rel="noopener noreferrer"&gt;Daily Loss Limiter for MT5&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Written by the Spectar FX team. Not financial advice. Trading involves high risk of loss.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>mql5</category>
      <category>trading</category>
      <category>tutorial</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
