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

 
Link_x:

In my example: random order number, volume - 5, at close price, with slippage 0, no arrow.
In the document example: calculation of order number by order_id, volume 1, by close price, with slippage 3, arrow is red.

Clever as hell, but there's one thing, See highlighted, you write Symbol().

And the code, "How?", is simple.

for(int i=OrdersTotal()-1;i>=0;i--)  //
    {
   if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
       {
     if(OrderType()==OP_BUY) price=Bid;
     else                    price=Ask;
     OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
       }
    }    
Something like this
 
r772ra:
Somehow

Wow! Now it closes positions, but instantly. Let's do this:
if(AccountProfit() > 50)
{
for(int i=OrdersTotal()-1;i>=0;i--) 
{
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderType()==OP_BUY) price=Bid;
else                    price=Ask;
OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
}
}   
}

And get a code that closes all positions at a potential profit of $50.
Thank you very much!
Now everything is clear!
Cool! :)
 
Hello, all! Help me, smart people. I'm setting Profit=10 pips (i.e. when I get a total profit of two pairs equal to 10 pips) both open deals should be closed. However, my trades are not being closed. Where is the error?
extern double lotAU=0.01;
extern double lotEA=0.01;
extern double Profit=10;
extern string Сomment           = "KVAZ_EURAUD_AUDUSD";
extern int Magic                = 1111;

int EASell, EABuy, AUSell, AUBuy;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
double prof;
double bidEA = MarketInfo("EURAUD",MODE_BID);
double askEA = MarketInfo("EURAUD",MODE_ASK);
double bidAU = MarketInfo("AUDUSD",MODE_BID);
double askAU = MarketInfo("AUDUSD",MODE_ASK);

string symEA = "EURAUD";
string symAU = "AUDUSD";

for(int i=OrdersTotal()-1; i>=0; i--)
{
 OrderSelect(i,SELECT_BY_POS);
 prof=prof+OrderProfit();
 Print("Profit="+prof);
}
if(prof>=Profit)
{
 for(i=OrdersTotal()-1; i>=0; i--)
 {
  OrderSelect(i,SELECT_BY_POS);
  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,0);
 }
}

double LineEA = iCustom (Symbol(), 0, "Ind_2 Line+1", 0, 1); // Первый инструмент
double LineAU = iCustom (Symbol(), 0, "Ind_2 Line+1", 1, 1); // Второй инструмент

if(NumberOfPositions("EURAUD")==0 && LineEA > 0.1 && LineAU < -0.1)
    {
       EASell = OrderSend(symEA,OP_SELL,lotEA,bidEA,3,0,0,"KVAZ_EURAUD_AUDUSD",Magic,0,Red);
    }
if(NumberOfPositions("AUDUSD")==0 && LineEA > 0.1 && LineAU < -0.1)
    {
       AUSell = OrderSend(symAU,OP_SELL,lotAU,bidAU,3,0,0,"KVAZ_EURAUD_AUDUSD",Magic,0,Red);
    }
if(NumberOfPositions("EURAUD")==0 && LineEA < -0.1 && LineAU > 0.1)
    {
       EABuy = OrderSend(symEA,OP_BUY,lotEA,bidEA,3,0,0,"KVAZ_EURAUD_AUDUSD",Magic,0,Red);
    }
if(NumberOfPositions("AUDUSD")==0 && LineEA < -0.1 && LineAU > 0.1)
    {
       AUBuy = OrderSend(symAU,OP_BUY,lotAU,bidAU,3,0,0,"KVAZ_EURAUD_AUDUSD",Magic,0,Red);
    }
    
  return(0);
  }
//+------------------------------------------------------------------+

int NumberOfPositions(string sy="", int op=-1, int mn=1111) {      //|  Параметры:                                                                |
                                                                  //|    sy - наименование инструмента   (""   - любой символ,                   |
                                                                  //|                                     NULL - текущий символ)                 |
                                                                  //|    op - операция                   (-1   - любая позиция)                  |
                                                                  //|    mn - MagicNumber                (-1   - любой магик)                    
  int i, k=OrdersTotal(), kp=0;

  if (sy=="0") sy=Symbol();
  for (i=0; i<k; i++) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if (OrderSymbol()==sy || sy=="") {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (op<0 || OrderType()==op) {
            if (mn<0 || OrderMagicNumber()==mn) kp++;
          }
        }
      }
    }
  }
  return(kp);
}

Mind you, I understand that when I close, the EA just doesn't see the orders on the symbol and the meigic that it needs to close. And that's it............

Yes, there was also a variant of this. Only one order was closed, and the 10 pips profit condition was not met. In short, it was bullshit.

for(int i=OrdersTotal()-1; i>=0; i--)
{
 OrderSelect(i,SELECT_BY_POS);
 prof=prof+OrderProfit();
 Print("Profit="+prof);
}
if(prof>=Profit)
{
 for(i=OrdersTotal()-1; i>=0; i--)
 {
  OrderSelect(i,SELECT_BY_POS);
  {
  if(OrderSymbol()!= Symbol()  || OrderMagicNumber()!=Magic) continue;
  OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),5,0);
  }
 }
}


 
Hello, could you please tell me how to make an arrow appear on the chart at the current bar and at the current price when certain conditions are reached and an alert is triggered once, giving a message? For example: when a muving crosses from bottom to top, an up arrow with the message "buy" and from top to bottom a down arrow with the message "sell", respectively. Thanks in advance.
 
alexey1979621:
Hello, all! Help me, smart people. I'm setting Profit=10 pips (i.e. when I get a total profit of two pairs equal to 10 pips) both open deals should be closed. However, my trades are not being closed. Where is the error?

Mind you, I understand that when I close, the EA just doesn't see the orders on the symbol and the meigic that it needs to close. And that's it............

Yes, there was one more variant. A trade was closing only one position, while the condition of the profit of 10 points was not fulfilled. Anyway, it was bullshit.


1. We should close the position using the ask or bid of the instrument, for which it is opened

not like you do with OrderClosePrice()

write it this way

if(prof>=Profit)
   {
  for(int i=OrdersTotal()-1;i>=0;i--) 
      {
     if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
         {
     double AS=MarketInfo(OrderSymbol(), MODE_ASK);
     double BI=MarketInfo(OrderSymbol(), MODE_BID);

       if(OrderType()==OP_BUY) price=BI;
       else                    price=AS;

       OrderClose(OrderTicket(),OrderLots(),price,3,CLR_NONE);
         }
      }   
   }
Something like this
 
There is a possibility of a stop being triggered and then the flags need to be reset to their original state. Is this problem solved by removing the EA from the chart?
 
r772ra:

1. We close the position using the asc or bid of the instrument for which it is opened

not like you do with OrderClosePrice()

write it this way

Something like this

Done, but that doesn't solve the problem as applied to the above owl. I think when closing, the EA simply does not see the orders on the symbol and the majors it needs to close.
 
alexey1979621:
Hello, all! Help me, smart people. I'm setting Profit=10 pips (i.e. when I get a total profit of two pairs equal to 10 pips) both open deals should be closed. However, my trades are not being closed. Where is the error?

Mind you, I understand that when I close, the EA just doesn't see the orders on the symbol and the meigic that it needs to close. And that's it............

Yes, there was also an option like this. The deal has been closed only once and the 10 pips profit condition has not been respected. In short, it was bullshit.

Error on error. Maybe this will make something work:

extern double lotAU=0.01;
extern double lotEA=0.01;
extern double Profit=10;
extern string EXP_Comment           = "KVAZ_EURAUD_AUDUSD";
extern int Magic                = 1111;

int EASell, EABuy, AUSell, AUBuy;
string gsa_SMB[2];
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
  gsa_SMB[0] = "EURAUD";
  gsa_SMB[1] = "AUDUSD";
   return (0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
    int    li_N;
    double prof = 0., ld_Price = 0.;

    for (int i = OrdersTotal() - 1; i >= 0; i--)
    {
        OrderSelect (i,SELECT_BY_POS);
        li_N = fGet_NumSymbol (OrderSymbol(), gsa_SMB);
        if (li_N < 0) continue;
        if (OrderMagicNumber() != Magic) continue;
        prof += OrderProfit();
        Print ("Profit=", prof);
    }
    if (prof>=Profit)
    {
        for (i=OrdersTotal()-1; i>=0; i--)
        {
            OrderSelect (i, SELECT_BY_POS);
            li_N = fGet_NumSymbol (OrderSymbol(), gsa_SMB);
            if (li_N < 0) continue;
            if (OrderMagicNumber() != Magic) continue;
            if (OrderType() == OP_BUY) ld_Price = MarketInfo (gsa_SMB[li_N], MODE_BID);
            else if (OrderType() == OP_SELL) ld_Price = MarketInfo (gsa_SMB[li_N], MODE_ASK);
            OrderClose (OrderTicket(), OrderLots(), ld_Price, 5);
        }
    }
    double bidEA = MarketInfo ("EURAUD", MODE_BID),
           askEA = MarketInfo ("EURAUD", MODE_ASK),
           bidAU = MarketInfo ("AUDUSD", MODE_BID),
           askAU = MarketInfo ("AUDUSD", MODE_ASK),
           LineEA = iCustom (Symbol(), 0, "Ind_2 Line+1", 0, 1), // Первый инструмент
           LineAU = iCustom (Symbol(), 0, "Ind_2 Line+1", 1, 1); // Второй инструмент

    if (NumberOfPositions (gsa_SMB[0], -1, Magic) == 0)
    {
        if (LineEA > 0.1) if (LineAU < -0.1)
        {EASell = OrderSend (gsa_SMB[0], OP_SELL, lotEA, bidEA, 3, 0, 0, EXP_Comment, Magic, 0, Red);}
        if (LineEA < -0.1) if (LineAU > 0.1)
        {EABuy = OrderSend (gsa_SMB[0], OP_BUY, lotEA, askEA, 3, 0, 0, EXP_Comment, Magic, 0, Blue);}
    }
    if (NumberOfPositions (gsa_SMB[1], -1, Magic) == 0)
    {
        if (LineEA < -0.1) if (LineAU > 0.1)
        {AUBuy = OrderSend (gsa_SMB[1], OP_BUY, lotAU, askAU, 3, 0, 0, EXP_Comment, Magic, 0, Blue);}
        if (LineEA > 0.1) if (LineAU < -0.1)
        {AUSell = OrderSend (gsa_SMB[1], OP_SELL, lotAU, bidAU, 3, 0, 0, EXP_Comment, Magic, 0, Red);}
    }
    return (0);
}
//+------------------------------------------------------------------+
int fGet_NumSymbol (string fs_Symbol, string ar_SMB[])
{
    for (int li_IND = 0; li_IND < 2; li_IND++)
    {if (fs_Symbol == ar_SMB[li_IND]) return (li_IND);}
    return (-1);
}
//+------------------------------------------------------------------+

???

 
alexey1979621:
Done, but that doesn't solve the problem with the owl above. I think when closing, the EA just doesn't see the orders on the symbol and the majors it needs to close.


It should not see either symbol or magik, everything is correct in your EA. Look at the logs to see what kind of error it produces.
 
TarasBY:

Error on error. Maybe this will make it work:

???



Please point out, kindly, where you found "error upon error".
Reason: