Time: 2014-12-04 | Download file:Sentiment_zone_oscillator_histo_arrows_+_alerts_mtf_nmc.mq4
//------------------------------------------------------------------ #property copyright "www.forex-tsd.com" #property link "www.forex-tsd.com" //------------------------------------------------------------------ #property indicator_separate_window #property indicator_buffers 6 #property indicator_color1 Green #property indicator_color2 Red #property indicator_color3 Silver #property indicator_color4 LimeGreen #property indicator_color5 PaleVioletRed #property indicator_color6 Goldenrod #property indicator_style4 STYLE_DOT #property indicator_style5 STYLE_DOT #property indicator_width1 2 #property indicator_width2 2 #property indicator_width3 2 #property indicator_width6 2 // // // // // extern string TimeFrame = "Current time frame"; extern int SZOPeriod = 14; extern int SZOLongPeriod = 30; extern int SZOPrice = PRICE_CLOSE; extern double SZOPercent = 95; extern int AveragePeriod = 14; extern int AverageType = MODE_EMA; extern bool alertsOn = false; extern bool alertsOnCurrent = true; extern bool alertsMessage = true; extern bool alertsSound = false; extern bool alertsNotify = false; extern bool alertsEmail = false; extern string soundFile = "alert2.wav"; extern bool ShowArrows = false; extern string arrowsIdentifier = "szo Arrows1"; extern double arrowsUpperGap = 1.0; extern double arrowsLowerGap = 1.0; extern color arrowsUpColor = LimeGreen; extern color arrowsDnColor = Red; extern int arrowsUpCode = 241; extern int arrowsDnCode = 242; // // // // // double szoUp[]; double szoDn[]; double szo[]; double upz[]; double dnz[]; double avg[]; double prices[]; double trend[]; string indicatorFileName; bool returnBars; bool calculateValue; int timeFrame; //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // int init() { IndicatorBuffers(8); SetIndexBuffer(0,szoUp); SetIndexStyle(0,DRAW_HISTOGRAM); SetIndexBuffer(1,szoDn); SetIndexStyle(1,DRAW_HISTOGRAM); SetIndexBuffer(2,szo); SetIndexBuffer(3,upz); SetIndexBuffer(4,dnz); SetIndexBuffer(5,avg); SetIndexBuffer(6,prices); SetIndexBuffer(7,trend); // // // // // indicatorFileName = WindowExpertName(); calculateValue = TimeFrame=="calculateValue"; if (calculateValue) { return(0); } returnBars = TimeFrame=="returnBars"; if (returnBars) { return(0); } timeFrame = stringToTimeFrame(TimeFrame); // // // // // IndicatorShortName(timeFrameToString(timeFrame)+" Sentiment zone oscillator ("+SZOPeriod+","+SZOLongPeriod+")"); return(0); } int deinit() { deleteArrows(); return(0); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // int start() { int counted_bars=IndicatorCounted(); if(counted_bars<0) return(-1); if(counted_bars>0) counted_bars--; int limit=MathMin(Bars-counted_bars,Bars-1); if (returnBars) { upz[0] = MathMin(limit+1,Bars-1); return(0); } // // // // // if (calculateValue || timeFrame == Period()) { for(int i=limit; i>=0; i--) { prices[i] = iMA(NULL,0,1,0,MODE_SMA,SZOPrice,i); double useValue = 0; if (prices[i]>prices[i+1]) useValue = 1; if (prices[i]0) trend[i] = 1; if (szo[i]<0) trend[i] =-1; if (trend[i] == 1) szoUp[i] = szo[i]; if (trend[i] ==-1) szoDn[i] = szo[i]; // // // // // double hi = szo[ArrayMaximum(szo,SZOLongPeriod,i)]; double lo = szo[ArrayMinimum(szo,SZOLongPeriod,i)]; double rn = hi-lo; upz[i] = lo+rn*SZOPercent/100.0; dnz[i] = hi-rn*SZOPercent/100.0; // // // // // if (ShowArrows) { deleteArrow(Time[i]); if (trend[i] != trend[i+1]) { if (trend[i] == 1) drawArrow(i,arrowsUpColor,arrowsUpCode,false); if (trend[i] ==-1) drawArrow(i,arrowsDnColor,arrowsDnCode, true); } } } for(i=limit; i>=0; i--) avg[i] = iMAOnArray(szo,0,AveragePeriod,0,AverageType,i); // // // // // if (alertsOn) { if (alertsOnCurrent) int whichBar = 0; else whichBar = 1; if (trend[whichBar] != trend[whichBar+1]) if (trend[whichBar] == 1) doAlert("crossing zero up"); else doAlert("crossing zero down"); } return(0); } // // // // // 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]); szoUp[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,0,y); szoDn[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,1,y); szo[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,2,y); upz[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,3,y); dnz[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,4,y); avg[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",SZOPeriod,SZOLongPeriod,SZOPrice,SZOPercent,AveragePeriod,AverageType,alertsOn,alertsOnCurrent,alertsMessage,alertsSound,alertsNotify,alertsEmail,soundFile,ShowArrows,arrowsIdentifier,arrowsUpperGap,arrowsLowerGap,arrowsUpColor,arrowsDnColor,arrowsUpCode,arrowsDnCode,5,y); } return(0); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // double workTema[][3]; #define _ema1 0 #define _ema2 1 #define _ema3 2 double iTema(double price, double period, int r, int instanceNo=0) { if (ArrayRange(workTema,0)!= Bars) ArrayResize(workTema,Bars); instanceNo*=3; r = Bars-r-1; // // // // // double alpha = 2.0 / (1.0+period); workTema[r][_ema1+instanceNo] = workTema[r-1][_ema1+instanceNo]+alpha*(price -workTema[r-1][_ema1+instanceNo]); workTema[r][_ema2+instanceNo] = workTema[r-1][_ema2+instanceNo]+alpha*(workTema[r][_ema1+instanceNo]-workTema[r-1][_ema2+instanceNo]); workTema[r][_ema3+instanceNo] = workTema[r-1][_ema3+instanceNo]+alpha*(workTema[r][_ema2+instanceNo]-workTema[r-1][_ema3+instanceNo]); return(workTema[r][_ema3+instanceNo]+3.0*(workTema[r][_ema1+instanceNo]-workTema[r][_ema2+instanceNo])); } //------------------------------------------------------------------- // //------------------------------------------------------------------- // // // // // 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 tchar = StringGetChar(s, length); if((tchar > 96 && tchar < 123) || (tchar > 223 && tchar < 256)) s = StringSetChar(s, length, tchar - 32); else if(tchar > -33 && tchar < 0) s = StringSetChar(s, length, tchar + 224); } return(s); } //------------------------------------------------------------------ // //------------------------------------------------------------------ // // // // // void doAlert(string doWhat) { static string previousAlert="nothing"; static datetime previousTime; string message; if (previousAlert != doWhat || previousTime != Time[0]) { previousAlert = doWhat; previousTime = Time[0]; // // // // // message = StringConcatenate(Symbol()," ",timeFrameToString(timeFrame)," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," Sentiment zone oscillator ",doWhat); if (alertsMessage) Alert(message); if (alertsNotify) SendNotification(message); if (alertsEmail) SendMail(StringConcatenate(Symbol()," Sentiment zone oscillator "),message); if (alertsSound) PlaySound(soundFile); } } // // // // // void drawArrow(int i,color theColor,int theCode,bool up) { string name = arrowsIdentifier+":"+Time[i]; double gap = iATR(NULL,0,20,i); // // // // // ObjectCreate(name,OBJ_ARROW,0,Time[i],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); } // // // // // void deleteArrows() { string lookFor = arrowsIdentifier+":"; int lookForLength = StringLen(lookFor); for (int i=ObjectsTotal()-1; i>=0; i--) { string objectName = ObjectName(i); if (StringSubstr(objectName,0,lookForLength) == lookFor) ObjectDelete(objectName); } } // // // // // void deleteArrow(datetime time) { string lookFor = arrowsIdentifier+":"+time; ObjectDelete(lookFor); }