Forex Factory (https://www.forexfactory.com/forum.php)
-   Platform Tech (https://www.forexfactory.com/forumdisplay.php?f=69)
-   -   Help with indicator - green for up and red for down (https://www.forexfactory.com/showthread.php?t=263445)

elchoco Nov 1, 2010 7:29pm | Post# 1

Help with indicator - green for up and red for down
 
There's two issues with this code which I can't solve. The indicator is supposed to show how many pips away from the open of the day each candle has closed at.

Issues:

1: I can't make historical values be green for positive and red for negative. They all change depending on the current bar's value.

2: The historical data has to be built up one candle at a time in real time. ie the indicator isn't showing historical data. In the pic below you'll see there's no data for the indicator beyond about 30 bars back when I attached it.

As usual it's probably due to some fundamental misunderstandings, however any suggestions or pointers in the right direction would be really appreciated.

Inserted Code
int i, counted_bars=IndicatorCounted();

   i=Bars-counted_bars-1;
   while(i>=0)
     {
      double close = iClose(NULL, 0, 0);
      double open =iOpen(NULL, PERIOD_D1, 0);
      double diff =(close-open)/Point;
       color colour = Lime;

           if (diff < 0) { 
              colour = Red;
          }
          SetIndexStyle(0, DRAW_HISTOGRAM, 0 , 5, colour);
          ExtMapBuffer1[i] = diff;
   i--;
   }

http://www.alfasecure.com/images/errors.gif

elchoco Nov 1, 2010 8:04pm | Post# 2

ha!
 
I just refreshed and there's a previous post with a clue just below. Let's see...

http://www.forexfactory.com/showthread.php?t=206185

Ok, That was some help. I now get the green/red Bars Showing. However there's still an issue with not seeing historical values and the indicator has to build up in real time. Any thoughts?

Here's the new code:

Inserted Code
#property indicator_separate_window
#property indicator_buffers 2
#property  indicator_color1  Red
#property  indicator_color2  Lime

#property indicator_minimum -1
#property indicator_maximum 1


//----
color colour;
extern color color1 = Red;
extern color color2 = Lime;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];

//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM, 0, 5, color1);
   SetIndexBuffer(0, ExtMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM, 0, 5, color2);
   SetIndexBuffer(1, ExtMapBuffer2);
 
//----
 
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
  
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for visible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   int i=Bars-ExtCountedBars-1;
   while(i>=0)
     {
      double close = iClose(NULL, 0, i);                  // The Close of each bar
      double open =iClose(NULL, PERIOD_D1, i-1);   //  Yesterday's closing price
      double diff =(close-open)/Point;                       
      
      if (diff<0) 
        {
         ExtMapBuffer1[i]=diff;
        } 
      else
        {
         ExtMapBuffer2[i]=diff;
        } 

     i--;
    }
//----
   return(0);
}

Darn, actually it's still buggy.


© Forex Factory