average_price_v_1_0

Download script
Author: Joca
average_price_v_1_0

This tool is designed to help traders visualize their current market position by calculating and displaying a real-time "Average Price" level on their trading chart. Instead of making you do the manual math for multiple open positions, this script automatically aggregates your data to show you exactly where your break-even point or primary price target lies based on your total exposure.

How it Works

The logic behind the script follows a three-step process:

1. Data Collection The tool scans all your currently open trades. It identifies which trades belong to the specific currency pair you are viewing and separates them into "Buy" and "Sell" categories. For each category, it calculates:

  • The total number of open orders.
  • The total volume (lot size) traded.
  • The total financial result (including profit, swaps, and commissions).

2. Mathematical Calculation Once the data is gathered, the script performs a net calculation. It determines your "Net Lots" (the difference between your total buys and sells) and your "Net Result."

Using these figures, it calculates a specific distance from the current market price. This distance represents how many pips away your average position sits relative to your current profit or loss. It then identifies the exact price level where your net position balances out.

3. Visual Display Finally, the script provides two visual aids on your chart:

  • The Average Price Line: A horizontal line is drawn automatically. This line changes color based on your net exposure:
    • Blue: Indicates a positive net position (Buy-leaning).
    • Red: Indicates a negative net position (Sell-leaning).
    • White: Indicates a perfectly balanced position (Net Lots are zero).
  • The Information Label: A text box appears on your screen providing a snapshot of your current status. It shows the Average Price, the distance in pips, your total profit/loss in your account currency, your net lot size, and the total number of active orders.

Key Benefits

By using this logic, you can quickly see if your overall position is "in the green" or "in the red" at a glance. It simplifies complex portfolio management by consolidating multiple trades into a single visual reference point, helping you make faster decisions on when to take profit or cut losses based on your total net exposure rather than looking at individual trades one by one.

Orders Execution
Checks for the total of open orders
9 Views
0 Downloads
0 Favorites
average_price_v_1_0
//+------------------------------------------------------------------+
//|                                           Average Price v1.0.mq4 |
//|                                        Joca - nc32007a@gmail.com |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Joca"
#property indicator_chart_window

extern color font_color=Navy;
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 Buy_Profit;

   int Total_Sell_Trades;
   double Total_Sell_Size;
   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;

   for(int i=0;i<OrdersTotal();i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
        {
         if(OrderType()==OP_BUY && OrderSymbol()==Symbol())
           {
            Total_Buy_Trades++;
            Total_Buy_Size+=OrderLots();
            Buy_Profit+=OrderProfit()+OrderSwap()+OrderCommission();
           }
         if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
           {
            Total_Sell_Trades++;
            Total_Sell_Size+=OrderLots();
            Sell_Profit+=OrderProfit()+OrderSwap()+OrderCommission();
           }
        }
     }

   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)
     {
      if(Net_Lots!=0)
        {
         distance=(Net_Result/(MathAbs(Net_Lots)*MarketInfo(Symbol(),MODE_TICKVALUE)))*MarketInfo(Symbol(),MODE_TICKSIZE);
         Average_Price=Bid-distance;
        }
      else
        {
         distance=(Net_Result*Point)/MarketInfo(Symbol(),MODE_TICKVALUE);
         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,x+20);
      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",font_color);
     }

   return(0);
  }
//+------------------------------------------------------------------+

Comments