Загрузка страницы

MQL5 Tutorial - Advanced Break Even Stop

https://mql5tutorial.com/?s=stop+loss

With MQL5 for Metatrader5 we create an Expert Advisor that can set a Break Even Stop as soon as we reach a profit of 200 points. With a Break Even Stop Loss you can let the trade run without the risk to make a loss after the Breakeven-Stop has been placed...
In this video we are going to talk about a Break Even Stop. A Break Even Stop would be triggered when our buy trade here becomes profitable so if it rises above a certain amount of profit, this line will become red because now we have activated the Break Even Stop and from now on we can let our trades run because we will not make any further losses with this trade. Now how can we create a little Expert Advisor that is able to set the Break Even Stop for a buy trade? To do that please click this little button here in your Meta Trader or press the F4 key and now you should see the Meta Editor and here you want to click on File, New, Expert Advisor from Templates, Continue. I will call it Advanced Break Even Stop, Continue, Continue and Finish. And let's remove everything above the OnTick function and the two comment lines.
First of all we need something that we can trade so we need to include the file Trade.mqh; it's a standard library that comes with MQL 5 in the hash mode, and from that library we are going to create an instance of C trade that instance will have the name trade and to open a buy trade the first thing we need to know is the ask price, so let's calculate the current ask price by using symbol info double for the current symbol we use Symbol_Ask and the last Parameter_Digits will give us the number of digits after the decimal point, remember we have currency pairs with three digits and other currency pairs might have five digits and if we don't have any open positions that would be the case if positions total is below one, we need to open a test buy position so please make sure that you only trade this one on your demo account and to open that position we would use Trade.Buy, our position size will be ten micro lots. We want to trade the current chart, we will open the position for the current ask price on the chart. The initial stop loss will be one thousand points below our open price and our take profit will be five hundred points above the ask price.
And after that we are going to call our little method that we will call check Break Even Stop, and we will pass the ask process as a parameter. It will be a method called check Break Even Stop and don't forget to define the double and please don't forget to define the parameter double ask, we want to check all open positions for the current symbol so let's create a FOR loop here and now we need some details for our position so let's get the symbol by using Position Get Symbol that counter here re presents the current position. And if the symbol of the position equals the symbol on my chart we use Position Get Integer, Position_Tickets to get the position ticket number and we also need the open price for our position so we use a double variable called position buy price, and we calculate the value by using position get double, Position_Price_Open, and if the current ask price is two hundred points above the buy price so if ask is bigger than position by price plus two hundred points that would be the time when we want to modify our current stop loss so we use straight dot position modify for the current position ticket and we want to set the stop loss to the position buy price so that is our Break Even Point.
Let's close the F loop here and we also need to close the FOR loop and the Break Even Stop function. So let's click on the compile button here or press F7, and that worked without any errors and without any warnings so now we can press F4 or click here to go back to Meta Trader. And in Meta Trader you want to click on View Strategy Tester or press control and R. And here in our Strategy Tester you need to select the file Advanced Break Even Stop.ex5, pick any currency pair, enable the visualisation modes and start your back test. Now we need to have a little bit profit here and as soon as we reach a certain amount, the green line here changes to the red Break Even Stop. Let's wait until this happens again and here it is. Okay now you know how to create a Break Even Stop in MQL 5, and you have coded it yourself with a few lines of MQL 5 codes.

Видео MQL5 Tutorial - Advanced Break Even Stop канала MQL5 Tutorial
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
25 мая 2017 г. 0:15:59
00:06:17
Яндекс.Метрика