//+------------------------------------------------------------------+ //| IchimokuAlert_v1.1.mq5 | //| Copyright © 2013, TrendLaboratory | //| http://finance.groups.yahoo.com/group/TrendLaboratory | //| E-mail: igorad2003@yahoo.co.uk | //+------------------------------------------------------------------+ #property copyright "Copyright © 2013, TrendLaboratory" #property link "http://finance.groups.yahoo.com/group/TrendLaboratory" //--- indicator settings #property indicator_chart_window #property indicator_buffers 12 #property indicator_plots 10 #property indicator_label1 "Tenkan-sen" #property indicator_type1 DRAW_LINE #property indicator_color1 Red #property indicator_label2 "Kijun-sen" #property indicator_type2 DRAW_LINE #property indicator_color2 Blue #property indicator_label3 "Tenkan Kijun Cross Above" #property indicator_type3 DRAW_ARROW #property indicator_color3 DeepSkyBlue #property indicator_width3 3 #property indicator_label4 "Tenkan Kijun Cross Below" #property indicator_type4 DRAW_ARROW #property indicator_color4 OrangeRed #property indicator_width4 3 #property indicator_label5 "Chinkou Cloud Cross Above" #property indicator_type5 DRAW_ARROW #property indicator_color5 DeepSkyBlue #property indicator_width5 1 #property indicator_label6 "Chinkou Cloud Cross Below" #property indicator_type6 DRAW_ARROW #property indicator_color6 OrangeRed #property indicator_width6 1 #property indicator_label7 "Chinkou Price Cross Above" #property indicator_type7 DRAW_ARROW #property indicator_color7 DeepSkyBlue #property indicator_width7 1 #property indicator_label8 "Chinkou Price Cross Below" #property indicator_type8 DRAW_ARROW #property indicator_color8 OrangeRed #property indicator_width8 1 #property indicator_label9 "Chinkou Span" #property indicator_type9 DRAW_LINE #property indicator_color9 LimeGreen #property indicator_label10 "Senkou Span A;Senkou Span B" #property indicator_type10 DRAW_FILLING #property indicator_color10 SandyBrown,Thistle //--- input parameters input int Tenkan = 9; // Tenkan-sen input int Kijun = 26; // Kijun-sen input int Senkou = 52; // Senkou Span B input bool AutoPeriodsDetect = true; input int SignalMode = 4; // Signal Mode: 0-off,1-Tenkan/Kijun,2-Chinkou/Cloud,3-Chinkou/Price,4-all together input int AlertMode = 4; // Alert Mode: 0-off,1-Tenkan/Kijun,2-Chinkou/Cloud,3-Chinkou/Price,4-all together input int WarningMode = 1; // Warning Mode: 0-off,1-on input int SoundsNumber = 1; // Number of sounds after Signal input int SoundsPause = 5; // Pause in sec between sounds input string UpSound = "alert.wav"; input string DnSound = "alert2.wav"; //--- indicator buffers double TenkanBuffer[]; double KijunBuffer[]; double UpSignal1[]; double DnSignal1[]; double UpSignal2[]; double DnSignal2[]; double UpSignal3[]; double DnSignal3[]; double ChinkouBuffer[]; double SpanABuffer[]; double SpanBBuffer[]; double atr[]; int atr_handle, tenkan, kijun, senkou; string prevmess[3]; datetime pTime[3], preTime, ptime; //+------------------------------------------------------------------+ //| Custom indicator initialization function | //+------------------------------------------------------------------+ void OnInit() { if(Period() <= PERIOD_H1 && AutoPeriodsDetect) { tenkan = 72; kijun = 144; senkou = 288; } else { tenkan = Tenkan; kijun = Kijun; senkou = Senkou; } //--- indicator buffers mapping SetIndexBuffer( 0, TenkanBuffer,INDICATOR_DATA); SetIndexBuffer( 1, KijunBuffer,INDICATOR_DATA); SetIndexBuffer( 2, UpSignal1,INDICATOR_DATA); PlotIndexSetInteger(2,PLOT_ARROW,241); SetIndexBuffer( 3, DnSignal1,INDICATOR_DATA); PlotIndexSetInteger(3,PLOT_ARROW,242); SetIndexBuffer( 4, UpSignal2,INDICATOR_DATA); PlotIndexSetInteger(4,PLOT_ARROW,233); SetIndexBuffer( 5, DnSignal2,INDICATOR_DATA); PlotIndexSetInteger(5,PLOT_ARROW,234); SetIndexBuffer( 6, UpSignal3,INDICATOR_DATA); PlotIndexSetInteger(6,PLOT_ARROW,225); SetIndexBuffer( 7, DnSignal3,INDICATOR_DATA); PlotIndexSetInteger(7,PLOT_ARROW,226); SetIndexBuffer( 8,ChinkouBuffer,INDICATOR_DATA); SetIndexBuffer( 9, SpanABuffer,INDICATOR_DATA); SetIndexBuffer(10, SpanBBuffer,INDICATOR_DATA); SetIndexBuffer(11, atr,INDICATOR_CALCULATIONS); //--- IndicatorSetInteger(INDICATOR_DIGITS,_Digits+1); //--- sets first bar from what index will be drawn PlotIndexSetInteger(0,PLOT_DRAW_BEGIN, tenkan); PlotIndexSetInteger(1,PLOT_DRAW_BEGIN, kijun); PlotIndexSetInteger(5,PLOT_DRAW_BEGIN,senkou-1); //--- lines shifts when drawing PlotIndexSetInteger(8,PLOT_SHIFT,-kijun); PlotIndexSetInteger(9,PLOT_SHIFT, kijun); //--- change labels for DataWindow PlotIndexSetString(0,PLOT_LABEL,"Tenkan-sen("+string(tenkan)+")"); PlotIndexSetString(1,PLOT_LABEL,"Kijun-sen("+string(kijun)+")"); PlotIndexSetString(5,PLOT_LABEL,"Senkou Span A;Senkou Span B("+string(senkou)+")"); atr_handle = iATR(NULL,0,10); //--- initialization done } //+------------------------------------------------------------------+ //| get highest value for range | //+------------------------------------------------------------------+ double Highest(const double&array[],int range,int fromIndex) { double res = 0; //--- res = array[fromIndex]; for(int i=fromIndex;i>fromIndex-range && i>=0;i--) { if(res < array[i]) res = array[i]; } //--- return(res); } //+------------------------------------------------------------------+ //| get lowest value for range | //+------------------------------------------------------------------+ double Lowest(const double&array[],int range,int fromIndex) { double res = 0; //--- res = array[fromIndex]; for(int i=fromIndex;i>fromIndex-range && i>=0;i--) { if(res > array[i]) res = array[i]; } //--- return(res); } //+------------------------------------------------------------------+ //| Ichimoku Kinko Hyo | //+------------------------------------------------------------------+ 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 &TickVolume[], const long &Volume[], const int &Spread[]) { int limit, copied = 0; //--- if(prev_calculated==0) limit = senkou + kijun + tenkan; else limit = prev_calculated - 1; //--- copied = CopyBuffer(atr_handle,0,0,rates_total-1,atr); if(copied<0) { Print("not all ATRs copied. Will try on next tick Error =",GetLastError(),", copied =",copied); return(0); } for(int i=limit;i KijunBuffer[i] && TenkanBuffer[i-1] < KijunBuffer[i-1]) { UpSignal1[i] = Low[i] - 0.5*atr[i]; if(i == rates_total - 1 && WarningMode > 0) WarningSound(true,SoundsNumber,SoundsPause,UpSound,Time[rates_total-1]); } if(TenkanBuffer[i] < KijunBuffer[i] && TenkanBuffer[i-1] > KijunBuffer[i-1]) { DnSignal1[i] = High[i] + 0.5*atr[i]; if(i == rates_total - 1 && WarningMode > 0) WarningSound(true,SoundsNumber,SoundsPause,DnSound,Time[rates_total-1]); } } if(SignalMode == 2 || SignalMode == 4) { if(ChinkouBuffer[i] > MathMax(SpanABuffer[i-2*Kijun],SpanBBuffer[i-2*Kijun]) && ChinkouBuffer[i-1] < MathMax(SpanABuffer[i-2*Kijun-1],SpanBBuffer[i-2*Kijun-1])) { UpSignal2[i] = Low[i] - 0.5*atr[i]; if(i == rates_total - 1 && WarningMode > 0) WarningSound(true,SoundsNumber,SoundsPause,UpSound,Time[rates_total-1]); } if(ChinkouBuffer[i] < MathMin(SpanABuffer[i-2*Kijun],SpanBBuffer[i-2*Kijun]) && ChinkouBuffer[i-1] > MathMin(SpanABuffer[i-2*Kijun-1],SpanBBuffer[i-2*Kijun-1])) { DnSignal2[i] = High[i] + 0.5*atr[i]; if(i == rates_total - 1 && WarningMode > 0) WarningSound(true,SoundsNumber,SoundsPause,DnSound,Time[rates_total-1]); } } if(SignalMode == 3 || SignalMode == 4) { if(ChinkouBuffer[i] > Close[i-Kijun] && ChinkouBuffer[i-1] < Close[i-Kijun-1]) { UpSignal3[i] = Low[i] - 0.5*atr[i]; if(i == rates_total - 1 && WarningMode > 0) WarningSound(true,SoundsNumber,SoundsPause,UpSound,Time[rates_total-1]); } if(ChinkouBuffer[i] < Close[i-Kijun] && ChinkouBuffer[i-1] > Close[i-Kijun-1]) { DnSignal3[i] = High[i] + 0.5*atr[i]; if(i == rates_total - 1 && WarningMode > 0) WarningSound(true,SoundsNumber,SoundsPause,DnSound,Time[rates_total-1]); } } } //--- int cbar = rates_total-2; if(AlertMode == 1 || AlertMode == 4) { bool crossabove1 = TenkanBuffer[cbar] > KijunBuffer[cbar] && TenkanBuffer[cbar-1] < KijunBuffer[cbar-1]; bool crossbelow1 = TenkanBuffer[cbar] < KijunBuffer[cbar] && TenkanBuffer[cbar-1] > KijunBuffer[cbar-1]; if(crossabove1 || crossbelow1) { if(isNewBar(Period(),0)) { BoxAlert(crossabove1," Tenkan crosses Kijun: BUY Signal at " +DoubleToString(Open[cbar+1],_Digits),0); BoxAlert(crossbelow1," Tenkan crosses Kijun: SELL Signal at "+DoubleToString(Open[cbar+1],_Digits),0); } } } if(AlertMode == 2 || AlertMode == 4) { bool crossabove2 = ChinkouBuffer[cbar] > MathMax(SpanABuffer[cbar-2*Kijun],SpanBBuffer[cbar-2*Kijun]) && ChinkouBuffer[cbar-1] < MathMax(SpanABuffer[cbar-2*Kijun-1],SpanBBuffer[cbar-2*Kijun-1]); bool crossbelow2 = ChinkouBuffer[cbar] < MathMin(SpanABuffer[cbar-2*Kijun],SpanBBuffer[cbar-2*Kijun]) && ChinkouBuffer[cbar-1] > MathMin(SpanABuffer[cbar-2*Kijun-1],SpanBBuffer[cbar-2*Kijun-1]); if(crossabove2 || crossbelow2) { if(isNewBar(Period(),1)) { BoxAlert(crossabove2," Chinkou crosses Cloud: BUY Signal at " +DoubleToString(Open[cbar+1],_Digits),1); BoxAlert(crossbelow2," Chinkou crosses Cloud: SELL Signal at "+DoubleToString(Open[cbar+1],_Digits),1); } } } if(AlertMode == 3 || AlertMode == 4) { bool crossabove3 = ChinkouBuffer[cbar] > Close[cbar-Kijun] && ChinkouBuffer[cbar-1] < Close[cbar-Kijun-1]; bool crossbelow3 = ChinkouBuffer[cbar] < Close[cbar-Kijun] && ChinkouBuffer[cbar-1] > Close[cbar-Kijun-1] ; if(crossabove3 || crossbelow3) { if(isNewBar(Period(),2)) { BoxAlert(crossabove3," Chinkou crosses Price: BUY Signal at " +DoubleToString(Open[cbar+1],_Digits),2); BoxAlert(crossbelow3," Chinkou crosses Price: SELL Signal at "+DoubleToString(Open[cbar+1],_Digits),2); } } } return(rates_total); } //+------------------------------------------------------------------+ string timeframeToString(ENUM_TIMEFRAMES timeframe) { switch(timeframe) { case PERIOD_M1 : return("M1") ; //break; case PERIOD_M2 : return("M2") ; //break; case PERIOD_M3 : return("M3") ; //break; case PERIOD_M4 : return("M4") ; //break; case PERIOD_M5 : return("M5") ; //break; case PERIOD_M6 : return("M6") ; //break; case PERIOD_M10 : return("M10"); //break; case PERIOD_M12 : return("M12"); //break; case PERIOD_M15 : return("M15"); //break; case PERIOD_M20 : return("M20"); //break; case PERIOD_M30 : return("M30"); //break; case PERIOD_H1 : return("H1") ; //break; case PERIOD_H2 : return("H2") ; //break; case PERIOD_H3 : return("H3") ; //break; case PERIOD_H4 : return("H4") ; //break; case PERIOD_H6 : return("H6") ; //break; case PERIOD_H8 : return("H8") ; //break; case PERIOD_H12 : return("H12"); //break; case PERIOD_D1 : return("D1") ; //break; case PERIOD_W1 : return("W1") ; //break; case PERIOD_MN1 : return("MN1"); //break; default : return("Current"); } } datetime iTime1(string symbol,ENUM_TIMEFRAMES timeframe,int index) { if(index < 0) return(-1); static datetime timearray[]; if(CopyTime(symbol,timeframe,index,1,timearray) > 0) return(timearray[0]); else return(-1); } bool isNewBar(ENUM_TIMEFRAMES timeframe,int index) { bool res = false; if(timeframe >= 0) { if (iTime(NULL,timeframe,0)!= pTime[index]) { res = true; pTime[index] = iTime(NULL,timeframe,0); } } else res = true; return(res); } bool BoxAlert(bool cond,string text,int index) { string mess = " " + text; if (cond && mess != prevmess[index]) { Alert (mess); prevmess[index] = mess; return(true); } return(false); } bool Pause(int sec) { if(TimeCurrent() >= preTime + sec) {preTime = TimeCurrent(); return(true);} return(false); } void WarningSound(bool cond,int num,int sec,string sound,datetime ctime) { static int i; if(cond) { if(ctime != ptime) i = 0; if(i < num && Pause(sec)) {PlaySound(sound); ptime = ctime; i++;} } }