Closing of positions. On indicator signal.

 

Good evening! A problem has arisen.

I need to close a position. By the signal of the indicator. In addition, I need to be able to close the position by Stop Loss or Take Profit. (Just in case the indicator signal is too late. - This often happens):

if                    (... ... ...  )         
   {
  ticket=OrderSend(Symbol(),0,Lot,Ask,Slippage,Bid-SL*Point,Ask+TP*Point,
                                               NULL,MAGIC ,0,CLR_NONE);

Subsequently, in order to close the open positions I stipulate:

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>75 &&  Stochast_0<75)     {
                   OrderClose(OrderTicket(),OrderLots(),Ask,3,Green); 
                 return(0); // выходим
                }       
               }   
 //--------------------------------------------------------
if (OrderType() == OP_SELL) { ... .... ....

However, positions are still closed only at stoploss or takeprofit. (with a very rare - single exception!)

Although the indicator on the chart shows - that almost all open positions should be closed just by the signal of the indicator. Please tell me - where there may be a mistake ... ?

 

I usually implement this kind of functionality:

//+----------------------------------------------------------------------------+
//|  Управление позициями                                                      |
//+----------------------------------------------------------------------------+
void ManagePositions() {
  double sl=0, tp=0;
  int    ms[2];
 
  ArrayInitialize(ms, 0);
  GetTradeSignal(ms);
  if (ExistPositions("", -1, Magic)) {
    if (ms[1]>0) ClosePositions("", OP_BUY , Magic);
    if (ms[1]<0) ClosePositions("", OP_SELL, Magic);
  } else {
    if (ms[0]>0) {
      if (StopLoss>0) sl=Ask-StopLoss*Point; else sl=0;
      if (TakeProfit>0) tp=Ask+TakeProfit*Point; else tp=0;
      OpenPosition(OP_BUY, sl, tp, Magic);
    }
    if (ms[0]<0) {
      if (StopLoss>0) sl=Bid+StopLoss*Point; else sl=0;
      if (TakeProfit>0) tp=Bid-TakeProfit*Point; else tp=0;
      OpenPosition(OP_SELL, sl, tp, Magic);
    }
  }
}
As you can see, functions are written that perform quite specific actions. And then these functions are combined in such a way as to implement the desired tactics for working with positions.
 
Probably something with Stochast_1, Stochast_0, everything else is fine
 
Thank you, KIM IV. I'll try to figure it out.
 
Integer:
Probably something with Stochast_1, Stochast_0, everything else is fine


No..... The stochasticks are fine. (work on opening prices)

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);
 
Buy is closed with a bid. In this case, the close is triggered only if a quote that differs from the specified spread happens to be in the latest quote stream. If such a quote has not yet managed (by old age) to leave the list of quotes considered by the broker, then the close order has a chance to be executed.
 

Did I understand you correctly? The Buy position should close

OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);

And the Sell position must be closed:

OrderClose(OrderTicket(),OrderLots(),Ask,3,Green); ?
 
rid:

Did I understand you correctly?

You got it right in general.

 
Exactly! The problem is in Ask
 

Still don't want to close positions by stochastic! I did it right! I inserted a switch in the external parameters. At the end of the code - separate block for closing positions! I have provided a green triangle for closing.

The tactics is simple: crossing of the channel borders formed by slow MAs with deviations up and down. The code is very simple. Below is an Expert Advisor.

хотел выложить весь код - не принимет сервер! - пишет больше допустимого
//********* Закрытие позиций ****************************************
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_

May someone have a look at it? And tell me what's wrong....

Maybe when opening an order the bids and askchi are inaccurately set? I've always been confused with this:

ticket=OrderSend(Symbol(),... ....
Files:
 
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)".
Reason: