Hello good people. Thank you all for your tireless and selfless work.
Can anyone explain to me the logic or algorithm of the following code? Why does it repaint and can it be made not to repaint?
Code: Select all
#property copyright "Copyright © november 2015, "
#property link ""
#property strict
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 RoyalBlue //DodgerBlue
#property indicator_color2 Crimson //OrangeRed
#property indicator_color3 LightSteelBlue //White
#property indicator_color4 LightSteelBlue //White
#property indicator_width1 2
#property indicator_width2 2
#property indicator_style3 STYLE_DOT
#property indicator_style4 STYLE_DOT
input int p = 10; // period
input int s = 5; // smoothing
input double distance = 2.0; // distance
input bool showBb = true; // show bollinger bands
input bool showCl = true; // show center line
input int ATR = 1000; // ATR period
input int cb = 1000; // candles back
double fx1[],fx2[],hp[];
double z1,z2,ki;
int fs;
double upper[],lower[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int OnInit(void) {
//--- indicator buffers mapping
IndicatorBuffers(5);
SetIndexBuffer(0,fx1);
SetIndexBuffer(1,fx2);
SetIndexBuffer(2,lower);
SetIndexBuffer(3,upper);
SetIndexBuffer(4,hp);
if(showBb){
SetIndexStyle(2,DRAW_LINE);
SetIndexStyle(3,DRAW_LINE);
}
else{
SetIndexStyle(2,DRAW_NONE);
SetIndexStyle(3,DRAW_NONE);
}
if(showCl){
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
}
else{
SetIndexStyle(0,DRAW_NONE);
SetIndexStyle(1,DRAW_NONE);
}
SetIndexEmptyValue(0,0.0);
SetIndexEmptyValue(1,0.0);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
const int prev_calculated,
const datetime &time[],
const double &open[],
const double &high[],
const double &low[],
const double &close[],
const long &tick_volume[],
const long &volume[],
const int &spread[])
{
int i;
SetIndexDrawBegin(0,Bars-cb);
SetIndexDrawBegin(1,Bars-cb);
double avg;
ki=2.0/(p+1);
for (i=cb; i>=0; i--) {fx1[i]=Close[i];}
for (int m=0; m<=s; m++){
z1=fx1[0];
for (i=0; i<=cb; i++) {z1=z1+(fx1[i]-z1)*ki; hp[i]=z1;}
z2=fx1[cb];
for (i=cb; i>=0; i--) {z2=z2+(fx1[i]-z2)*ki; fx1[i]=(hp[i]+z2)/2;}
}
fs=0;
for (i=cb; i>=0; i--){
if (fx1[i]>fx1[i+1]) fs=1;
if (fx1[i]<fx1[i+1]) {if (fs==1) fx2[i+1]=fx1[i+1]; fs=2;}
if (fs==2) fx2[i]=fx1[i]; else fx2[i]=0.0;
avg = iATR(NULL,0,ATR, i+10);
upper[i] = hp[i] + distance*avg;
lower[i] = hp[i] - distance*avg;
}
//--- return value of prev_calculated for next call
return(rates_total);
}