Sponsored by
Sponsored by

FX Blue P/L Manager for MT4 - User guide

Author: fxblue

1Introduction

1.1Purpose of document

This document describes the P/L Manager expert advisor (EA) for MetaTrader 4.

The EA is a simple tool for managing the profitability of orders in MT4. It does not place any orders itself. Instead, when the combined profit/loss on open positions hits a defined target, P/L Manager closes all the open positions and deletes all pending orders. You can specify both high and low triggers: i.e. a take-profit level and a stop-loss level.

P/L Manager can either be applied to a single symbol (e.g. EURUSD), or to all open positions on all symbols. It can also be restricted so that it only examines orders with a specific magic number (i.e. created by a particular expert advisor).

When P/L Manager's trigger is hit and positions are closed down, the EA can issue an alert on screen and/or by email. You can also configure the EA to shut down MetaTrader 4 in order to prevent further trading by automated systems. In addition to high and low triggers at which orders are closed, you can also define triggers at which alerts are sent (but no action is taken).

You can also choose to terminate all positions manually, by setting the ImmediateOrderClose property. In other words, P/L Manager can be used as a simple close-all script.

One further point: you can run a normal EA (i.e. which places automated orders) on one chart, and run P/L Manager on another chart for the same symbol. P/L Manager can then act as a fail-safe for the EA, closing out its positions if the EA misbehaves in some way and exceeds a profit target or stop-loss. Alternatively, P/L Manager can be used to provide alerting on another EA, sending out messages if its profitability goes outside certain bounds.

1.2Licence

Unless otherwise specified in writing by FX Blue, this software is licensed for personal use only. You are not allowed to distribute it to other people, regardless of whether or not money is changing hands. You only have a licence to use the software if you, personally, downloaded it from our website.

The software is provided entirely at the user's risk, and you should check it thoroughly before deploying it on computers trading real money. FX Blue accepts no responsibility whatsoever for any loss of any kind caused by installation or use of the software.

2.1Installation of P/L Manager

P/L Manager is installed in the usual MetaTrader 4 fashion: the EX4 file needs to be copied into the experts directory of the MetaTrader installation.

There are some other steps which users may want to take at the time of installation:

  • Configure email details for the EA to use (on a global basis, across all EAs).
  • Turn the "Allow live trading" setting on by default for all EAs.
  • If you want to use P/L Manager to shut down MetaTrader 4 when its target is hit, you must turn on the "Allow DLL imports" setting.

2.2Starting P/L Manager

You start P/L Manager by attaching the EA to a chart in MetaTrader. The EA's "Allow live trading" setting must be turned on before starting. The "Allow DLL imports" setting must also be turned on if you want to shut down MT4 when the target is hit.

You can use P/L Manager in either of two ways:

  • If you want P/L Manager to handle the orders for a single symbol (e.g. EURUSD), then attach it to a chart for that symbol, and leave its "ApplyToAllSymbols" setting set to false.
  • If you want P/L Manager to handle all orders for all instruments, then attach it to any chart, and set its "ApplyToAllSymbols" setting set to true. (If the EA is to manage all orders, then it does not matter which chart the EA is attached to.)

You can also choose to restrict the EA so that it only looks at orders which have a specific "magic number". This is designed to allow it to manage the orders created by a particular expert advisor.

Finally, before starting you will usually want to do one of the following:

  • Set the high and low triggers at which P/L Manager should close orders: the CloseAboveUpperPnL and CloseBelowLowerPnL parameters. These can be also be changed while P/L Manager is running, but please note that MetaTrader disables an EA while its properties window is open. Try to keep the window open for as short a time as possible when altering settings.
  • Set the ImmediateOrderClose parametr. Instead of watching for particular triggers, P/L Manager simply closes all applicable orders as soon as the first market tick is received from the broker. In other words, it acts as a simple close-all script.

2.3Stopping P/L Manager

You can stop P/L Manager simply by removing it from its chart.

2.4P/L Manager's status indicator

While it is running, P/L Manager displays a series of status indicators in the top left corner of its chart (though the position can be altered, using EA settings, to minimise overlay on other graph elements).

P/L Manager displays the following information:

  • Its current status (usually "Monitoring P/L").
  • The current P/L for the selected orders (either for a specific symbol, or for all symbols)
  • The high and low triggers at which orders will be closed.

2.4.1Debugging/activity indicator

P/L Manager also displays another, smaller indicator in the top left of the chart, directly underneath MetaTrader's display of the symbol and its latest price. This is for monitoring/debugging purposes, and shows the current or most recent status of the EA. There are three main states which this indicator can show:

  • The EA is waiting for another EA to finish placing orders.
  • The EA is in the middle of processing a market tick.
  • The EA is currently idle, and displays how long (in milliseconds) it took to take the appropriate action for the most recent market tick it received.

2.5Removing P/L Manager when its trigger has been hit

When the P/L Manager's target has been hit, you can optionally configure it to do either of the following:

Remove the EA from its chart.

Close MT4

If you don't do either of these things, the P/L Manager will continue running, and will close future trades if the profitability levels are breached again.

2.6Alerts and logs from P/L Manager

P/L Manager can be configured to display an alert on screen and/or by email when its trigger is hit. (The EA will also send alerts if it encounters a serious error, such as failure to read the details of an order from MetaTrader.)

You can also use the AlertAboveUpperPnL and AlertBelowLowerPnL settings to define thresholds at which P/L Manager alerts you, but does not take any action. For example, if "AlertAboveUpperPnL" is set to 100, and "CloseAboveUpperPnL" is set to 200, then the EA will issue an alert when the profit on open positions hits $100, and will close the positions (and issue another alert) when the profit hits $200.

These P/L alerts are not sent repeatedly, e.g. if the profitability bounces above and below the threshold defined by "AlertAboveUpperPnL". The alert is only sent once, and will not be sent again until the EA is reloaded - whether by re-adding it to the chart, changing the time period on its chart etc.

2.6.1Email alerts

Email alerts only occur if valid email settings have been configured in MetaTrader (using Tools/Options). Please note that the from and to addresses for MetaTrader emails are fixed; they cannot be specified by an individual EA. In other words, each EA cannot have a bespoke list of email recipients (at least, not without using its own email sending system rather than the one which MetaTrader provides).

To send emails to multiple addresses, use a method such as creating a Google Group list, and send MetaTrader's emails to the address of the group.

2.7Shutting down MetaTrader 4 and preventing further automated trading

You can configure P/L Manager to shut down MT4 when the upper or lower target is hit. If you are using P/L Manager to control the behaviour of other EAs, then a complete shutdown of the MT4 software is the only reliable way of preventing those EAs from placing further orders.

You enable the shutdown of MT4 using the CloseMT4 parameter. If you are sending email alerts, then you need to use the CloseDelaySeconds parameter to delay the shutdown and give MT4 time to send the email. However, please note that EAs can place further orders during the CloseDelaySeconds period.

3.1General settings

Setting

Description

CloseAboveUpperPnL

Open orders are closed once their combined profitability hits or rises above this upper boundary.

CloseBelowLowerPnL

Open orders are closed once their combined profitability hits or rises above this lower boundary. (N.B. If the orders are currently in profit, you can implement a "break-even stop" by setting this value to zero.

RestrictToMagicNumber

If non-zero, P/L Manager only processes orders which have this specific "magic number" (i.e. are created by a particular EA). If this setting is zero, P/L Manager ignores the magic number of orders.

ApplyToAllSymbols

If this is set to false, P/L Manager only looks at orders for the symbol on the chart which it is attached to. If this setting is set to true, P/L Manager processes all open positions, across all symbols.

3.2Immediate order close

Setting

Description

ImmediateOrderClose

Simply terminates all applicable orders (i.e. depending on ApplyToAllSymbols) as soon as the first market tick is received from the broker.

3.3Alerts

Setting

Description

DoVisualAlerts

If true, P/L Manager displays alerts on screen when its trigger is hit (and, optionally, when the profitability-alerts below are hit).

DoEmailAlerts

If true, P/L Manager issues alerts by email when its trigger is hit (and, optionally, when the profitability-alerts below are hit). This setting is effectively ignored unless you configure email details in MetaTrader.

AlertAboveUpperPnL

Defines an upper threshold for profitability above which P/L Manager sends an alert, but does not take any action. Can be used to send an interim notification before open positions are closed out.

AlertBelowLowerPnL

Defines a lower threshold for profitability below which P/L Manager sends an alert, but does not take any action. Can be used to send an interim notification before open positions are closed out.

3.4MT4 shut-down

The following settings control the optional shutdown of MetaTrader 4 when the upper or lower target is hit:

Setting

Description

CloseMT4

If turned on, MT4 is shut down when the target is hit. The "Allow DLL imports" setting in MT4 must be turned on.

CloseDelaySeconds

Number of seconds to wait before closing MT4. If you have turned on email notifications (DoEmailAlerts = true), then you need to allow enough time for MT4 to send the email before shutting down.

RemoveEA

If turned on, the EA removes itself from its chart when the target is hit

3.5Status box positioning and font

The following settings control the positioning etc of P/L Manager's status box on the chart.

Setting

Description

StatusBoxTop

Offset, in pixels, of the top of the status box from the top of the chart window

StatusBoxLeft

Offset, in pixels, of the left-hand edge of the status box from the left of the chart window

StatusBoxLineSpace

Number of pixels between each line in the status display. May need to be altered depending on chosen font and size.

StatusBoxFont

Font used for the status box (e.g. Arial, Verdana)

StatusBoxFontSize

Font size use for the status box

StatusBoxColour

Colour of the text in the status box

3.6Advanced settings

Setting

Description

IncludeSwapInProfit

Determines whether or not P/L Manager includes swap in its calculation of the profitability of open orders.

SlippageAllowed

Number of pips of slippage allowed when placing market orders to close existing positions. (N.B. MetaTrader's documentation says that this parameter is in points, not pips, but this seems unlikely to be correct.)