Learning logic - page 4

 

Here is the original start() function from this indicator

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);
}

The function uses calculation of the average. It needs to be replaced with a standard function.

It looks like this

//=================================================================================================
// Замена расчета среднего на стандартную функцию
//=================================================================================================
//   Старый вариант расчета
//      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);
//=================================================================================================

Attached is the variant of the indicator with the correction

Files:
 

Now on to the logic of the indicator itself (I'm not saying that the proposed version is ideal)

I just like it better

      // Вариант два. Убираем избыточные условия  и делаем одно обращение к функции
      
      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;
       } 
Files:
 
That's a good example. Only it's more about optimisation than logic.
 
denis_orlov:
A good example. But it is more about optimization than logic.


And logic too, the logic of thinking.

The third option is to abandon the logical conditions in the indicator completely. The question arises - is it possible.

Let's try

For this let's add a couple of logical variables

The entire code of the start function now is

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);
}
Files:
 

if(counted_bars < 0) return(-1);

Based on what logic is this line present?

 
Roger:

if(counted_bars < 0) return(-1);

Based on what logic is this line present?


It's not my line. It's the author's

Optimal code for 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);
}
Files:
 

Comparative analysis of the work options

Optimal option number 2. Boolean conditions cannot be discarded

Option 5 is based on it

Files:
 

Forgot all about the script

 
Vinin:

Forgot all about the script

witty method, I'll take it ))
 
Mathemat:

I would like to add about the section criticised by gip:


I don't understand why we have to dance around boolean variables when we can write it this way:

   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);
Reason: