Closing of positions. On indicator signal. - page 2

 

In my case it was "<=", but Stochastic values change very strongly even with every tick - there are four more decimal places, i.e. on one tick the values may change from (for example) 75.0003 to 74.0900.

So, the sign "=" can be omitted without any fear. But of course, in order not to think - I think I will.

 
Looked at the expert - everything seems to be correct. Couldn't check it - no indicator.
Try adding Print before OrderClose and see if the close signal appears.
 

Done. Added. No, .....! The Closing Notice doesn't appear in the logbook.

//********* Закрытие позиций ****************************************
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()==MagicNum)   { 
//-----------------------------------------------------                  
if (OrderType() == OP_BUY) { 
      if(Stochast_1>=75 && Stochast_0<75)     {
         Print ( OrderClose(OrderTicket(),OrderLots(),Bid,3,Green)); // закрываем позицию
                 return(0); // выходим
         
              }       
     }  
 //--------------------------------------------------------
if (OrderType() == OP_SELL) { 
      if(Stochast_1<=25 && Stochast_0>25)    {
               Print  (OrderClose(OrderTicket(),OrderLots(),Ask,3,Green)); // закрываем позицию
                 return(0); // выходим
       
              }       
     }  
 //-------------------------------------------------------                       
    }  // Symbol()  
  } // select
 } //total
} //Close_ 
 //****************************************************************************

I don't get it! I put this block in another EA. And a third. ... . But the situation has not changed! Positions are not closed. Although seldom, rarely (once or twice on the history = 1 year) and flashes a green triangle close on the visual chart. But it should not be like that! Positions, judging by the code, should close like a sewing machine shuttle!

The NOnLagMA indicator is uploaded

Files:
 

Maybe this block shouldn't work? After all, we have stops in the code when we open: - stoploss and takeprofit. And maybe all other closing conditions are ignored for this reason?

ticket=OrderSend(Symbol(),0,Lots,Ask,Slippage,Bid-SL_long*Point,Ask+TP_long*Point, NULL,MagicNum, 0,CLR_NONE);

I'll have to deal with it in some other way.

 
rid:

I don't understand anything!

The position closing block was inside the "if(OrdersTotal() == 0)" clause
You should be more attentive.)

Here is the start() function of the test variant (without SL and TP, with correct messages and without unnecessary slip):
int start()
  {
 
 if(Time[0] <= prevtime) 
       return(0);
 prevtime = Time[0];
  if (UseTrailing) TrailPositions();   
//----
   double NLg_Signal_0 =iCustom(NULL,0,"NonLagMA_v5",0,NLg_Signal,1,0,0,0,0,0,0); 
   double NLg_Signal_1 =iCustom(NULL,0,"NonLagMA_v5",0,NLg_Signal,1,0,0,0,0,0,1); 
   double NLg_long0    =iCustom(NULL,0,"NonLagMA_v5",0,NLg_buy,0,0,0,0,DV_buy,0,0);
   double NLg_short0   =iCustom(NULL,0,"NonLagMA_v5",0,NLg_sell,0,0,0,0,DV_sell,0,0);
 
  if (OrdersTotal()   == 0)//если нет открытых позиций
  { 
//---------проверяем условие на покупку-----------------      
       if   ( 
              (NLg_Signal_1<=NLg_long0)  &&
              (NLg_Signal_0>NLg_long0)  )
       { 
              ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,Slippage,0.0,0.0, NULL,MagicNum,0,CLR_NONE);
              if(ticket < 0)  { Print("Ошибка #", GetLastError(), " при открытии ордера BUY" ); prevtime = Time[1]; }
       }
    //---------проверяем условие на продажу----------------- 
         if  (   
              (NLg_Signal_1>=NLg_short0)  &&
              (NLg_Signal_0<NLg_short0)  )
       { 
              ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,Slippage,0.0,0.0, NULL,MagicNum,0,CLR_NONE);
              if(ticket < 0)  { Print("Ошибка #", GetLastError(), " при открытии ордера SELL" ); prevtime = Time[1]; }
       }
   }
//------------------------------------------------------------------
//********* Закрытие позиций ****************************************
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()==MagicNum)   { 
    //-----------------------------------------------------                  
    if (OrderType() == OP_BUY) { 
          if(Stochast_1>75 && Stochast_0<75)     {
               OrderClose(OrderTicket(),OrderLots(),Bid,3,Green); // закрываем позицию
                     return(0); // выходим
                    }       
         }  
     //--------------------------------------------------------
    if (OrderType() == OP_SELL) { 
          if(Stochast_1<25 && Stochast_0>25)    {
                     OrderClose(OrderTicket(),OrderLots(),Ask,3,Green); // закрываем позицию
                     return(0); // выходим
                    }       
         }  
 //-------------------------------------------------------                       
    }  // Symbol()  
  } // select
 } //total
} //Close_ 
 //****************************************************************************   
   return(0);
      }



But I wouldn't use this EA on a real account if I were you. It should be put in order, or in "readiness", if you like ;)
 

Yes, of course. Thank you for your help! I understand your comment about my mistake.

And have taken note of your recommendations. There's still a long way to go before real trading...

p.s/ Everything is working.... ! And how it worked!

 
Profits are understandably not increased by such closures. But the drawdown..... Often decreases significantly.
 
komposter:
I haven't looked at the code.
But one thing is for sure: instead of "if(Stochast_1>75 && Stochast_0<75)" I would make "if(Stochast_1>75 && Stochast_0<=75)".

A better way would be: if(Stochast_1-75.0>0 && Stochast_0-75.0<=0) in order to avoid unnecessary conversions of the int and double types.
 
And how do we avoid unnecessary int and double. ? I can't think of any. Moreover, we may replace numbers with external parameters - extern int Up_lim = 80; extern int Low_lim = 20;
 
rid:
And how do we avoid unnecessary int and double. ? I can't figure it out. Besides, instead of numbers we may use external parameters extern int Up_lim = 80; extern int Low_lim = 20;

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

'Comparison of real numbers'.

Reason: