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

 
Roger:

Please point out, kindly, where you found "mistake upon mistake"?
Have your eyes swollen already? I hint: see order opening prices (other than the closing price already mentioned earlier).
 
TarasBY:
Have your eyes swollen yet? Hint: see order opening prices (other than the closing price already mentioned earlier).


And more details.
 
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);
    }
 
It's not "not opening", it's "not closing" orders. The closing code is correct.
 



extern double Lots       = 0.1;
extern int TakeProfit    = 50;
extern int Step          = 50;
extern double Multipler  = 2;
extern int Slippage      = 5;
extern int Magic         = 123;

int ticket;
double price, TP, lastlot;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {

   

   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
     if ( CountTrades()==0)
     {
       double ima = iMA(Symbol(),0,14,0,MODE_SMA,PRICE_CLOSE,1);
       
       if (Ask>ima)
       {
         ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0,0,"",Magic,0,Blue);
         if (ticket>0)
         {
            TP = NormalizeDouble (Ask+TakeProfit*Point,Digits);
            OrderModify(ticket,OrderOpenPrice(),0,TP,0); 
         }
       }
       else if (Bid<ima)
       ticket = OrderSend(Symbol(),OP_SELL,Lots,Ask,Slippage,0,0,"",Magic,0,Red);
       if (ticket>0)
         {
            TP = NormalizeDouble (Bid-TakeProfit*Point,Digits);
            OrderModify(ticket,OrderOpenPrice(),0,TP,0); 
         }
       }
       
// закрыты все скобки, кроме первой, которая начинает цикл "старт". На этот момент открыт 1й ордер, дальше в
// случае потребности доливаемся.
       else
       {
         int order_type = FindLastOrderType();
         if (order_type = OP_BUY)
         {
           price = FindLastPrice (OP_BUY);
           if (Ask <= price - Step*Point)
           {
             lastlot = FindLastLots (OP_BUY);
             lastlot = NormalizeDouble (lastlot* Multipler,2);
             ticket = OrderSend(Symbol(),OP_BUY,lastlot,Ask,Slippage,0,0,"",Magic,0,Blue);
             if (ticket>0)
               ModifyOrders (OP_BUY);
           }
         }
         else if (order_type = OP_SELL()
         {
           price = FindLastPrice (OP_SELL);
           if (Bid <= price + Step*Point)
           {
             lastlot = FindLastLots (OP_SELL);
             lastlot = NormalizeDouble (lastlot* Multipler,2);
             ticket = OrderSend(Symbol(),OP_SELL,lastlot,Bid,Slippage,0,0,"",Magic,0,Blue);
             if (ticket>0)
               ModifyOrders (OP_SELL);
           }
         }
       }  
    
   return(0);
  }
//+------------------------------------------------------------------+
void ModifyOrder(int otype)
{
  double avgprice = 0,
         order_lots = 0;
         
         price = 0;
  for (int i = OrdersTotal()-1; i>=0; i--)
  {
    if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
    {
       if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType() == otype)
       {
          price +=OrderOpenPrice() * OrderLots();
          order_lots +=OrderLots(); 
       } 
    }
  }
  avgprice = NormalizeDouble (price / order_lots, Digits);
  if (otype == OP_BUY) TP = NormalizeDouble (avgprice + TakeProfit * Point,Digits);
  if (otype == OP_SELL) TP = NormalizeDouble (avgprice - TakeProfit * Point,Digits);
  
  for (i = OrdersTotal()-1; i>=0; i--)
  {
    if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
    {
       if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType() == otype)
       
         OrderModify(OrderTicket(), OrderOpenPrice(),0,TP,0);
       }
  }
  
}
//+------------------------------------------------------------------+
double FindLastLots (int otype)
{
  double oldopenprice,oldlots;
  int oldticket;
  
  ticket = 0;
  
  for (int i = OrdersTotal()-1; i>=0; i--)
  {
    if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES)) 
    {
      if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType() == otype)
      {
         oldticket = OrderTicket();
         if (oldticket>ticket)
         {
            oldlots = OrderLots();
            ticket = oldticket; 
         } 
      }  
    }
  }
  return (oldlots);
}
//+------------------------------------------------------------------+
double FindLastPrice (int otype)
{
  double oldopenprice;
  int oldticket;
  
  ticket = 0;
  
  for (int i = OrdersTotal()-1; i>=0; i--)
  {
     if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
     {
       if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic && OrderType() == otype)
       {
         oldticket = OrderTicket();
         if (oldticket>ticket)
         {
           oldopenprice = OrderOpenPrice();
           ticket = oldticket;
         }
       }
     } 
  }
  return (oldopenprice);
}
//+------------------------------------------------------------------+
int FindLastOrderType()
{
  for (int i = OrdersTotal()-1; i>=0; i--)
  {
    if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
    {
     if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
       return (OrderType()); 
    }
  }
  return (-1);
}
//+------------------------------------------------------------------+
int CountTrades()
{
  int count = 0;
  for (int i = OrdersTotal()-1; i>=0; i--)
  {
    if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
    {
      if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)
      count++;
    }
  }
  return (count);
}
//+------------------------------------------------------------------+
Guys, really please help me find a bug in the board, it won't compile.
 
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.

The variant is simple (for tests) and works, it sees everything.
 
TarasBY:
Thanks, took your code apart. There is indeed an error on opening (my eye is already wet). I have redesigned it on the demo where there are already opened positions on our Meijic with total positive profit. However, the position has not closed.
 
Roger:

It shouldn't see the symbol or the magician, your EA says it correctly. Look at the logs to see what error it produces.
The logs are... With your comments, the EA code is like this.
extern double lotEU=0.01;
extern double lotGU=0.01;
extern double Profit=12;
extern string Сomment           = "KVAZ_EURUSD_GBPUSD";
extern int Magic                = 1114;

int EUSell, EUBuy, GUSell, GUBuy;
double price;
//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
double prof;
double bidEU = MarketInfo("EURUSD",MODE_BID);
double askEU = MarketInfo("EURUSD",MODE_ASK);
double bidGU = MarketInfo("GBPUSD",MODE_BID);
double askGU = MarketInfo("GBPUSD",MODE_ASK);

string symEU = "EURUSD";
string symGU = "GBPUSD";



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);
         }
      }   
   }


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

if(NumberOfPositions("EURUSD")==0 && LineEU > 0.1 && LineGU < -0.1)
    {
       EUSell = OrderSend(symEU,OP_SELL,lotEU,bidEU,3,0,0,"KVAZ_EURUSD_GBPUSD",Magic,0,Red);
    }
if(NumberOfPositions("GBPUSD")==0 && LineEU > 0.1 && LineGU < -0.1)
    {
       GUBuy = OrderSend(symGU,OP_BUY,lotGU,bidGU,3,0,0,"KVAZ_EURUSD_GBPUSD",Magic,0,Blue);
    }
if(NumberOfPositions("EURUSD")==0 && LineEU < -0.1 && LineGU > 0.1)
    {
       EUBuy = OrderSend(symEU,OP_BUY,lotEU,askEU,3,0,0,"KVAZ_EURUSD_GBPUSD",Magic,0,Blue);
    }
if(NumberOfPositions("GBPUSD")==0 && LineEU < -0.1 && LineGU > 0.1)
    {
       GUSell = OrderSend(symGU,OP_SELL,lotGU,askGU,3,0,0,"KVAZ_EURUSD_GBPUSD",Magic,0,Red);
    }
    
  return(0);
  }
//+------------------------------------------------------------------+

int NumberOfPositions(string sy="", int op=-1, int mn=1114) {      //|  Параметры:                                                                |
                                                                  //|    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);
}
 
Trader7777:
Guys, please help me very much to find a bug in the board, it doesn't compile.


Look at this line

else if(order_type = OP_SELL()

 
alexey1979621:
Thanks, took your code apart. There is indeed an error on opening (my eye is already soaking wet). I put the modified EA to a demo where there are already open positions on our Meijic with total positive profit. However, the position has not closed.

I see in the new code (which is below in the post) the default parameter passed to the function has changed:

int NumberOfPositions(string sy="", int op=-1, int mn=1114)

The original version had a different Magik. I've corrected the reference to this function in my code. Look under which Magik orders are open now and set this Magik number in your EA.

P.S. And using different encodings when naming variables is not a sign of good form... I mean:

extern string Сomment           = "KVAZ_EURUSD_GBPUSD";