#property indicator_separate_window
#property indicator_buffers 10
#property indicator_color1 clrLightSalmon
#property indicator_color2 clrLightSalmon
#property indicator_color3 clrAqua
#property indicator_color4 clrAqua
#property indicator_color5 clrOrangeRed
#property indicator_color6 clrOrangeRed
#property indicator_color7 clrBlue
#property indicator_color8 clrWhite
#property indicator_color9 clrLime
#property indicator_color10 clrOrange
#property indicator_style1 STYLE_DOT
#property indicator_style2 STYLE_DOT
#property indicator_style3 STYLE_DOT
#property indicator_style4 STYLE_DOT
#property indicator_style5 STYLE_DOT
#property indicator_style6 STYLE_DOT
#property indicator_style7 STYLE_DASH
//
//
//
//
//
//
//
//
//
//
enum enPrices
{
pr_close, // Close
pr_open, // Open
pr_high, // High
pr_low, // Low
pr_median, // Median
pr_typical, // Typical
pr_weighted, // Weighted
pr_average, // Average (high+low+open+close)/4
pr_medianb, // Average median body (open+close)/2
pr_tbiased, // Trend biased price
pr_haclose, // Heiken ashi close
pr_haopen , // Heiken ashi open
pr_hahigh, // Heiken ashi high
pr_halow, // Heiken ashi low
pr_hamedian, // Heiken ashi median
pr_hatypical, // Heiken ashi typical
pr_haweighted, // Heiken ashi weighted
pr_haaverage, // Heiken ashi average
pr_hamedianb, // Heiken ashi median body
pr_hatbiased // Heiken ashi trend biased price
};
extern ENUM_TIMEFRAMES TimeFrame = PERIOD_CURRENT;
extern int EnvelopePeriod = 21;
extern int EnvelopeMaShift = 0;
extern ENUM_MA_METHOD EnvelopeMaMode = MODE_EMA;
extern ENUM_APPLIED_PRICE EnvelopePrice = PRICE_CLOSE;
extern int CenterMaPeriod = 89;
extern int CenterMaShift = 0;
extern ENUM_MA_METHOD CenterMaMode = MODE_EMA;
extern ENUM_APPLIED_PRICE CenterMaPrice = PRICE_CLOSE;
extern enPrices PriceType = pr_close;
extern double InnerDeviation = 0.236;
extern double MidDeviation = 0.382;
extern double OuterDeviation = 0.618;
extern bool drawDivergences = false;
extern int DivergearrowSize = 0;
extern double DivergencearrowsUpperGap = 0.002;
extern double DivergencearrowsLowerGap = 0.002;
extern bool ShowClassicalDivergence = true;
extern bool ShowHiddenDivergence = true;
extern bool drawIndicatorTrendLines = true;
extern bool drawPriceTrendLines = true;
extern string drawLinesIdentificator = "fibenvdiverge1";
extern color divergenceBullishColor = clrLime;
extern color divergenceBearishColor = clrMagenta;
extern bool divergenceAlert = true;
extern bool divergenceAlertsMessage = true;
extern bool divergenceAlertsSound = true;
extern bool divergenceAlertsEmail = false;
extern bool divergenceAlertsNotify = false;
extern string divergenceAlertsSoundName = "alert.wav";
extern bool alertsOn = false; // Turn alerts on
extern bool alertsOnCurrent = true; // Alerts on currnt (still opened) bar
extern bool alertsOnCenterCross = false;
extern bool alertsOnInnerEnvCross = true;
extern bool alertsMessage = true; // Alerts should show pop-up message
extern bool alertsSound = false; // Alerts should play alert sound
extern bool alertsPushNotif = false; // Alerts should send push notification
extern bool alertsEmail = false; // Alerts should send email
extern bool arrowsVisible = false; // Arrows visible?
extern bool arrowsOnNewest = false; // Arrows drawn on newst bar of higher time frame bar?
extern string arrowsIdentifier = "fibenv Arrows1"; // Unique ID for arrows
extern double arrowsUpperGap = 0.5; // Upper arrow gap
extern double arrowsLowerGap = 0.5; // Lower arrow gap
extern color arrowsUpColor = clrLimeGreen;
extern color arrowsDnColor = clrRed;
extern int arrowsUpCode = 241;
extern int arrowsDnCode = 242;
extern bool Interpolate = true;
//
//
//
//
//
double InUpEnv[];
double InDnEnv[];
double MiUpEnv[];
double MiDnEnv[];
double OuUpEnv[];
double OuDnEnv[];
double center[];
double prc[];
double bullishDivergence[];
double bearishDivergence[];
double trend[];
double cross[];
string indicatorFileName;
bool returnBars;
string indicatorName;
string labelNames;
//+------------------------------------------------------------------
//|
//+------------------------------------------------------------------
//
//
//
//
//
int init()
{
IndicatorBuffers(12);
SetIndexBuffer(0,InUpEnv);
SetIndexBuffer(1,InDnEnv);
SetIndexBuffer(2,MiUpEnv);
SetIndexBuffer(3,MiDnEnv);
SetIndexBuffer(4,OuUpEnv);
SetIndexBuffer(5,OuDnEnv);
SetIndexBuffer(6,center);
SetIndexBuffer(7,prc);
SetIndexBuffer(8,bullishDivergence); SetIndexStyle(8,DRAW_ARROW,0,DivergearrowSize); SetIndexArrow(8,233);
SetIndexBuffer(9,bearishDivergence); SetIndexStyle(9,DRAW_ARROW,0,DivergearrowSize); SetIndexArrow(9,234);
SetIndexBuffer(10,trend);
SetIndexBuffer(11,cross);
//
//
//
//
//
indicatorFileName = WindowExpertName();
returnBars = TimeFrame==-99;
TimeFrame = MathMax(TimeFrame,_Period);
SetIndexShift(0,EnvelopeMaShift * TimeFrame/Period());
SetIndexShift(1,EnvelopeMaShift * TimeFrame/Period());
SetIndexShift(2,EnvelopeMaShift * TimeFrame/Period());
SetIndexShift(3,EnvelopeMaShift * TimeFrame/Period());
SetIndexShift(4,EnvelopeMaShift * TimeFrame/Period());
SetIndexShift(5,EnvelopeMaShift * TimeFrame/Period());
SetIndexShift(6,CenterMaShift * TimeFrame/Period());
labelNames = "Fib envelopes_DivergenceLine "+drawLinesIdentificator+":";
indicatorName = timeFrameToString(TimeFrame)+" Fib Envelopes";
IndicatorShortName(indicatorName);
return (0);
}
int deinit()
{
int length=StringLen(labelNames);
for(int i=ObjectsTotal()-1; i>=0; i--)
{
string name = ObjectName(i);
if(StringSubstr(name,0,length) == labelNames) ObjectDelete(name);
}
string lookFor = arrowsIdentifier+":";
int lookForLength = StringLen(lookFor);
for (i=ObjectsTotal()-1; i>=0; i--)
{
string objectName = ObjectName(i);
if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName);
}
return(0);
}
//+------------------------------------------------------------------
//|
//+------------------------------------------------------------------
//
//
//
//
//
int start()
{
int counted_bars = IndicatorCounted();
int limit;
if(counted_bars < 0) return(-1);
if(counted_bars > 0) counted_bars--;
limit = MathMin(Bars - counted_bars,Bars-1);
if (returnBars) { InUpEnv[0] = MathMin(limit+1,Bars-1); return(0); }
//
//
//
//
//
if (TimeFrame == Period())
{
for (int i=limit; i>=0; i--)
{
InUpEnv[i] = (1+InnerDeviation/100)*iMA(NULL,0,EnvelopePeriod,EnvelopeMaShift,EnvelopeMaMode,EnvelopePrice,i);
InDnEnv[i] = (1-InnerDeviation/100)*iMA(NULL,0,EnvelopePeriod,EnvelopeMaShift,EnvelopeMaMode,EnvelopePrice,i);
MiUpEnv[i] = (1+MidDeviation /100)*iMA(NULL,0,EnvelopePeriod,EnvelopeMaShift,EnvelopeMaMode,EnvelopePrice,i);
MiDnEnv[i] = (1-MidDeviation /100)*iMA(NULL,0,EnvelopePeriod,EnvelopeMaShift,EnvelopeMaMode,EnvelopePrice,i);
OuUpEnv[i] = (1+OuterDeviation/100)*iMA(NULL,0,EnvelopePeriod,EnvelopeMaShift,EnvelopeMaMode,EnvelopePrice,i);
OuDnEnv[i] = (1-OuterDeviation/100)*iMA(NULL,0,EnvelopePeriod,EnvelopeMaShift,EnvelopeMaMode,EnvelopePrice,i);
center[i] = iMA(NULL,0,CenterMaPeriod,CenterMaShift,CenterMaMode,CenterMaPrice,i);
prc[i] = getPrice(PriceType,Open,Close,High,Low,i);
trend[i] = trend[i+1];
cross[i] = 0;
if (prc[i]>center[i]) trend[i] = 1;
if (prc[i]
InUpEnv[i]) cross[i] =-1;
if (drawDivergences)
{
CatchBullishDivergence(i);
CatchBearishDivergence(i);
}
//
//
//
//
//
if (arrowsVisible)
{
string lookFor = arrowsIdentifier+":"+(string)Time[i]; ObjectDelete(lookFor);
if (trend[i]!=trend[i+1])
{
if (trend[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,false);
if (trend[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode, true);
}
}
}
//
//
//
//
//
if (alertsOn)
{
if (alertsOnCurrent)
int whichBar = 0;
else whichBar = 1;
static datetime time1 = 0;
static string mess1 = "";
if (alertsOnCenterCross && trend[whichBar] != trend[whichBar+1])
{
if (trend[whichBar] == 1 ) doAlert(time1,mess1,whichBar,"crossing center up");
if (trend[whichBar] == -1 ) doAlert(time1,mess1,whichBar,"crossing center down");
}
static datetime time2 = 0;
static string mess2 = "";
if (alertsOnInnerEnvCross && cross[whichBar] != cross[whichBar+1])
{
if (cross[whichBar] == 1 ) doAlert(time2,mess2,whichBar,"crossing inner lower envelope");
if (cross[whichBar] == -1 ) doAlert(time2,mess2,whichBar,"crossing inner upper envelope");
}
}
return (0);
}
//
//
//
//
//
limit = MathMax(limit,MathMin(Bars-1,iCustom(NULL,TimeFrame,indicatorFileName,-99,0,0)*TimeFrame/Period()));
for (i=limit;i>=0; i--)
{
int y = iBarShift(NULL,TimeFrame,Time[i]);
InUpEnv[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,EnvelopePeriod,0,EnvelopeMaMode,EnvelopePrice,CenterMaPeriod,0,CenterMaMode,CenterMaPrice,PriceType,InnerDeviation,MidDeviation,OuterDeviation,drawDivergences,DivergearrowSize,DivergencearrowsUpperGap,DivergencearrowsLowerGap,ShowClassicalDivergence,ShowHiddenDivergence,drawIndicatorTrendLines,drawPriceTrendLines,drawLinesIdentificator,divergenceBullishColor,divergenceBearishColor,divergenceAlert,divergenceAlertsMessage,divergenceAlertsSound,divergenceAlertsEmail,divergenceAlertsNotify,divergenceAlertsSoundName,alertsOn,alertsOnCurrent,alertsOnCenterCross,alertsOnInnerEnvCross,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,0,y);
InDnEnv[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,EnvelopePeriod,0,EnvelopeMaMode,EnvelopePrice,CenterMaPeriod,0,CenterMaMode,CenterMaPrice,PriceType,InnerDeviation,MidDeviation,OuterDeviation,drawDivergences,DivergearrowSize,DivergencearrowsUpperGap,DivergencearrowsLowerGap,ShowClassicalDivergence,ShowHiddenDivergence,drawIndicatorTrendLines,drawPriceTrendLines,drawLinesIdentificator,divergenceBullishColor,divergenceBearishColor,divergenceAlert,divergenceAlertsMessage,divergenceAlertsSound,divergenceAlertsEmail,divergenceAlertsNotify,divergenceAlertsSoundName,alertsOn,alertsOnCurrent,alertsOnCenterCross,alertsOnInnerEnvCross,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,1,y);
MiUpEnv[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,EnvelopePeriod,0,EnvelopeMaMode,EnvelopePrice,CenterMaPeriod,0,CenterMaMode,CenterMaPrice,PriceType,InnerDeviation,MidDeviation,OuterDeviation,drawDivergences,DivergearrowSize,DivergencearrowsUpperGap,DivergencearrowsLowerGap,ShowClassicalDivergence,ShowHiddenDivergence,drawIndicatorTrendLines,drawPriceTrendLines,drawLinesIdentificator,divergenceBullishColor,divergenceBearishColor,divergenceAlert,divergenceAlertsMessage,divergenceAlertsSound,divergenceAlertsEmail,divergenceAlertsNotify,divergenceAlertsSoundName,alertsOn,alertsOnCurrent,alertsOnCenterCross,alertsOnInnerEnvCross,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,2,y);
MiDnEnv[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,EnvelopePeriod,0,EnvelopeMaMode,EnvelopePrice,CenterMaPeriod,0,CenterMaMode,CenterMaPrice,PriceType,InnerDeviation,MidDeviation,OuterDeviation,drawDivergences,DivergearrowSize,DivergencearrowsUpperGap,DivergencearrowsLowerGap,ShowClassicalDivergence,ShowHiddenDivergence,drawIndicatorTrendLines,drawPriceTrendLines,drawLinesIdentificator,divergenceBullishColor,divergenceBearishColor,divergenceAlert,divergenceAlertsMessage,divergenceAlertsSound,divergenceAlertsEmail,divergenceAlertsNotify,divergenceAlertsSoundName,alertsOn,alertsOnCurrent,alertsOnCenterCross,alertsOnInnerEnvCross,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,3,y);
OuUpEnv[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,EnvelopePeriod,0,EnvelopeMaMode,EnvelopePrice,CenterMaPeriod,0,CenterMaMode,CenterMaPrice,PriceType,InnerDeviation,MidDeviation,OuterDeviation,drawDivergences,DivergearrowSize,DivergencearrowsUpperGap,DivergencearrowsLowerGap,ShowClassicalDivergence,ShowHiddenDivergence,drawIndicatorTrendLines,drawPriceTrendLines,drawLinesIdentificator,divergenceBullishColor,divergenceBearishColor,divergenceAlert,divergenceAlertsMessage,divergenceAlertsSound,divergenceAlertsEmail,divergenceAlertsNotify,divergenceAlertsSoundName,alertsOn,alertsOnCurrent,alertsOnCenterCross,alertsOnInnerEnvCross,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,4,y);
OuDnEnv[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,EnvelopePeriod,0,EnvelopeMaMode,EnvelopePrice,CenterMaPeriod,0,CenterMaMode,CenterMaPrice,PriceType,InnerDeviation,MidDeviation,OuterDeviation,drawDivergences,DivergearrowSize,DivergencearrowsUpperGap,DivergencearrowsLowerGap,ShowClassicalDivergence,ShowHiddenDivergence,drawIndicatorTrendLines,drawPriceTrendLines,drawLinesIdentificator,divergenceBullishColor,divergenceBearishColor,divergenceAlert,divergenceAlertsMessage,divergenceAlertsSound,divergenceAlertsEmail,divergenceAlertsNotify,divergenceAlertsSoundName,alertsOn,alertsOnCurrent,alertsOnCenterCross,alertsOnInnerEnvCross,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,5,y);
center[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,EnvelopePeriod,0,EnvelopeMaMode,EnvelopePrice,CenterMaPeriod,0,CenterMaMode,CenterMaPrice,PriceType,InnerDeviation,MidDeviation,OuterDeviation,drawDivergences,DivergearrowSize,DivergencearrowsUpperGap,DivergencearrowsLowerGap,ShowClassicalDivergence,ShowHiddenDivergence,drawIndicatorTrendLines,drawPriceTrendLines,drawLinesIdentificator,divergenceBullishColor,divergenceBearishColor,divergenceAlert,divergenceAlertsMessage,divergenceAlertsSound,divergenceAlertsEmail,divergenceAlertsNotify,divergenceAlertsSoundName,alertsOn,alertsOnCurrent,alertsOnCenterCross,alertsOnInnerEnvCross,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,6,y);
prc[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,EnvelopePeriod,0,EnvelopeMaMode,EnvelopePrice,CenterMaPeriod,0,CenterMaMode,CenterMaPrice,PriceType,InnerDeviation,MidDeviation,OuterDeviation,drawDivergences,DivergearrowSize,DivergencearrowsUpperGap,DivergencearrowsLowerGap,ShowClassicalDivergence,ShowHiddenDivergence,drawIndicatorTrendLines,drawPriceTrendLines,drawLinesIdentificator,divergenceBullishColor,divergenceBearishColor,divergenceAlert,divergenceAlertsMessage,divergenceAlertsSound,divergenceAlertsEmail,divergenceAlertsNotify,divergenceAlertsSoundName,alertsOn,alertsOnCurrent,alertsOnCenterCross,alertsOnInnerEnvCross,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,7,y);
bullishDivergence[i] = EMPTY_VALUE;
bearishDivergence[i] = EMPTY_VALUE;
int firstBar = iBarShift(NULL,0,iTime(NULL,TimeFrame,y));
if (i==firstBar)
{
bullishDivergence[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,EnvelopePeriod,0,EnvelopeMaMode,EnvelopePrice,CenterMaPeriod,0,CenterMaMode,CenterMaPrice,PriceType,InnerDeviation,MidDeviation,OuterDeviation,drawDivergences,DivergearrowSize,DivergencearrowsUpperGap,DivergencearrowsLowerGap,ShowClassicalDivergence,ShowHiddenDivergence,drawIndicatorTrendLines,drawPriceTrendLines,drawLinesIdentificator,divergenceBullishColor,divergenceBearishColor,divergenceAlert,divergenceAlertsMessage,divergenceAlertsSound,divergenceAlertsEmail,divergenceAlertsNotify,divergenceAlertsSoundName,alertsOn,alertsOnCurrent,alertsOnCenterCross,alertsOnInnerEnvCross,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,8,y);
bearishDivergence[i] = iCustom(NULL,TimeFrame,indicatorFileName,PERIOD_CURRENT,EnvelopePeriod,0,EnvelopeMaMode,EnvelopePrice,CenterMaPeriod,0,CenterMaMode,CenterMaPrice,PriceType,InnerDeviation,MidDeviation,OuterDeviation,drawDivergences,DivergearrowSize,DivergencearrowsUpperGap,DivergencearrowsLowerGap,ShowClassicalDivergence,ShowHiddenDivergence,drawIndicatorTrendLines,drawPriceTrendLines,drawLinesIdentificator,divergenceBullishColor,divergenceBearishColor,divergenceAlert,divergenceAlertsMessage,divergenceAlertsSound,divergenceAlertsEmail,divergenceAlertsNotify,divergenceAlertsSoundName,alertsOn,alertsOnCurrent,alertsOnCenterCross,alertsOnInnerEnvCross,alertsMessage,alertsSound,alertsPushNotif,alertsEmail,arrowsVisible,arrowsOnNewest,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,9,y);
}
//
//
//
//
//
if (!Interpolate || y==iBarShift(NULL,TimeFrame,Time[i-1])) continue;
//
//
//
//
//
datetime time = iTime(NULL,TimeFrame,y);
for(int n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;
for(int k = 1; k < n; k++)
{
InUpEnv[i+k] = InUpEnv[i] + (InUpEnv[i+n] - InUpEnv[i]) * k/n;
InDnEnv[i+k] = InDnEnv[i] + (InDnEnv[i+n] - InDnEnv[i]) * k/n;
MiUpEnv[i+k] = MiUpEnv[i] + (MiUpEnv[i+n] - MiUpEnv[i]) * k/n;
MiDnEnv[i+k] = MiDnEnv[i] + (MiDnEnv[i+n] - MiDnEnv[i]) * k/n;
OuUpEnv[i+k] = OuUpEnv[i] + (OuUpEnv[i+n] - OuUpEnv[i]) * k/n;
OuDnEnv[i+k] = OuDnEnv[i] + (OuDnEnv[i+n] - OuDnEnv[i]) * k/n;
center[i+k] = center[i] + (center[i+n] - center[i]) * k/n;
prc[i+k] = prc[i] + (prc[i+n] - prc[i]) * k/n;
}
}
return(0);
}
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//
string sTfTable[] = {"M1","M5","M10","M15","M30","H1","H4","D1","W1","MN"};
int iTfTable[] = {1,5,10,15,30,60,240,1440,10080,43200};
string timeFrameToString(int tf)
{
for (int i=ArraySize(iTfTable)-1; i>=0; i--)
if (tf==iTfTable[i]) return(sTfTable[i]);
return("");
}
//
//
//
//
//
double workHa[][4];
double getPrice(int price, const double& open[], const double& close[], const double& high[], const double& low[], int i, int instanceNo=0)
{
if (price>=pr_haclose && price<=pr_hatbiased)
{
if (ArrayRange(workHa,0)!= Bars) ArrayResize(workHa,Bars);
int r = Bars-i-1;
//
//
//
//
//
double haOpen;
if (r>0)
haOpen = (workHa[r-1][instanceNo+2] + workHa[r-1][instanceNo+3])/2.0;
else haOpen = (open[i]+close[i])/2;
double haClose = (open[i] + high[i] + low[i] + close[i]) / 4.0;
double haHigh = MathMax(high[i], MathMax(haOpen,haClose));
double haLow = MathMin(low[i] , MathMin(haOpen,haClose));
if(haOpen haOpen)
return((haHigh+haClose)/2.0);
else return((haLow+haClose)/2.0);
}
}
//
//
//
//
//
switch (price)
{
case pr_close: return(close[i]);
case pr_open: return(open[i]);
case pr_high: return(high[i]);
case pr_low: return(low[i]);
case pr_median: return((high[i]+low[i])/2.0);
case pr_medianb: return((open[i]+close[i])/2.0);
case pr_typical: return((high[i]+low[i]+close[i])/3.0);
case pr_weighted: return((high[i]+low[i]+close[i]+close[i])/4.0);
case pr_average: return((high[i]+low[i]+close[i]+open[i])/4.0);
case pr_tbiased:
if (close[i]>open[i])
return((high[i]+close[i])/2.0);
else return((low[i]+close[i])/2.0);
}
return(0);
}
//
//
//
//
//
void CatchBullishDivergence(int shift)
{
shift++;
bullishDivergence[shift] = EMPTY_VALUE;
ObjectDelete(labelNames+"l"+DoubleToStr(Time[shift],0));
ObjectDelete(labelNames+"l"+"os" + DoubleToStr(Time[shift],0));
if(!IsIndicatorLow(shift)) return;
int currentLow = shift;
int lastLow = GetIndicatorLastLow(shift+1);
//
//
//
//
//
if (prc[currentLow] > prc[lastLow] && Low[currentLow] < Low[lastLow])
{
if (ShowClassicalDivergence)
{
bullishDivergence[currentLow] = prc[currentLow] - DivergencearrowsLowerGap;
if (drawPriceTrendLines) DrawPriceTrendLine("l",Time[currentLow],Time[lastLow],Low[currentLow],Low[lastLow], divergenceBullishColor,STYLE_SOLID);
if (drawIndicatorTrendLines)DrawIndicatorTrendLine("l",Time[currentLow],Time[lastLow],prc[currentLow],prc[lastLow],divergenceBullishColor,STYLE_SOLID);
if (divergenceAlert) DisplayAlert("Classical bullish divergence",currentLow);
}
}
//
//
//
//
//
if (prc[currentLow] < prc[lastLow] && Low[currentLow] > Low[lastLow])
{
if (ShowHiddenDivergence)
{
bullishDivergence[currentLow] = prc[currentLow] - DivergencearrowsLowerGap;
if (drawPriceTrendLines) DrawPriceTrendLine("l",Time[currentLow],Time[lastLow],Low[currentLow],Low[lastLow], divergenceBullishColor, STYLE_DOT);
if (drawIndicatorTrendLines) DrawIndicatorTrendLine("l",Time[currentLow],Time[lastLow],prc[currentLow],prc[lastLow],divergenceBullishColor, STYLE_DOT);
if (divergenceAlert) DisplayAlert("Reverse bullish divergence",currentLow);
}
}
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
//
//
//
void CatchBearishDivergence(int shift)
{
shift++;
bearishDivergence[shift] = EMPTY_VALUE;
ObjectDelete(labelNames+"h"+DoubleToStr(Time[shift],0));
ObjectDelete(labelNames+"h"+"os" + DoubleToStr(Time[shift],0));
if(!IsIndicatorPeak(shift)) return;
int currentPeak = shift;
int lastPeak = GetIndicatorLastPeak(shift+1);
//
//
//
//
//
if (prc[currentPeak] < prc[lastPeak] && High[currentPeak]>High[lastPeak])
{
if (ShowClassicalDivergence)
{
bearishDivergence[currentPeak] = prc[currentPeak] + DivergencearrowsUpperGap;
if (drawPriceTrendLines) DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak], divergenceBearishColor,STYLE_SOLID);
if (drawIndicatorTrendLines) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],prc[currentPeak],prc[lastPeak],divergenceBearishColor,STYLE_SOLID);
if (divergenceAlert) DisplayAlert("Classical bearish divergence",currentPeak);
}
}
//
//
//
//
//
if (prc[currentPeak] > prc[lastPeak] && High[currentPeak] < High[lastPeak])
{
if (ShowHiddenDivergence)
{
bearishDivergence[currentPeak] = prc[currentPeak] + DivergencearrowsUpperGap;
if (drawPriceTrendLines) DrawPriceTrendLine("h",Time[currentPeak],Time[lastPeak],High[currentPeak],High[lastPeak], divergenceBearishColor, STYLE_DOT);
if (drawIndicatorTrendLines) DrawIndicatorTrendLine("h",Time[currentPeak],Time[lastPeak],prc[currentPeak],prc[lastPeak],divergenceBearishColor, STYLE_DOT);
if (divergenceAlert) DisplayAlert("Reverse bearish divergence",currentPeak);
}
}
}
//
//
//
//
//
bool IsIndicatorPeak(int shift)
{
if(prc[shift] >= prc[shift+1] && prc[shift] > prc[shift+2] && prc[shift] > prc[shift-1])
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
bool IsIndicatorLow(int shift)
{
if(prc[shift] <= prc[shift+1] && prc[shift] < prc[shift+2] && prc[shift] < prc[shift-1])
return(true);
else
return(false);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
int GetIndicatorLastPeak(int shift)
{
for(int i = shift+5; i < Bars; i++)
{
if(prc[i] >= prc[i+1] && prc[i] > prc[i+2] && prc[i] >= prc[i-1] && prc[i] > prc[i-2])
return(i);
}
return(-1);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
//
//
int GetIndicatorLastLow(int shift)
{
for (int i = shift+5; i < Bars; i++)
{
if (prc[i] <= prc[i+1] && prc[i] < prc[i+2] && prc[i] <= prc[i-1] && prc[i] < prc[i-2])
return(i);
}
return(-1);
}
//
//
//
//
//
void DisplayAlert(string doWhat, int shift)
{
string dmessage;
static datetime lastAlertTime;
if(shift <= 2 && Time[0] != lastAlertTime)
{
dmessage = StringConcatenate(Symbol()," "+timeFrameToString(_Period)+" at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Fib envelopes ",doWhat);
if (divergenceAlertsMessage) Alert(dmessage);
if (divergenceAlertsNotify) SendNotification(dmessage);
if (divergenceAlertsEmail) SendMail(StringConcatenate(Symbol()," Fib envelopes "),dmessage);
if (divergenceAlertsSound) PlaySound(divergenceAlertsSoundName);
lastAlertTime = Time[0];
}
}
//
//
//
//
//
void DrawPriceTrendLine(string first,datetime t1, datetime t2, double p1, double p2, color lineColor, double style)
{
string label = labelNames+first+"os"+DoubleToStr(t1,0);
ObjectDelete(label);
ObjectCreate(label, OBJ_TREND, 0, t1, p1, t2, p2, 0, 0);
ObjectSet(label, OBJPROP_RAY, 0);
ObjectSet(label, OBJPROP_COLOR, lineColor);
ObjectSet(label, OBJPROP_STYLE, style);
}
//
//
//
//
//
void DrawIndicatorTrendLine(string first,datetime t1, datetime t2, double p1, double p2, color lineColor, double style)
{
int indicatorWindow = WindowFind(indicatorName);
if (indicatorWindow < 0) return;
//
//
//
//
//
string label = labelNames+first+DoubleToStr(t1,0);
ObjectDelete(label);
ObjectCreate(label, OBJ_TREND, indicatorWindow, t1, p1, t2, p2, 0, 0);
ObjectSet(label, OBJPROP_RAY, 0);
ObjectSet(label, OBJPROP_COLOR, lineColor);
ObjectSet(label, OBJPROP_STYLE, style);
}
//
//
//
//
//
void doAlert(datetime& previousTime, string& previousAlert, int forBar, string doWhat)
{
string message;
if (previousAlert != doWhat || previousTime != Time[forBar]) {
previousAlert = doWhat;
previousTime = Time[forBar];
//
//
//
//
//
message = StringConcatenate(Symbol()," ",timeFrameToString(_Period)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Fib envelopes ",doWhat);
if (alertsMessage) Alert(message);
if (alertsPushNotif) SendNotification(message);
if (alertsEmail) SendMail(StringConcatenate(Symbol()," Fib envelopes "),message);
if (alertsSound) PlaySound("alert2.wav");
}
}
//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//
void drawArrow(int i,color theColor,int theCode,bool up)
{
string name = arrowsIdentifier+":"+(string)Time[i];
double gap = iATR(NULL,0,20,i);
//
//
//
//
//
datetime time = Time[i]; if (arrowsOnNewest) time += _Period*60-1;
ObjectCreate(name,OBJ_ARROW,0,time,0);
ObjectSet(name,OBJPROP_ARROWCODE,theCode);
ObjectSet(name,OBJPROP_COLOR,theColor);
if (up)
ObjectSet(name,OBJPROP_PRICE1,High[i] + arrowsUpperGap * gap);
else ObjectSet(name,OBJPROP_PRICE1,Low[i] - arrowsLowerGap * gap);
}