Time: 2010-03-12 | Download file:Sma_centered_envelope_mtf+alerts~.mq4
//------------------------------------------------------------------ #property copyright "www.forex-tsd.com" #property link "www.forex-tsd.com" //------------------------------------------------------------------ #property indicator_chart_window #property indicator_buffers 5 #property indicator_color1 Gold #property indicator_color2 Gold #property indicator_color3 Red #property indicator_color4 Red #property indicator_color5 Green // // // // // extern string TimeFrame = "current"; extern int HalfLength = 25; extern int Price = PRICE_CLOSE; extern int EnvelopeShift = 0; extern double upperDeviation = 0.05; extern double lowerDeviation = 0.05; extern string note = "turn on Alert = true; turn off = false"; extern bool alertsOn = false; extern bool alertsOnCurrent = false; extern bool alertsMessage = true; extern bool alertsSound = true; extern bool alertsEmail = false; extern string soundfile = "alert2.wav"; extern color xc_Text = LightGray; // // // // // double smBuf[]; double smBufDa[]; double smBufDb[]; double UpEnv[]; double DnEnv[]; double trend[]; int gi_PipsDecimal; // // // // // string indicatorFileName; bool calculateValue; bool returnBars; int timeFrame; //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // int init() { HalfLength=MathMax(HalfLength,1); IndicatorBuffers(6); SetIndexBuffer(0,smBuf); SetIndexDrawBegin(0,HalfLength); SetIndexBuffer(1,smBufDa); SetIndexDrawBegin(1,HalfLength); SetIndexBuffer(2,smBufDb); SetIndexDrawBegin(2,HalfLength); SetIndexBuffer(3,UpEnv); SetIndexDrawBegin(3,HalfLength); SetIndexBuffer(4,DnEnv); SetIndexDrawBegin(4,HalfLength); SetIndexBuffer(5,trend); // // // // // indicatorFileName = WindowExpertName(); returnBars = TimeFrame=="returnBars"; if (returnBars) return(0); calculateValue = TimeFrame=="calculateValue"; if (calculateValue) return(0); timeFrame = stringToTimeFrame(TimeFrame); SetIndexShift(0,EnvelopeShift * timeFrame/Period()); SetIndexShift(1,EnvelopeShift * timeFrame/Period()); SetIndexShift(2,EnvelopeShift * timeFrame/Period()); SetIndexShift(3,EnvelopeShift * timeFrame/Period()); SetIndexShift(4,EnvelopeShift * timeFrame/Period()); // // // // // IndicatorShortName(timeFrameToString(timeFrame)+" Sma centered envelopes ("+HalfLength+")"); return(0); } int deinit() { return(0); } //+------------------------------------------------------------------ //| //+------------------------------------------------------------------ // // int start() { int counted_bars=IndicatorCounted(); int i,j,limit; if(counted_bars<0) return(-1); if(counted_bars>0) counted_bars--; limit=MathMin(Bars-1,Bars-counted_bars+HalfLength); if (returnBars) { smBuf[0] = limit+1; return(0); } // // // // // if (calculateValue || timeFrame==Period()) { if (trend[limit] == -1) ClearPoint(limit,smBufDa,smBufDb); for (i=limit; i>=0; i--) { double sum = iMA(NULL,0,1,0,MODE_SMA,Price,i); double sumw = 1; for(j=1; j<=HalfLength; j++) { sum += iMA(NULL,0,1,0,MODE_SMA,Price,i+j); sumw += 1; if (j<=i) { sum += iMA(NULL,0,1,0,MODE_SMA,Price,i-j); sumw += 1; } } smBuf[i] = sum/sumw; UpEnv[i] = (1+upperDeviation/100)*smBuf[i]; DnEnv[i] = (1-lowerDeviation/100)*smBuf[i]; smBufDa[i] = EMPTY_VALUE; smBufDb[i] = EMPTY_VALUE; trend[i] = trend[i+1]; if (smBuf[i]>smBuf[i+1]) trend[i] = 1; if (smBuf[i]=0; i--) { int y = iBarShift(NULL,timeFrame,Time[i]); smBuf[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",HalfLength,Price,EnvelopeShift,upperDeviation,lowerDeviation,0,y); smBufDa[i] = EMPTY_VALUE; smBufDb[i] = EMPTY_VALUE; UpEnv[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",HalfLength,Price,EnvelopeShift,upperDeviation,lowerDeviation,3,y); DnEnv[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",HalfLength,Price,EnvelopeShift,upperDeviation,lowerDeviation,4,y); trend[i] = iCustom(NULL,timeFrame,indicatorFileName,"calculateValue",HalfLength,Price,EnvelopeShift,upperDeviation,lowerDeviation,5,y); // // // // // } for (i=limit;i>=0;i--) if (trend[i]==-1) PlotPoint(i,smBufDa,smBufDb,smBuf); manageAlerts(); // РАССЧЕТ ДИСТАНЦИИ КАНАЛА double ld_DistPts, ld_DistPips; // Distance to mid /*ld_DistPts = MathAbs(Bid - MdBuffer[0]); ld_DistPips = Convert_2_Pips(ld_DistPts); ObjectCreate("!Mid",OBJ_TEXT,0,0,0); ObjectSet("!Mid",OBJPROP_TIME1,Time[0]+(3*Period()*60)); ObjectSet("!Mid",OBJPROP_PRICE1,MdBuffer[0]); ObjectSetText("!Mid",DoubleToStr(ld_DistPips,gi_PipsDecimal),8,"Arial",xc_Text);*/ // Distance to upper ld_DistPts = MathAbs(Bid - UpEnv[0]); ld_DistPips = Convert_2_Pips(ld_DistPts); ObjectCreate("!Upp",OBJ_TEXT,0,0,0); ObjectSet("!Upp",OBJPROP_TIME1,Time[0]+(3*Period()*60)); ObjectSet("!Upp",OBJPROP_PRICE1,UpEnv[0]); ObjectSetText("!Upp",DoubleToStr(ld_DistPips,gi_PipsDecimal),8,"Arial",xc_Text); // Distance to lower ld_DistPts = MathAbs(Bid - DnEnv[0]); ld_DistPips = Convert_2_Pips(ld_DistPts); ObjectCreate("!Low",OBJ_TEXT,0,0,0); ObjectSet("!Low",OBJPROP_TIME1,Time[0]+(3*Period()*60)); ObjectSet("!Low",OBJPROP_PRICE1,DnEnv[0]); ObjectSetText("!Low",DoubleToStr(ld_DistPips,gi_PipsDecimal),8,"Arial",xc_Text); return(0); } //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ // // // // // void ClearPoint(int i,double& first[],double& second[]) { if ((second[i] != EMPTY_VALUE) && (second[i+1] != EMPTY_VALUE)) second[i+1] = EMPTY_VALUE; else if ((first[i] != EMPTY_VALUE) && (first[i+1] != EMPTY_VALUE) && (first[i+2] == EMPTY_VALUE)) first[i+1] = EMPTY_VALUE; } // // // // // void PlotPoint(int i,double& first[],double& second[],double& from[]) { if (first[i+1] == EMPTY_VALUE) { if (first[i+2] == EMPTY_VALUE) { first[i] = from[i]; first[i+1] = from[i+1]; second[i] = EMPTY_VALUE; } else { second[i] = from[i]; second[i+1] = from[i+1]; first[i] = EMPTY_VALUE; } } else { first[i] = from[i]; second[i] = EMPTY_VALUE; } } //+------------------------------------------------------------------- //| //+------------------------------------------------------------------- // // // // // 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); } // // // // // void manageAlerts() { if (!calculateValue && 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,"sloping up"); if (trend[whichBar] ==-1) doAlert(whichBar,"sloping 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)+" Sma centered envelopes ",doWhat); if (alertsMessage) Alert(message); if (alertsEmail) SendMail(StringConcatenate(Symbol()," Sma centered envelopes "),message); if (alertsSound) PlaySound("alert2.wav"); } } //БЛОК УПРАВЛЕНИЯ ЦИФЕРКАМИ void Object_Create(string ps_name,int pi_x,int pi_y,string ps_text=" ",int pi_size=12, string ps_font="Arial",color pc_colour=CLR_NONE) { //---- // if (colour==CLR_NONE) colour=xcBackground; ObjectCreate(ps_name,OBJ_LABEL,0,0,0,0,0); ObjectSet(ps_name,OBJPROP_CORNER,3); ObjectSet(ps_name,OBJPROP_COLOR,pc_colour); ObjectSet(ps_name,OBJPROP_XDISTANCE,pi_x); ObjectSet(ps_name,OBJPROP_YDISTANCE,pi_y); ObjectSetText(ps_name,ps_text,pi_size,ps_font,pc_colour); //---- return ; } //+------------------------------------------------------------------+ //| convert to points | //+------------------------------------------------------------------+ double Convert_2_Pts(double pd_Pips) { //---- int pd_Points=pd_Pips; // Default - no conversion if (Digits == 5 || (Digits == 3 && StringFind(Symbol(), "JPY") != -1)) pd_Points=pd_Pips*10; if (Digits == 6 || (Digits == 4 && StringFind(Symbol(), "JPY") != -1)) pd_Points=pd_Pips*100; //---- return(pd_Points); } //+------------------------------------------------------------------+ //| convert to pips | //+------------------------------------------------------------------+ double Convert_2_Pips(double pd_Points) { //---- double pd_Pips=pd_Points/Point; // Default - no conversion if (Digits == 5 || (Digits == 3 && StringFind(Symbol(), "JPY") != -1)) { pd_Pips=pd_Points/Point/10; } if (Digits == 6 || (Digits == 4 && StringFind(Symbol(), "JPY") != -1)) { pd_Pips=pd_Points/Point/100; } //---- return(pd_Pips); } //+------------------------------------------------------------------+ //| get the pips decimal places | //+------------------------------------------------------------------+ int Get_Pips_Decimal() { //---- int pi_PipsDecimal = 0; // Default - no decimals if (Digits == 5 || (Digits == 3 && StringFind(Symbol(), "JPY") != -1)) { pi_PipsDecimal = 1; } if (Digits == 6 || (Digits == 4 && StringFind(Symbol(), "JPY") != -1)) { pi_PipsDecimal = 2; } //---- return(pi_PipsDecimal); } //+------------------------------------------------------------------+