So Close! I just want to compare % change from previous bar

Back to topics list To post a new topic, please log in or register
avatar
2
mushapa100 2016.09.23 07:54
 

Simple task. I have an array that simply takes the percent change from the previous tick. Then I want a histogram to show if the current tick is higher than the last. When I apply ArraySetAsSeries() it doesn't even work. I have placed it in multiple spots in the code and it either doesn't do anything or does all sorts of crazy histograms. Here is the the code. I would suggest removing the ArraySetSeries to see how the buffer should look. I have read anything I can get my hands on ArraySetSeries and I haven't ran into this issue yet. Please help and I am grateful!

 

#property indicator_separate_window
#property indicator_buffers 2
#property indicator_plots 2

#property indicator_color1 Red
#property indicator_color2 Yellow

//--- buffers
double BLIPbar[]; 
double pc[];

int init()
{
   SetIndexBuffer(0,pc);
   SetIndexBuffer(1,BLIPbar); SetIndexStyle(1,DRAW_HISTOGRAM); 
   return(0);
}

int start()
{

   int i,counted_bars=IndicatorCounted();
      if(counted_bars < 0) return(-1);
      if(counted_bars>0) counted_bars--;
      int limit = MathMin(Bars-counted_bars,Bars-1);

   for(i = limit; i >= 0; i--)
   
   ArraySetAsSeries(pc,true); // setting up percent change (pc) into a series so I can go from right to left indexing
   pc[i] =(iClose(NULL, 0, i)- iClose(NULL, 0,i+1))/iClose(NULL, 0,i); // finding the percent change and hopefully filling the series data                 

   {
    for(i = limit; i >= 0; i--)
    if (pc[i] > pc[1]) // Comparing current tick to last tick. I have tried multiple variations of pc[0], pc[2], etc and nothing comes out right. 
    BLIPbar[i] = pc[i]; // when current percent change is higher than last I just want a bar to "BLIP" and show up.
   }
   return(0);
} 
avatar
1200
FMIC 2016.09.23 08:11 #
 
  1. They are not called ticks, they are Close Prices for the Bars. Ticks are individual price quotes and there can be hundreds ou even thousands of ticks per bar.
  2. Use "Close[ i ]" instead of "iClose(NULL,0,i)".
  3. Percentage change is against the previous bar and not against the current bar.
  4. Please remeber that the current bar is in flux and incomplete with the close price continually updating.
  5. There is no need for the ArraySetAsSerries in this case because you are using the old style of coding. So remove it.
  6. Your braces are in the wrong place for the loops.
  7. Remember that you current code will cause this indicator to continuously redraw (which is not good)!
  8. And there are a few other problematic points as well:
    #property indicator_separate_window
    #property indicator_buffers 2
    #property indicator_plots 2
    
    #property indicator_color1 Red
    #property indicator_color2 Yellow
    
    //--- buffers
    double BLIPbar[]; 
    double pc[];
    
    int init()
    {
       SetIndexBuffer(0,pc);
       SetIndexBuffer(1,BLIPbar); SetIndexStyle(1,DRAW_HISTOGRAM); 
       return(0);
    }
    
    int start()
    {
       int i,counted_bars=IndicatorCounted();
          if(counted_bars < 0) return(-1);
          if(counted_bars>0) counted_bars--;
          int limit = MathMin(Bars-counted_bars,Bars-1);
    
       pc[0] = ( Close[0] - Close[1] ) / Close[1];
       BLIPbar[0] = EMPTY_VALUE; // What value do you want it to be?
    
       for(i = limit; i > 0; i--)
       {
          pc[i] = ( Close[i] - Close[i+1] ) / Close[i+1]; // finding the percent change and hopefully filling the series data
    
          if(pc[i] > pc[0]) // Comparing current tick to last tick. I have tried multiple variations of pc[0], pc[2], etc and
             BLIPbar[i] = pc[i]; // when current percent change is higher than last I just want a bar to "BLIP" and show up.
          else
             BLIPbar[i] = EMPTY_VALUE; // You were missing the "else". What value do you want it to be when it is not greater?
       }
    
       return(0);
    }
  9. Once you have fixed it, please consider using the updated modern style of MQL4+ code and not the old style.
avatar
12985
WHRoeder 2016.09.23 14:46 #
 
  1.   int i,counted_bars=IndicatorCounted();
          if(counted_bars < 0) return(-1);
          if(counted_bars>0) counted_bars--;
          int limit = MathMin(Bars-counted_bars,Bars-1);
    No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 forum
  2. pc[i] = ( Close[i] - Close[i+1] ) / Close[i+1]
    Your look back is one. So when limit is Bars-1, you access Close[Bars] which does not exist.
  3. Do your lookbacks correctly.
avatar
2
mushapa100 2016.09.23 15:16 #
 

Fantastic! Thank you for your guidance. I will work on all of these points. I am aware about the continuously redrawing effect and in the end will only calculate established closed bars :) I won't be able to address these for another 14 hours, but I appreciate this first step!!

Back to topics list  

To add comments, please log in or register