CurrencyPositions

Download script
CurrencyPositions

This tool is designed to provide traders with a clear, real-time overview of their net currency exposure across all open forex positions. Instead of looking at individual trades one by one, it aggregates your entire portfolio to show you exactly how much of each specific currency you are currently "long" or "short."

How the Logic Works

1. Portfolio Scanning The script begins by scanning every active trade currently open in your account. It looks at all types of orders, including standard market positions as well as pending orders (like Buy Limits, Sell Stops, etc.).

2. Trade Direction and Volume Analysis For every position identified, the script determines two key pieces of information:

  • Direction: It identifies if you are buying or selling. A "Buy" is treated as a positive value, while a "Sell" is treated as a negative value.
  • Size: It multiplies that direction by the number of lots used in the trade to calculate the total volume of that specific position.

3. Currency Identification The script specifically looks at Forex currency pairs (formatted as three letters for the base currency and three letters for the quote currency, such as EUR/USD). It breaks these pairs down into their two individual components.

4. Net Position Calculation This is the core logic of the tool. For every trade, it performs a "balancing" calculation:

  • It adds the volume to the total for the first currency in the pair (the Base).
  • It subtracts the volume from the total for the second currency in the pair (the Quote).

By doing this across all trades, the script automatically offsets overlapping currencies. For example, if you have a trade that involves USD as both a base and a quote currency, the script will net those figures out to show your actual remaining exposure to that currency.

5. Visual Reporting Finally, the script compiles these totals into a clean, organized list on your chart display. It provides a summary of various major and minor currencies (including USD, EUR, GBP, JPY, CHF, CAD, AUD, NZD, SGD, HKD, RUR, DKK, NOK, SEK, ZAR, and MXN), allowing you to see your total net exposure at a glance.

Orders Execution
Checks for the total of open orders
7 Views
0 Downloads
0 Favorites
CurrencyPositions
//+------------------------------------------------------------------+
//|                                            CurrencyPositions.mq4 |
//|                                                      Forex only! |
//|                                                          By: ZOR |
//+------------------------------------------------------------------+

#property indicator_chart_window
#property indicator_buffers 1
//---- indicator parameters
double ExtMapBuffer[];
//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   int total = OrdersTotal();
   
   double USD=0,EUR=0,GBP=0,JPY=0,CHF=0,CAD=0,AUD=0,NZD=0,SGD=0,HKD=0,RUR=0,DKK=0,NOK=0,SEK=0,ZAR=0,MXN=0; // Add if more needed
   
   for(int i = total - 1; i >= 0; i--) 
      {
         OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
         string instrument = OrderSymbol();
         int side = OrderType();
         double lot = OrderLots();
         double position;
         string X1, X2;

         if (side == OP_BUY || side == OP_BUYLIMIT || side == OP_BUYSTOP) position = 1;
         if (side == OP_SELL || side == OP_SELLLIMIT || side == OP_SELLSTOP) position = -1;
         position = position * lot;
         
         X1 = StringSubstr(instrument,0,3);
         X2 = StringSubstr(instrument,3,6);
         
         if(StringLen(instrument)==6) // Pre-check forex symbols
            {
            if(X1=="USD") {USD = USD + position;}
            if(X1=="EUR") {EUR = EUR + position;}
            if(X1=="GBP") {GBP = GBP + position;}
            if(X1=="JPY") {JPY = JPY + position;}
            if(X1=="CHF") {CHF = CHF + position;}
            if(X1=="CAD") {CAD = CAD + position;}
            if(X1=="AUD") {AUD = AUD + position;}
            if(X1=="NZD") {NZD = NZD + position;}
            if(X1=="SGD") {SGD = SGD + position;}
            if(X1=="HKD") {HKD = HKD + position;}
            if(X1=="RUR") {RUR = RUR + position;}
            if(X1=="DKK") {DKK = DKK + position;}
            if(X1=="NOK") {NOK = NOK + position;}
            if(X1=="SEK") {SEK = SEK + position;}
            if(X1=="ZAR") {ZAR = ZAR + position;}
            if(X1=="MXN") {MXN = MXN + position;} // Add if more needed

            if(X2=="USD") {USD = USD - position;}
            if(X2=="EUR") {EUR = EUR - position;}
            if(X2=="GBP") {GBP = GBP - position;}
            if(X2=="JPY") {JPY = JPY - position;}
            if(X2=="CHF") {CHF = CHF - position;}
            if(X2=="CAD") {CAD = CAD - position;}
            if(X2=="AUD") {AUD = AUD - position;}
            if(X2=="NZD") {NZD = NZD - position;}
            if(X2=="SGD") {SGD = SGD - position;}
            if(X2=="HKD") {HKD = HKD - position;}
            if(X2=="RUR") {RUR = RUR - position;}
            if(X2=="DKK") {DKK = DKK - position;}
            if(X2=="NOK") {NOK = NOK - position;}
            if(X2=="SEK") {SEK = SEK - position;}
            if(X2=="ZAR") {ZAR = ZAR - position;}
            if(X2=="MXN") {MXN = MXN - position;} // Add if more needed
           }
      }
      
   string data = "\n" + "Net currency positions:" + "\n" +
            "USD = " + DoubleToStr(USD,2) + " \n" +
            "EUR = " + DoubleToStr(EUR,2) + " \n" +
            "GBP = " + DoubleToStr(GBP,2) + " \n" +
            "JPY = " + DoubleToStr(JPY,2) + " \n" +
            "CHF = " + DoubleToStr(CHF,2) + " \n" +
            "CAD = " + DoubleToStr(CAD,2) + " \n" +
            "AUD = " + DoubleToStr(AUD,2) + " \n" +
            "NZD = " + DoubleToStr(NZD,2) + " \n" +
            "SGD = " + DoubleToStr(SGD,2) + " \n" +
            "HKD = " + DoubleToStr(HKD,2) + " \n" +
            "RUR = " + DoubleToStr(RUR,2) + " \n" +
            "DKK = " + DoubleToStr(DKK,2) + " \n" +
            "NOK = " + DoubleToStr(NOK,2) + " \n" +
            "SEK = " + DoubleToStr(SEK,2) + " \n" +
            "ZAR = " + DoubleToStr(ZAR,2) + " \n" +
            "MXN = " + DoubleToStr(MXN,2) + " \n"; // Add if more needed
            
   Comment(data);
   
   return(0);
  }

Comments