Swap Calculation Walkthrough
Most "how is swap calculated" explanations assume points mode and stop there, which is the single most common reason a DIY swap calculation disagrees with what a broker actually charges: MQL4's MODE_SWAPTYPE has four distinct values - points, base currency, interest percent, and margin currency - and each one is computed with a genuinely different formula, not a scaled variant of the others. Guessing the mode, rather than reading it, is where most hand calculations go wrong. This script reads MODE_SWAPTYPE first and prints which of the four modes is actually in force for the chart's symbol before doing any arithmetic. In points mode it converts the published points-per-night figure into account currency the same way a spread cost is converted - via TickValue and TickSize, not Point alone, since tick size can be a multiple of Point on some symbols. In interest-percent mode it applies the annualised rate to an approximate notional value (contract size x lots x current price) divided by a configurable day-count basis, since brokers differ on whether they use 360 or 365 days. In the two currency modes it uses the published per-lot currency figure directly, flagging plainly that no currency conversion is attempted if that currency differs from the account currency - a real gap, not a rounding error, and one this script does not try to paper over. It then applies the triple-swap multiplier if today happens to be the broker's triple-swap day of the week (configurable, since this varies by broker and is commonly but not universally Wednesday), and, if any ticket is currently open on the symbol, prints that ticket's live OrderSwap() next to the hand-calculated projection - with an explicit note that OrderSwap() is a running cumulative total since the ticket opened, not an isolated "tonight only" figure, so the two numbers are not directly comparable without recording OrderSwap() immediately before and after one rollover yourself. Notes and limitations - Base-currency and margin-currency swap modes are reported without any currency conversion to the account currency. If the swap currency differs from your account currency, the hand-calculated figure needs a manual conversion applied on top - this is stated plainly in the output, not silently assumed away. - Interest-percent mode's notional value (contract size x lots x current price) is an approximation of what brokers actually base the charge on; exact conventions (using bid vs ask, end-of-day snapshot price vs current price) vary by broker and are not all published. - OrderSwap() is cumulative since the ticket opened, not a per-night figure - a direct numeric comparison against the hand-calculated "tonight" projection is only valid on a ticket's very first night, or by taking a before/after difference across exactly one rollover as described in HOW TO USE. - The triple-swap day is a broker-specific convention, not a platform constant - MQL4 has no built-in way to read it, so InpTripleSwapDayOfWeek must be set correctly by hand from the symbol's contract specification.
Script · MT4 · v1.0 · Free
About Swap Calculation Walkthrough
Swap Calculation Walkthrough is a script for MetaTrader 4. It can alert you with a pop-up, sound, email or push message. It has 4 settings, such as Lots, Triple swap day of week and Annual basis days. MQL source code included.
Files in the download: SwapCalculationWalkthrough.mq4, SwapCalculationWalkthrough.txt.
Settings
| Setting | Default | What it does |
|---|---|---|
| Lots | 0.10 | Lot size to project the swap for |
| Triple swap day of week | 3 | Day-of-week that carries triple swap at this broker (0=Sun..6=Sat, 3=Wed is common but varies) |
| Annual basis days | 360 | Days-in-year basis used for interest-percent mode (360 or 365 depending on broker) |
| Compare open orders | true | Also print any open order's live OrderSwap() on this symbol |
How to install
- Download the ZIP from your PolyMaria library and unzip it.
- In MetaTrader 4 open File → Open Data Folder, then go to MQL4 → Scripts.
- Copy the .mq4 file(s) there. Put any .mqh files in MQL4 → Include.
- Open the .mq4 file in MetaEditor and press Compile (F7) if MetaTrader does not show it.
- Drag the script onto a chart to run it once.
Tags: Alerts · MT4 Script · MQL4 Source Code