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

 
evillive:

Why count pending orders? And who is BU();?

It's a strange method to "separate" own positions from others, isn't it better to

BU(); is a Breakeven function.

What's wrong with my variant?

сюда пересчёт рыночных позиций и отложек
What would it look like in my case?
 
alexey1979621:
What would it look like in my case?

The same as it looked, just a couple more curly braces added:


void CountTrades() // количество открытых ордеров
   {
    for(int i=OrdersTotal()-1; i>=0; i--) 
    {
     if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
     { 
      if (OrderSymbol()=Symbol() && OrderMagicNumber()=Magic)//отделяем свои ордера. Магик задается в настройках
      {
      int typ=OrderType();      //однократный вызов функции ускоряет работу
      switch (typ)
      {
       case 0: bs++;
       case 1: ss++;
       case 2: blms++;
       case 3: slms++;
       case 4: bsts++;
       case 5: ssts++;
       default: break;
      }
      }        
     }
    }
    return;  
   }
 
Well guys:)
 
evillive:

The same as it looked, just a couple more curly brackets will be added:


I corrected, compiled, tested - the problem remains - When a certain condition is reached, the Expert Advisor opens a trade and closes it at Take or Stop Loss. At this point, the Expert Advisor does not open deals anymore, despite the fact that the conditions for opening deals are fulfilled.

Here is the code in full.

extern double  Lots             = 0.1;
extern string Сomment           = "Pattern_1";
extern int TakeProfit           = 10;     
extern int StopLoss             = 0;   
extern int Step                 = 2;   
extern int StepOtl              = 4;   


extern int BULevel              = 2;
extern int   NotBULevel         = 2;         // Уровень безубытка в пунктах

extern int Slippage             = 2; // проскальзывание 
extern int Magic                = 111;

int ticket1, ticket2, bs, ss, bsts, ssts, slms, blms;//добавил колич. ордеров по типам и их тикеты
int timeprev;
double price1, price2; //цены открытия ордеров

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
{
      return(0);
}

//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
{
 double SL,TP;
 int slv=MarketInfo(Symbol(),MODE_STOPLEVEL);
 if(Step<=slv || StepOtl<=slv) {Print("Step или StepOtl слишком мал"); return(0);}
 
 CountTrades();   //подсчет ордеров по типам.
 BU();
 
 //если нет рыночных ордеров-----------------------------------------
 if(bs+ss+bsts+ssts+blms+slms==0)                                     
 {
  if (Open[1]>Close[1] && Open[2]<Close[2] && High[1]>High[2] && Low[1]<Low[2])  // продажа
  {
   TP=NormalizeDouble(Bid - TakeProfit * Point, Digits);  
   SL=NormalizeDouble(Bid + StopLoss*Point,Digits);
   if(TakeProfit==0) TP=0;
   if(StopLoss==0) SL=0;                       
   ticket1=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,SL,TP,"Pattern_1",Magic,0,Red);//Сразу с тейк-профитом, магик в настройках
   if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES)) price1=OrderOpenPrice();//цена первого ордера
   SL=NormalizeDouble(Bid+StepOtl*Point-StopLoss*Point,Digits);
   if(StopLoss==0) SL=0;
   ticket2=OrderSend(Symbol(),OP_BUYSTOP,Lots,NormalizeDouble(Bid+StepOtl*Point,Digits),0,SL,0,"Pattern_1",Magic,0,Blue);//тикет для BuyStop
   if(OrderSelect(ticket2,SELECT_BY_TICKET,MODE_TRADES)) price2=OrderOpenPrice();//цена второго ордера
  }
  
  if (Open[1]<Close[1] && Open[2]>Close[2] && High[1]>High[2] && Low[1]<Low[2]) // покупка
  {
   TP=NormalizeDouble(Ask + TakeProfit * Point, Digits); 
   if(TakeProfit==0) TP=0;
   SL=NormalizeDouble(Ask-StopLoss*Point,Digits);
   if(StopLoss==0) SL=0;
   ticket1=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,SL,TP,"Pattern_1",Magic,0,Blue);//с тейком, магик в настройках 
   if(OrderSelect(ticket1,SELECT_BY_TICKET,MODE_TRADES)) price1=OrderOpenPrice();//цена первого ордера
   SL=NormalizeDouble(Bid-StepOtl*Point+StopLoss*Point,Digits);
   if(StopLoss==0) SL=0;
   ticket2=OrderSend(Symbol(),OP_SELLSTOP,Lots,NormalizeDouble(Bid-StepOtl*Point,Digits),0,SL,0,"Pattern_1",Magic,0,Red);//тикет для SellStop
   if(OrderSelect(ticket2,SELECT_BY_TICKET,MODE_TRADES)) price2=OrderOpenPrice();//цена второго ордера
  }
 }
 

 return(0);
}
     
 //+------------------------------------------------------------------+
void CountTrades() // количество открытых ордеров
   {
    for(int i=OrdersTotal()-1; i>=0; i--) 
    {
     if (OrderSelect(i, SELECT_BY_POS,MODE_TRADES))
     { 
      if (OrderSymbol()==Symbol() && OrderMagicNumber()==Magic)//отделяем свои ордера. Магик задается в настройках
      {
      int typ=OrderType();      //однократный вызов функции ускоряет работу
      switch (typ)
      {
       case 0: bs++;
       case 1: ss++;
       case 2: blms++;
       case 3: slms++;
       case 4: bsts++;
       case 5: ssts++;
       default: break;
      }        
     }
    }
    }
    return;  
   }
 //+------------------------------------------------------------------+

void BU()
{
 for(int a=OrdersTotal()-1; a>=0; a--)   
 {
  if (OrderSelect(a, SELECT_BY_POS, MODE_TRADES)) 
  {      
   if(OrderMagicNumber()!=Magic  || OrderSymbol()!=Symbol()) continue;
   int typ=OrderType();                                                 //вызываем функции
   int tic=OrderTicket();                                               //один раз
   double oop=OrderOpenPrice();                                         //это ускоряет работу
   double otp=OrderTakeProfit();                                        //советника
   double osl=OrderStopLoss();
   
   if(typ==OP_BUY) 
   {
    if(oop<=NormalizeDouble(Bid-BULevel*Point-NotBULevel*Point,Digits) && oop>osl)// последнеее условие БУ + 20 пипсов
    OrderModify(tic,oop,NormalizeDouble(oop+NotBULevel*Point,Digits),otp,0,Green);
    OrderDelete(ticket2,Yellow);
   }       
 
   if(typ==OP_SELL) 
   {
    if(oop>=NormalizeDouble(Ask+BULevel*Point+NotBULevel*Point,Digits) && (oop<osl || osl==0))// последнеее условие БУ + 20 пипсов
    OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-NotBULevel*Point,OrderTakeProfit(),0,Red);  
    OrderDelete(ticket2,Yellow);
   } 
  }
 }
 return;
}
 
artmedia70:
About good - that's a pitchfork in the water. If it's fast, it's also a bit of a stretch... And if it's cheap, see point 1.

Then it goes like this:

1. Good is a bit of a stick in the mud. 2.About fast, it's also a two-edged sword... 3. And if it's cheap - see point 1.

Now I see!

 
alexey1979621:

Fixed, compiled, tested - the problem remains - When a certain condition occurs, the Expert Advisor opens a trade and closes it at Take or Stop Loss. At this point, the Expert Advisor does not open deals anymore, despite the fact that the conditions for opening deals are fulfilled.

Here is the code in full.


Where does it count?

bs+ss+bsts+ssts+blms+slms
They're global. And they don't seem to reset until they're checked. And their calculation before the check - where?
 
alexey1979621:

Fixed, compiled, tested - the problem remains - When a certain condition occurs, the Expert Advisor opens a trade and closes it at Take or Stop Loss. At this point, the Expert Advisor does not open deals anymore, despite the fact that the conditions for opening deals are fulfilled.

Here is the code in full.


BU re-do it, you can't delete market positions, and because of a frozen stop there won't be any more positions ))))
 
alexey1979621:

Fixed, compiled, tested - the problem remains - When a certain condition occurs, the Expert Advisor opens a trade and closes it at Take or Stop Loss. At this point, the Expert Advisor does not open deals anymore, despite the fact that the conditions for opening deals are fulfilled.

Here is the code in full.



       case 0: bs++;   break;
       case 1: ss++;   break;
       case 2: blms++; break;
       case 3: slms++; break;
       case 4: bsts++; break;
       case 5: ssts++; break;
       default: break;
 
evillive:
BU redo it, you can't delete market positions, and because of a hanging stop there won't be any more positions )))


Let the gurus comment on whether the BU() function needs break (commented out) or not, and let alexey1979621 think, whether this function is called there?

Also, if CountTrades(); function is used like this, it's equal to OrdersTotal(), simpler and faster.


void BU()
{
 bool bu=false;
 for(int a=OrdersTotal()-1; a>=0; a--)   
 {
  if (OrderSelect(a, SELECT_BY_POS, MODE_TRADES)) 
  {      
   if(OrderMagicNumber()==Magic  || OrderSymbol()==Symbol())
   {
   int typ=OrderType();                                                 //вызываем функции
   int tic=OrderTicket();                                               //один раз
   double oop=OrderOpenPrice();                                         //это ускоряет работу
   double otp=OrderTakeProfit();                                        //советника
   double osl=OrderStopLoss();
   
   if(typ==OP_BUY) 
   {
    if(oop<=NormalizeDouble(Bid-BULevel*Point-NotBULevel*Point,Digits) && oop>osl)// последнеее условие БУ + 20 пипсов
    OrderModify(tic,oop,NormalizeDouble(oop+NotBULevel*Point,Digits),otp,0,Green);
    bu=true;
    //break;
   }       
 
   if(typ==OP_SELL) 
   {
    if(oop>=NormalizeDouble(Ask+BULevel*Point+NotBULevel*Point,Digits) && (oop<osl || osl==0))// последнеее условие БУ + 20 пипсов
    OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-NotBULevel*Point,OrderTakeProfit(),0,Red);  
    bu=true;
    //break;
   } 
   if(bu==true && (typ==OP_BUYSTOP || typ==OP_SELLSTOP))
   {
    OrderDelete(tic,Yellow); 
    bu=false;
   }       
   }
  }
 }
 return;
}
 
evillive:

Let the gurus comment whether a break is needed here or not, and let alexey1979621 think, is this function called there?

Also, if we use CountTrades(); so, it's equal to OrdersTotal(), easier and faster.

I'm not a guru, but I'll answer: all cases will be executed until a break; occurs:
Reason: