HedgeHelper
What is this
This is a plugin for MT4 that adjusts its logic to manage StopOut execution with automatic hedging. In addition, it manages StopLoss / TakeProfit orders to keep clients from going into negative free margin if an order gets executed.
Installation
Install the prerequisites
Make sure Visual C++ Redistributable for Visual Studio 2015 is already installed on the server.
Download the plugin
A fully working, time-limited version can be downloaded from nuget.cplugin.com/feeds/apps/CPlugin.MT4Plugin.HedgeHelper/versions.
Deploy it
Unpack and copy the .dll file to your /plugins/ folder, then restart the MT4 server.
Detailed information about each part
Forbid Stop orders
- Forbid BuyStop / SellStop orders that would open a new, unhedged position;
- Allow BuyStop / SellStop orders that hedge other positions (with the maximum volume limited to the unhedged volume).
In short, the client is forbidden from increasing an unhedged position with BuyStop / SellStop orders. Only hedging (reducing the unhedged volume) is allowed.
Hedge on StopOut
Interrupt the attempts MT4 makes to close clients' positions when StopOut happens:
- If there are unhedged trades:
- Clear the StopLoss / TakeProfit on the unhedged positions;
- Open additional positions to make all unhedged positions fully hedged;
- If all trades are already hedged, close the most unprofitable hedged position.
Unhedge margin
Here the plugin calculates the possible free margin if any order gets executed:
- Forbid the client from closing a hedged position (making the position become unhedged) if the predicted free margin would be negative;
- Forbid updating StopLoss / TakeProfit if the possible free margin would be negative.
Margin calculation formulas:
| Mode | Formula |
|---|---|
| Forex | 2 * lots_hedged * margin_hedged + lots_unhedged * (initial_margin > 0 ? initial_margin : contract_size) / margin_divider / leverage |
| CFD Leverage | 2 * lots_hedged * margin_hedged + lots_unhedged * (initial_margin > 0 ? initial_margin : contract_size * open_price) / margin_divider / leverage |
| CFD | 2 * lots_hedged * margin_hedged + lots_unhedged * (initial_margin > 0 ? initial_margin : contract_size * open_price) / margin_divider |
| CFD Index | 2 * lots_hedged * margin_hedged + lots_unhedged * (initial_margin > 0 ? initial_margin : contract_size * open_price * tick_price / tick_size) / margin_divider |
| Futures | 2 * lots_hedged * margin_hedged + lots_unhedged * maintenance * (percentage / 100) |
Manage StopLoss orders
- Forbid updating the StopLoss level if, after that trade is closed, the unhedge margin would become higher than the current one.
- Forbid opening new positions if there are already orders in the same direction (and therefore allow opening new positions in the opposite direction):
- Clear the StopLoss on an existing order (on the same symbol) when opening an opposite position for hedging.
Settings
The first time you run it, if you don't have a settings file, the plugin starts normally and generates an example settings file automatically.
After restarting MT4 you will see the plugin and its settings:
enable=1
forbid stop orders=0
hedge on stopout=0
unhedge margin=1
manage stoploss orders=0
groups=*
symbols=*
news count=0
news time 1=01/01/1970 00:00, 0
add stop order=1
SO with hedged=1
verbose=2
enable — 1|0 to enable / disable the plugin entirely.
forbid stop orders — 1|0 to enable / disable the routine that blocks Stop orders when the client does not have open positions.
hedge on stopout — 1|0 to enable / disable the automatic hedge-positions routine when a stop out happens.
unhedge margin — 1|0 to enable / disable the unhedge-blocking routine that prevents the client's free margin from going to a negative value.
manage stoploss orders — 1|0 to enable / disable the StopLoss management routines.
groups — groups filter, standard format, e.g. !demo*,* to exclude demo* groups.
symbols — symbols filter. A security name can be used as well. For example, forex,*XAU*,XAG* to work only for symbols under the forex security group and symbols containing XAU or XAG.
news count — count of news records defined below.
news time <n> — news period definition.
verbose — verbosity of logging:
0— minimal logging, only errors and warning messages;1— general messages and configuration updates;2— most verbose, for debugging the plugin.
News mode
If no news time has been set up, the plugin works all the time. Otherwise it works only during the defined periods.
news count — number of news entries defined below.
news time <news index>=<news value> — each news period uses this form.
news index — index of the record, counting from 1.
news value — can be one of these formats:
<day>/<month>/<year> <hour>:<minute> <duration><day of week> <hour>:<minute> <duration>
Use the first format to set an exact date and time. To repeat every week, choose the second variant.
<duration> — how long (in minutes) the plugin should work.
MT4 Journal examples
0 14:43:23.702 CPlugin.HedgeHelper '1003': XAUUSD buy 0.00, sell 1.00, hedged volume 0.00, unhedged volume 1.00, avg. 1.00000, hedged position 0.00, unhedged position 125157.00, total position 125157.00, margin hedged 0, contract size 100, margin divider 1.00, margin 1251.57
0 14:43:23.703 CPlugin.HedgeHelper '1003' close order 'buy' 1.00 lots of 'XAUUSD' forbidden because free margin will be: -705.57, equity: 546.000000, margin: 0.00, freeMargin: 546.00, newMargin: 1251.57

