Download MetaTrader 5

Watch how to download trading robots for free

Interesting script?
So post a link to it -
let others appraise it

You liked the script? Try it in the MetaTrader 5 terminal


Stop Loss Take Profit - expert for MetaTrader 5

Vladimir Karputov | English Русский 中文 Español Deutsch 日本語 Português

votes: 17
2017.11.03 11:43
2018.02.28 10:46

If a trade is closed by Stop loss, the volume is doubled; if it's closed by Take profit the minimum volume is used. OnTradeTransaction is used to determine whether a trade was performed after the activation of Stop loss or Take profit.

The excellent enumeration ENUM_DEAL_REASON was added in build 1625:

ENUM_DEAL_REASON Reason description
... ...
DEAL_REASON_SL The operation was executed as a result of Stop Loss activation
DEAL_REASON_TP The operation was executed as a result of Take Profit activation
... ...

It can be checked in OnTradeTransaction.

In other words, it is a very simple and reliable way to determine that a trade resulted from Take Profit or Stop Loss.

At the moment (build 1626), this Expert Advisor can only be checked in a live test - by launching it on a chart or in a debug mode on real data (F5 in the MetaEditor editor). I used the following bypass maneuver:

In OnTradeTransaction, we determined whether Take Profit or Stop Loss triggered:

      if(deal_symbol==m_symbol.Name() && deal_magic==m_magic)
            else if(deal_reason==DEAL_REASON_TP)

Translated from Russian by MetaQuotes Software Corp.
Original code:

AFStar AFStar

A semaphore signal indicator.

ZigZagOnParabolic_channel ZigZagOnParabolic_channel

A channel based on peaks and troughs of ZigZagOnParabolic.

ColorFisher_m11 ColorFisher_m11

Oscillator using inverse Fisher Transform.


The iMA (Moving Average) indicator based on iCCI (Commodity Channel Index). It is an analogue of Previous Indicator's Data in the terminal.