Download MetaTrader 5

indicator modify

To add comments, please log in or register
dvetri
38
dvetri  

here is my *flat chart* indicator. I want to add ofset to every new bar which is the same type as their own previous bar ( every bar that is not the first of its own type).
The ofset value will be = (Open (present bar) - Open (first bar of the *present bar* type))
I'll be glad if someone can help.

//+------------------------------------------------------------------+
                                              
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

#property indicator_separate_window
#property indicator_buffers 4

#property indicator_color1 DarkGray
#property indicator_color2 DarkGray
#property indicator_color3 Red
#property indicator_color4 DeepSkyBlue
#property indicator_color5 Gold
#property indicator_color6 Aqua


#property indicator_level1 0
#property indicator_levelcolor White
#property indicator_levelstyle 0



extern int width=7;

extern int width0=3;

extern int width1=3;

//----
extern color color1 = DarkGray;
extern color color2 = DarkGray;
extern color color3 = Red;
extern color color4 = DeepSkyBlue;
extern color color5 = Gold;
extern color color6 = Aqua;

//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];


//----
int ExtCountedBars=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM, 0, width0, color1);
   SetIndexBuffer(0, ExtMapBuffer1);
   IndicatorDigits(Digits-5);
   
   SetIndexStyle(1,DRAW_HISTOGRAM, 0, width0, color2);
   SetIndexBuffer(1, ExtMapBuffer2);
   IndicatorDigits(Digits-5);
   
   SetIndexStyle(2,DRAW_HISTOGRAM, 0, width, color3);
   SetIndexBuffer(2, ExtMapBuffer3);
   IndicatorDigits(Digits-5);
   
   SetIndexStyle(3,DRAW_HISTOGRAM, 0, width, color4);
   SetIndexBuffer(3, ExtMapBuffer4);
   IndicatorDigits(Digits-5);
   
   SetIndexStyle(4,DRAW_HISTOGRAM, 0, width1, color5);
   SetIndexBuffer(4, ExtMapBuffer3);
   IndicatorDigits(Digits-5);
   
   SetIndexStyle(5,DRAW_HISTOGRAM, 0, width1, color6);
   SetIndexBuffer(5, ExtMapBuffer4);
   IndicatorDigits(Digits-5);
//----
   SetIndexDrawBegin(0,0);
   SetIndexDrawBegin(1,0);
   SetIndexDrawBegin(2,0);
   SetIndexDrawBegin(3,0);
   SetIndexDrawBegin(4,0);
   SetIndexDrawBegin(5,0);
   
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexBuffer(5,ExtMapBuffer6);
//---- initialization done
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- TODO: add your code here
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   double  x, y, z;
   if(Bars<=10) return(0);
   ExtCountedBars=IndicatorCounted();
//---- check for possible errors
   if (ExtCountedBars<0) return(-1);
//---- last counted bar will be recounted
   if (ExtCountedBars>0) ExtCountedBars--;
   int pos=Bars-ExtCountedBars-1;
   while(pos>=0)
     
      {
      if
      (Close[pos]>Open[pos])
      {
            
                       
            x=(Close[pos]-Open[pos])*100000;
            y=(High[pos]-Open[pos])*100000;
            z=(Open[pos]-Low[pos])*100000;

                     
            {
               ExtMapBuffer1[pos]=0;
               ExtMapBuffer2[pos]=z*-1;
            
               ExtMapBuffer3[pos]=0;
               ExtMapBuffer4[pos]=x;
 
               ExtMapBuffer5[pos]=0;
               ExtMapBuffer6[pos]=y;
            }
      }       
       
       
      if
      (Close[pos]==Open[pos])
      {
            
                       
            x=(Open[pos]-Close[pos])*100000;
            y=(High[pos]-Close[pos])*100000;
            z=(Open[pos]-Low[pos])*100000;

                     
            {
               ExtMapBuffer1[pos]=0;
               ExtMapBuffer2[pos]=0;
            
               ExtMapBuffer3[pos]=0;
               ExtMapBuffer4[pos]=0;
 
               ExtMapBuffer5[pos]=y*-1;
               ExtMapBuffer6[pos]=z;
            }
      } 
       
      
               
      if
      (Close[pos]<Open[pos])
      {
            
                       
            x=(Open[pos]-Close[pos])*100000;
            y=(Open[pos]-Low[pos])*100000;
            z=(High[pos]-Open[pos])*100000;

                     
            {
               ExtMapBuffer1[pos]=z;
               ExtMapBuffer2[pos]=0;
            
               ExtMapBuffer3[pos]=x*-1;
               ExtMapBuffer4[pos]=0;
 
               ExtMapBuffer5[pos]=y*-1;
               ExtMapBuffer6[pos]=0;
            }
      }  
     


                           
              pos--;
    }
//----
   return(0);
  }
//+------------------------------------------------------------------+
whroeder1
15917
whroeder1  
dvetri: I'll be glad if someone can help.

Help you with what? You haven't stated a problem.
  1. Drawing histograms on a separate chart, each goes to zero. See How to Draw Cnadle chart in indicator_separate_window ? (XDXD) - MQL4 and MetaTrader 4 - MQL4 programming forum
  2. int start(){
       ExtCountedBars=IndicatorCounted();
    //---- last counted bar will be recounted
       if (ExtCountedBars>0) ExtCountedBars--;
       int pos=Bars-ExtCountedBars-1;

  3. No need for the decrement. Contradictory information on IndicatorCounted() - MQL4 and MetaTrader 4 - MQL4 programming forum
  4. Start using the new Event Handling Functions Event Handling Functions - Functions - Language Basics - MQL4 Reference
  5. See How to do your lookbacks correctly.
  6.             y=(Open[pos]-Low[pos])*100000;
    
    Code assumes non-JPY pair 100000=1/_Point. You don't need the scaling; showing prices in the window might be useful and will help you debug your code.
  7. Your image also shows Heiken Ashi candles. Attached is some code to help you get those values.
Files:
temp.mq4 4 kb
dvetri
38
dvetri  

Thank you whroeder1

To add comments, please log in or register