#property copyright "Copyright © 2014, xux99" #property link "http://www.forexfactory.com/showthread.php?p=7863803" #property description "An indicator that alerts and draws a horizontal line to mark whenever a fractal is engulfed by a candle close before another fractal prints in the same direction." #property version "1.01" #property strict #property indicator_chart_window #property indicator_buffers 2 input uchar LongArrowCode = 217; input uchar ShortArrowCode = 218; input int ArrowSize = 8; input int LineWidth = 1; input ENUM_LINE_STYLE LineStyle = STYLE_SOLID; input color LongColour = clrBlue; input color ShortColour = clrPeru; input bool ShowComments = true; input bool SendEmail = false; input bool SendNotifications = false; input bool Alerts = false; input string AlertSound = "alert.wav"; input bool ScreenShots = false; double UpBuff[]; double DownBuff[]; double Vd[]; int Vi[150][150],Lines[10],LabelFontSize,tf; color Colour[9]; datetime NewCandle; ENUM_TIMEFRAMES TF[]={PERIOD_CURRENT,PERIOD_M1,PERIOD_M2,PERIOD_M3,PERIOD_M4,PERIOD_M5,PERIOD_M6,PERIOD_M10,PERIOD_M12,PERIOD_M15,PERIOD_M20,PERIOD_M30,PERIOD_H1,PERIOD_H2,PERIOD_H3,PERIOD_H4,PERIOD_H6,PERIOD_H8,PERIOD_H12,PERIOD_D1,PERIOD_W1,PERIOD_MN1}; string TimeFr[]={"0","M1","M2","M3","M4","M5","M6","M10","M12","M15","M20","M30","H1","H2","H3","H4","H6","H8","H12","D1","W1","MN"}; string Vs[150][150],TradeComment,LabelFontType,Prefix,Suffix; void OnInit() {int t1,t2,t3,t4; uchar Char[]; string s7; SetIndexBuffer(0,UpBuff); SetIndexBuffer(1,DownBuff); SetIndexStyle(0,DRAW_NONE); SetIndexStyle(1,DRAW_NONE); SetIndexEmptyValue(0,0.0); SetIndexEmptyValue(1,0.0); tf=0; t1=1; while(t1<=21 && tf==0) {tf=(PeriodSeconds(PERIOD_CURRENT)==PeriodSeconds(TF[t1]))*t1; t1++;} Colour[1]=LongColour; Colour[2]=ShortColour; Colour[3]=16777215; TradeComment="LineFractal"; LabelFontSize=10; LabelFontType="Segoe UI"; t2=StringToCharArray(Symbol(),Char,0,-1,CP_UTF8)-2; t3=-1; t4=-1; t1=0; while(t1<=t2) {if (t3==-1 && Char[t1]>=65 && Char[t1]<=90) {t3=t1;} if (t3!=-1 && t4==-1 && (Char[t1]<65 || Char[t1]>90)) {t4=t1;} t1++;} Prefix=""; if (t3>0) {Prefix=StringSubstr(Symbol(),0,t3);} Suffix=""; if (t4>0) {Suffix=StringSubstr(Symbol(),t4,0);} t1=StringLen(Symbol())-StringLen(Prefix)-StringLen(Suffix); Vs[30][1]=StringSubstr(Symbol(),StringLen(Prefix),t1); s7=StringConcatenate(" ",Vs[30][1]," ",TimeFr[tf]); Vs[30][4]=StringConcatenate(StringSubstr(TradeComment,0,27-StringLen(s7)),s7); t1=0; while(t1<=100) {Vs[41][t1]=StringConcatenate(TradeComment," ",t1); t1++;} Vs[32][1]="Long"; Vs[32][2]="Short"; Vs[32][3]="Up"; Vs[32][4]="Down"; } void OnDeinit(const int reason) {if (!IsTesting()) {RemoveObjects(TradeComment);}} 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 t1,t2,t3; bool b1,b2,b3; datetime h1; double d1; string s1,s2; b2=(NewCandle!=time[0]); if (b2) {t1=1; while(t1<=4) {Lines[t1]=0; t1++;}} t1=1; while(t1<=rates_total-1 && b2) {UpBuff[t1]=0; d1=iFractals(Symbol(),PERIOD_CURRENT,MODE_UPPER,t1); s1=StringConcatenate(TradeComment," up arrow ",TimeToString(time[t1])); s2=StringConcatenate(TradeComment," up line ",TimeToString(time[t1])); if (d1==0) {if (ObjectFind(0,s1)!=-1) {ObjectDelete(0,s1);} if (ObjectFind(0,s2)!=-1) {ObjectDelete(0,s2);}} if (d1>0) {DrawText(s1,CharToString(LongArrowCode),time[t1],d1,ArrowSize,"Wingdings",Colour[1],ANCHOR_LOWER); b1=false; b3=false; t2=t1-1; while(t2>0 && !b3 && !b1) {b1=(close[t2]>=d1); b3=(iFractals(Symbol(),PERIOD_CURRENT,MODE_UPPER,t2)>0); t2--;} t3=t2+b1; if (!b1 && t2>0 && ObjectFind(0,s2)!=-1) {ObjectDelete(0,s2);} if (b1 || t2==0) {Lines[1]+=1; UpBuff[t1]=d1; if (t2==0 && b1) {OrderAlert(1,t2+1);} h1=time[t3]; Lines[3]+=(int)(t2==0); DrawTrendLine(s2,0,h1,d1,time[t1],d1,Colour[1],LineStyle,LineWidth);}} DownBuff[t1]=0; d1=iFractals(Symbol(),PERIOD_CURRENT,MODE_LOWER,t1); s1=StringConcatenate(TradeComment," down arrow ",TimeToString(time[t1])); s2=StringConcatenate(TradeComment," down line ",TimeToString(time[t1])); if (d1==0) {if (ObjectFind(0,s1)!=-1) {ObjectDelete(0,s1);} if (ObjectFind(0,s2)!=-1) {ObjectDelete(0,s2);}} if (d1>0) {DrawText(s1,CharToString(ShortArrowCode),time[t1],d1,ArrowSize,"Wingdings",Colour[2],ANCHOR_UPPER); b1=false; b3=false; t2=t1-1; while(t2>0 && !b3 && !b1) {b1=(close[t2]<=d1); b3=(iFractals(Symbol(),PERIOD_CURRENT,MODE_LOWER,t2)>0); t2--;} t3=t2+b1; if (!b1 && t2>0 && ObjectFind(0,s2)!=-1) {ObjectDelete(0,s2);} if (b1 || t2==0) {Lines[2]+=1; DownBuff[t1]=d1; if (t2==0 && b1) {OrderAlert(2,t2+1);} h1=time[t3]; Lines[4]+=(int)(t2==0); DrawTrendLine(s2,0,h1,d1,time[t1],d1,Colour[2],LineStyle,LineWidth);}} t1++;} NewCandle=time[0]; ScreenInfo(); return(rates_total);} void DrawText(string r6,string r7,datetime x6,double f7,int x5,string r8,color x3,int x7) {if (r7=="" && ObjectFind(0,r6)!=-1) {ObjectDelete(0,r6);} if (r7!="") {if (ObjectFind(0,r6)==-1) {ObjectCreate(0,r6,OBJ_TEXT,0,0,0);} ObjectSetString(0,r6,OBJPROP_TEXT,r7); ObjectSetInteger(0,r6,OBJPROP_TIME,x6); ObjectSetDouble(0,r6,OBJPROP_PRICE,f7); ObjectSetInteger(0,r6,OBJPROP_FONTSIZE,x5); ObjectSetString(0,r6,OBJPROP_FONT,r8); ObjectSetInteger(0,r6,OBJPROP_COLOR,x3); ObjectSetInteger(0,r6,OBJPROP_ANCHOR,x7);}} void DrawTrendLine(string r6,int x1,datetime x6,double f6,datetime x7,double f7,color x3,int x4,int x5) {if (f6==0 && f7==0 && ObjectFind(0,r6)!=-1) {ObjectDelete(0,r6);} if (f6>0 || f7>0) {if (ObjectFind(0,r6)==-1) {ObjectCreate(0,r6,OBJ_TREND,x1,0,0,0,0); ObjectSetInteger(0,r6,OBJPROP_RAY,false);} ObjectSetInteger(0,r6,OBJPROP_TIME,x6); ObjectSetInteger(0,r6,OBJPROP_TIME,1,x7); ObjectSetDouble(0,r6,OBJPROP_PRICE,f6); ObjectSetDouble(0,r6,OBJPROP_PRICE,1,f7); ObjectSetInteger(0,r6,OBJPROP_COLOR,x3); ObjectSetInteger(0,r6,OBJPROP_STYLE,x4); ObjectSetInteger(0,r6,OBJPROP_WIDTH,x5);}} void RemoveObjects(string r6) {int t1; t1=ObjectsTotal(); while(t1>=0) {if (StringFind(ObjectName(t1),r6,0)!=-1) {ObjectDelete(0,ObjectName(t1));} t1--;}} void DrawLabel(string r6,string r7,ENUM_BASE_CORNER x4,int x1,int x2,int x5,string r8,color x3,bool b6) {if (r7=="" && ObjectFind(0,r6)!=-1) {ObjectDelete(0,r6);} if (r7!="") {if (ObjectFind(0,r6)==-1) {ObjectCreate(0,r6,OBJ_LABEL,0,0,0);} ObjectSetInteger(0,r6,OBJPROP_CORNER,x4); ObjectSetString(0,r6,OBJPROP_TEXT,r7); ObjectSetInteger(0,r6,OBJPROP_FONTSIZE,x5); ObjectSetString(0,r6,OBJPROP_FONT,r8); ObjectSetInteger(0,r6,OBJPROP_COLOR,x3); ObjectSetInteger(0,r6,OBJPROP_BACK,b6); ObjectSetInteger(0,r6,OBJPROP_XDISTANCE,x1); ObjectSetInteger(0,r6,OBJPROP_YDISTANCE,x2);}} void OrderAlert(int x7,int x8) {string s1; s1=StringConcatenate(Vs[32][x7+2]," farctal has been engulfed at ",TimeToString(Time[x8])); if (SendEmail) {SendMail(Vs[30][4],s1);} if (SendNotifications) {SendNotification(StringConcatenate(s1,", ",Vs[30][4]));} if (ScreenShots) {ChartScreenShot(0,StringConcatenate(TimeFr[tf],Vs[30][1],TimeToString(Time[x8])),1920,1080,ALIGN_RIGHT);} if (Alerts) {Alert(StringConcatenate(s1,", ",Vs[30][4])); if (AlertSound!="") {PlaySound(AlertSound);}}} void ScreenInfo() {int t1,t2,t7; double d1; if (ShowComments) {t1=0; while(t1<=100) {Vs[40][t1]=""; Vi[32][t1]=Colour[3]; t1++;} d1=(double)(Lines[3]+Lines[4])/(Lines[1]+Lines[2]); t7+=1; Vs[40][t7]=StringConcatenate("Total lines ",Lines[1]+Lines[2]); t1=1; while(t1<=2) {d1=Lines[t1+2]/Lines[t1]; t7+=1; Vs[40][t7]=StringConcatenate(Vs[32][t1+2]," lines ",Lines[t1]); t1++;} t2=14; t1=0; while(t1<=100) {DrawLabel(Vs[41][t1],Vs[40][t1],CORNER_LEFT_UPPER,2,t2,LabelFontSize,LabelFontType,Vi[32][t1],false); t2+=(int)(Vs[40][t1]!="")*14; t1++;}}}