İş tamamlandı
![](https://c.mql5.com/i/avatar/avatar_na2.png)
![](https://c.mql5.com/i/avatar/avatar_na2.png)
İş Gereklilikleri
Hi,
I need an Expert Advisor for MetaTrader 5. I would like to use this Expert Advisor for several index charts. The market entry signal is based on one custom trend indicator (code is attached) and one of the default trend strength indicators of MT5. The preferred timeframe is four hours, but this Expert Advisor should work for H1, M30 and M15 as well. The closing of positions is either made by a trailing stop, by another default indicator signal of MT5 or a signal of the custom indicator. I outlined the details below...
Looking forward to your responses!
Best regards,
SnakU
Trading strategy
- Signal to open a position
- A long position is opened if the following conditions are met:
- Both the slow and the fast trend line of the custom “Perfect Trend Line” indicator are below the close price of the completed previous (formed) candle.
- The value of the ADX indicator of the close price of the completed previous (formed) candle is greater than 25.
- The value of the of the ADX indicator of the close price of the current candle is higher than that of the ADX indicator of the close price the close price of the completed previous (formed) candle.
- No open long position already exists.
If all the conditions are met, a pending order is placed at the new candle. This is indicated in the attached picture 2.
- A short position is opened if the following conditions are met:
- Both the slow and the fast trend line of the custom “Perfect Trend Line” indicator are above the close price of the current candle.
- The value of the ADX indicator of the close price of the completed previous (formed) candle is greater than 25
- The value of the of the ADX indicator of the close price of the current candle is higher than that of the ADX indicator of the close price the close price of the completed previous (formed) candle.
- No open short position already exists.
If all the conditions are met, a pending order is placed at the new candle. This is indicated in the attached picture 2.
- Pending order parameter – part 1
- The pending order uses the expiration parameter “Today” and the order type is “Buy Limit” or “Sell Limit”. The “Buy Limit” price is set at a fixed percentage below the close price of the last completed candle, the “Sell Limit” price is set at a fixed percentage above the close price of the last completed candle. The fixed percentage is an input variable called LimitPercent, with an initial value of 0.1%
Buy Limit is calculated as follows: Close price of last completed candle*(100-LimitPercent)/100
Sell Limit is calculated as follows: Close price of last completed candle*(100+LimitPercent)/100
- Pending order parameter – part 2
- The pending order lot is calculated as a fixed percentage of the current account balance.
Lot = (Leverage/current ask or bid price) * (LotPercent / 100) * Account Balance where:
- Balance current account balance
- Leverage leverage to calculate the margin required to open a position with a size of 1 lot, initialised by a default value of 20
- LotPercent the percentage for the calculation of the lot, initialised by a default value of 10%
For example, when LotPercent = 10 %, a leverage of 20, a current account balance of 10,000 EUR and current ask/bid price is 13,724, the lot for DAX30 will be:
20/13,724*10/100*10,000=1.457
The obtained result is rounded off by the usual rules to the nearest correct tenth digit, in this case 1.46. If the calculated lot size is smaller than 0.1, the lot size should be rounded to 0.1.
The Ask price is used for long orders and the Bid price for short orders.
- Position modification – Stop Loss
- The StopLoss (SL) is calculated as a fixed percentage of the current account balance and is added to the current position, using the price of this position.
SL = Price of the opened position -/+ (SLPercent/100)/Position’s lot
- Balance current account balance
- Position’s lot as calculated under 2.
- SLPercent fixed accepted SL percentage
- “+” for short positions, “-“ for long positions
For example, when Position’s lot = 1.46, the current account balance = 10,000 EUR, the SL for a long/short position for DAX30 (assuming price of the opened position was 13,724) will be:
SL Long Position: 13,724-(0.02*10,000)/1,46=13,586.76
SL Short Position: 13,724+(0.02*10,000)/1,46=13,861.24
SL distance in both cases is (0.02*10,000)/1,46
- Position modification – Trailing Stop
- The modification is done by using a fixed SL distance, the one that is calculated in 4.
- If a long position is opened, the initial SL value is used as describe in 4.
- If a short position is opened, the initial SL value is used as describe in 4.
- Modifying the SL is done on a close price basis, taken from the completed previous (formed) candle, the current candle (uncompleted) is not used. Therefore, the modification should occur no more frequently than once in a candle.
- Moving the SL is permitted only in the direction of profit of the position - up for long position and down for short position.
- If you cannot set the SL at the level of the line, it should be set at the minimum allowable distance (but only if it complied with the previous rule of moving in the direction of the profit).
- The trailing stop feature should be configurable (AllowTrailing = true/false parameter).
6. Position closing
§ Closing of long positions should be done if one of the cases described below is true
1. Case 1: A stop loss is touched as outlined in 3. and 4. If it is possible, this is only checked on an adjustable timeframe, i.e., 1 minute by the input parameter TimerTime, and not on every tick.
2. Case 2: The MACD candle falls below the MACD signal line by more than a fixed percentage (Parameter name: LineOverrun), calculated as follows:
(Signal line value-MACD bar value)/(MACD bar value)>LineOverrun/100
If it is possible, this is only checked on an adjustable timeframe, i.e., 1 minute by the input parameter TimerTime, and not on every tick.
3. Case 3: The fast trend line value of the custom indicator “Perfect trend line” is higher than the current price, whereas the slow trend line value of the custom indicator “Perfect trend line” is lower than the current price
If it is possible, this is only checked on an adjustable timeframe, i.e., 1 minute by the input parameter TimerTime, and not on every tick.
Case 2 is illustrated in the attached picture 3 by the second arrow from the left (slow line = blue dotted line, fast line = red dotted line).
§ Closing of short positions should be done if one of the cases described below is true
1. Case 1: A stop loss is touched as outlined in 3. and 4. If it is possible, this is only checked on an adjustable timeframe, i.e., 1 minute by the input parameter TimerTime, and not on every tick.
2. Case 2: The MACD candle runs above the MACD signal line by more than a fixed percentage (Parameter name: LineOverrun), calculated as follows:
(Signal line value-MACD bar value)/(MACD bar value)>LineOverrun/100
If it is possible, this is only checked on an adjustable timeframe, i.e. 1 minute by the input parameter TimerTime, and not on every tick.
3. Case 3: The fast trend line value of the custom indicator “Perfect trend line” is higher than the current price, whereas the slow trend line value of the custom indicator “Perfect trend line” is lower than the current price.
If it is possible, this is only checked on an adjustable timeframe, i.e. 1 minute by the input parameter TimerTime, and not on every tick.Case 2 is illustrated below by the second arrow from the left (slow line = blue dotted line, fast line = red dotted line).
Case 2 is illustrated in the attached picture 4 by the second arrow from the left (slow line = blue dotted line, fast line = red dotted line).
- Miscellaneous
- The information on the opening/closing of positions and of modifications of the SL should be stored in the Journal.
- If there are any errors, it must print a message describing the error.
- When I run the EA on a real account, it should create a button on the chart, which permits trading. The work should begin only after I click on it.
- When I change an account, the EA should notify me about this and stop its work until a new launch is executed (an alternative - to ask the user whether it should continue its work).
8. Summary of adjustable input parameters
- Custom indicator “Perfect trend line”
- Fast length Default value: 10
- Slow length Default value: 20
- MACD
- Fast EMA Default value: 12
- Slow EMA Default value: 26
- MACD SMA Default value: 9
- Apply to Default value: Close
- ADX
- Period Default value: 20
- ADX-Level Default value: 25
- Pending order
- LimitPercent Default value: 0.1%
- Volume lot
- LotPercent Default value: 10%
- Stop Loss
- SLPercent Default value: 2%
- AllowTrailing Default value: True
- TimerTime Default value: 1min
- TakeProfit
- LineOverrun Default value: 5%