Please help with simple indicator

 

I have been trying to write an indicator that will assign a 0 for a down bar and a 1 for an up bar. Here is the code for it. All my indicator is doing is printing red bars. So it seems my algorithm is wrong even though I do not see how. I am getting the open of the last bar and if it is less than the close of that same bar then it means the bar went up and then visa versa. So please help show me what is wrong with this?

#property indicator_separate_window
#property  indicator_buffers 3
#property indicator_color2 Green
#property indicator_width2 3
#property indicator_color3 Red
#property indicator_width3 3
#property indicator_maximum 1
#property indicator_minimum 0

extern int TimeFrame  = 0;

string TF;
double Bar2[];
double Bar[];
double Bar_UP[];
double Bar_DN[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
 IndicatorDigits(1);
   
   SetIndexBuffer(0,Bar);
   SetIndexBuffer(1,Bar_UP);
   SetIndexBuffer(2,Bar_DN);
   
   SetIndexStyle(0,DRAW_NONE);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexStyle(2,DRAW_HISTOGRAM);
   
   SetIndexLabel(0,"Bar");
   SetIndexLabel(1,"");
   SetIndexLabel(2,"");
    if (TimeFrame<Period()) TimeFrame=Period();
                switch(TimeFrame)
        {
                case 1:         TF="M1";  break;
                case 5:         TF="M5";  break;
                case 15:                TF="M15"; break;
                case 30:                TF="M30"; break;
                case 60:                TF="H1";  break;
                case 240:       TF="H4";  break;
                case 1440:      TF="D1";  break;
                case 10080:     TF="W1";  break;
                case 43200:     TF="MN1"; break;
                default:          {TimeFrame = Period(); init(); return(0);}
        }
         
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
    int limit;
    double bar;
    double bar2;
    
   
   int counted_bars=IndicatorCounted();
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
//---- 
    if (TimeFrame<Period()) TimeFrame=Period();
   for(int i=0; i<limit; i++) {
    Bar[i] = Open[i];                                              <<< I am guessing my error starts here? >>>
    Bar2[i] =  Close[i];
   
   for(i=0; i<limit; i++) {
   if(Bar[i] < Bar2[i]){Bar_UP[i]=1;Bar_DN[i]=EMPTY_VALUE;}
   else if(Bar[i] > Bar2[i]){Bar_DN[i]=1;Bar_UP[i]=EMPTY_VALUE;}  
//----
      }
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+
 

I can't be much help as I haven't quite got to grips with histograms.

However you don't size the Bar2[] array and why do you need it anyway? Simply use Close[i] instead

 
Hmm looks like you over complicated it. Here's the bare bones version.

#property copyright "Copyright 2013, Jimdandy1958"
#property link      "http://www.jimdandyforex.com"
#property indicator_separate_window
#property indicator_buffers 2
#property indicator_color1 Green
#property indicator_width1 3
#property indicator_color2 Red
#property indicator_width2 3
#property indicator_maximum 1
#property indicator_minimum 0

extern int TimeFrame  = 0;

//string TF;
double Bar_UP[];
double Bar_DN[];


///////////////////////////////////
int init()
  {
   IndicatorDigits(1);
   
   SetIndexBuffer(0,Bar_UP);
   SetIndexBuffer(1,Bar_DN);
   
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   
   SetIndexLabel(0,"UP Candle");
   SetIndexLabel(1,"Down Candle");
   return(0);
  }
//////////////////////////////////////
int deinit()  {   return(0);  }
///////////////////////////////////////////
int start()
  {
   int limit;
   int counted_bars=IndicatorCounted();
   if(counted_bars>0) counted_bars--;
   limit=Bars-counted_bars;
   for(int i=0; i<limit; i++)
   {
   if(Open[i] < Close[i]){Bar_UP[i]=1;Bar_DN[i]=EMPTY_VALUE;}
   else if(Open[i] > Close[i]){Bar_DN[i]=1;Bar_UP[i]=EMPTY_VALUE;}  
   }
   return(0);
  }
//+------------------------------------------------------------------+
PipPip....Jimdandy
Files:
histo.mq4  2 kb
 

Thank you Jimdandy! That was very helpful!

I was over complicating everything and I had variables I didn't need. So, I am guessing that when I tried to set Bar[i] = Open[i] that I can't do that? Can I not set an array equal to another array?

Reason: