Any rookie question, so as not to clutter up the forum. Professionals, don't pass by. Nowhere without you - 6. - page 75

 
MisterD:
Hi again, can you please tell me where there is a mistake in the following code ?
I call the loop at the moment of "deinit".
It deletes all but the last order.
I think this line is most likely an error >> for (int i=1; i<=OrdersTotal(); i++) <<
However, logically everything seems to be correct... what did I miss ?

Thanks in advance!


Do the loop backwards

//-------------------------------------------------------------------------------------------
for (int i=OrdersTotal()-1; i>=0; i--)
{
   if (OrderSelect(i,SELECT_BY_POS)==true)
   {
      int Type=OrderType();
      if (OrderSymbol()!= Symbol() || Type <2)continue;
      int Ticket=OrderTicket();
      bool Modify =OrderDelete(Ticket);
      if (Modify == true) Alert ("Ордер Удалён");continue;
      if (Modify != true) Alert ("Удаление ордера не удалась");
   }
} 
//-------------------------------------------------------------------------------------------
 
MisterD:
Hi again, can you please tell me where there is a mistake in the following code ?
I call the loop at the moment of "deinit".
It deletes all but the last order.
I think this line is most likely an error >> for (int i=1; i<=OrdersTotal(); i++) <<
However, logically everything seems to be correct... what did I miss ?

Thanks in advance!

//-------------------------------------------------------------------------------------------
for (int i=1; i<=OrdersTotal(); i++)
{
if (OrderSelect(i-1,SELECT_BY_POS)==true)
{
int Type=OrderType();
if (OrderSymbol()!= Symbol() || Type <2)continue;
int Ticket=OrderTicket();
Modify=OrderDelete(Ticket);
if (Modify == true) Alert ("Order Deleted"); continue;
if (Modify != true) Alert ("Order deleted failed");
continue;
}
}
//-------------------------------------------------------------------------------------------


Try it like this:

for(int i=OrdersTotal()-1;i>=0;i--) {
  if(OrderSelect(i,......................
 

Good afternoon. a little help is needed. there is a function that closes one of the fattest lots in terms of profit.

I need to loop it.

Please advise how to use this function, for example if my account equity is more than 5 pips then this function should work.

void ClosePosWithMaxProfitInCurrency(string sy="", int op=-1, int mn=-1) {
  double pr=0;
  int    i, k=OrdersTotal(), np=-1;

  if (sy=="0") sy=Symbol();
  for (i=k-1; i>=0; i--) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
        if (mn<0 || OrderMagicNumber()==mn) {
          if (pr<OrderProfit()+OrderSwap()) {
            pr=OrderProfit()+OrderSwap();
            np=i;
          }
        }
      }
    }
  }
  if (np>=0) {
    if (OrderSelect(np, SELECT_BY_POS, MODE_TRADES)) {
      ClosePosBySelect();
    }
  } 

}

 
sannin:

Good afternoon. a little help is needed. there is a function that closes one of the fattest lots in terms of profit.

I need to loop it.

Please advise how to use it. For example if my account equity is higher than 5 pips then this function should work.

For example, if account equity is higher than 5 pips (for 4 characters) - in real market, if number of orders is higher than 3-4, they will easily turn to minus (according to results of deletion). :)))

P.S. As far as I understand, you need a ready-made code, while the algorithm for solving such a problem is useless to you!

 
TarasBY:
5 pips (for 4 signs) - in the real market if the number of orders greater than 3-4, it is easy to turn (by results of removal) into minus. :)))


I for one... and I've been paying attention to the gold lately... it's not as fast reacting as I thought it would be

I haven't worked with functions like this yet ... that's why I'm asking for support

 
Good afternoon, I have a problem with the development of the Expert Advisor, it does not close orders, although the conditions prescribed for closing by an indicator, please explain what it is, thanks in advance
Files:
 
sannin:


Well, I for one... and I've been paying attention to the gold lately... it's not as fast as I thought it would be

I haven't worked with the functions in this form yet ... so here is a request for support

First, you calculate the BU line of an aggregate position for the instrument (it is used to determine the conditions for closing orders when they reach profitability in pips):

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII+
//|  Автор : TarasBY, taras_bulba@tut.by                                              |
//+-----------------------------------------------------------------------------------+
//|        Определение Уровня БезУбытка по символу                                    |
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII+
double fGet_BreakEven (string fs_Symbol,     // Symbol
                       double fd_DeltaLots,  // разность объемов ордеров Buy и Sell
                       double fd_Profit)     // текущий профит по открытым ордера
{
    if (fd_DeltaLots == 0.) return (0.);
    double ld_BU = 0., ld_tickvalue = MarketInfo (fs_Symbol, MODE_TICKVALUE);   // цена одного пункта
//----
    //---- Уровень общего безубытка для открытых ордеров
    if (fd_DeltaLots > 0.) ld_BU = MarketInfo (fs_Symbol, MODE_BID) - (fd_Profit / (ld_tickvalue * fd_DeltaLots)) * MarketInfo (fs_Symbol, MODE_POINT);
    else if (fd_DeltaLots < 0.) ld_BU = MarketInfo (fs_Symbol, MODE_ASK) - (fd_Profit / (ld_tickvalue * fd_DeltaLots)) * MarketInfo (fs_Symbol, MODE_POINT);
//----
    return (ld_BU);
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII+

Then calculate the difference between the current price and the Breakeven line. If the "difference" is satisfactory, we proceed to the procedure of deleting orders. The first thing to do is to find the Ticket of the most profitable/lossmaking order:

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII+
//|  Автор : TarasBY, taras_bulba@tut.by                                              |
//+-----------------------------------------------------------------------------------+
//         Получаем Тикет самого прибыльного/убыточного ордера                        |
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII+
int fGet_TicketWithControlProfit (string fs_Symbol = "",  // Инструмент
                                  int fi_Magic = -1,      // Magic
                                  int fi_Type = -1,       // OrderType
                                  int fi_TypeProfit = 0)  // Профитность ордера среди остальных ордеров:
                                                          // >= 0 -  с самым большим профитом;
                                                          // < 0 - самый убыточный
{
    double ld_Profit = 0., ld_curProfit;
    int    li_Total = OrdersTotal(), li_Ticket = -1;
//----
    if (fs_Symbol == "") fs_Symbol = Symbol();
    if (fi_TypeProfit < 0) ld_Profit = 10000000000.;
    for (int i = li_Total - 1; i >= 0; i--)
    {
        if (!OrderSelect (i, SELECT_BY_POS, MODE_TRADES)) continue;
        if (OrderSymbol() != fs_Symbol) continue;
        if (fi_Magic > -1) if (OrderMagicNumber() != fi_Magic) continue;
        if (fi_Type > -1) if (OrderType() != fi_Type) continue;
        if (fi_Type > 1) continue;
        ld_curProfit = OrderProfit() + OrderSwap() + OrderCommission();
        if (fi_TypeProfit >= 0) {if (ld_Profit <= ld_curProfit) continue;}
        else if (ld_Profit >= ld_curProfit) continue;
        ld_Profit = ld_curProfit;
        li_Ticket = OrderTicket();
    }
//----
    return (li_Ticket);
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII+

The deletion can be done in either direction. Let us delete:

//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII+
//|  Автор : TarasBY, taras_bulba@tut.by                                              |
//+-----------------------------------------------------------------------------------+
//|        Функция закрытия "своих" ордеров по признаку                               |
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII+
int fClose_AllOrdersByProfit (string fs_Symbol = "",  // Инструмент
                              int fi_Magic = -1,      // Magic
                              int fi_Type = -1,       // OrderType
                              int fi_TypeProfit = 0)  // Направление закрытия ордеро по профитности:
                                                      // >= 0 - начиная от самого большого профита;
                                                      // < 0 - начиная от самого маленького профита;
{
    int li_Ticket = fGet_TicketWithControlProfit (fs_Symbol, fi_Magic, fi_Type, fi_TypeProfit),
        li_cnt = 0;
//----
    while (li_Ticket > 0)
    {
         //---- Здесь вставите свою процедуру закрытия 
         //---- что-то типа:
         //ClosePosByTicket (li_Ticket);
         li_Ticket = fGet_TicketWithControlProfit (fs_Symbol, fi_Magic, fi_Type, fi_TypeProfit);
         li_cnt++;
    }
//----
    return (li_cnt);
}
//IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII+

It's not too difficult to assemble the right unit further: you'll have to do it or go to the job... :)

 
I want to assign a price value to a variable.

a=(NormalizeDouble(OrderOpenPrice(),5));


But for some reason, the price is drawn with a four-digit value at 1.3360

Could you tell me the mistake?
 
lottamer:
I want to assign a price value to a variable.



But for some reason, the price is drawn with a four-digit value at 1.3360

Can you tell me the error?

Maybe the DC is four-digit? Or output the price without DoubleToStr(YourPrice,5)
 
Sepulca:

Maybe the DC is four-digit? Or display the price without DoubleToStr(YourPrice,5)


The DC is five-digit...

what does it mean WITHOUT DoubleToStr(YourPrice,5) ? I don't have DoubleToStr(YourPrice,5) anywhere