Flexible Leverage
This is a plugin for MetaTrader 4/5. It dynamically changes a client's leverage depending on their open positions. Each instrument type has different calculations: forex, metals and indices.
Configuration through the Web UI
We moved the plugin's settings management out of the standard MT4/5 plugin parameters and into a dedicated web-based control panel. MT4/5 limits how many parameters a plugin can handle, so storing a near-unlimited number of settings requires keeping them outside the MT4/5 platform.
Managing the settings therefore needs an external application. The plugin exposes a simple API (described below) so an external UI can communicate with it through the Manager API.
Install the plugin
Download, unpack and copy the DLL to the platform's Plugins folder, then:
- restart the platform;
- confirm it loaded by checking the logs;
- add an instance of this plugin to MT5 (in MT4 it is listed automatically).
Read the cloud overview
Have a look at Cloud-based web solutions for the general idea.
Register your company in Toolbox
Register your company in Toolbox and change its name from the default one.
- Add your MT4/5 servers to the Trade Platforms section.
- Confirm the server connects.
Activate the platform
In Toolbox, open the Trade Platforms section.
- Activate your platform so the system can connect to it.
- Look for the plugin instance, which Toolbox should discover automatically once the platform is connected.
Latest plugin version without GUI:
- Flexible Leverage (GUI-less version, page from history) documentation
- MT4 versions history: CPlugin.MT4Plugin.FlexibleLeverage
- MT5 version: CPlugin.MT5Plugin.FlexibleLeverage
Installation
You need to ensure that the Visual C++ Redistributable for Visual Studio 2015, 2017, 2019 and 2022 is installed on the server.
A fully working time-limited version can be downloaded from the MT4 plugin feed. Download, unzip and copy the CPlugin.FlexibleLeverage.dll file to your /plugins/ subfolder, then restart the MT4 server.
The MT5 version can be downloaded from the MT5 plugin feed. Download and unzip it — you will get the .dll file along with a short documentation, a settings example and the version history.
Plugin settings
Managers — manager logins permitted to change settings remotely, using the MT4/5 Telnet API / Manager API.
GUID — a unique identifier that lets automatic discovery run. Please do not change it; its value is generated automatically when the plugin starts for the very first time.
Settings through the UI
verbose — the logging verbosity level, one of:
- silence
- fatal error
- error
- warning
- info
- trace — the most verbose output, best used during initial setup and testing.
The limit is now 1024 policies. If you need more, contact us.
Policies
Each policy has:
enabled — flag to enable (1) or disable (0) that policy.
groups — masks of groups, separated by commas. Example: !demo,*
marginMode — process only the specified margin calculation mode, a value from the MT4 server API. When disabled, it uses the <securitiesMasks> parameter:
0—MARGIN_CALC_FOREX1—MARGIN_CALC_CFD2—MARGIN_CALC_FUTURES3—MARGIN_CALC_CFDINDEX(not supported yet)4—MARGIN_CALC_CFDLEVERAGE
securities — masks of securities (MT4 admin → Securities tab). An empty value disables this restriction.
<marginMode> and <securitiesMasks> are the third priority search filter.
Policy search routine
Multiple policies can match simultaneously, so we need to find the best policy reliably, not just the first one matched.
OR and AND logic describes the requirements during the search over parameters such as user group and symbol name, to decide whether the plugin should apply settings to an entity or not.
If any part of the routine is not defined, it is assumed to have passed that particular search. For example, you can specify logins and leave groups empty: the algorithm will only consider the logins filter and will bypass the groups logic, as if it always matches.
Logic: OR
Login competes with user group when both parameters satisfy the search. A policy where login matched has higher priority and will "win" the search over another policy where user group matched.
The same applies to symbol when it competes with security group. A policy where symbol matched will "win" over another policy where security group matched.
Searching only by user group or by login is not reliable, because it can be part of other policies with different symbols, for example. So we continue to search by symbol, and if not found, by security group and margin mode.
Logic: AND
This is a more strict algorithm, requiring you to pass all steps or none.
Policy Bands
Currency — you can specify how each band size is calculated, a value of:
- if none entered — the account's currency, the default mode;
- or any currency code, such as
USDorGBP.
Bands Type = percent | leverage
By default, if Bands Type is not specified, leverage is used (for backward compatibility with old settings files and plugin versions). Setting percent means you set band values as percentages (1 = 1%). The plugin then ignores the MT4 symbol's percentage value and uses the value from the plugin settings instead.
Policy search by logins
This is the very first and highest priority search filter.
<logins mask> can hold a list of logins and masks separated by commas, such as: 1000,2000*,!20005
Policy search by symbols
The second priority search filter.
<symbols mask> can hold a list of symbols and masks separated by commas, such as: !EUR*,*USD*
If any symbol name passes this mask, the securitiesMasks filter is skipped, regardless of what mask is specified there.
Policy band settings
Each band has:
limit — bottom line of volume (specified in currency).
value — leverage or percentage to apply within this band. 100 means 1:100; 1 = 1%, respectively.
For each band, the plugin uses the minimum value between the band's setting and the user's actual leverage. So if a user has leverage 1:200 and you put 1:500 in the band, the plugin uses 1:200.
To provide leverage for the remaining volume, you can set a last line using the format <leverage>. This sets the leverage for volume above your previous line's volume.
Bands use the client's group currency.
Here and below, USD is just an example.
"Reject after NOP" feature
The plugin can reject further attempts to open more orders once the total net open position is exceeded.
Clients will get a 'Too Many Orders' error in their trade terminal. Limit orders will be deleted with the comment deleted.
<verbosity> — from silence to verbosity, controlling output to the journal.
<group mask and/or logins/mask> — you can specify masks of groups and logins separated by commas (spaces are not allowed). Example: 5000*,!50001,real* 100k 1 EUR
<NOP> — net open position.
<enable> — flag to enable (1) or disable (0) that rule.
<currency> — optional field to specify the currency assumed for the <NOP> field.
"Recalc margin rates" feature
Recalculate margin rates after a symbol's settings change.
<now> — request the plugin to recalculate rates as soon as possible. When the plugin finishes recalculating, it resets this parameter back to off. You should monitor this parameter (the GUI will not fetch updates until you explicitly request it). Once it is set back to off, you need to restart the MTx platform to apply those rates. Without a restart, MT4 will not consider any changes made by the plugin.
<monitor> — monitor symbol settings updates; when one is found, it runs the recalculate feature.
<SO disable> — forbid execution of Stop Out, if any. To be disabled manually after the process is done!
<logins masks> — list of logins to work with (better to specify, to reduce running time).
<groups masks> — list of group masks separated by commas (better to specify, to reduce running time).
Examples
For all groups starting with 'demo', symbols that use the 'forex' margin calculation formula, and accounts with logins starting with 1000 and 2000, use two bands: up to 100k USD the leverage is 1:50; for the rest of the volume the leverage is 1:10.
Forex
The flexible leverage model automatically adapts to clients' trading positions and the total USD volume they keep open. As the total open USD volume per instrument increases, the maximum leverage offered decreases accordingly.
Example:
| Open Volume USD | Maximum Leverage | Margin used |
|---|---|---|
| 0 - 3m | Max 1:500 | $6,000 ($3m/500) |
| 3m - 5m | Max 1:200 | $10,000 ($2m/200) |
| 5m - 15m | Max 1:100 | $100,000 ($10m/100) |
| 15m - 30m | Max 1:50 | $300,000 ($4m/50) |
| 30m + | max 1:20 |
Actual leverage for 30m is 1:72.
Note: where the account leverage is lower, the account leverage is used instead.
The sum of the total positions volume in USD is calculated as follows. One lot is 100,000 units of the base currency (the first currency), so we convert it to USD using the current applicable rate when required.
For example:
- 1 lot USDJPY = 100k USD (no conversion needed)
- 1 lot EURUSD @ 1.1350 = 113,500 USD (multiply 100k by 1.1350)
- 2 lots EURJPY = 227k USD (multiply 200k by the 1.1350 EURUSD value at the time of opening)
- 30 lots USDCAD = 3m USD
Therefore, total volume: 100,000 + 113,500 + 227,000 + 3,000,000 = 1,440,500 USD.
Every time a new position is opened, the USD value of the new trades is added, and the margin calculations are performed based on the table above.
Note: BUY or SELL direction does not affect this. Hedge positions need a new 100% margin.
- Buy 15 lots USDJPY = 1.5m USD
- Sell 15 lots USDJPY = 1.5m USD
- Buy 10 lots USDCAD = 1m USD
Total volume 4m USD. Assuming the account has 1:500:
- the first 3m are in the first band, 1:500, needing $6,000;
- the extra 1m is in the second band, 1:200, needing another $5,000;
- total margin required: 11,000 USD.
In the example above, if the account has 1:200:
- the first 3m are in the first band, 1:500, but the account leverage is 1:200, so it caps at 15,000;
- the extra 1m is in the second band, 1:200, same as the account's, so 5,000;
- total margin required: 20,000 USD.
Metals
Metals valued in USD are calculated separately on EACH metal, and any forex exposure is ignored. For example, if a trader has 30 lots Buy on EURUSD and then starts trading SILVER, their margin requirement for SILVER (forex) is not influenced by the existing forex positions. If the client now opens GOLD, it is also not affected by the existing positions (SILVER and EURUSD); but if they open new SILVER positions, dynamic leverage rules start to apply.
Example:
| Open Volume (USD) | Maximum Leverage | Margin used |
|---|---|---|
| 0 - 250k | Max 1:200 (0.5%) | 1,250 ($250k/200) |
| 250k - 500k | Max 1:100 (1%) | 2,500 ($250k/100) |
| 500k - 2m | Max 1:50 (2%) | 30,000 ($1.5m/50) |
| 2m - 5m | Max 1:20 (5%) | 150,000 ($3m/20) |
| 5m + | Max 1:10 (10%) |
GOLD price $1250.
40 lots Gold at $1250 is $5m (1250 × 100 × 40). Used margin is 183,750 USD (table above). Note: BUY or SELL direction does not affect this. Hedge positions need a new extra 100% margin.
Indices
Indices margin is calculated per lot per single instrument. As the trading volume per instrument increases, the maximum leverage offered decreases accordingly, as per the following table.
Example:
| Open Lots | Margin Requirement |
|---|---|
| 0-30 | Standard Margin Per Instrument |
| 30-50 | Margin × 2 |
| 50-100 | Margin × 5 |
| 100-200 | Margin × 8 |
| 200+ | Margin × 10 |
For indices this is done per trading instrument, so if a client has positions open across multiple index instruments, the leverage is calculated separately on each index instrument. For example, if a trader has a position in EURUSD and Gold and wants to trade DAX, their margin requirement for DAX is not altered by any other existing trades, unless the existing trades are DAX.
Note: BUY or SELL direction does not affect this. 1 Buy lot and 1 Sell lot equals 2 lots. Hedge positions need a new 100% margin.
Example 1
Client with a USD account at 1:500. Open 15 lots Buy DAX (1 lot is 1000 USD): required margin is 15,000 USD. Open an extra 20 lots Sell DAX, for a total of 35 lots (15 Buy + 20 Sell):
- required margin is 30,000 USD for the first 30 lots (first band);
- the remaining 5 lots need 10,000 (5 × 1000 × 2, second band);
- total margin used: 40,000 USD.
Example 2
Client with a USD account at 1:500.
- Has open USDJPY 10 lots (1 million USD, first band). Required margin: 2,000 USD.
- Opens 2 lots DAX (1 lot is 1000 USD). Required margin: 2,000 USD.
- Total margin requirements: 2,000 + 2,000 = 4,000 USD.
- Now the client opens 1 lot HSI (1 lot is 7,000 USD). Required margin: 7,000 USD.
- New total margin requirements: 4,000 + 7,000 = 11,000 USD.
- Now the client opens another 35 lots of DAX (1 lot is 1000 USD). Total DAX lots = 2 (existing) + 35 (new) = 37 lots. New margin for the extra 35 DAX: 28 × 1000 + 7 × 1000 × 2 = 42,000 USD.
Total for all trades:
- 10 lots USDJPY = 2,000 USD
- 2 lots DAX = 2,000 USD
- 1 lot HSI = 7,000 USD
- 35 lots DAX (new) = 42,000 USD
- Total: 53,000 USD
API for settings management
To manage settings from any remote system (a custom GUI), the plugin has an API built on top of the Manager API: ExternalCommand in MT4 and CustomCommand in MT5.
To send anything to the plugin, you have to follow a generic payload format:
{CMD:enum}[;{ID:guid};{payload:JSON}]
Always use the ANSI code page.
Square brackets mean optional data.
ID — the unique identifier of the plugin, which can be found in the plugin settings; it is generated when the plugin starts.
CMD has a value of:
- get
- set
- discovery
payload — pure JSON data as-is.
Each call to the plugin, when the plugin accepts it (ID is valid, data validated), returns a value in the format shown below:
{ "success": true, "description": "any text or null", "payload": "JSON structure" }
Commands
Get settings
Request the current settings from the server.
Request example:
get;4bf69175-3ce0-4623-9659-be0400b9a883
This returns the current plugin settings.
Response example:
{ "success": true, "description": "any meaningful text, especially during failure", "payload": { "enabled": true, "verbose": 5, "policies": [{ "<policy definition>": "..." }] } }
Here and below, the data is from the plugin settings file as-is.
Set settings
Push settings back to the server.
Request example:
set;ccf822b4-cef3-4554-b500-6406726c60bb;{"enabled":true,"verbose":0,"rejectAfterVerbose":0,"rejectAfter":[{"NOP":1000000,"enabled":false,"groupsOrLoginsMasks":"*test*","currency":"USD"}],"policies":[{"groups":"demo*","enabled":true,"marginMode":-1,"securitiesMasks":"","bandsCurrency":"USD","bandsType":0,"loginsMasks":"","symbolsMasks":"","bands":[{"limit":500000,"leverage":100.0}]}],"recalc":{"loginsMasks":"","groupsMasks":"","now":false,"monitor":false,"SODisable":false}}
Demo version limitations
The free demo version is limited to processing the first 100 trades. To continue testing, you can restart your MT4/5 server. Contact us to purchase a full license.

