Download MetaTrader 5
To add comments, please log in or register
Are you accustomed to using the standard library? MQL5 has one!
Michael Anthony Lobrij
11
Michael Anthony Lobrij 2015.04.01 17:26 

How can I show in MT4 the total LOT of all open sell orders (but not the pending orders)?

Below script works perfectly to see the amount of sell/buy orders. But now I'd like to modify so I only see the total sell LOT size. Could you please help me?

EA code: (this works for counting amount of orders, but now it needs to count the amount of total sell LOT)

int OnInit()
  {
//---
      // this is number of orders not the ammount
      // we can solve the ammount later.
      int nBuyOrders = 0;
      int nSellOrders = 0;
     
      for(int i=0; i < OrdersTotal(); i++)
      {
       
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
         {
            // match the symbol EUR/USD or EUR/CHF, etc
            if(OrderSymbol() == Symbol())
            {
               if(OrderType() == OP_BUY)
                  nBuyOrders++;
               if(OrderType() == OP_SELL)
                  nSellOrders++;
            }
         }   
      }
     
      string sBuyText = "Total Buy = ";
      sBuyText += nBuyOrders;
      string sSellText = "Total Sell = ";
      sSellText += nSellOrders;
     
      ObjectCreate("ObjName", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("ObjName", sBuyText, 7, "Verdana", Yellow);
      ObjectSet("ObjName", OBJPROP_CORNER, 0);
      ObjectSet("ObjName", OBJPROP_XDISTANCE, 20);
      ObjectSet("ObjName", OBJPROP_YDISTANCE, 20);
     
      ObjectCreate("ObjName2", OBJ_LABEL, 0, 0, 0);
      ObjectSetText("ObjName2", sSellText, 7, "Verdana", Yellow);
      ObjectSet("ObjName2", OBJPROP_CORNER, 0);
      ObjectSet("ObjName2", OBJPROP_XDISTANCE, 20);
      ObjectSet("ObjName2", OBJPROP_YDISTANCE, 40);
     
    
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---

  }


Below code we could implement in above EA. Do you see anything interesting to use?

int total=OrdersTotal();

double totalots=0;

for(int 1=0; i<total; i++)

{ OrderSelect(i, SELECT_BY_POS, MODE_TRADES)

totalots+=OrderLots(); //this gives the total no of lots opened in current ordes

}


Lots=(AccountBalance()/(CurrencyLotSize/100))/(Margin_Per_Lot/1000);

   Sum( OrderOpenPrice() * OrderLots() ) / Sum( OrderLots() ) = Weighted Average OrderOpenPrice()


if(OrderType()==OP_BUY)

for (int i = 0; i < OrdersTotal(); i++)
{
 if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == FALSE) break;//If no order then get outta here!

if(OrderSymbol() == Symbol() && OrderMagicNumber() == Magic)
{

if (OrderType() == OP_BUY)

}

Drazen Penic
2404
Drazen Penic 2015.04.01 18:34  

You need OrderLots() function. Something like this:

 

      double totalBuyLots = 0;
      double totalSellLots = 0;
      
      for(int i=0; i < OrdersTotal(); i++)
      {
        
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
         {
            // match the symbol EUR/USD or EUR/CHF, etc
            if(OrderSymbol() == Symbol())
            {
               if(OrderType() == OP_BUY)
                  totalBuyLots += OrderLots();
               if(OrderType() == OP_SELL)
                  totalSellLots += OrderLots();
            }
         }    
      }

 

In the indicators folder you will find "iExposure" indicator. It contains code for counting and summing orders. Check it out.

As a note, you should probably build this project as indicator instead of EA. Also, OnInit executes only when you start your EA. Lot summing code should go to the OnTick() function.

Michael Anthony Lobrij
11
Michael Anthony Lobrij 2015.04.02 19:25  

Hi Drazen, thank you. Thanks to you it's working now. This thread is closed. Below is the working code to check the lot size of all buy orders. And seperately all sell orders:


//+------------------------------------------------------------------+
//|                                                        testt.mq4 |
//|                        Copyright 2015, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2015, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
void PrintText(string object, string text, int x, int y)
{
   ObjectCreate(object, OBJ_LABEL, 0, 0, 0);
   ObjectSetText(object, text, 9, "Verdana", Yellow);
   ObjectSet(object, OBJPROP_CORNER, 0);
   ObjectSet(object, OBJPROP_XDISTANCE, x);
   ObjectSet(object, OBJPROP_YDISTANCE, y);
}

void CalculateLots()
{
      double totalBuyLots = 0;
      double totalSellLots = 0;
     
      for(int i=0; i < OrdersTotal(); i++)
      {
         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == true)
         {
            //if(OrderSymbol() == Symbol())
            {
               if(OrderType() == OP_BUY)
                  totalBuyLots += OrderLots();
               if(OrderType() == OP_SELL)
                  totalSellLots += OrderLots();
            }
         }   
      }
     
      string sBuyText = "Total Buy = ";
      sBuyText += DoubleToString(totalBuyLots, 2);
      string sSellText = "Total Sell = ";
      sSellText += DoubleToString(totalSellLots, 2);
     
      PrintText("BuyObject", sBuyText, 20, 20);
      PrintText("SellObject", sSellText, 20, 40);
}

int OnInit()
  {
//---
   CalculateLots();
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
  
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
      CalculateLots();
  }
//+------------------------------------------------------------------+

  


Automated Trading and Strategy Testing
Automated Trading and Strategy Testing
  • www.mql5.com
MQL5: language of trade strategies built-in the MetaTrader 5 Trading Platform, allows writing your own trading robots, technical indicators, scripts and libraries of functions
/
To add comments, please log in or register