This is a plugin for MetaQuotes MetaTrader 4/5. It dynamically changes leverage for a client depend on his open positions. Each instrument type has different calculations: forex, metals and indices.
Configuration through Web UI #
Recently, we run out of standard MT4/5 plugin's settings management to the very own Web-based UI portal. It is because MT4/5 has limitation of number of parameters that it can handle, to store near to infinite number of settings it needs to store it outside MT4/5 platform.
To manage it, it needs external application. The plugin has simple API (described below) to communicate with external UI using Manager API.
- Download, unpack and copy DLL to platform's Plugins folder,
- restart platform
- ensure it get loaded by checking logs
- add instance of this plugin to MT5 (in MT4 it will be listed automatically)
- Have a look to Cloud based web solutions to get general idea
- Register your company at Admin portal and change its name from default one
- Add your MT4/5 servers to the TradePlatforms section
- Ensure it gets connected .
- Navigate to https://toolbox.cplugin.net and check TradePlatforms section
- activate () your platform to let system get connected (bottom right corner,)
- search for plugin instance that it should discover automatically after being connected
Latest plugin version without GUI:
- Flexible Leverage (GUI-less version page from history) documentation
- MT4 versions history : http://nuget.cplugin.com/nuget/apps/package/CPlugin.MT4Plugin.FlexibleLeverage/1.25.0
- MT5 version : https://nuget.cplugin.com/nuget/apps/package/CPlugin.MT5Plugin.FlexibleLeverage/1.0.1.12
Installation #
You need ensure that Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019 is installed to the server.
A fully working time-limited version can be downloaded from here: http://nuget.cplugin.com/feeds/apps/CPlugin.MT4Plugin.FlexibleLeverage/versions
Download, unzip and copy CPlugin.FlexibleLeverage.dll file to your /plugins/ subfolder, then restart MT4 server.
MT5 version can be downloaded from http://nuget.cplugin.com/feeds/apps/CPlugin.MT5Plugin.FlexibleLeverage/versions
Download and unzip it after, you will get .dll file with short documentation, settings example and version history.
Plugin settings #
Managers
- manager logins who permitted to change settings remotely, using MT4/5 Telnet API / Manager API.
GUID
– unique identifier to let automatic discovery run. Please do not change it, its value automatically generated when plugin started for very first time.
Settings through UI #
verbose -
verbosity of logging, a value of:
- silence
- fatal error
- error
- warning
- info
- trace – most verbosity output, best to be used during initial set up/testing.
If you use old plugin version: In MT5, maximum setting rows is 128. In that case, it suggested separating complicates settings to multiple plugin instances.
Very imporant
After MT4 FL v1.26+ and MT5 FL v1.16 there is no more ability to manage settings from MT4/5 Admin, because there is a (platform's) limit of rows specified, it limited number of policies you can specify, so customers complained about that limit and only the way was to switch for external storage of settings, outside of MT4/5 platform.
So now the limit is 1024 policies. If you still need more – contact us.
API for settings management #
To manage settings from any remote system (custom GUI) plugin has its own API built on top of ManagerAPI, ExternalCommand
in MT4 and CustomCommand
in MT5.
To send anything to plugin, you have to follow a generic type of payload test:
{CMD:enum}[;{ID:guid};{payload:JSON}]
Always use ANSI code page.
Square brackets mean optional data.
ID
– unique identifier of plugin which can be found in plugin settings, when it starts it generate unique value.
CMD
has a value of
{ get, set, discovery }
payload
- pure json data as is.
Each call to plugin, when plugin accept it (ID if valid, data validated) will return value of format shown below:
{ "success": true|false, "description" : "any text or null", "payload": "JSON structure" }
Commands #
Get settings #
Request current settings from server.
Request example:
get ;4bf69175-3ce0-4623-9659-be0400b9a883 |
Will return current plugin settings
Response example is:
|
Set settings #
Push settings back to 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}}
Response will be like this:
Response example
|
Discovery mechanism #
To get all known plugin form server there is magic packet to be sent to MT4/5 server and if plugin not in list of specified IDs, it will reply. So, we can one by one collect information about all plugins (which supported that featue) installed on server.
Discovery request body format
discovery[;{guid1}[;{guid2}]] |
Square brackets means that argiment is optional. So, once you send only keyword discovery for very first time it wil reply:
Disovery response example
discovered;2750b7bd-a6e3-44eb-8669-29b910e3554c;CPlugin.MT4Plugin.FlexibleLeverage;127 |
which has format of:
discovered;{ID};{pluginType};{version} |
So, step by step:
- send 'discovery' keyword and check response.
- check ResultCode of
ExternalCommand
in MT4 andCustomCommand
in MT5 call- OK - means that we get reply from plugin
- not OK - stop, no mo plugin will reply
- parse response, store ID
- check ResultCode of
- send discovery request with known ID like a: discovery;{guid1}
- check ResultCode of
ExternalCommand
in MT4 andCustomCommand
in MT5 call- OK - means that we get reply from plugin
- not OK - stop, no mo plugin will reply
- parse response, store ID
- check ResultCode of
- send discovery request with known ID like a: discovery;{guid1};{guid2} etc until it return not OK result code
Policies #
Each policy declare:
groups -
masks of groups, separated by comma. example: !demo,*
enabled -
flag to enable(1
) or disable(0
) that policy.
marginMode -
proceed only specified margin calculation mode, a value from MT4 server API:
disabled - use <securitiesMasks>
parameter instead
0 - MARGIN_CALC_FOREX
1 - MARGIN_CALC_CFD
2 - MARGIN_CALC_FUTURES
3 - MARGIN_CALC_CFDINDEX (not supported yet)
4 - MARGIN_CALC_CFDLEVERAGE
securities -
masks of securities (MT4 admin → securities tab). Empty value - disables such restriction.
<marginMode>
and <securitiesMasks>
is third priority search filter.
Policy search routine
In other words, policy search routine is logical operation:
<logins> OR <symbols> OR <security group>
OR - logical OR operation, means that if parameter is TRUE, whole expression is TRUE.
Policy Bands #
Currency
- you can specify how each band size must be calculated, a value of:
- if none entered - the account's currency, the default mode;
- or any currency code can be entered like: USD or GBP
Bands Type = percent | leverage
By default, if Bands Type
not specified, leverage
will be used (back compatibility with old settings file and plugin version)
Having percent
means that you set band values in percentage (1 = 1%). Plugin then will ignore MT4 symbol's percentage
value and use value from plugin settings instead.
Policy search by logins #
This is very first and highest priority search filter.
<logins mask>
can have list of logins and masks separated by comma like: 1000,2000*,!20005
Policy search by symbols #
Second priority search filter.
<symbols mask>
can have list of symbols and masks separated by comma like: !EUR*,*USD*
If any symbol name passed by this mask, then securitiesMasks
filter will be skipped from being checked, does not matter what mask specified there.
Policy band settings #
Each band has:limit
- bottom line of volume (specified in currency).value -
leverage|percentage to be within this band. 100 means 1:100, 1 = 1%, respectively.
For each band plugin uses minimal value between band's setting and actual user's leverage. So if user has leverage 1:200 and in band you put 1:500 plugin will use 1:200.
To provide leverage for rest volume you can set last line using format: <leverage>
So, you able to set leverage for volume that are higher than your previous line volume.
Bands use client's group currency.
Here and below, USD is just for example.
"Reject after NOP" feature #
Plugin can reject further attempts to open more orders after getting over total net open position.
Clients will get 'Too Many Orders' erorr in their trade terminal. Limit orders will be deleted with comment 'deleted[NOP Achieved]'.
<verbosity>
- from silence till verbosity output to journal.
<group mask and/or logins/mask>
- you can specify masks of groups and logins separated by comma (spaces 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 currency we assume secified in <NOP>
field.
"Recalc margin rates" feature #
Recalculate margin rates after symbol settings change.
<now> - request plugin to recalculate rates as soon as possible. When plugin done with recalculations, it will reset this parameter back to off. You should monitor this parameter (GUI will not fetch updates until you explicitly request it). Once it set back to off, you need to restart MTx platform, to apply those rates. Without restart - MT4 will not take into consideration any changes made by plugin.
<monitor> - monitor symbol settings update, when found - it will run recalculate feature.
<SO disable> - Forbid executing of StopOut if so. To be disabled manually after process done!
<logins masks> - list of logins to work with. (better to specify to reduce running time).
<groups masks> - list of masks of groups separated by comma. (better to specify to reduce running time).
Examples #
for all groups starting from 'demo', symbols that use 'forex' margin calculation formula, accounts with login started from 1000 and 2000 use two bands: until 100k USD leverage will be 1:50, for rest volume leverage will be 1:10.
Forex #
Flexible leverage model will automatically be adapted to clients trading positions and total USD volume they keep open. As the total open USD volume per Instrument of a client 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 less the account leverage will be used instead.
The sum of the total positions volume in USD is calculated in the following way:
1 lot is 100000 units of the base currency (first currency) so we transform it in USD using the current applicable rate if required,
For example:
- 1 lot USDJPY = 100k USD (no conversion needed)
- 1 lot EURUSD @ 1.1350= 113500 USD (multiply 100k by 1.1350)
- 2 lot EURJPY = 227k USD (multiply 200k by 1.1350 EURUSD VALUE AT THE TIME OF OPENING)
- 30 lots USDCAD = 3m USD
Therefore total volume: 100000 + 113500 + 227000 +3,000,000 = 1440500 USD
Every time a new position is open the USD value of the new trades is added and the margin calculations are perform based on above table.
Note BUY or SELL direction is not affecting this. Hedge positions need NEW 100% margin
- Buy 15 lots USDJPY= 1.5m USD
- Sell 15 lots USDJPY= 1.5m USD
- But 10 lots USDCAD= 1m USD
Total volume 4m USD
Assuming the account has 1:500
First 3m are in first band 1:500 needs $6000
Extra 1 m are in second band 1:200 so needs another $5000
Total margin required 11000 USD
In the above example if account has 1:200
First 3m are in first band 1:500 but account leverage is 1:200 so overcomes needs 15000
Extra 1 m are in second band 1:200 same as account’s so 5000
Total margin required 20000 USD
Metals #
Metals valued USD will be calculated separately on EACH metal any forex exposure will be ignored. For example, if a trader has 30 lots Buy on EURUSD, and then starts trading SILVER, his/her margin requirement for SILVER (FOREX), will not be affected by the existing FOREX positions. If client now choses opens GOLD is also not affected by the existing positions (SILVER and EURUSD) but if he opens new SILVER positions dynamic leverage rules starts 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 + Μax | 1:10 (10%) |
GOLD price $1250
40 lots Gold at $1250 is $5m (125010040)
Used Margin is 183,750 USD (table above)
Note BUY or SELL direction is not affecting this. Hedge positions need NEW extra 100% margin
Indices #
INDICES margin are calculated per lot per single instrument. As the trading volume per Instrument of a client 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 indices Instruments, the leverage will be calculated separately on each Index instrument. For example, if a trader has a position in EURUSD and Gold, and wants to trade DAX his/her margin requirement for DAX will not be affected by any other existing trades, unless if the existing trades are DAX.
Note, BUY or SELL direction is not impacting this. 1 Buy lot and 1 Sell lot equals 2 lots.
Hedge positions need NEW 100% margin.
Example 1: #
Client with USD account 1:500
OPEN 15 LOTS buy DAX (for 1 lot is 1000 USD)
Required margin is 15000 USD
Opens extra 20 lots Sell DAX. TOTAL lots 35 (15 Buy+ 20 Sell)
Required margin is 30000 USD for the first 30 lots (first band)
and the remaining 5 needs 10000 (510002 second band)
Total margin used 40000 USD
Example 2: #
Client with USD account 1:500
Has open USDJPY 10 lots (1 million USD first band)
Required margin for this 2000 USD
Open 2 lot DAX (for 1 lot is 1000 USD)
Required margin for this 2000 USD
Total margin requirements 2000 + 2000 = 4000 USD
Now client opens 1 lot HSI (for 1 lot is 7000 USD)
Required margin for this 7000 USD
New Total margin requirements 4000+7000 = 11000 USD
Now client opens another 35lots of DAX (for 1 lot is 1000 USD)
Total DAX lots = 2(existing) + 35(new) = 37 lots
New Margin for the extra 35 DAX 281000 +71000*2
42000 USD
TOTAL for all trades
10 lots USDJPY = 2000 USD
2 lots DAX = 2000 USD
1 lots HIS = 7000 USD
35 lots DAX (new) = 42000 USD
Total: 53000 USD
Demo version limitations #
Free demo version functionality is limited to first 100 trades to be processed. To continue testing you could restart your MT4/5 server. Contact us to purchase full license.