Any questions from newcomers on MQL4 and MQL5, help and discussion on algorithms and codes - page 1248

 
jaffer wilson:

@ Artyom Trishkin @ Vladimir Pastushak

Doesn't work in my case. I am not asking for a new post. I know it calculates and returns margin for one type of trade. But it is useless if a trade is already open and I want to know the margin consumed by one trade of a particular symbol.

Can you help me please? If you don't understand my situation, let me know.

Simple explanation of the scenario:

You see this field in ToolBox:

This is the margin for the symbols: AUDJPY, EURUSD and EURTRY.

I know I can get this value with AccountInfoDouble (ACCOUNT_MARGIN). But I can't get the value of a specific symbol. For example, I want to know how much margin EURUSD is consuming. That I can't perform any function. Hence, I want to know how I can calculate it.

I hope this explanation is enough to make sense.

//+------------------------------------------------------------------+
#include <Trade\PositionInfo.mqh> CPositionInfo     m_position;
//+------------------------------------------------------------------+
struct str__
  {
   string            symbol;
   double            margin;
  };
str__ symb[];
//+------------------------------------------------------------------+
void OnTick()
  {

   int total = PositionsTotal(), c = 0;

   ArrayResize(symb, total);

   for(int k = total - 1; k >= 0; k--)
      if(m_position.SelectByIndex(k))
        {
         symb[c].symbol = m_position.Symbol();

         if(m_position.PositionType() == POSITION_TYPE_BUY)
            symb[c].margin = SymbolMargin(symb[c].symbol, ORDER_TYPE_BUY, m_position.Volume());

         if(m_position.PositionType() == POSITION_TYPE_SELL)
            symb[c].margin = SymbolMargin(symb[c].symbol, ORDER_TYPE_SELL, m_position.Volume());

         c++;
        }
        
        ArrayPrint(symb,2);
  }
//+------------------------------------------------------------------+
double  SymbolMargin(string aSymbol, ENUM_ORDER_TYPE aType, double aLot = 1.0)
  {

   double margin = 0;

   MqlTick m_tick;
   if(SymbolInfoTick(aSymbol, m_tick))
     {
      if(m_tick.ask != 0.0)
         if(OrderCalcMargin(aType, aSymbol, aLot, m_tick.ask, margin))
            return (margin * aLot);
      if(m_tick.bid != 0.0)
         if(OrderCalcMargin(aType, aSymbol, aLot, m_tick.bid, margin))
            return (margin * aLot);
     }

   return -1;
  }
//+------------------------------------------------------------------+
 
Vladimir Pastushak :

@Vladimir Pastushak I will try it. But let me tell you, it's not a workable solution. The value given by OrderCalcMargin () changes each time. But the margin value doesn't change. If I close any trade, the margin value will be the same as it was when I opened the trade. But when I use OrderCalcMargin (), the value I get changes and is not at all like the original value.

Please let me know if you have also tried this on your side. I know it won't work. Nevertheless, I will use it as you insist.

 
Vladimir Pastushak :

I opened a trade with a lot size of 50 in EURUSD. See what I got after using your function:

(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589500.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589505.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589495.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589490.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589485.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589490.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589485.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589480.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589475.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589470.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589465.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589455.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589460.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589465.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589460.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589455.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589440.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589440.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589440.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589445.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589450.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589460.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589465.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589460.00 

See, the margin is constantly changing. But the value of a trade already opened does not change. Can you tell me what to do now?

 
jaffer wilson:

I opened a trade with a lot size of 50 in EURUSD. See what I got after using your function:

See, the margin is constantly changing. But the value of a trade already opened does not change. Can you tell me what to do now?

//+------------------------------------------------------------------+
#include <Trade\PositionInfo.mqh> CPositionInfo     m_position;
//+------------------------------------------------------------------+
struct str__
  {
   string            symbol;
   double            margin;
  };
str__ symb[];
//+------------------------------------------------------------------+
void OnTick()
  {

   int total = PositionsTotal(), c = 0;

   ArrayResize(symb, total);

   for(int k = total - 1; k >= 0; k--)
      if(m_position.SelectByIndex(k))
        {
         symb[c].symbol = m_position.Symbol();

         if(m_position.PositionType() == POSITION_TYPE_BUY)
            symb[c].margin = SymbolMargin(symb[c].symbol, ORDER_TYPE_BUY, m_position.Volume(), m_position.PriceOpen());

         if(m_position.PositionType() == POSITION_TYPE_SELL)
            symb[c].margin = SymbolMargin(symb[c].symbol, ORDER_TYPE_SELL, m_position.Volume(), m_position.PriceOpen());

         c++;
        }

   ArrayPrint(symb, 2);
  }
//+------------------------------------------------------------------+
double  SymbolMargin(string aSymbol, ENUM_ORDER_TYPE aType, double aLot = 1.0, double aOpenPrice = 0)
  {

   double margin = 0;


   if(aOpenPrice != 0.0)
      if(OrderCalcMargin(aType, aSymbol, aLot, aOpenPrice, margin))
         return (margin * aLot);
   if(aOpenPrice != 0.0)
      if(OrderCalcMargin(aType, aSymbol, aLot, aOpenPrice, margin))
         return (margin * aLot);


   return -1;
  }
//+------------------------------------------------------------------+
 
Vladimir Pastushak :

I have tried the above code.

I got this:

(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589100.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589100.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589100.00 
(EURUSD,M1)         [symbol]  [margin]
(EURUSD,M1)     [ 0 ] "EURUSD" 589100.00 

But the expected value was this:


 
Vladimir Pastushak :

For other symbols, the margin value still changes. Hence, OrderCalcMargin () is not the solution. You got it. Try it on your side first, please.

 
jaffer wilson:

For other symbols, the margin value still changes. Hence, OrderCalcMargin () is not the solution. You got it. Try it on your side first, please.

2 orders 5891 * 2 = 11782 If you need to process the array symbol by symbol... It's not a problem...

 
Vladimir Pastushak :

2 orders 5891 * 2 = 11782 If you need to process an array character by character... It's not a problem...

But I made only one trade on EURUSD. And the function doesn't work properly with other symbols.

 

Can you tell me if there is a hook for the mql5 indicator? To set two display properties.

#property indicator_separate_window
#property indicator_chart_window

And somehow bind different output, each to its own mapping property.

 
Roman:

Can you tell me if there is a hook for the mql5 indicator? To set two display properties.

And somehow bind different output, each to its own mapping property.

There are no such hooks. Not on the right or on the left. Neither for MQL5, nor for MQL4.

Use your own constructions on the main chart from the subwindow.

Reason: