https://mql5tutorial.com/?s=history
In this video, we're going to create an Expert Advisor in MQL5 that calculates and displays the type of the last closed position in the history. This tutorial will guide you through the process of coding this Expert Advisor, compiling it, and testing it in MetaTrader.
After opening MetaEditor and creating a new Expert Advisor, we include the Trade.mqh file and create an instance of the CTrade class named 'trade'. This instance is used for executing trades.
In the OnTick function, we calculate the current Ask and Bid prices using NormalizeDouble and SymbolInfoDouble. We then check if there are no open positions and, if true, execute a buy order using tradeBuy
Now, let's focus on the GetLastClosedPositionType() function, which is the heart of our Expert Advisor:
Initialization: We start by declaring necessary variables. TotalNumberOfDeals holds the total number of deals in the history, TicketNumber stores the ticket number of each deal, OrderType and DealEntry help in identifying the type of deal, and MySymbol and PositionDirection are strings to store the symbol of the deal and the direction of the position, respectively.
History Selection: The function uses HistorySelect(0,TimeCurrent()) to select all deals from the account history up to the current time. This is crucial for analyzing the entire history of closed positions.
Iterating Through Deals: We then iterate through each deal using a for loop. For each deal, we retrieve the ticket number using HistoryDealGetTicket(i).
Analyzing Each Deal: For each deal with a valid ticket number, we retrieve the deal type (OrderType) and the symbol (MySymbol). We also check the deal entry type (DealEntry) to determine if the deal represents a closed position.
Determining the Position Type: If the deal's symbol matches the current symbol (_Symbol) and the deal entry type indicates a closed position (DEAL_ENTRY_OUT), we then determine the type of the closed position. If the order type was a sell (ORDER_TYPE_SELL), it means the closed position was a buy trade, so we set PositionDirection to "BUY-TRADE". Conversely, if the order type was a buy (ORDER_TYPE_BUY), the closed position was a sell trade, and we set PositionDirection to "SELL-TRADE".
Returning the Result: After iterating through all deals, the function returns the PositionDirection string. This string indicates whether the last closed position in the history was a buy or sell trade.
This GetLastClosedPositionType() function is essential for our Expert Advisor. It allows us to analyze the trading history and understand the nature of the last closed trade, which is crucial for making informed trading decisions.
After coding, we compile the code by pressing F7 and ensure there are no errors. Then, in MetaTrader, we test the Expert Advisor using the Strategy Tester to see it in action, displaying the type of the last closed position.
The trade.Buy method in our code executes a buy order, and the GetLastClosedPositionType function plays a vital role in analyzing the deal history to determine the type of the last closed position.
In this video, you've learned how to create an Expert Advisor that not only executes trades but also analyzes the history of closed positions to display the type of the last closed position. This functionality, coded with a few lines of MQL5 code, provides valuable insights into your trading strategy.
Thank you for watching! If you need a deeper understanding of the code, consider checking out our premium course or starting with our basic videos. We look forward to seeing you in our next video, where we'll explore more exciting aspects of MQL5 programming.
Смотрите видео MQL5 TUTORIAL - SIMPLE CHECK LAST CLOSED POSITION TYPE FUNCTION онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь MQL5 Tutorial 01 Январь 1970, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 2,267 раз и оно понравилось 37 людям.