GapHelper plugin
MT4 plugin that lets a client receive a different scenario outcome when Stop Loss (SL), Take Profit (TP), pending and stop-out orders are executed.
Installation
Install the prerequisites
Make sure that Visual C++ Redistributable for Visual Studio 2017 is already installed on the server.
Download the plugin
A fully working, time-limited version can be downloaded here: CPlugin.MT4Plugin.GapHelper versions.
Deploy
Unpack and copy the .dll file to your /plugins/ folder, then restart the MT4 server.
Settings
The first time the plugin runs, if there is no settings file, it starts normally and generates an example settings file automatically.
Example settings file (CPlugin.GapHelper.ini):
enable=1
ticks counting mode=satisfied
comment order=1
dealer bypass SL=1
dealer bypass Pending=0
dealer bypass Stopout=1
ticks SL=3
ticks TP=3
ticks Pending=3
ticks Stopout=3
groups=!demo*,!test*,*
symbols=,forex,*XAU*,*XAG*,
stop loss mode=last
take profit mode=order
stop order mode=first
limit order mode=last
stopout mode=worst
news count=2
news time 1=2016/08/01 01:30 30
news time 2=fri 22:00 240
verbose=2
manager=10
debug symbols=EURUSD,Forex
ticks Open=3
ticks Close=3
open mode=worst
close mode=worst
enable — 1/0 to enable/disable the plugin globally.
ticks counting mode — any of:
satisfied— the previous n ticks all satisfy the requirement to fire the order;following— n ticks after the first price that activates the order.
ticks SL, ticks TP, ticks Pending, ticks Open, ticks Close — number of ticks needed to confirm an SL / TP / Pending, Open and Close order respectively.
groups — groups filter, standard format used, 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.
stop loss mode, take profit mode, limit order mode, stopout mode, open mode, close mode — parameters to manage the order execution mode for Stop Loss, Take Profit, limit orders and stop-out orders respectively. The value can be one of:
first— use the first tick from the tick sequence;last— use the last tick from the tick sequence;best— best quoted price based on the ticks (number of ticks configured) in the array;fair-worst— among the best prices in the array, pick the worst;worst— use the worst price from the tick sequence;trade— use the price set by the client (useful for Stop Loss, Take Profit and pending orders).
verbose — choose logging verbosity:
0— minimal logging, errors and warnings only;1— general messages and configuration updates;2— most verbose, for debugging the plugin.
comment order — enable (1) / disable (0) a feature that marks orders executed by the plugin with a prefix; both the client and the broker will see this comment.
dealer bypass SL — enable (1) / disable (0) a feature that makes SL orders execute on the first tick that comes from the manager/dealer (instead of the regular feed). The tick-counting feature is bypassed, which means there is no need to confirm the tick sequence with multiple ticks in a row.
dealer bypass Pending — enable (1) / disable (0) the same feature for pending orders.
fair-worst mode is switched to best, and the order mode is switched to first.fair-worst mode. Whenever fair-worst mode fails to find a best price — if all following prices are worse than the one set in the order — it selects the worst among them.v2.0 feature: Virtual Dealer mode
In this mode the plugin captures market order attempts the way a dealer does, awaits the required number of ticks before deciding which price to use, and then executes the request according to the settings.
Additional parameters in the settings file
manager — login of the dealer (any manager account) on whose behalf the plugin will act;
debug symbols — MetaQuotes mask where you can specify symbol names and security group names. It lets you see the price stream for the selected symbols right in the MT4 journal, for easier debugging of execution while selecting the preferred mode.
Ticks counting algorithms — not taken into account during market orders (Open or Close).
fair-worst mode has no meaning for Open / Close orders either, and is switched to worst mode.
News mode
If no news time has been set up, the plugin works all the time. Otherwise it works only during the configured 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.
Notes on the MT4 journal
For example, when you set verbosity to 2, the MT4 journal shows messages like this:
Order #100 'buy stop' confirmed, open price updated from 100.02 to 103.02, 'worst' from:
The Buy Stop order has been confirmed by the plugin and the price has been adjusted from 100.02 to 103.02.
worst— describes the algorithm chosen to calculate the execution price: the last n ticks (configurable for each order type) in the order they arrived.100.02— the Buy Stop level set by the client.
Examples
Assume that 3 ticks are needed to confirm Stop Loss, Take Profit and pending order execution.
Buy trade, Take Profit will not fire
TP: 100.00
Ticks:
- 99.00
- 98.00
- 97.00
- 96.00
- 101.00 ← plugin denies TP execution because the tick 3 price is less than 100.00
- 102.00 ← plugin denies TP execution because the tick 4 price is less than 100.00
- 95.00 ← order remains open
Buy trade, Take Profit will fire
TP: 100.00
Ticks:
- 99.00
- 98.00
- 97.00
- 96.00
- 101.00 ← plugin denies TP execution because the tick 3 price is less than 100.00
- 103.00 ← plugin denies TP execution because the tick 4 price is less than 100.00
- 102.00 ← TP fires at level 100.00, as set up by the client
Buy trade, Stop Loss will not fire
SL: 100.00
Ticks:
- 101.00
- 102.00
- 103.00
- 104.00
- 99.00 ← plugin denies SL execution because the tick 3 price is greater than 100.00
- 98.00 ← plugin denies SL execution because the tick 4 price is greater than 100.00
- 105.00 ← order remains open
Buy trade, Stop Loss will fire
SL: 100.00
Ticks:
- 101.00
- 102.00
- 103.00
- 104.00
- 99.00 ← plugin denies SL execution because the tick 3 price is greater than 100.00
- 97.00 ← plugin denies SL execution because the tick 4 price is greater than 100.00
- 98.00 ← SL fires at level 98.00, the last confirmed price
Buy Stop will not fire
Price: 100.00
Ticks:
- 96.00
- 97.00
- 98.00
- 99.00
- 101.00 ← plugin denies executing the Buy Stop order because the tick 3 price is less than 100.00
- 102.00 ← plugin denies executing the Buy Stop order because the tick 4 price is less than 100.00
- 95.00 ← order remains pending
Buy Stop will fire
Price: 100.00
Ticks:
- 96.00
- 97.00
- 98.00
- 99.00
- 101.00 ← plugin denies executing the Buy Stop order because the tick 3 price is less than 100.00
- 103.00 ← plugin denies executing the Buy Stop order because the tick 4 price is less than 100.00
- 102.00 ← order opened at price 102.00
Buy trade, Stop Loss will fire using a tick thrown by the dealer
SL: 100.00
Assume that dealer bypass SL=1.
Ticks:
- 101.00
- 102.00
- 103.00
- 104.00
- 99.00 ← this tick was thrown by the manager; the plugin executes this order using the price of 99.00
More complex example: Buy trade, Stop Loss will fire normally
SL: 100.00
Assume that dealer bypass SL=0.
Ticks:
- 104.00
- 103.00
- 102.00
- 101.00
- 99.00 ← this tick was thrown by the manager; the plugin denies SL execution because the tick 3 price is greater than 100.00. Last 3 ticks: 102.00 101.00 99.00
- 100.5 ← tick from the regular feed, price is better than SL. Sequence interrupted
- 98.90 ← (manager) plugin denies SL execution because the tick 6 price is greater than 100.00. Last 3 ticks: 99.00 100.5 98.90
- 100.4 ← (feed), price is better than SL. Sequence interrupted
- 98.80 ← (manager) plugin denies SL execution because the tick 8 price is greater than 100.00. Last 3 ticks: 98.90 100.4 98.80
- 97.00 ← (manager) plugin denies SL execution because the tick 8 price is greater than 100.00. Last 3 ticks: 100.4 98.80 97.00
- 98.00 ← (manager) SL fires at level 98.00, the last confirmed price from the sequence: 98.80 97.00 98.00
If you set dealer bypass SL=1, the order is executed at price 99.00 when tick 5 arrives.
Buy Limit order fired
Price: 100.00
Ticks:
- 104.00
- 103.00
- 102.00
- 101.00
- 99.50
- 100.50 ← plugin denies executing the Buy Limit order because the tick 4 price is less than 100.00. Last 3 ticks: 101.00 95.00 100.50
- 102.50 ← plugin denies executing the Buy Limit order because the tick 4 price is less than 100.00. Last 3 ticks: 99.50 100.50 102.50
- 101.50 ← order opened at price 101.50. Last 3 ticks: 100.50 102.50 101.50
Buy trade, Stop-out will not fire
For instance, a Buy order is open, and when the price goes down below the level of 100.00 MT4 tries to close it because the margin level becomes critically low.
Ticks:
- 101.00
- 102.00
- 103.00
- 104.00
- 99.00 ← plugin denies stop-out execution because only one (99.00) of the last three ticks was used to execute the stop-out order.
- 98.00 ← plugin denies stop-out execution because only two (99.00 and 98.00) of the last three ticks were used to execute the stop-out order.
- 105.00 ← order remains open. MT4 did not try to fire the stop-out order.
In the MT4 journal you can see messages like:
0 17:36:50.738 '1000': reach stopout ml 40.49%, equity: 158.45, margin: 391.29 ← MT4 noticed a moment for executing stop-out
0 17:36:50.739 CPlugin.GapHelper Stopout skipped, last ticks: , stopout prices: ← plugin prevents this, order remains open
Buy trade, Stop-out will fire
For instance, a Buy order is open, and when the price goes down below the level of 100.00 MT4 tries to close it because the margin level becomes critically low. The last price algorithm is used in this example.
Ticks:
- 101.00
- 102.00
- 103.00
- 104.00
- 99.00 ← plugin denies stop-out execution because only one (99.00) of the last three ticks was used to execute the stop-out order.
- 97.00 ← plugin denies stop-out execution because only two (97.00) of the last three ticks were used to execute the stop-out order.
- 98.00 ← stop-out fires at level 98.00, the last confirmed price. All three ticks (99.00, 97.00 and 98.00) in a row were used by MT4 to execute the stop-out order.
All execution modes in one scheme
| Order | Incoming ticks (mode → price) |
|---|---|
| BUY, TP = 100 | first 102, best 103, fair-worst 101, worst 98, last 99 |
| SELL, TP = 100 | first 98, best 97, fair-worst 99, worst 102, last 101 |
| BUY STOP @ 100 | first 101, best 97, fair-worst 99, worst 102, last 98 |
| SELL STOP @ 100 | first 99, best 103, fair-worst 101, worst 98, last 102 |
| BUY, SL = 100 | first 99, best 102, fair-worst 101, worst 97, last 98 |
| SELL, SL = 100 | first 102, best 98, fair-worst 99, worst 103, last 101 |
| BUY LIMIT @ 100 | first 101, best 97, fair-worst 99, worst 102, last 98 |
| SELL LIMIT @ 100 | first 99, best 103, fair-worst 101, worst 98, last 102 |
| BUY, StopOut | first 99, best 102, worst 97, last 98 (order → first, fair-worst → best) |
| SELL, StopOut | first 102, best 98, worst 103, last 101 (order → first, fair-worst → best) |

