Join our fan page
- Views:
- 688
- Rating:
- Published:
- 2025.04.17 08:44
-
Need a robot or indicator based on this code? Order it on Freelance Go to Freelance
MQL5 Indicator for Calculating the Average Price of a Hedge Account
Introduction
This code was generated by chatgpt, but I made some adjustments and posted the video on youtube:
In trading, it's common to use hedge accounts, where you can simultaneously hold long and short positions in the same asset. A common problem for traders operating in this model is determining the average price of open positions.
This article presents an MQL5 indicator that automatically calculates the average price of open positions for a given symbol and Magic Number and displays the result on a chart.
How does the indicator work?
The indicator takes the following steps:
- Filters all openpositions in MetaTrader 4, checking the asset (symbol) and Magic Number configured by the user.
- Itseparates buy and sell trades, calculating the volume and total cost of each type.
- Calculates the weighted average price, taking into account the total volume bought and sold.
- Displays a line on the chart corresponding to the average price of the net position.
Code Explanation
1. Calculating the Average Price
The CalculateHedgeAveragePrice() function loops through all open positions and:
- Separates purchases and sales.
- Calculates the weighted average price for each direction.
- Determines whether the net position is long or short.
- Returns the corresponding average price.
2. Initialising the indicator
In the OnInit() function, we create the buffer that will store the average price for display on the chart.
3. Filling the Buffer
The OnCalculate() function updates the indicator buffer using ArrayFill() , making the code more efficient.
How to Use in MetaTrader 5
- Copy and paste the code into a new .mq5 file inside the Indicators folder.
- Compile in MetaEditor.
- Add the indicator to the chart in MetaTrader 5.
- Set the Magic Number of the trades you want to monitor.
Conclusion
This MQL5 indicator is useful for traders operating on hedge accounts, helping to monitor the average price of open positions. It can be customised for different assets and strategies.
Translated from Portuguese by MetaQuotes Ltd.
Original code: https://www.mql5.com/pt/code/57320

Better Volume is an advanced indicator designed to analyse the behaviour of volume on price charts. It combines volume information with metrics such as candlestick range and moving averages to identify important patterns in the market, such as buy/sell climaxes, churn and weak candles. With its dynamic visual interface and volume categorisation, the indicator offers valuable insights for traders who want to make informed decisions based on volume flow.

My first code on the site, which requires improvement. The idea to create an ideal tool for traders, in the work on the main component of any trading system-Risk Manager