学习逻辑 - 页 4

 

以下是该指标的原始start()函数

int start()
{
   int limit;
   double a;
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
   limit = Bars - counted_bars;
   for(int i = 0; i < limit; i++)
    {
      for(int j = 0; j < nPeriod; j++)
       {
         a = a + (iHigh(NULL, 0, i + j) + iLow(NULL, 0, i + j) + iClose(NULL, 0, i + j) * 2) / 4;
       }       
      MaBuffer[i]  =  a / nPeriod;
      a = 0;
      if(iClose(NULL, 0, i) > MaBuffer[i])
       {
         MaTUp[i] = MaBuffer[i] + iATR(NULL, 0, nPeriod, i) * Deviation;
         MaTDn[i] = MaBuffer[i] - iATR(NULL, 0, nPeriod, i);
       }  
      else if(iClose(NULL, 0, i) < MaBuffer[i])
       {
         MaTDn[i] = MaBuffer[i] - iATR(NULL, 0, nPeriod, i) * Deviation;
         MaTUp[i] = MaBuffer[i] + iATR(NULL, 0, nPeriod, i);
       } 
      else if(iClose(NULL, 0, i) == MaBuffer[i])
       {
         MaTDn[i] = MaBuffer[i] - iATR(NULL, 0, nPeriod, i) * Deviation;
         MaTUp[i] = MaBuffer[i] + iATR(NULL, 0, nPeriod, i) * Deviation;
       }  
    }  
   //-----
   return(0);
}

该函数采用计算平均数的方式。它需要被替换成一个标准功能。

它看起来像这样

//=================================================================================================
// Замена расчета среднего на стандартную функцию
//=================================================================================================
//   Старый вариант расчета
//      for(int j = 0; j < nPeriod; j++)
//       {
//         a = a + (iHigh(NULL, 0, i + j) + iLow(NULL, 0, i + j) + iClose(NULL, 0, i + j) * 2) / 4;
//       }       
//      MaBuffer[i]  =  a / nPeriod;
//      a = 0;
//=================================================================================================
//   Новый вариант расчета

      MaBuffer[i]=iMA(NULL, 0, nPeriod, 0,MODE_SMA,PRICE_WEIGHTED,i);
//=================================================================================================

附上带有修正的指标变体

附加的文件:
 

现在谈谈指标本身的逻辑(我并不是说提议的版本是理想的)。

我只是更喜欢它

      // Вариант два. Убираем избыточные условия  и делаем одно обращение к функции
      
      atr=iATR(NULL, 0, nPeriod, i);
      MaTDn[i] = MaBuffer[i] - atr * Deviation;
      MaTUp[i] = MaBuffer[i] + atr * Deviation;

      if(iClose(NULL, 0, i) > MaBuffer[i])
       {
         MaTDn[i] = MaBuffer[i] - atr;
       }  
      else if(iClose(NULL, 0, i) < MaBuffer[i])
       {
         MaTUp[i] = MaBuffer[i] + atr;
       } 
附加的文件:
 
这是个好例子。只是它更多地是关于优化而不是逻辑。
 
denis_orlov:
一个很好的例子。但这更多是关于优化而不是逻辑。


还有逻辑,思维的逻辑。

第三种选择是完全放弃指标中的逻辑条件。问题出现了--这是否可能。

让我们试试

为此,让我们添加几个逻辑变量

现在启动函数的全部代码是

int start()
{
   int limit;
   double atr;
   bool bUP, bDN;
   int counted_bars = IndicatorCounted();
   if(counted_bars < 0) return(-1);
   if(counted_bars > 0) counted_bars--;
   limit = Bars - counted_bars;
   for(int i = 0; i < limit; i++)
    {

      MaBuffer[i]=iMA(NULL, 0, nPeriod, 0,MODE_SMA,PRICE_WEIGHTED,i);

      // Вариант три. 
      
      atr=iATR(NULL, 0, nPeriod, i);
      bUP=Close[i] < MaBuffer[i];
      bDN=Close[i] > MaBuffer[i];
      MaTDn[i] = MaBuffer[i] - atr - atr * (Deviation - 1.0) * bUP;
      MaTUp[i] = MaBuffer[i] + atr + atr * (Deviation - 1.0) * bDN;

    }  
   //-----
   return(0);
}
附加的文件:
 

如果(counted_bars < 0) 返回(-1)。

这句话是基于什么逻辑出现的?

 
Roger:

如果(counted_bars < 0) 返回(-1)。

这句话是基于什么逻辑出现的?


这不是我的线。这是作者的

start()的最佳代码

int start()
{
   int limit;
   double atr;
   int counted_bars = IndicatorCounted();
   
   limit = Bars - counted_bars-1;
   if(Bars - counted_bars > 2) limit = Bars - nPeriod-1;

   for(int i = limit; i >=0; i--)
    {
      MaBuffer[i]=iMA(NULL, 0, nPeriod, 0,MODE_SMA,PRICE_WEIGHTED,i);
      atr=iATR(NULL, 0, nPeriod, i);

      MaTDn[i] = MaBuffer[i] - iATR(NULL, 0, nPeriod, i) * Deviation;
      MaTUp[i] = MaBuffer[i] + iATR(NULL, 0, nPeriod, i) * Deviation;

      if(iClose(NULL, 0, i) > MaBuffer[i])
       {
         MaTDn[i] = MaBuffer[i] - iATR(NULL, 0, nPeriod, i);
       }  
      else if(iClose(NULL, 0, i) < MaBuffer[i])
       {
         MaTUp[i] = MaBuffer[i] + iATR(NULL, 0, nPeriod, i);
       } 

    }  
   //-----
   return(0);
}
附加的文件:
 

工作选项的比较分析

最佳选择2。布尔条件不能被丢弃

选项5是基于它的

附加的文件:
 

忘记了剧本的事

附加的文件:
 
Vinin:

忘记了剧本的事

诙谐的方法,我接受))。
 
Mathemat:

我想对吉普 批评的部分进行补充。


我不明白,既然我们可以这样写,为什么还要在布尔变量上跳舞。

   showEUR  = ( StringFind(Symbol(), "EUR", 0) != -1);
   showUSD  = ( StringFind(Symbol(), "USD", 0) != -1);
   showGBP  = ( StringFind(Symbol(), "GBP", 0) != -1);
   showCHF  = ( StringFind(Symbol(), "CHF", 0) != -1);
   showJPY  = ( StringFind(Symbol(), "JPY", 0) != -1);