Home Products Metatrader Indicators Metatrader Stop Loss Take Profit Indicator

Metatrader Stop Loss Take Profit Indicator


This Metatrader indicator is for calculating optimal stop loss and take profit levels. It avoids the need for choosing arbitrary (or fixed) trade exit points.

SKU: N/A Category:

The indicator analyses market behaviour and calculates where the stop and take profit levels should be placed to achieve a certain trade win ratio within a given time period. It uses the theory of maximals with a modified random walk model to estimate confidence intervals for price movements (read more).

It is suitable for manual trading and can also be used as a custom indicator to provide direct input to an expert advisor.

The indicator provides five signal lines:

  • 0 – Buy side stop loss (pips)
  • 1 – Buy side take profit (pips)
  • 2 – Sell side stop loss (pips)
  • 3 – Sell side take profit (pips)
  • 4 – Rate of change of SL/TP spread


Figure 1: Metatrader input box
Figure 1: Metatrader input box ©

Trade time: The first two inputs are the most important. The target trade time is defined in chart bars and is used to calculate a confidence interval (probability envelope) over which the price is likely to range. This must be greater than 5 bars and less than 1000. Longer (shorter) intervals can be set by changing the period of the chart.

For example on a 5-minute (M5) chart 288 bars will correspond to a target trade time of one day. To calculate for a five day period, the chart can be changed to hourly (H1) and the target trade time set to 120 bars.

Win ratio: The target win ratio defines the percentage of winning trades that you are trying to achieve. A higher percentage will create a higher ratio between the stop and take profit levels. A lower percentage will create a narrower ratio. Note that the trade exit points are calculated from recent volatility measurements whereas the future market behaviour may not replicate the past. For this reason it is important to understand that the trade time and win ratio are targets not guarantees.

Figure 2: Advisor panel & trade exits displayed on chart
Figure 2: Advisor panel & trade exits displayed on chart ©

Market behaviour: There are three options to choose from. The random walk model makes no prior assumptions about market direction. The trending model assumes a constant drift based on the current market trend (at your chosen chart period). In practice what this does is introduce a slight bias. For example, when buying in an upward trend the drift would cause the stops to be calculated slightly higher than with the “random walk model”. When buying in a downward trend the stops would be calculated lower.

The anti-trending model assumes the current trend is about to reverse and as such negates the drift parameter. It therefore does the opposite of the above. In a rising market, buy stops will be placed lower due to the assumption that a reversal is imminent.

Averaging period: The averaging period is the interval over which the volatility is measured and on which the trade exit points are calculated. A low averaging period will cause the signal lines to change quickly but will also include more “noise”. A higher averaging period will lead to smoother signal lines but they will be less responsive to rapid changes in volatility.

Trade side: Choose which outputs are to be displayed on the chart. Choosing “buy side” will display the buy side outputs as a histogram and sell side as a single line. Choosing “sell side” will do the opposite. If this parameter is set to both, then both outputs will be displayed as a histogram on the chart. Note this will only change the display in the sub window of the chart. It will not alter the outputs (or order of them) that the indicator produces.

Figure 3: Chart showing both buy and sell side signal
Figure 3: Chart showing both buy and sell side signal ©

Indicator Usage

The outputs from the indicator are based on statistical calculations of the probability of certain price movements over a given time period. The trade logic (or trader) should make the final decision as to whether to place the trade or not based on the following:

  • The absolute values of the TP/SL levels
  • The spread between the TP/SL levels
  • The rate of change of the spread between TP/SL levels

Absolute TP/SL levels: The indicator may calculate a need for a very high SL under certain volatile conditions (to meet your target win ratio). Clearly the trade should be avoided if it falls outside of the risk limits for maximum losses. This can be used to filter out (or select) trades with potentially higher risk levels when used in conjunction with a money management system.

Figure 4: Increasing SL/TP due to high volatility
Figure 4: Increasing SL/TP due to high volatility ©

In the EUR/USD example above, after a sudden burst of volatility the indicator calculates a need for a stop loss at -862 pips (buy side) to meet a 75% win ratio. The trade logic could decide to remain on hold and wait for a more predictable entry point. This comes after a few hours when the stop reduces to 420 pips.

This logic needs to be customized according to your strategy and risk requirements. Keep in mind that filtering out trades with high stop losses will also most likely filter out the most profitable trades as well.

TP/SL Spread: This is the absolute difference between the TP and SL and is a measure of volatility. The more volatility in the market, the wider the spread between the TP and SL will need to be. The trading strategy can choose to place trades according to this spread. For example, it can select periods when the market is either ranging or trending.

Figure 5: Spread changes indicating different market conditions
Figure 5: Spread changes indicating different market conditions ©

The GBP/USD example above shows two highlighted periods where the spread is high – suggesting the market is trending strongly – confirmed by the chart. The third highlighted period shows an interval where the market is ranging. Note that the TP/SL spread is much narrower (and flatter) during this period.

Rate of change of TP/SL Spread: The last signal line is the rate of change of the spread between the trade exit points. A high positive value indicates the spread is increasing quickly (divergence). A high negative value indicates the spread is contracting quickly (convergence). When this line is closer to zero, it means that the TP and SL values are not changing usually indicating a fairly “flat” market.

The example below shows an expanded view of the sub window in Figure 3 above.

Figure 6: The rate of change of the spread can be used as a signal
Figure 6: The rate of change of the spread can be used as a signal ©

For a more detailed explanation please see the frequently asked questions page.

MQL Code

Accessing the indicator outputs can be achieved easily with MQL using the custom indicator function (iCustom). The example code below demonstrates:

double buy_sl, buy_tp, sell_sl, sell_tp, roc;


Simply replace “test\myindicator” with the name of the indicator in your local Metatrader folder. The above code will use the default parameters. To enter custom parameters, simply enter them as a list after the indicator name.

Current version 3.44