RETAIL_NETTING_MONEY

Download script
Author: Copyright 2020, MetaQuotes Software Corp.

This script is designed for the MetaTrader 4 platform to provide traders with a clear visual breakdown of their current open positions. Instead of just showing individual trades, it calculates several key mathematical averages and "weights" to help you understand your overall exposure and the break-even points for your portfolio.

Core Functionality

The script automatically scans all currently active trades on your account for a specific currency symbol. It then performs three main types of calculations:

1. Weighted Average Price (The "Net Weight" Line) The script identifies your net position by subtracting your total sell volume from your total buy volume. It then calculates the average entry price based on that net quantity.

  • Visual Output: This is displayed as a Gold horizontal line on your chart.
  • Purpose: This tells you exactly where your "true" average entry point is relative to your current net exposure.

2. Portfolio Average Price (The "Total Volume" Line) This calculation looks at the average entry price of all trades, regardless of whether they are buys or sells, based on their total volume.

  • Visual Output: This is displayed as a Lime horizontal line on your chart.
  • Purpose: It helps you see the general price level where the majority of your active capital was deployed.

3. Central Equilibrium (The "Mean" Line) The script finds the highest and lowest entry prices among all your open positions and calculates the exact midpoint between them.

  • Visual Output: This is displayed as a Snow/White horizontal line on your chart.
  • Purpose: This provides a visual "center of gravity" for your current trading zone, showing the balanced middle point between your most successful and least successful entries.

Advanced Features & Notifications

  • Swap and Commission Awareness: The script is capable of accounting for swap rates and commissions in its internal logic to provide a more accurate picture of the costs associated with holding positions.
  • Dynamic Updates: As you open or close trades, the lines on your chart will automatically move, update, or disappear depending on whether you still have active positions.
  • Profit Alerts: If the script detects a specific profit target (defined in the settings) and your current positions meet that criteria, it will trigger an audible and visual alert on your screen.
  • Detailed Logging: For those who prefer to monitor data in the "Experts" tab, the script prints detailed technical data regarding trade counts, average prices, and swap differences for every run.

Summary for the User

This tool is a portfolio management aid. It transforms a list of complicated individual trades into three simple visual markers. By looking at these lines, you can instantly see your "break-even" zones and understand the overall balance of your trading strategy at a glance.

Orders Execution
Checks for the total of open orders
Miscellaneous
It issuies visual alerts to the screen
10 Views
0 Downloads
0 Favorites
RETAIL_NETTING_MONEY
ÿþ//+------------------------------------------------------------------+

//|                                                        MONEY.mq4 |

//|                        Copyright 2020, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

//+------------------------------------------------------------------+

#property copyright "Copyright 2020, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

#property script_show_inputs

input bool calcswap=true;//€Q†¹pî]ÓNo`

input double calcoutprice=0.0;//¡‹—{÷N<h)R¦m

//+------------------------------------------------------------------+

//| Script program start function                                    |

//+------------------------------------------------------------------+

void OnStart()

  {

//---

   double buy_lots = 0,sell_lots=0;

   double price_sum = 0;

   double value_sum = 0;

   double comiss_sum = 0;

   double calc_profit_sum =0;

   double max = DBL_MIN;

   double min = DBL_MAX;

   datetime now=TimeCurrent();

   int total=OrdersTotal();

   int num = 0;

   double diffs[][2];

   ArrayResize(diffs,total);

   ArrayInitialize(diffs,0.0);

//------

   for(int i = total - 1; i >= 0; i--)

     {

      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

         if(OrderSymbol()==_Symbol)

           {

            double price=OrderOpenPrice();

            if(OrderType() == OP_BUY)

              {

               if(calcoutprice>0 && OrderProfit())

                  calc_profit_sum+=OrderSwap()+OrderProfit()*(calcoutprice-price)/(Bid - price);

               if(calcswap)

                 {

                  diffs[num][0] = OrderProfit() ? OrderSwap() * (Bid - price) /OrderProfit() : Ask - Bid;

                  diffs[num][1] = (int)(now-OrderOpenTime())/PeriodSeconds(PERIOD_D1);//iBarShift(_Symbol,PERIOD_D1,(datetime)OrderOpenTime());

                 }

               buy_lots +=OrderLots();

               value_sum +=OrderLots()*(price-diffs[num][0]);

              }

            if(OrderType() == OP_SELL)

              {

               if(calcoutprice>0 && OrderProfit())

                  calc_profit_sum+=OrderSwap()+OrderProfit()*(calcoutprice-price)/(Ask - price);

               if(calcswap)

                 {

                  diffs[num][0]  = OrderProfit() ? OrderSwap() * (Ask - price) /OrderProfit() :  Bid-Ask;

                  diffs[num][1] = (int)(now-OrderOpenTime())/PeriodSeconds(PERIOD_D1);

                 }

               sell_lots +=OrderLots();

               value_sum -=OrderLots()*(price-diffs[num][0]);

              }

            if(OrderType()<2 && price!=0)

              {

               max = MathMax(max,price);

               min = MathMin(min,price);

               price_sum += price;

               comiss_sum += OrderCommission();

               num++;

              }

           }

     }

   for(int i=num-1; i>=0; i--)

      printf("[%d],%.0f,%f",i,diffs[i][1],diffs[i][0]/_Point);

//-----------

   string name_0=_Symbol+"_weight";

   string name_1=_Symbol+"_avgp";

   string name_2=_Symbol+"_mean";

   if(buy_lots!=sell_lots)

     {

      double quantity=buy_lots-sell_lots;

      double avgp=NormalizeDouble(value_sum /quantity,_Digits);// total_lots

      printf("1:%f,%f,%f",quantity,avgp,comiss_sum/(buy_lots+sell_lots));



      if(ObjectFind(0,name_0)<0)

        {

         ObjectCreate(0,name_0,OBJ_HLINE,0,0,avgp);

         ObjectSetInteger(0,name_0,OBJPROP_COLOR,clrGold);

         ObjectSetInteger(0,name_0,OBJPROP_STYLE,DRAW_SECTION);

        }

      else

         ObjectMove(name_0,0,0,avgp);

     }

   else

      ObjectDelete(0,name_0);

   if(calc_profit_sum!=0)

      Alert(StringFormat("price %f %s's profit:%f",calcoutprice,_Symbol,calc_profit_sum));

//--------------

   if(num > 0)

     {

      double avgp=NormalizeDouble(price_sum / num,_Digits);

      double mean=NormalizeDouble((max + min) / 2.0,_Digits);

      printf("2:%d,%f,%f,",num,avgp,mean);



      if(ObjectFind(0,name_1)<0)

        {

         ObjectCreate(0,name_1,OBJ_HLINE,0,0,avgp);

         ObjectSetInteger(0,name_1,OBJPROP_COLOR,clrLime);

         ObjectSetInteger(0,name_1,OBJPROP_STYLE,DRAW_SECTION);

        }

      else

         ObjectMove(name_1,0,0,avgp);



      if(ObjectFind(0,name_2)<0)

        {

         ObjectCreate(0,name_2,OBJ_HLINE,0,0,mean);

         ObjectSetInteger(0,name_2,OBJPROP_COLOR,clrSnow);

         ObjectSetInteger(0,name_2,OBJPROP_STYLE,DRAW_SECTION);

        }

      else

         ObjectMove(name_2,0,0,mean);

     }

   else

     {

      ObjectDelete(0,name_1);

      ObjectDelete(0,name_2);

     }

  }

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

Comments