Download indicators : TrendStrength oma channelmtf alerts

CABAFX00074TrendStrength oma channel_mtf_alerts indicator

 

 

 

 

 

 

//+------------------------------------------------------------------+
//|                                  TrendStrength jurik channel.mq4 |
//|                                                                  |
//|                                         http://www.forextsd.com  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2010, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  DarkOrange
#property indicator_width1  4
#property indicator_color2  White
#property indicator_style2  STYLE_DASH
#property indicator_color3  Red
#property indicator_width3  1
#property indicator_color4  LimeGreen
#property indicator_width4  1
#property indicator_level1  0
#property indicator_levelcolor Violet

extern string TimeFrame        = "Current time frame";
extern int    OmaLength        = 21;
extern double OmaSpeed         = 8.0;
extern bool   OmaAdaptive      = true;
extern int    HighLowPeriod    = 30;
extern bool   ShowChannel      = true;
extern bool   ShowZigZag       = true;
extern bool   Interpolate      = true;

extern int    buyarrowwidth    = 2;
extern color  buyarrowcolor    = Lime;
extern int    sellarrowwidth   = 2;
extern color  sellarrowcolor   = Crimson;
extern int    buy1arrowwidth   = 2;
extern color  buy1arrowcolor   = Aqua;
extern int    sell1arrowwidth  = 2;
extern color  sell1arrowcolor  = Magenta;

extern string _                = "alerts settings";
extern bool   alertsOn         = true;
extern bool   alertsOnCurrent  = true;
extern bool   alertsMessage    = true;
extern bool   alertsSound      = false;
extern bool   alertsEmail      = false;

double TS[];
double ZigZagBuffer[];
double HighBuffer[];
double LowBuffer[];
double ZigZagLow[];
double ZigZagHigh[];
double trend[];
double atrend[];
double stored[][7];

string indicatorFileName;
bool   returnBars;
bool   calculateValue;
int    timeFrame;

int init()
{
IndicatorBuffers(7);
SetIndexBuffer(0,TS);           SetIndexLabel(0,"TS");
SetIndexBuffer(1,ZigZagBuffer); SetIndexLabel(1, NULL); SetIndexStyle(1,DRAW_SECTION);
SetIndexBuffer(2,HighBuffer);
SetIndexBuffer(3,LowBuffer);
SetIndexBuffer(4,ZigZagLow);
SetIndexBuffer(5,ZigZagHigh);
SetIndexBuffer(6,trend);

SetIndexEmptyValue(0,0);
SetIndexEmptyValue(1,0);
SetIndexEmptyValue(4,0);
SetIndexEmptyValue(5,0);
SetIndexEmptyValue(6,0);

if (ShowChannel)
{
SetIndexStyle(2,DRAW_LINE);
SetIndexStyle(3,DRAW_LINE);
}
else
{
SetIndexStyle(2,DRAW_NONE);
SetIndexStyle(3,DRAW_NONE);
}

OmaSpeed          = MathMax(OmaSpeed ,-1.5);
indicatorFileName = WindowExpertName();
calculateValue    = (TimeFrame=="calculateValue"); if (calculateValue) return(0);
returnBars        = (TimeFrame=="returnBars");     if (returnBars)     return(0);
timeFrame         = stringToTimeFrame(TimeFrame);

IndicatorShortName(timeFrameToString(timeFrame) + " TrendStrength oma channel");

return(0);
}

int deinit()
{
for(int j=Bars; j>=0; j--)
{
ObjectDelete("Sell" +DoubleToStr(j,0));
ObjectDelete("Buy"  +DoubleToStr(j,0));
ObjectDelete("Sell1"+DoubleToStr(j,0));
ObjectDelete("Buy1" +DoubleToStr(j,0));
}
return(0);
}

int start()
{
int lastZag;
int counted_bars=IndicatorCounted();
int i,r,limit;

if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit = MathMin(Bars-counted_bars,Bars-1);
if (ArrayRange(stored,0) != Bars) { ArrayResize(stored,Bars); }
if (returnBars)  { TS[0] = limit+1; return(0); }

if (ShowZigZag)
for (lastZag=limit+1; lastZag<Bars; lastZag++) if (ZigZagLow[lastZag] != 0 || ZigZagHigh[lastZag] != 0) break;

if (calculateValue || timeFrame==Period())
{

for(i=limit, r=Bars-i-1; i>=0; i--, r++)
{
double     tmp = iMA(NULL,0, 5,0, MODE_EMA,PRICE_CLOSE,i);
double ma1=tmp - iMA(NULL,0,10,0,MODE_SMMA,PRICE_CLOSE,i);
double ma2=tmp - iMA(NULL,0,20,0,MODE_SMMA,PRICE_CLOSE,i);
double ma3=tmp - iMA(NULL,0,30,0,MODE_SMMA,PRICE_CLOSE,i);
double ma4=tmp - iMA(NULL,0,40,0,MODE_SMMA,PRICE_CLOSE,i);
double ma5=tmp - iMA(NULL,0,50,0,MODE_SMMA,PRICE_CLOSE,i);
double ma6=tmp - iMA(NULL,0,60,0,MODE_SMMA,PRICE_CLOSE,i);
double ma7=tmp - iMA(NULL,0,70,0,MODE_SMMA,PRICE_CLOSE,i);
double ma8=tmp - iMA(NULL,0,80,0,MODE_SMMA,PRICE_CLOSE,i);
double ma9=tmp - iMA(NULL,0,90,0,MODE_SMMA,PRICE_CLOSE,i);

TS[i] = iAverage((ma1+ma2+ma3+ma4+ma5+ma6+ma7+ma8+ma9)/9,OmaLength,OmaSpeed,OmaAdaptive,r,0);

LowBuffer[i] = TS[ArrayMinimum(TS,HighLowPeriod,i)];
HighBuffer[i]= TS[ArrayMaximum(TS,HighLowPeriod,i)];

trend[i] = trend[i+1];
if (TS[i] > LowBuffer[i]  && TS[i+1]<=LowBuffer[i+1])  trend[i] =  1;
if (TS[i] < HighBuffer[i] && TS[i+1]>=HighBuffer[i+1]) trend[i] = -1;

if (TS[i] > LowBuffer[i] && TS[i+1]==LowBuffer[i+1])
{
ObjectCreate("Buy"+DoubleToStr(i,0),OBJ_ARROW,0,Time[i],Low[i]-iATR(NULL,0,20,i)/2.0);
ObjectSet   ("Buy"+DoubleToStr(i,0),OBJPROP_ARROWCODE,228);
ObjectSet   ("Buy"+DoubleToStr(i,0),OBJPROP_WIDTH,buyarrowwidth);
ObjectSet   ("Buy"+DoubleToStr(i,0),OBJPROP_COLOR,buyarrowcolor);

}

if (TS[i] < HighBuffer[i] && TS[i+1]==HighBuffer[i+1])
{
ObjectCreate("Sell"+DoubleToStr(i,0),OBJ_ARROW,0,Time[i],High[i]+iATR(NULL,0,20,i)/2.0);
ObjectSet   ("Sell"+DoubleToStr(i,0),OBJPROP_ARROWCODE,230);
ObjectSet   ("Sell"+DoubleToStr(i,0),OBJPROP_WIDTH,sellarrowwidth);
ObjectSet   ("Sell"+DoubleToStr(i,0),OBJPROP_COLOR,sellarrowcolor);

}

if (HighBuffer[i] > HighBuffer[i+1] && HighBuffer[i+1]==HighBuffer[i+2])
{
ObjectCreate("Buy1"+DoubleToStr(i,0),OBJ_ARROW,0,Time[i],Low[i]-iATR(NULL,0,20,i)/2.0);
ObjectSet   ("Buy1"+DoubleToStr(i,0),OBJPROP_ARROWCODE,225);
ObjectSet   ("Buy1"+DoubleToStr(i,0),OBJPROP_WIDTH,buy1arrowwidth);
ObjectSet   ("Buy1"+DoubleToStr(i,0),OBJPROP_COLOR,buy1arrowcolor);
}

if (LowBuffer[i] < LowBuffer[i+1] && LowBuffer[i+1]==LowBuffer[i+2])
{
ObjectCreate("Sell1"+DoubleToStr(i,0),OBJ_ARROW,0,Time[i],High[i]+iATR(NULL,0,20,i)/2.0);
ObjectSet   ("Sell1"+DoubleToStr(i,0),OBJPROP_ARROWCODE,226);
ObjectSet   ("Sell1"+DoubleToStr(i,0),OBJPROP_WIDTH,sell1arrowwidth);
ObjectSet   ("Sell1"+DoubleToStr(i,0),OBJPROP_COLOR,sell1arrowcolor);
}

if (!ShowZigZag) continue;

ZigZagLow[i]  = 0;
ZigZagHigh[i] = 0;
if (LowBuffer[i] < LowBuffer[i+1])
{
if (ZigZagBuffer[lastZag] == LowBuffer[lastZag])
ZigZagBuffer[lastZag] = 0;
ZigZagBuffer[i]       =  LowBuffer[i];
ZigZagLow[i]          =  LowBuffer[i];
lastZag = i;
continue;
}
if (HighBuffer[i] > HighBuffer[i+1])
{
if (ZigZagBuffer[lastZag] == HighBuffer[lastZag])
ZigZagBuffer[lastZag] = 0;
ZigZagBuffer[i]       =  HighBuffer[i];
ZigZagHigh[i]         =  HighBuffer[i];
lastZag = i;
continue;
}
if (ZigZagBuffer[i] != 0)
{
ZigZagBuffer[i] = 0;
for (lastZag=i+1; lastZag<Bars; lastZag++)
{
if (ZigZagLow[lastZag]  != 0) { ZigZagBuffer[lastZag] = ZigZagLow[lastZag];  break; }
if (ZigZagHigh[lastZag] != 0) { ZigZagBuffer[lastZag] = ZigZagHigh[lastZag]; break; }
}
}
}
manageAlerts();

return(0);
}

if (ShowZigZag)
for (lastZag=limit+1; lastZag<Bars; lastZag++) if (ZigZagLow[lastZag] != 0 || ZigZagHigh[lastZag] != 0) break;

limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
for(i=limit; i>=0; i--)
{
int y = iBarShift(NULL,timeFrame,Time[i]);

TS[i]           = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",OmaLength,OmaSpeed,OmaAdaptive,HighLowPeriod,ShowChannel,ShowZigZag,0,y);
ZigZagBuffer[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",OmaLength,OmaSpeed,OmaAdaptive,HighLowPeriod,ShowChannel,ShowZigZag,1,y);
HighBuffer[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",OmaLength,OmaSpeed,OmaAdaptive,HighLowPeriod,ShowChannel,ShowZigZag,2,y);
LowBuffer[i]    = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",OmaLength,OmaSpeed,OmaAdaptive,HighLowPeriod,ShowChannel,ShowZigZag,3,y);
ZigZagLow[i]    = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",OmaLength,OmaSpeed,OmaAdaptive,HighLowPeriod,ShowChannel,ShowZigZag,4,y);
ZigZagHigh[i]   = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",OmaLength,OmaSpeed,OmaAdaptive,HighLowPeriod,ShowChannel,ShowZigZag,5,y);
trend[i]        = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",OmaLength,OmaSpeed,OmaAdaptive,HighLowPeriod,ShowChannel,ShowZigZag,6,y);

trend[i] = trend[i+1];
if (TS[i] > LowBuffer[i]  && TS[i+1]<=LowBuffer[i+1])  trend[i] =  1;
if (TS[i] < HighBuffer[i] && TS[i+1]>=HighBuffer[i+1]) trend[i] = -1;

if (timeFrame <= Period() || y==iBarShift(NULL,timeFrame,Time[i-1])) continue;
if (!Interpolate) continue;

datetime time = iTime(NULL,timeFrame,y);
for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;
double factor = 1.0 / n;
for(int k = 1; k < n; k++)

TS[i+k]            = k*factor*TS[i+n]            + (1.0-k*factor)*TS[i];
ZigZagBuffer[i+k]  = k*factor*ZigZagBuffer[i+n]  + (1.0-k*factor)*ZigZagBuffer[i];
HighBuffer[i+k]    = k*factor*HighBuffer[i+n]    + (1.0-k*factor)*HighBuffer[i];
LowBuffer[i+k]     = k*factor*LowBuffer[i+n]     + (1.0-k*factor)*LowBuffer[i];
ZigZagLow[i+k]     = k*factor*ZigZagLow[i+n]     + (1.0-k*factor)*ZigZagLow[i];
ZigZagHigh[i+k]    = k*factor*ZigZagHigh[i+n]    + (1.0-k*factor)*ZigZagHigh[i];
}
manageAlerts();
return(0);

}

void manageAlerts()
{
if (alertsOn)
{
if (alertsOnCurrent)
int whichBar = 0;
else     whichBar = 1; whichBar = iBarShift(NULL,0,iTime(NULL,timeFrame,whichBar));
if (trend[whichBar] != trend[whichBar+1])
{
if (trend[whichBar] == 1) doAlert(whichBar,"up");
if (trend[whichBar] ==-1) doAlert(whichBar,"down");
}
}
}

//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
static string   previousAlert="nothing";
static datetime previousTime;
string message;

if (previousAlert != doWhat || previousTime != Time[forBar]) {
previousAlert  = doWhat;
previousTime   = Time[forBar];

//
//
//
//
//

message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," - ",timeFrameToString(timeFrame)+" Trendstrength oma trend changed to ",doWhat);
if (alertsMessage) Alert(message);
if (alertsEmail)   SendMail(StringConcatenate(Symbol()," Trendstrength oma "),message);
if (alertsSound)   PlaySound("alert2.wav");
}
}

#define E1  0
#define E2  1
#define E3  2
#define E4  3
#define E5  4
#define E6  5
#define res 6

//
//
//
//
//

double iAverage(double price, double averagePeriod, double const, bool adaptive, int r, int ashift=0)
{
double e1=stored[r-1][E1+ashift];  double e2=stored[r-1][E2+ashift];
double e3=stored[r-1][E3+ashift];  double e4=stored[r-1][E4+ashift];
double e5=stored[r-1][E5+ashift];  double e6=stored[r-1][E6+ashift];

//
//
//
//
//

if (adaptive && (averagePeriod > 1))
{
double minPeriod = averagePeriod/2.0;
double maxPeriod = minPeriod*5.0;
int    endPeriod = MathCeil(maxPeriod);
double signal    = MathAbs((price-stored[r-endPeriod][res]));
double noise     = 0.00000000001;

for(int k=1; k<endPeriod; k++) noise=noise+MathAbs(price-stored[r-k][res]);

averagePeriod = ((signal/noise)*(maxPeriod-minPeriod))+minPeriod;
}

//
//
//
//
//

double alpha = (2.0+const)/(1.0+const+averagePeriod);

e1 = e1 + alpha*(price-e1); e2 = e2 + alpha*(e1-e2); double v1 = 1.5 * e1 - 0.5 * e2;
e3 = e3 + alpha*(v1   -e3); e4 = e4 + alpha*(e3-e4); double v2 = 1.5 * e3 - 0.5 * e4;
e5 = e5 + alpha*(v2   -e5); e6 = e6 + alpha*(e5-e6); double v3 = 1.5 * e5 - 0.5 * e6;

//
//
//
//
//

stored[r][E1+ashift]  = e1;  stored[r][E2+ashift] = e2;
stored[r][E3+ashift]  = e3;  stored[r][E4+ashift] = e4;
stored[r][E5+ashift]  = e5;  stored[r][E6+ashift] = e6;
stored[r][res+ashift] = price;
return(v3);
}

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
tfs = StringUpperCase(tfs);
for (int i=ArraySize(iTfTable)-1; i>=0; i--)
if (tfs==sTfTable[i] || tfs==""+iTfTable[i]) return(MathMax(iTfTable[i],Period()));
return(Period());
}
string timeFrameToString(int tf)
{
for (int i=ArraySize(iTfTable)-1; i>=0; i--)
if (tf==iTfTable[i]) return(sTfTable[i]);
return("");
}

//
//
//
//
//

string StringUpperCase(string str)
{
string   s = str;

for (int length=StringLen(str)-1; length>=0; length--)
{
int char = StringGetChar(s, length);
if((char > 96 && char < 123) || (char > 223 && char < 256))
s = StringSetChar(s, length, char - 32);
else if(char > -33 && char < 0)
s = StringSetChar(s, length, char + 224);
}
return(s);
}

 

 

 

 

Click Here to Download

0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *