Please fix this indicator or EA - page 46

 

ROC + ma

Here you go

k3rn3l:
sorry but in isn t what i m looking for..i need roc indicator and a moving average calculate on it....
Files:
roc__ma.mq4  3 kb
 
mladen:
Here you go

thank you very much:) you are the best

 

hi Mladen..what s the error in this indicator??

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_color1 Blue

#property indicator_color2 Red

//---- input parameters

extern int AtrPeriod=10;

//---- buffers

double AtrBuffer[];

double TempBuffer[];

double Matr[];

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

//| Custom indicator initialization function |

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

int init()

{

string short_name;

//---- 1 additional buffer used for counting.

IndicatorBuffers(3);

//---- indicator line

SetIndexStyle(0,DRAW_LINE);

SetIndexBuffer(0,AtrBuffer);

SetIndexBuffer(1,TempBuffer);

SetIndexStyle(2,DRAW_LINE);

SetIndexBuffer(2,Matr);

//---- name for DataWindow and indicator subwindow label

short_name="ATR("+AtrPeriod+")";

IndicatorShortName(short_name);

SetIndexLabel(0,short_name);

//----

SetIndexDrawBegin(0,AtrPeriod);

SetIndexDrawBegin(2,Matr);

//----

return(0);

}

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

//| Average True Range |

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

int start()

{

int i,counted_bars=IndicatorCounted();

//----

if(Bars<=AtrPeriod) return(0);

//---- initial zero

if(counted_bars<1)

for(i=1;i<=AtrPeriod;i++) AtrBuffer=0.0;

//----

i=Bars-counted_bars-1;

while(i>=0)

{

double high=High;

double low =Low;

if(i==Bars-1) TempBuffer=high-low;

else

{

double prevclose=Close;

TempBuffer=MathMax(high,prevclose)-MathMin(low,prevclose);

}

i--;

}

//----

if(counted_bars>0) counted_bars--;

int limit=Bars-counted_bars;

for(i=0; i<limit; i++)

AtrBuffer=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

for(i=0; i<limit; i++)

Matr = iMAOnArray(AtrBuffer,0,AtrPeriod,0,MODE_SMA,i);

//----

return(0);

}

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

...

k3rn3l

Nothing wrong as I see. The blue line is an average true range and it gives the same results as the built in ATR. If you want the 3rd line to be visible too, just change the #property indicator_buffers from 2 to 3 and it will show the average of the ATR line too. If you want it even more simplified (code wise), then do something like this :

property indicator_separate_window

#property indicator_buffers 3

#property indicator_color1 Blue

#property indicator_color2 Red

#property indicator_color3 Green

//---- input parameters

extern int AtrPeriod=10;

//---- buffers

double AtrBuffer[];

double TempBuffer[];

double Matr[];

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

//| Custom indicator initialization function |

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

int init()

{

SetIndexBuffer(0,AtrBuffer); SetIndexDrawBegin(0,AtrPeriod);

SetIndexBuffer(1,TempBuffer); SetIndexDrawBegin(1,AtrPeriod);

SetIndexBuffer(2,Matr); SetIndexDrawBegin(2,AtrPeriod);

IndicatorShortName("ATR("+AtrPeriod+")");

return(0);

}

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

//| Average True Range |

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

int start()

{

int countedBars = IndicatorCounted();

if (countedBars<0) return(-1);

if (countedBars>0) countedBars--;

int limit = MathMin(Bars-countedBars,Bars-1);

for(int i=limit; i>=0; i--)

if(i==Bars-1)

TempBuffer=High-Low;

else TempBuffer=MathMax(High,Close)-MathMin(Low,Close);

for(i=0; i<limit; i++) AtrBuffer = iMAOnArray(TempBuffer,0,AtrPeriod,0,MODE_SMA,i);

for(i=0; i<limit; i++) Matr = iMAOnArray(AtrBuffer,0,AtrPeriod,0,MODE_SMA,i);

return(0);

}

k3rn3l:
hi Mladen..what s the error in this indicator??

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_color1 Blue

#property indicator_color2 Red

//---- input parameters

extern int AtrPeriod=10;

//---- buffers

double AtrBuffer[];

double TempBuffer[];

double Matr[];

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

//| Custom indicator initialization function |

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

int init()

{

string short_name;

//---- 1 additional buffer used for counting.

IndicatorBuffers(3);

//---- indicator line

SetIndexStyle(0,DRAW_LINE);

SetIndexBuffer(0,AtrBuffer);

SetIndexBuffer(1,TempBuffer);

SetIndexStyle(2,DRAW_LINE);

SetIndexBuffer(2,Matr);

//---- name for DataWindow and indicator subwindow label

short_name="ATR("+AtrPeriod+")";

IndicatorShortName(short_name);

SetIndexLabel(0,short_name);

//----

SetIndexDrawBegin(0,AtrPeriod);

SetIndexDrawBegin(2,Matr);

//----

return(0);

}

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

//| Average True Range |

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

int start()

{

int i,counted_bars=IndicatorCounted();

//----

if(Bars<=AtrPeriod) return(0);

//---- initial zero

if(counted_bars<1)

for(i=1;i<=AtrPeriod;i++) AtrBuffer=0.0;

//----

i=Bars-counted_bars-1;

while(i>=0)

{

double high=High;

double low =Low;

if(i==Bars-1) TempBuffer=high-low;

else

{

double prevclose=Close;

TempBuffer=MathMax(high,prevclose)-MathMin(low,prevclose);

}

i--;

}

//----

if(counted_bars>0) counted_bars--;

int limit=Bars-counted_bars;

for(i=0; i<limit; i++)

AtrBuffer=iMAOnArray(TempBuffer,Bars,AtrPeriod,0,MODE_SMA,i);

if(counted_bars>0) counted_bars--;

limit=Bars-counted_bars;

for(i=0; i<limit; i++)

Matr = iMAOnArray(AtrBuffer,0,AtrPeriod,0,MODE_SMA,i);

//----

return(0);

}

//+------------------------------------------------------------------+
 
mladen:
k3rn3l

Nothing wrong as I see. The blue line is an average true range and it gives the same results as the built in ATR. If you want the 3rd line to be visible too, just change the #property indicator_buffers from 2 to 3 and it will show the average of the ATR line too. If you want it even more simplified (code wise), then do something like this :

property indicator_separate_window

#property indicator_buffers 3

#property indicator_color1 Blue

#property indicator_color2 Red

#property indicator_color3 Green

//---- input parameters

extern int AtrPeriod=10;

//---- buffers

double AtrBuffer[];

double TempBuffer[];

double Matr[];

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

//| Custom indicator initialization function |

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

int init()

{

SetIndexBuffer(0,AtrBuffer); SetIndexDrawBegin(0,AtrPeriod);

SetIndexBuffer(1,TempBuffer); SetIndexDrawBegin(1,AtrPeriod);

SetIndexBuffer(2,Matr); SetIndexDrawBegin(2,AtrPeriod);

IndicatorShortName("ATR("+AtrPeriod+")");

return(0);

}

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

//| Average True Range |

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

int start()

{

int countedBars = IndicatorCounted();

if (countedBars<0) return(-1);

if (countedBars>0) countedBars--;

int limit = MathMin(Bars-countedBars,Bars-1);

for(int i=limit; i>=0; i--)

if(i==Bars-1)

TempBuffer=High-Low;

else TempBuffer=MathMax(High,Close)-MathMin(Low,Close);

for(i=0; i<limit; i++) AtrBuffer = iMAOnArray(TempBuffer,0,AtrPeriod,0,MODE_SMA,i);

for(i=0; i<limit; i++) Matr = iMAOnArray(AtrBuffer,0,AtrPeriod,0,MODE_SMA,i);

return(0);

}

ooooook..thank you..just what i need . Now..can you insert in it the bollinger bandwith???

 

Where? What should be the basic value around which the band should be calculated?

k3rn3l:
ooooook..thank you..just what i need . Now..can you insert in it the bollinger bandwith???
 
mladen:
Where? What should be the basic value around which the band should be calculated?

I want to have a unique indicator that calculate atr+ma and bandwith+ma and both calculate on the close..first part is complete(atr+ma), now i want to insert the bandwidth+ma...do you think this is possible?

 

...

All you have to add is a standard deviation (multiplied by some multiplier if you wish)

Bollinged band width is simply a standard deviation (you have the built in metatrader iStrDev() function to calculate that) multiplied by an arbitrary number (best is to define an external parameter for that purpose)

k3rn3l:
I want to have a unique indicator that calculate atr+ma and bandwith+ma and both calculate on the close..first part is complete(atr+ma), now i want to insert the bandwidth+ma...do you think this is possible?
 
mladen:
All you have to add is a standard deviation (multiplied by some multiplier if you wish) Bollinged band width is simply a standard deviation (you have the built in metatrader iStrDev() function to calculate that) multiplied by an arbitrary number (best is to define an external parameter for that purpose)

I have thought of something like this:

#property indicator_separate_window

#property indicator_buffers 8

#property indicator_color1 Blue

#property indicator_color2 Red

#property indicator_color3 Green

#property indicator_color4 Yellow

#property indicator_color5 Blue

//---- input parameters

extern int AtrBandPeriod=10;

extern int BandsShift=0;

extern double BandsDeviations=2.0;

//---- buffers

double AtrBuffer[];

double TempBuffer[];

double Matr[];

double MovingBuffer[];

double UpperBuffer[];

double LowerBuffer[];

double Bandwidth[];

double MaBandwidth[];

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

//| Custom indicator initialization function |

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

int init()

{

SetIndexBuffer(0,AtrBuffer); SetIndexDrawBegin(0,AtrBandPeriod);

SetIndexBuffer(1,TempBuffer); SetIndexDrawBegin(1,AtrBandPeriod);

SetIndexBuffer(2,Matr); SetIndexDrawBegin(2,AtrBandPeriod);

SetIndexStyle(3, DRAW_LINE); SetIndexDrawBegin(3,AtrBandPeriod+BandsShift);

SetIndexBuffer(3,Bandwidth);

SetIndexStyle(4,12); SetIndexDrawBegin(4,AtrBandPeriod+BandsShift);

SetIndexBuffer(4,MovingBuffer);

SetIndexStyle(5,12); SetIndexDrawBegin(5,AtrBandPeriod+BandsShift);

SetIndexBuffer(5,UpperBuffer);

SetIndexStyle(6,12); SetIndexDrawBegin(6,AtrBandPeriod+BandsShift);

SetIndexBuffer(6,LowerBuffer);

SetIndexStyle(7, DRAW_LINE); SetIndexDrawBegin(7,AtrBandPeriod+BandsShift);

SetIndexBuffer(7,MaBandwidth);

IndicatorShortName("ATR("+AtrBandPeriod+")");

return(0);

}

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

//| Average True Range |

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

int start()

{

int countedBars = IndicatorCounted();

if (countedBars<0) return(-1);

if (countedBars>0) countedBars--;

int limit = MathMin(Bars-countedBars,Bars-1);

for(int i=limit; i>=0; i--)

if(i==Bars-1)

TempBuffer=High-Low;

else TempBuffer=MathMax(High,Close)-MathMin(Low,Close);

for(i=0; i<limit; i++) AtrBuffer = iMAOnArray(TempBuffer,0,AtrBandPeriod,0,MODE_SMA,i);

for(i=0; i<limit; i++) Matr = iMAOnArray(AtrBuffer,0,AtrBandPeriod,0,MODE_SMA,i);

int k,counted_bars=IndicatorCounted();

double deviation;

double sum,oldval,newres;

Print("here");

if(Bars<=AtrBandPeriod) return(0);

if(counted_bars<1)

for(i=1;i<=AtrBandPeriod;i++)

{

MovingBuffer=EMPTY_VALUE;

UpperBuffer=EMPTY_VALUE;

LowerBuffer=EMPTY_VALUE;

Bandwidth=EMPTY_VALUE;

Print("Buffers cleaned");

}

limit=Bars-counted_bars;

if(counted_bars>0) limit++;

for(i=0; i<limit; i++)

MovingBuffer=iMA(NULL,0,AtrBandPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i);

i=Bars-AtrBandPeriod+1;

if(counted_bars>AtrBandPeriod-1) i=Bars-counted_bars-1;

while(i>=0)

{

sum=0.0;

k=i+AtrBandPeriod-1;

oldval=MovingBuffer;

while(k>=i)

{

newres=Close[k]-oldval;

sum+=newres*newres;

k--;

}

deviation=BandsDeviations*MathSqrt(sum/AtrBandPeriod);

UpperBuffer=oldval+deviation;

LowerBuffer=oldval-deviation;

Bandwidth=MathFloor((UpperBuffer - LowerBuffer)*100);

for(i=0; i<limit; i++) MaBandwidth = iMAOnArray(Bandwidth,0,AtrBandPeriod,0,MODE_SMA,i);

return(0);

}

}

 

...

Try something like this :

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_color1 Blue

#property indicator_color2 Red

extern int DevPeriod=10;

extern int AvgPeriod=10;

double DevBuffer[];

double AvgBuffer[];

//------------------------------------------------------------------

//

//------------------------------------------------------------------

int init()

{

SetIndexBuffer(0,DevBuffer);

SetIndexBuffer(1,AvgBuffer);

IndicatorShortName("StdDev ("+DevPeriod+")");

return(0);

}

int start()

{

int i,countedBars = IndicatorCounted();

if (countedBars<0) return(-1);

if (countedBars>0) countedBars--;

int limit = MathMin(Bars-countedBars,Bars-1);

for(i=0; i<limit; i++) DevBuffer = iStdDev(NULL,0,DevPeriod,0,MODE_SMA,PRICE_CLOSE,i);

for(i=0; i<limit; i++) AvgBuffer = iMAOnArray(DevBuffer,0,AvgPeriod,0,MODE_SMA,i);

return(0);

}

[/PHP]

k3rn3l:
I have thought of something like this:

[PHP]

#property indicator_separate_window

#property indicator_buffers 8

#property indicator_color1 Blue

#property indicator_color2 Red

#property indicator_color3 Green

#property indicator_color4 Yellow

#property indicator_color5 Blue

//---- input parameters

extern int AtrBandPeriod=10;

extern int BandsShift=0;

extern double BandsDeviations=2.0;

//---- buffers

double AtrBuffer[];

double TempBuffer[];

double Matr[];

double MovingBuffer[];

double UpperBuffer[];

double LowerBuffer[];

double Bandwidth[];

double MaBandwidth[];

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

//| Custom indicator initialization function |

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

int init()

{

SetIndexBuffer(0,AtrBuffer); SetIndexDrawBegin(0,AtrBandPeriod);

SetIndexBuffer(1,TempBuffer); SetIndexDrawBegin(1,AtrBandPeriod);

SetIndexBuffer(2,Matr); SetIndexDrawBegin(2,AtrBandPeriod);

SetIndexStyle(3, DRAW_LINE); SetIndexDrawBegin(3,AtrBandPeriod+BandsShift);

SetIndexBuffer(3,Bandwidth);

SetIndexStyle(4,12); SetIndexDrawBegin(4,AtrBandPeriod+BandsShift);

SetIndexBuffer(4,MovingBuffer);

SetIndexStyle(5,12); SetIndexDrawBegin(5,AtrBandPeriod+BandsShift);

SetIndexBuffer(5,UpperBuffer);

SetIndexStyle(6,12); SetIndexDrawBegin(6,AtrBandPeriod+BandsShift);

SetIndexBuffer(6,LowerBuffer);

SetIndexStyle(7, DRAW_LINE); SetIndexDrawBegin(7,AtrBandPeriod+BandsShift);

SetIndexBuffer(7,MaBandwidth);

IndicatorShortName("ATR("+AtrBandPeriod+")");

return(0);

}

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

//| Average True Range |

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

int start()

{

int countedBars = IndicatorCounted();

if (countedBars<0) return(-1);

if (countedBars>0) countedBars--;

int limit = MathMin(Bars-countedBars,Bars-1);

for(int i=limit; i>=0; i--)

if(i==Bars-1)

TempBuffer=High-Low;

else TempBuffer=MathMax(High,Close)-MathMin(Low,Close);

for(i=0; i<limit; i++) AtrBuffer = iMAOnArray(TempBuffer,0,AtrBandPeriod,0,MODE_SMA,i);

for(i=0; i<limit; i++) Matr = iMAOnArray(AtrBuffer,0,AtrBandPeriod,0,MODE_SMA,i);

int k,counted_bars=IndicatorCounted();

double deviation;

double sum,oldval,newres;

Print("here");

if(Bars<=AtrBandPeriod) return(0);

if(counted_bars<1)

for(i=1;i<=AtrBandPeriod;i++)

{

MovingBuffer=EMPTY_VALUE;

UpperBuffer=EMPTY_VALUE;

LowerBuffer=EMPTY_VALUE;

Bandwidth=EMPTY_VALUE;

Print("Buffers cleaned");

}

limit=Bars-counted_bars;

if(counted_bars>0) limit++;

for(i=0; i<limit; i++)

MovingBuffer=iMA(NULL,0,AtrBandPeriod,BandsShift,MODE_SMA,PRICE_CLOSE,i);

i=Bars-AtrBandPeriod+1;

if(counted_bars>AtrBandPeriod-1) i=Bars-counted_bars-1;

while(i>=0)

{

sum=0.0;

k=i+AtrBandPeriod-1;

oldval=MovingBuffer;

while(k>=i)

{

newres=Close[k]-oldval;

sum+=newres*newres;

k--;

}

deviation=BandsDeviations*MathSqrt(sum/AtrBandPeriod);

UpperBuffer=oldval+deviation;

LowerBuffer=oldval-deviation;

Bandwidth=MathFloor((UpperBuffer - LowerBuffer)*100);

for(i=0; i<limit; i++) MaBandwidth = iMAOnArray(Bandwidth,0,AtrBandPeriod,0,MODE_SMA,i);

return(0);

}

}

Reason: