MQL5 Tutorial - Simple Sell Position Close explained (in 4 min)
Автор: MQL5 Tutorial
Загружено: 2024-07-01
Просмотров: 857
Описание:
https://mql5tutorial.com/?s=sell+posi...
In this video, we are going to create an Expert Advisor that opens and closes sell positions based on certain conditions. Let's see how we can do that.
First, we start Metaeditor by clicking the little icon or pressing F4.
We begin by including the Trade.mqh library, which provides functions for trading operations. We then create an instance of the CTrade class named trade.
In the OnTick function, which is called on every tick, we first get the current Bid price and normalize it to the number of digits specified for the symbol.
Next, we check if the total number of positions is less than 10. If this condition is met, we open a sell position of 0.10 lots, which is equivalent to 10 micro lots. The trade.Sell function takes several parameters: the lot size (0.10), the symbol (NULL for the current symbol), the price (Bid), the stop loss (Bid + 1000 * _Point), the take profit (Bid - 150 * _Point), and a comment (NULL).
If the total number of positions is exactly 10, we call the CloseAllSellPositions function to close all open sell positions.
The CloseAllSellPositions function iterates through all open positions in reverse order. For each position, it retrieves the ticket number and the position type. If the position type is a sell position, it closes the position using the trade.PositionClose function.
At the end, we press F7 to compile the code.
If this was too fast for you or if you don't understand what all the code is doing, you may want to check out the Premium course on our website or watch one of the basic videos first.
If the compilation works, we go back to MetaTrader by pressing F4 or clicking on the icon.
Back in MetaTrader, we press Control and R to start the strategy tester, pick the Expert Advisor that we have just created, enable the visual mode, and start a strategy test.
We should then see the Expert Advisor on the chart.
If you are already a Premium course member and have an idea for a video like this one, you can send us an email.
In this video, we have learned how to create an Expert Advisor that opens and closes sell positions based on the number of open positions. We have coded this Expert Advisor ourselves with a few lines of MQL code.
Thanks for watching, and I will see you in the next video.
Повторяем попытку...
Доступные форматы для скачивания:
Скачать видео
-
Информация по загрузке: