Color line loose color after changing timeframe

 

Hi 2 all,

and thanks for taking your time!


The indicator loose the color after changing time frame and go back  - I have to recompile, than it's all right again...where is my fault?


E.g.: Start D1: ok; switching to H4: ok; going back to D1: all red; going to H4: all red...


Thanks in advance...


Lupo

//+------------------------------------------------------------------+
//|                                                    Color ADX.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net/"

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Red // (falling) ADX - is normal ADX; changes color via ADXUpBuffer, when ADX rises
#property indicator_width1 2
#property indicator_color2 Green // rising ADX
#property indicator_width2 2
#property indicator_level1 40.0 //horizontal line
#property indicator_levelwidth 1
#property indicator_levelcolor Black
#property indicator_levelstyle 0
//---- input parameters
extern int ADXPeriod=14;
//---- buffers
double ADXBuffer[];
double ADXUpBuffer[];
double Trend[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   IndicatorBuffers(3);
//---- indicator buffers
   SetIndexBuffer(0,ADXBuffer);
   SetIndexBuffer(1,ADXUpBuffer);
   SetIndexBuffer(2,Trend);
   
//---- name for DataWindow and indicator subwindow label
   IndicatorShortName("Color ADX("+ADXPeriod+")");
   SetIndexLabel(0,"ADX");
   SetIndexLabel(1,"ADXUp");
//----
   SetIndexDrawBegin(0,ADXPeriod);
   SetIndexDrawBegin(1,ADXPeriod); 
//----
   return(0);
  }

//+------------------------------------------------------------------+
//| Average Directional Movement Index                               |
//+------------------------------------------------------------------+
int start()
  {
//----main loop
   for(int i=1;i<Bars;i++)
   { 
      ADXBuffer[i]=iADX(NULL,0,ADXPeriod,PRICE_CLOSE,0,i);
      if(ADXBuffer[i]>ADXBuffer[i+1]) Trend[i]=1;  //checks if rising ADX
      
      if(Trend[i]==1)     //rising ADX
         {
         ADXUpBuffer[i] = ADXBuffer[i];   //Up Buffer filled with ADX values
         if(Trend[i+1]!=1) ADXUpBuffer[i+1] = ADXBuffer[i+1];  //if there is a trendchange fill also the Up Buffer of the previous bar
         }     
   }  
//----
    return(0);
  }
//+------------------------------------------------------------------+
 
  1.  for(int i=1;i<Bars;i++){ 
       :
             if(Trend[i+1]!=1) ADXUpBuffer[i+1] = ADXBuffer[i+1]; 
    What is the value of Trend[i+1] and ADXBuffer[i+1]? You haven't set them yet! Count DOWN
  2. Why are you recalculating every bar every tick? Do it write
    int counted = IndicatorCounted();
    if(counted < ADXPeriod) counted = ADXPeriod;
    for(int i = Bars-1 - counted; i >= 0; i--)
  3.      if(Trend[i]==1)     //rising ADX
    What do you do if the trend is not rising?
 
Thanks 2 U, WHRoeder!
Reason: