//+------------------------------------------------------------------+
//| 1.2011 |
//| |
//| DGA |
//+------------------------------------------------------------------+
#property copyright "1.2011"
#property link "DGA"
#property indicator_separate_window
#property indicator_minimum -10
#property indicator_maximum 100
#property indicator_buffers 12
#property indicator_color1 Lime
#property indicator_width1 1
#property indicator_color2 Yellow//82,105,82
#property indicator_width2 0
#property indicator_color3 Red
#property indicator_width3 2
#property indicator_color4 Yellow//82,105,82
#property indicator_width4 0
#property indicator_color5 Lime
#property indicator_width5 2
#property indicator_color6 Red
#property indicator_width6 0
//---- input parameters
extern int Profit3 = 11;
extern int Profit3_MODE = MODE_EMA;
extern int Profit3_PRICE = PRICE_CLOSE;
extern int Ma_Profit3 = 11,
Ma_Profit3_MODE = MODE_EMA;
extern int BuyTrigger = 80;
extern int SellTrigger = 20;
extern color BuyTriggerColor = Red;
extern color SellTriggerColor = Lime;
extern int MainTrendLong = 85;
extern int MainTrendShort = 15;
extern color MainTrendLongColor = White;
extern color MainTrendShortColor = White;
//---- buffers
double RSIBuffer[];
double PosBuffer[];
double NegBuffer[];
double bdn[],bup[];
double sdn[],sup[];
double marsioma[];
string short_name;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
short_name = StringConcatenate("Profit3(",Profit3,")");
IndicatorBuffers(8);
SetIndexBuffer(0,RSIBuffer);
SetIndexBuffer(2,bup);
SetIndexBuffer(1,bdn);
SetIndexBuffer(3,sdn);
SetIndexBuffer(4,sup);
SetIndexBuffer(5,marsioma);
SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexStyle(3,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexStyle(4,DRAW_HISTOGRAM,STYLE_SOLID,1);
SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,1);
SetIndexBuffer(6,PosBuffer);
SetIndexBuffer(7,NegBuffer);
IndicatorShortName(short_name);
SetIndexDrawBegin(0,Profit3);
SetIndexDrawBegin(1,Profit3);
SetIndexDrawBegin(2,Profit3);
SetIndexDrawBegin(3,Profit3);
SetIndexDrawBegin(4,Profit3);
SetIndexDrawBegin(5,Profit3);
SetIndexDrawBegin(6,Profit3);
SetIndexDrawBegin(7,Profit3);
//----
drawLine(BuyTrigger,"BuyTrigger", BuyTriggerColor);
drawLine(SellTrigger,"SellTrigger", SellTriggerColor );
drawLine(MainTrendLong,"MainTrendLong", MainTrendLongColor );
drawLine(MainTrendShort,"MainTrendShort",MainTrendShortColor );
return(0);
}
//+------------------------------------------------------------------+
//| Relative Strength Index |
//+------------------------------------------------------------------+
int start()
{
int i,counted_bars=IndicatorCounted();
double rel,negative,positive;
//----
if(Bars<=Profit3) return(0);
//---- initial zero
if(counted_bars<1)
for(i=1;i<=Profit3;i++) RSIBuffer[Bars-i]=0.0;
//----
i=Bars-Profit3-1;
int ma = i;
if(counted_bars>=Profit3) i=Bars-counted_bars-1;
while(i>=0)
{
double sumn=0.0,sump=0.0;
if(i==Bars-Profit3-1)
{
int k=Bars-2;
//---- initial accumulation
while(k>=i)
{
double cma = iMA(Symbol(),0,Profit3,0,Profit3_MODE,Profit3_PRICE,k);
double pma = iMA(Symbol(),0,Profit3,0,Profit3_MODE,Profit3_PRICE,k+1);
rel=cma-pma;
if(rel>0) sump+=rel;
else sumn-=rel;
k--;
}
positive=sump/Profit3;
negative=sumn/Profit3;
}
else
{
//---- smoothed moving average
double ccma = iMA(Symbol(),0,Profit3,0,Profit3_MODE,Profit3_PRICE,i);
double ppma = iMA(Symbol(),0,Profit3,0,Profit3_MODE,Profit3_PRICE,i+1);
rel=ccma-ppma;
if(rel>0) sump=rel;
else sumn=-rel;
positive=(PosBuffer[i+1]*(Profit3-1)+sump)/Profit3;
negative=(NegBuffer[i+1]*(Profit3-1)+sumn)/Profit3;
}
PosBuffer[i]=positive;
NegBuffer[i]=negative;
if(negative==0.0) RSIBuffer[i]=0.0;
else
{
RSIBuffer[i]=100.0-100.0/(1+positive/negative);
bdn[i] = 0;
bup[i] = 0;
sdn[i] = 0;
sup[i] = 0;
if(RSIBuffer[i]>MainTrendLong)
bup[i] = -10;
if(RSIBuffer[i]<MainTrendShort)
bdn[i] = -10;
if(RSIBuffer[i]<20 && RSIBuffer[i]>RSIBuffer[i+1])
sup[i] = -10;
if(RSIBuffer[i]>80 && RSIBuffer[i]<RSIBuffer[i+1])
sdn[i] = -10;
}
i--;
}
while(ma>=0)
{
marsioma[ma] = iMAOnArray(RSIBuffer,0,Ma_Profit3,0,Ma_Profit3_MODE,ma);
ma--;
}
//----
return(0);
}
//+------------------------------------------------------------------+
void drawLine(double lvl,string name, color Col )
{
ObjectDelete(name);
ObjectCreate(name, OBJ_HLINE, WindowFind(short_name), Time[0], lvl,Time[0],lvl);
ObjectSet(name, OBJPROP_STYLE, STYLE_DOT);
ObjectSet(name, OBJPROP_COLOR, Col);
ObjectSet(name,OBJPROP_WIDTH,1);
}