Closing of positions. On indicator signal. - page 3

 
goldtrader:

firstly, it is incorrect to compare real Stochast_ and integer 75, and secondly, it is more correct to compare their difference to zero.

I think in this situation an error of 8 decimal places will not affect the result.
 

Another problem arises with the Position Closing Block. I put this block in my favourite Expert Advisor. It only opens deals strictly in the direction of the trend (my congratulations to it for that...)

But depending on the strength of the trend, the Expert Advisor is (and does it!) consistently able to open several deals, - one after another! This is where I need the block of closing positions at the signal of the indicator. But here is the problem! When and if several positions are opened within a trend and correction against the trend starts, the indicator in the block gives a signal to close. As I need it.

However, for some reason, not all open positions are closed. Basically, only the earliest one. Those which have been opened later do not react to the closing signal - but continue movement. And further, - "history repeats itself like a farce"! With each closing signal, only one, the earliest open position is closed, and the others continue to move! The lot of all positions is constant.

Here is the visual mode chart. Closing by the signal of the indicator is marked with a green triangle. It is clearly seen that only one position is closed each time by the signal. I would like all open positions to be closed! Please advise me, what is the reason?

A reminder of code :

/********* Закрытие позиций ****************************************
if (AutoClose) {  
//----------------------------------------------------------------------
if (ExpertOrder(MagicLong)) {   //есть открытые ордера на покупку
      if(Stochast_1>=Up_lim && Stochast_0<Up_lim)     {
        OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);//закрываем позицию
                 return(0); // выходим         
              }  }
 //--------------------------------------------------------
if (ExpertOrder(MagicShort)) {   //есть открытые ордера на продажу
      if(Stochast_1<=Low_lim && Stochast_0>Low_lim)    {
        OrderClose(OrderTicket(),OrderLots(),Ask,3,Green);//закрываем позицию
                 return(0); // выходим
              }  } 
 //-------------------------------------------------------                       
}  //if (AutoClose)
 
rid:

Another problem arises with the Closing Block...

Where is the loop on OrdersTotal() ?
 
granit77:
Where is the loop on OrdersTotal() ?
Even if there is one, it is from 0 to OrdersTotal()-1, and it should be the other way round ;)
 

No, that's not the case here. Orders are defined - differently:

//---------------------------------------------------------------------------------+
//---- Функция проверки наличия ордеров эксперта ----------------------------------+
//---------------------------------------------------------------------------------+
// false - ордеров данного эксперта нет;                                           |
// true  - есть ордера данного эксперта;                                           |
//---------------------------------------------------------------------------------+
bool ExpertOrder (int VersionID){
bool result=false;
int  _OrdersTotal=OrdersTotal();
if (_OrdersTotal>0) { for (int jk=0; jk<_OrdersTotal; jk++) {    
      if (OrderSelect(jk, SELECT_BY_POS, MODE_TRADES)) {
        if (OrderMagicNumber()==VersionID) 
          { result=true;  break; }//Найден ордер принадлежащий эксперту
        else  { result=false; } 
       } } } else { result=false; }  
//---------------------        
return(result);}

And this version of the code is more comfortable for me! Because my long and short positions open independently of each other.

//---------проверяем условие на покупку----------------------------
if (LONG) {     //если  "выключатель" включен
 if  ( ... ... ...      )
   {
  ticket=OrderSend(Symbol(),0,Lot,Ask,Slippage,Bid-SL*Point,
                                        Ask+TP*Point,NULL,MagicLong,0,CLR_NONE); 
 

However, it's still the same with the standard closure. ...

if (AutoClose) {  
//----переменные для закрытия позиций ----
double Stochast_0 =iStochastic(NULL,0,Stochastic_period,3,3,MODE_SMA,0,MODE_MAIN,0);
double Stochast_1 =iStochastic(NULL,0,Stochastic_period,3,3,MODE_SMA,0,MODE_MAIN,1);
//----------------------------------------------------------------------
  for (int v=0; v<OrdersTotal(); v++)                             {       
      if (OrderSelect(v, SELECT_BY_POS, MODE_TRADES))               {           
        if (OrderSymbol()==Symbol()&& OrderMagicNumber()==Magic)   { 
//-----------------------------------------------------                  
if (OrderType() == OP_BUY) { 
      if(Stochast_1>=Up_lim && Stochast_0<Up_lim)     {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,Green); // закрываем позицию
                 return(0); // выходим         
              }       
             }  
 //--------------------------------------------------------
if (OrderType() == OP_SELL) { 
      if(Stochast_1<=Low_lim && Stochast_0>Low_lim)    {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Green); // закрываем позицию
                 return(0); // выходим       
              }       
             }  
 //-------------------------------------------------------                       
    }  // Symbol()  
  } // select
 } //total
} //Close_
 
rid:

However, it's still the same with the standard closure. ...

if (AutoClose) {  
//----переменные для закрытия позиций ----
double Stochast_0 =iStochastic(NULL,0,Stochastic_period,3,3,MODE_SMA,0,MODE_MAIN,0);
double Stochast_1 =iStochastic(NULL,0,Stochastic_period,3,3,MODE_SMA,0,MODE_MAIN,1);
//----------------------------------------------------------------------
  for (int v=0; v<OrdersTotal(); v++)                             {       
      if (OrderSelect(v, SELECT_BY_POS, MODE_TRADES))               {           
        if (OrderSymbol()==Symbol()&& OrderMagicNumber()==Magic)   { 
//-----------------------------------------------------                  
if (OrderType() == OP_BUY) { 
      if(Stochast_1>=Up_lim && Stochast_0<Up_lim)     {
         OrderClose(OrderTicket(),OrderLots(),Bid,3,Green); // закрываем позицию
                 return(0); // выходим         
              }       
             }  
 //--------------------------------------------------------
if (OrderType() == OP_SELL) { 
      if(Stochast_1<=Low_lim && Stochast_0>Low_lim)    {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Green); // закрываем позицию
                 return(0); // выходим       
              }       
             }  
 //-------------------------------------------------------                       
    }  // Symbol()  
  } // select
 } //total
} //Close_

You may want to try doing a reverse loop from OrdersTotal to 0. That should help.
 

How to do it? Please write, if it's not too much of a burden. Here's the chart by the way - with the above "standard": Everything is the same - only one position closes on the signal of the indicator -

 
rid:

How do you do that?

According to komposter and Vinin, instead of:
for (int v=0; v<OrdersTotal(); v++)

write:
for (int v=OrdersTotal()-1; v>0; v--)

It works for me.
 
granit77:
rid:

How do you do it?

If komposter and Vinin are to be believed, instead:
for (int v=0; v<OrdersTotal(); v++)

write:
for (int v=OrdersTotal()-1; v>0; v--)

It works for me.

You're absolutely right. Although KimV has scripts and libraries which implement all this. Maybe you should look for it on his forum.