donchian channel

 

Hi experts.

I tried writing code for Donchian channel. But every time the new bar comes, the channel drops to ZERO.

I tried everything but still the same issue. Please help and guide me, where am I wrong.


int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//Converting arrays into series

ArraySetAsSeries(UpperBuffer,true);
ArraySetAsSeries(LowerBuffer,true);
ArraySetAsSeries(high,true);
ArraySetAsSeries(low,true);
   
//Optimisation to avoid recalculation of data for already calculated data

int first = 0;
if(prev_calculated > 0)
  {first = prev_calculated-1;   }

   for(int i = first ; i<=rates_total-1 ; i++)
     {   
         UpperBuffer[i] = high[iHighest(_Symbol,_Period,MODE_HIGH,Bar_Period,i)];
         LowerBuffer[i] = low[iLowest(_Symbol,_Period,MODE_LOW,Bar_Period,i)];
     }   


   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
 

Why are you "writing code for Donchian channel."?

If you would have searched here or if you just click on the link provides by MQ about Donchian channel you would have found at least this article that contains not only the indicator but as well the trading system. And believe me its easier to amend a working system than trying to write everything from the scratch - especially for beginner.

Bear in mind there's almost nothing that hasn't already been programmed for MT4/MT5 and is ready even for you.
Its easier and the result has fff: far fewer faults than a do-it-yourself-EA generally speaking!

 
Carl Schreiber #:

Why are you "writing code for Donchian channel."?

If you would have searched here or if you just click on the link provides by MQ about Donchian channel you would have found at least this article that contains not only the indicator but as well the trading system. And believe me its easier to amend a working system than trying to write everything from the scratch - especially for beginner.

Bear in mind there's almost nothing that hasn't already been programmed for MT4/MT5 and is ready even for you.
Its easier and the result has fff: far fewer faults than a do-it-yourself-EA generally speaking!

Hi Carl, I appreciate your suggestion. But writing a code by self help fine tune skills and gives a better learning. I can easily copy, but i wanted to know where am i wrong. 

Here, i even tried a for loop to calculate Highest/Lowest, but still there was same error. I you can point my mistake, i will be greatful.

 

For series arrays, count down.

int first = (rates_total==prev_calculated) ? 0 : (prev_calculated==0) ? rates_total-1-Bar_Period : rates_total-1-prev_calculated;

for(int i = first ; i>=0 ; i--)
 
Ernst Van Der Merwe #:

For series arrays, count down.

Wow sir... You are genius. A mentor like you is a blessing... Thank you
Reason: