average_price_v_3_0
Download scriptThis tool is designed to provide traders with a clear, real-time visual overview of their portfolio's performance and positioning on a specific currency pair. Instead of forcing you to manually calculate the impact of multiple open positions, the script automatically analyzes your active trades and highlights your "Average Price" directly on the chart.
Core Functionality
The logic operates by scanning all your currently open trades for the specific symbol you are viewing. It distinguishes between your "Buy" (Long) positions and your "Sell" (Short) positions to calculate several key metrics simultaneously:
- Position Totals: It calculates the total number of trades, the total volume (lots) used, and the cumulative profit/loss (including swaps and commissions).
- Weighted Average Calculation: Rather than just looking at the entry price of a single trade, it calculates a weighted average based on your position sizes. This tells you the "break-even" point or the mathematical center of your current exposure.
- Net Exposure: It determines your "Net Lots"—subtracting your Sell volume from your Buy volume to see your actual directional bias (whether you are net long, net short, or neutral).
Visual Indicators
To make this information easy to digest at a glance, the script translates these complex calculations into two visual elements on your screen:
- Dynamic Average Line: The script draws a solid horizontal line on your chart representing the calculated Average Price.
- The line automatically changes color based on your net position: Blue for a net Buy, Red for a net Sell, and White if your positions are perfectly balanced (Net Zero).
- Real-Time Information Label: A text display appears on the chart that updates instantly. This label provides a summary of:
- The exact Average Price.
- The current distance in Pips from the market price.
- Your total Profit/Loss in your account currency.
- Your current Net Lots and total number of Open Orders.
How it Benefits the Trader
By automating these calculations, the script removes the guesswork from managing complex portfolios. It allows you to see exactly how far "in the money" or "out of the money" your total combined position is at any given second, helping you make faster, more informed decisions regarding take-profit or stop-loss levels.
//+------------------------------------------------------------------+
//| Average Price v3.0.mq4 |
//| Joca - nc32007a@gmail.com |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Joca"
#property indicator_chart_window
//---
extern color font_color=White;
extern int font_size=12;
//---
int PipAdjust,NrOfDigits;
double point;
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void OnInit()
{
ObjectDelete("Average_Price_Line_"+Symbol());
ObjectDelete("Information_"+Symbol());
//---
NrOfDigits=Digits;
//---
if(NrOfDigits==5 || NrOfDigits==3) PipAdjust=10;
else
if(NrOfDigits==4 || NrOfDigits==2) PipAdjust=1;
//---
point=Point*PipAdjust;
//---
return;
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
void deinit()
{
ObjectDelete("Average_Price_Line_"+Symbol());
ObjectDelete("Information_"+Symbol());
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
int Total_Buy_Trades;
double Total_Buy_Size;
double Total_Buy_Price;
double Buy_Profit;
//---
int Total_Sell_Trades;
double Total_Sell_Size;
double Total_Sell_Price;
double Sell_Profit;
//---
int Net_Trades;
double Net_Lots;
double Net_Result;
//---
double Average_Price;
double distance;
double Pip_Value=MarketInfo(Symbol(),MODE_TICKVALUE)*PipAdjust;
double Pip_Size=MarketInfo(Symbol(),MODE_TICKSIZE)*PipAdjust;
//---
int total=OrdersTotal();
//---
for(int i=0;i<total;i++)
{
int ord=OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
{
if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
{
Total_Buy_Trades++;
Total_Buy_Price+= OrderOpenPrice()*OrderLots();
Total_Buy_Size += OrderLots();
Buy_Profit+=OrderProfit()+OrderSwap()+OrderCommission();
}
if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
{
Total_Sell_Trades++;
Total_Sell_Size+=OrderLots();
Total_Sell_Price+=OrderOpenPrice()*OrderLots();
Sell_Profit+=OrderProfit()+OrderSwap()+OrderCommission();
}
}
}
if(Total_Buy_Price>0)
{
Total_Buy_Price/=Total_Buy_Size;
}
if(Total_Sell_Price>0)
{
Total_Sell_Price/=Total_Sell_Size;
}
Net_Trades=Total_Buy_Trades+Total_Sell_Trades;
Net_Lots=Total_Buy_Size-Total_Sell_Size;
Net_Result=Buy_Profit+Sell_Profit;
//---
ObjectDelete("Average_Price_Line_"+Symbol());
ObjectDelete("Information_"+Symbol());
//---
if(Net_Trades>0 && Net_Lots!=0)
{
distance=(Net_Result/(MathAbs(Net_Lots*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE));
if(Net_Lots>0)
{
Average_Price=Bid-distance;
}
if(Net_Lots<0)
{
Average_Price=Ask+distance;
}
}
if(Net_Trades>0 && Net_Lots==0)
{
distance=(Net_Result/((MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE));
Average_Price=Bid-distance;
}
ObjectDelete("Average_Price_Line_"+Symbol());
ObjectCreate("Average_Price_Line_"+Symbol(),OBJ_HLINE,0,0,Average_Price);
ObjectSet("Average_Price_Line_"+Symbol(),OBJPROP_WIDTH,3);
//---
color cl=Blue;
if(Net_Lots<0) cl=Red;
if(Net_Lots==0) cl=White;
//---
ObjectSet("Average_Price_Line_"+Symbol(),OBJPROP_COLOR,cl);
ObjectCreate("Information_"+Symbol(),OBJ_LABEL,0,0,0);
//---
int x,y;
ChartTimePriceToXY(0,0,Time[0],Average_Price,x,y);
//---
ObjectSet("Information_"+Symbol(),OBJPROP_XDISTANCE,220);
ObjectSet("Information_"+Symbol(),OBJPROP_YDISTANCE,y);
ObjectSetText("Information_"+Symbol(),"Avrg= "+DoubleToStr(Average_Price,NrOfDigits)+" "+DoubleToStr(distance/(point),1)+" pips ("+Net_Result+" "+AccountInfoString(ACCOUNT_CURRENCY)+") Lots= "+Net_Lots+" Orders= "+Net_Trades,font_size,"Arial",White);
//---
return(0);
}
//+------------------------------------------------------------------+
Comments