Почему не срабатывает условие or оно же ||?

 

Закрытие позиции происходит: 1.если сигнальная линия пересекает нулевую отметку или 2.сигнальная линия пересекает главную. Однако второе условие для закрытия не срабатывает.((( Подскажите почему?

//+------------------------------------------------------------------+
//|                                                  MACD Sample.mq4 |
//|                      Copyright © 2005, MetaQuotes Software Corp. |
//|                                       https://www.metaquotes.net/ |
//+------------------------------------------------------------------+
 
extern double TakeProfit = 50;
extern double Lots = 0.1;
extern double TrailingStop = 30;
extern double MACDOpenLevel=3;
extern double MACDCloseLevel=2;
extern double MATrendPeriod=26;
 
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   double MacdCurrent, MacdPrevious, SignalCurrent;
   double SignalPrevious, MaCurrent, MaPrevious;
   int cnt, ticket, total;
// initial data checks
// it is important to make sure that the expert works with a normal
// chart and the user did not make any mistakes setting external 
// variables (Lots, StopLoss, TakeProfit, 
// TrailingStop) in our case, we check TakeProfit
// on a chart of less than 100 bars
   if(Bars<100)
     {
      Print("bars less than 100");
      return(0);  
     }
   if(TakeProfit<10)
     {
      Print("TakeProfit less than 10");
      return(0);  // check TakeProfit
     }
// to simplify the coding and speed up access
// data are put into internal variables
   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);
   SignalCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,0);
   SignalPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1);
   MaCurrent=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,0);
   MaPrevious=iMA(NULL,0,MATrendPeriod,0,MODE_EMA,PRICE_CLOSE,1);
 
   total=OrdersTotal();
   if(total<1) 
     {
      // no opened orders identified
      if(AccountFreeMargin()<(1000*Lots))
        {
         Print("We have no money. Free Margin = ", AccountFreeMargin());
         return(0);  
        }
      // check for long position (BUY) possibility
      if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious && MaCurrent>MaPrevious)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,"macd sample",16384,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
      // check for short position (SELL) possibility
      if(MacdCurrent>0 && MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious && MaCurrent<MaPrevious)
        {
         ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,3,0,0,"macd sample",16384,0,Red);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("SELL order opened : ",OrderOpenPrice());
           }
         else Print("Error opening SELL order : ",GetLastError()); 
         return(0); 
        }
      return(0);
     }
   // it is important to enter the market correctly, 
   // but it is more important to exit it correctly...   
   for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&   // check for opened position 
         OrderSymbol()==Symbol())  // check for symbol
        {
         if(OrderType()==OP_BUY)   // long position is opened
           {
            // should it be closed?
            if(SignalCurrent>0 && SignalPrevious<0 || MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious)
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); // close position
                 return(0); // exit
                }
           }
         else // go to short position
           {
            // should it be closed?
            if(SignalCurrent<0 && SignalPrevious>0 || MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious)
              {
               OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); // close position
               return(0); // exit
              }
           }
        }
     }
   return(0);
  }
// the end.
 

Переставить второе условие закрытия в стоп-лосс тоже не решение. (

      if(MacdCurrent<0 && MacdCurrent>SignalCurrent && MacdPrevious<SignalPrevious && MaCurrent>MaPrevious)
        {
         ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious,0,"macd sample",16384,0,Green);
         if(ticket>0)
           {
            if(OrderSelect(ticket,SELECT_BY_TICKET,MODE_TRADES)) Print("BUY order opened : ",OrderOpenPrice());
           }
         else Print("Error opening BUY order : ",GetLastError()); 
         return(0); 
        }
 

Проанализируйте разницу в этих кодах:
в этом

(SignalCurrent>0 && SignalPrevious<0 || MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious)
и в этом
((SignalCurrent>0 && SignalPrevious<0) || (MacdCurrent<SignalCurrent && MacdPrevious>SignalPrevious))
 

Выдаёт кучу ошибок при компиляции:

Êîìïèëÿöèÿ 'MACD Sample 1.mq4'...
'||' - unexpected token D:\Forex\Straighthold Trader Test\experts\MACD Sample 1. mq4 (91, 53)
'(' - unexpected token D:\Forex\Straighthold Trader Test\experts\MACD Sample 1. mq4 (91, 56)
'<' - assignment expected D:\Forex\Straighthold Trader Test\experts\MACD Sample 1.mq4 (91, 68)
'&&' - assignment expected D:\Forex\Straighthold Trader Test\experts\MACD Sample 1.mq4 (91, 83)
'>' - assignment expected D:\Forex\Straighthold Trader Test\experts\MACD Sample 1.mq4 (91, 98)
')' - assignment expected D:\Forex\Straighthold Trader Test\experts\MACD Sample 1.mq4 (91, 113)
'else' - unexpected token D:\Forex\Straighthold Trader Test\experts\MACD Sample 1.mq4 (97, 10)
'||' - unexpected token D:\Forex\Straighthold Trader Test\experts\MACD Sample 1. mq4 (100, 53)
'(' - unexpected token D:\Forex\Straighthold Trader Test\experts\MACD Sample 1. mq4 (100, 56)
'>' - assignment expected D:\Forex\Straighthold Trader Test\experts\MACD Sample 1.mq4 (100, 68)
'&&' - assignment expected D:\Forex\Straighthold Trader Test\experts\MACD Sample 1.mq4 (100, 83)
'<' - assignment expected D:\Forex\Straighthold Trader Test\experts\MACD Sample 1.mq4 (100, 98)
')' - assignment expected D:\Forex\Straighthold Trader Test\experts\MACD Sample 1.mq4 (100, 113)
13 îøèáîê, 0 ïðåäóïðåæäåíèé

 

Прошу прощения - всё правильно.

Спасибо SK.