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.

Demo version limitations. Without a license the plugin processes only the first 100 requests. After that you need to restart the server to reset the counter. You can purchase a license for unlimited usage at any time — see Contact.
Important to know. The plugin needs "Manual only" execution mode set on the security group you intend to use it with.Furthermore, StopOut automation in the dealer's terminal might cancel the plugin's logic and bypass its responses for keeping orders open on stop out. You must disable that automation if you have it enabled.

Detailed information about each part

Forbid Stop orders

  1. Forbid BuyStop / SellStop orders that would open a new, unhedged position;
  2. 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:

  1. If there are unhedged trades:
    1. Clear the StopLoss / TakeProfit on the unhedged positions;
    2. Open additional positions to make all unhedged positions fully hedged;
  2. If all trades are already hedged, close the most unprofitable hedged position.
Be aware when entering the right settings. If you have dealers' automation enabled, make sure it will not intercept StopOut orders before the plugin does — so un-tick the Stop Out field.

Unhedge margin

Here the plugin calculates the possible free margin if any order gets executed:

  1. Forbid the client from closing a hedged position (making the position become unhedged) if the predicted free margin would be negative;
  2. Forbid updating StopLoss / TakeProfit if the possible free margin would be negative.

Margin calculation formulas:

ModeFormula
Forex2 * lots_hedged * margin_hedged + lots_unhedged * (initial_margin > 0 ? initial_margin : contract_size) / margin_divider / leverage
CFD Leverage2 * lots_hedged * margin_hedged + lots_unhedged * (initial_margin > 0 ? initial_margin : contract_size * open_price) / margin_divider / leverage
CFD2 * lots_hedged * margin_hedged + lots_unhedged * (initial_margin > 0 ? initial_margin : contract_size * open_price) / margin_divider
CFD Index2 * lots_hedged * margin_hedged + lots_unhedged * (initial_margin > 0 ? initial_margin : contract_size * open_price * tick_price / tick_size) / margin_divider
Futures2 * lots_hedged * margin_hedged + lots_unhedged * maintenance * (percentage / 100)

Manage StopLoss orders

  1. Forbid updating the StopLoss level if, after that trade is closed, the unhedge margin would become higher than the current one.
  2. Forbid opening new positions if there are already orders in the same direction (and therefore allow opening new positions in the opposite direction):
    1. 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

enable1|0 to enable / disable the plugin entirely.

forbid stop orders1|0 to enable / disable the routine that blocks Stop orders when the client does not have open positions.

hedge on stopout1|0 to enable / disable the automatic hedge-positions routine when a stop out happens.

unhedge margin1|0 to enable / disable the unhedge-blocking routine that prevents the client's free margin from going to a negative value.

manage stoploss orders1|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:

  1. <day>/<month>/<year> <hour>:<minute> <duration>
  2. <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