Работа завершена
![](https://c.mql5.com/i/avatar/avatar_na2.png)
![](https://c.mql5.com/i/avatar/avatar_na2.png)
Техническое задание
I need an EA for MetaTrader 4. The market entry signal is based on trendline breakouts. Positions are closed by stop loss, take profit and reverse signal. All positions are accompanied by a trailing stop, based on the average between the maximum and minimum of the last n candlesicks. Some calculations are done as a fraction of the Average True Range indicator. Lot sizes are calculated as a percentage of the balance.
Trendlines
This system is based on trendlines, which are drawn connecting fractals.
Fractals are patterns where highs or lows are in the middle of a series of bars. A first order bearish fractal appears in a series of three consecutive bars, when the high of the central bar is preceded by a lower high and is followed by a lower high. The opposite for bullish fractals. Similarly, a second order bearish fractal is found in a series of five consecutive bars where the highest high is located on the central bar, and therefore, preceded by two lower highs and followed by two lower highs. The opposite would be a bullish second order fractal (see Image 1). An indicator for fractals is already built in MT4 (Indicators>Bill Williams>Fractals).
This expert advisor should draw trendlines connecting second order fractals. A bullish trendline will be drawn connecting the last two second order bullish fractals, if the most recent bullish fractal is higher than the previous bullish fractal. A bullish trendline can be also drawn connecting the last and the third-to-last second order fractals, if the most recent bullish fractal is higher than the third-to-last bullish fractal. The fourth and further fractals on the left will be ignored. To draw a trendline, fractals must be separated by at least five candlesticks.
The opposite conditions will be considered to draw a bearish trendline (see Image 2).
Trendlines must be drawn considering second order fractals. Only in the case that no second order fractal appears in the last nine candlesticks, trendlines should be drawn based on first order fractals, following the same rules (see Image 3).
Signal to open a position
A long position is opened when a candlestick closes above a bearish trendline, plus a determinate number of pips in the same direction, calculated as a percentage of the 14 periods Average True Range (“ATR(14)”, henceforth) value in the last bar. This parameter should be adjustable and called Breakstrength (please allow fractional numbers). For example, if the ATR(14) value in the last bar is 100, and the Breakstrength is set as 10%, then 10 points should be added to the level of the breakout point to allow the opening of a position.
Conversely, a short position is opened when a candlestick closes below a bullish trendline, plus a determinate number of pips specified in Breakstrength (see Image 4).
Only one position at a time can be opened, even if a candlestick breaks two different trendlines. If an opposite signal occurs (i.e., a candlestick breaks out a trendline in the opposite direction) the active position must be closed, and the opposite operation should be opened (see Image 5).
Stop loss
Initial stop loss
Every position is opened with an initial stop loss, that should be placed after the most recent fractal in the opposite direction (i.e. the last down fractal for long positions, or the last up fractal for short positions). A given number of pips, calculated as a percentage of the ATR(14), should be added to this initial stop loss (this parameter should be adjustable and called InitialSLBuffer, see Image 6).
Trailing stop loss
The initial stop loss should not move during the first n candlesticks following the entry point. This parameter should be adjustable and called StartTrailSL. After the n+1 candlestick following the entry point, the stop loss should be moved below the minimum of the n+1 candlestick in long positions, or above the maximum of the n+1 candlestick in short positions, plus a given number of pips. This parameter should be adjustable, calculated as a fraction of the ATR, and called TrailSLBuffer (see Image 7).
A line displaying the average between the maximum and minimum of the last n candlesticks should be calculated with each candle (called Kijun line, as in the Ichimoku indicator, already built in MT4). The number of candlesticks used to calculate this line should be adjustable and called KijunPeriod.
As soon as a candlestick opens and closes above this Kijun line in long positions, or below, in short positions, the stop loss level should be moved below or above the Kijun line, for long and short positions respectively, plus a number of pips specified by the TrailSLBuffer (see Image 8).
The trailing stop loss must move only in the direction of profit.
Momentum Filter
No position should be opened if in the last 10 bars price moves faster than a determinate number of pips, from mimimum to maximum. This maximum number of pips to allow trading should be adjustable, as a multiple of the ATR(14), and called MaxMomentum (see Image 9).
Time Filter
Opening positions shoud be only allowed during a determinate period of the session (e.g.: from 7:00 to 18:00). If a position is still open after this time, it shoud be closed following the Marked Exit rules of the next paragraph.
Market Exit
Part of the position should be closed if profit reaches a percentage of the number of pips risked by the initial stop loss (both parameters, the percentage of the position to be closed, and the percentage of pips of the initial stop loss, should be adjustable and called ClosePercent and InitialFraction respectively). The rest of the position should be closed when price hits the stop loss or when an opposite signal occurs, whatever happens first (see Image 10).
Position volume
The number of lots is calculated as a percentage of the current balance (this parameter should be adjustable and called RiskPercentage).
For example, equity is $10000, and we want to risk 1%, that is, RiskPercentage is set to 1%. The EURUSD pip value is $10 on a standard lot. Initial stop loss is at 200 pips distance. Then, the lot size should be:
$100 risked / (200 pips *$10 per pip) = 0,05 lot size.
Calculations should be rounded to the nearest microlot size.
Miscellaneous:
When the Expert Advisor is runned, it should attach the used indicators with the specified parameters.
If there are any errors, it must print a message describing the error.