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

 
Rustam Bikbulatov:

Seen and tried. Helped a lot already but there is a limit to my understanding

So you don't want the number of orders but the volume in lots?

//+------------------------------------------------------------------+
int fMarketOrdersOpenVolume(const ENUM_ORDER_TYPE order_type)
  {
   int total=OrdersTotal();
   double lots=0;
   for(int i=total-1;i>=0;i--)
     {
      if(OrderSelect(i,SELECT_BY_POS))
        {
         if(OrderType()!=order_type) continue;
         if(OrderMagicNumber()==123 && OrderSymbol()==Symbol())
            lots+=OrderLots();
        }
     }
   return lots;
  }
//+------------------------------------------------------------------+
 
Artyom Trishkin:

So you don't want the number of orders, but the volume in lots?

double fMarketOrdersOpenVolume(const ENUM_ORDER_TYPE order_type)
 
Konstantin Nikitin:

Ahrinet!!!!!! What I needed!!!!!! I never would have thought of that!!!!! Thank you so much!!!!!

 
Konstantin Nikitin:

Yeah, I missed it in my worries.

 
Thank you guys for helping us!!! Well done you all!!!
 

However, hello!

Can you advise on this situation: based on TMA_Fair I open on the boundary and close on the opposite boundary, but it does not close on the other boundary! Where is the bug in the code?

void OnTick()
{
  PriceHigh = iCustom(NULL, 0, "ExtremeTMALine st2050 v1", TimeFrame, TMAPeriod, Price, ATRMultiplier, ATRPeriod, TrendThreshold, ShowCenterLine, alertsOn, alertsMessage, alertsSound, alertsEmail, MaxBars, ShowInfo, ShowInfo_WindowNo, ShowInfo_X, ShowInfo_Y, ShowInfo_ColorLabel, ShowInfo_ColorSwing, ShowInfo_ColorUp, ShowInfo_ColorDown, 1, 0);
  PriceLow  = iCustom(NULL, 0, "ExtremeTMALine st2050 v1", TimeFrame, TMAPeriod, Price, ATRMultiplier, ATRPeriod, TrendThreshold, ShowCenterLine, alertsOn, alertsMessage, alertsSound, alertsEmail, MaxBars, ShowInfo, ShowInfo_WindowNo, ShowInfo_X, ShowInfo_Y, ShowInfo_ColorLabel, ShowInfo_ColorSwing, ShowInfo_ColorUp, ShowInfo_ColorDown, 2, 0);
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+  
  if (CountSell() == 0 && Bid >= PriceHigh)
  {
      ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, Slippage, 0, 0, "ExtremeTMALine_Robot", Magic, 0, Red);
      if (ticket>0)
      {
       SL = NormalizeDouble(Bid + StopLoss*Point, Digits);
       
       if(OrderSelect(ticket, SELECT_BY_TICKET))
          if(!OrderModify (ticket, OrderOpenPrice(), SL, 0, 0))
          Print ("Ошибка модификации ордера на продажу!!!"); 
      } else Print ("Ошибка открытия ордера на продажу!!!");
        
  }
//+------------------------------------------------------------------+  
  if(Ask <= PriceLow && CountSell() > 0)
  {
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
      if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES))
      {
        if (OrderMagicNumber() == Magic && OrderType() == OP_SELL)
            if (!OrderClose(OrderTicket(), OrderLots(), Ask, Slippage, Black))
            Print ("Ошибка закрытия ордера на продажу!!!");  
      } else Print ("Ошибка открытия ордера на продажу!!!");
    }
  }  
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+  
  
  if (CountBuy() == 0 && Ask <= PriceLow)
  {
      ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, Slippage, 0, 0, "ExtremeTMALine_Robot", Magic, 0, Blue);
      if (ticket>0)
      {
       SL = NormalizeDouble(Ask - StopLoss*Point, Digits);
       
       if(OrderSelect(ticket, SELECT_BY_TICKET))
          if (!OrderModify (ticket, OrderOpenPrice(), SL, 0, 0))
          Print ("Ошибка модификации ордера на покупку!!!");  
      } else Print ("Ошибка открытия ордера на покупку!!!");
        
  }
//+------------------------------------------------------------------+  
   if(Bid >= PriceHigh && CountBuy() > 0)
  {
    for(int i = OrdersTotal()-1; i >= 0; i--)
    {
      if (OrderSelect (i, SELECT_BY_POS, MODE_TRADES))
      {
        if (OrderMagicNumber() == Magic && OrderType() == OP_BUY)
           if(!OrderClose(OrderTicket(), OrderLots(), Bid, Slippage, Green))
           Print ("Ошибка закрытия ордера на покупку!!!"); 
      } else Print ("Ошибка открытия ордера на покупку!!!");
    }
  }
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
int CountSell()
{
 int count = 0;
     for (int trade = OrdersTotal()-1; trade>=0; trade--)
     {
          if(OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))
          {
             if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_SELL)
             count++;
          }
     }
     return (count);
}
//+------------------------------------------------------------------+
int CountBuy()
{
 int count = 0;
     for (int trade = OrdersTotal()-1; trade>=0; trade--)
     {
          if(OrderSelect(trade, SELECT_BY_POS, MODE_TRADES))
          {
             if (OrderSymbol() == Symbol() && OrderMagicNumber() == Magic && OrderType() == OP_BUY)
             count++;
          }
     }
     return (count);
}
//+------------------------------------------------------------------+
 
Domovoi44:

However, hello!

Can you advise on this situation: based on TMA_Fair I open on the boundary and close on the opposite boundary, but it does not close on the other boundary! Where is the bug in the code?

If you follow the indicator closely, you can see that it redraws 10 or more bars backwards. This is the whole reason.

 
Alexey Viktorov:

If you follow the indicator closely, you will notice that it redraws 10 bars or more backwards. This is the whole reason.

Ehhhhhhh man!!! I'll keep digging) Thanks for the help!!!
 
Can you tell me if there is a pit volume divergence indicator for MT-4?
 

Good afternoon, please help! I'm trying to write a lot count control. If my account becomes +10% profit, the lot is doubled. If +20%, then lot*4. If in my account decreases, then respectively decreases the lot size. I do not understand how to make Lots_New take a new value at each new trade.

double Bal= AccountBalance();
double GetLots()
{
double Lots_New;
double Lots = 0.1;
   
   if(Bal <=AccountBalance()+0.1*AccountBalance()){
   Lots_New=Lots*2;
   }
   if(Bal >=AccountBalance()+0.1*AccountBalance()){
   Lots_New=Lots/2;
   }
   if(Bal ==AccountBalance()){
   Lots_New=Lots;
   }

Alert("Lot new= ",Lots_New);
return (Lots_New);
}
Reason: