//+------------------------------------------------------------------+ //| Awesome Oscillator Divergence.mq5 | //| Copyright © 2013, Mehrdad Shiri | //| E-mail: m100shiri@yahoo.com | //+------------------------------------------------------------------+ //---- author of the indicator #property copyright "Copyright © 2013, Mehrdad Shiri" //---- link to the website of the author #property link "m100shiri@yahoo.com" //---- indicator version number #property version "1.00" #property description "Awesome Oscillator Divergence" //--- indicator settings #property indicator_separate_window #property indicator_buffers 10 #property indicator_plots 5 //------------------------------------------------------------------------------------------ Awesome_Oscillator #property indicator_label1 "AO" #property indicator_type1 DRAW_COLOR_HISTOGRAM // DRAW_COLOR_LINE DRAW_COLOR_HISTOGRAM #property indicator_color1 clrDarkGreen,clrDarkRed #property indicator_style1 STYLE_SOLID #property indicator_width1 2 // double Ext_AO_Buffer[]; double ExtColor_AO_Buffer[]; int AO_handle; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #property indicator_label2 "AO_BUY_Divergence_Regular" #property indicator_type2 DRAW_ARROW #property indicator_color2 clrBlue #property indicator_style2 STYLE_SOLID #property indicator_width2 1 #property indicator_label3 "AO_SELL_Divergence_Regular" #property indicator_type3 DRAW_ARROW #property indicator_color3 clrRed #property indicator_style3 STYLE_SOLID #property indicator_width3 1 // double AO_BUY_Divergence_Regular_Buffer[],AO_SELL_Divergence_Regular_Buffer[]; uchar AO_BUY_Divergence_Regular_Code=233; uchar AO_SELL_Divergence_Regular_Code=234; // int AO_BUY_Divergence_Regular_Shift=10; int AO_SELL_Divergence_Regular_Shift=10; //+++++++ #property indicator_label4 "AO_BUY_Divergence_Hidden" #property indicator_type4 DRAW_ARROW #property indicator_color4 clrAqua #property indicator_style4 STYLE_SOLID #property indicator_width4 1 #property indicator_label5 "AO_SELL_Divergence_Hidden" #property indicator_type5 DRAW_ARROW #property indicator_color5 clrOrange #property indicator_style5 STYLE_SOLID #property indicator_width5 1 // double AO_BUY_Divergence_Hidden_Buffer[],AO_SELL_Divergence_Hidden_Buffer[]; uchar AO_BUY_Divergence_Hidden_Code=241; uchar AO_SELL_Divergence_Hidden_Code=242; // int AO_BUY_Divergence_Hidden_Shift=-10; int AO_SELL_Divergence_Hidden_Shift=-10; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //--- bars minimum for calculation #define DATA_LIMIT 37 double Zero_Value=0.0; string short_name; int TOTAL_BAR; int count_bar_above=0; int count_bar_down=0; int LastTrough_bar=0; int LastPeak_bar=0; int ALL_LastPeak_bar[200]; int ALL_LastTrough_bar[200]; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ //+-----------------------------------+ //| INDICATOR INPUT PARAMETERS | //+-----------------------------------+ input double Deviation_percent=0.05; //+-----------------------------------+ enum WHEN_DRAW_LINE { yes, no }; input WHEN_DRAW_LINE Indicator_Trend_Line_Allowed_across_through_the_histogram_body=no; //+-----------------------------------+ input uint Numberof_Alerts_Maximum_Iterations=2; uint counterBUY=0; uint counterSELL=0; double Ask,Bid; string text,sAsk,sBid,sPeriod; datetime SignalTime; datetime LastSignalTime; double SIGNAL_ALARM_BUY_Divergence_Regular_Buffer[]; double SIGNAL_ALARM_BUY_Divergence_Hidden_Buffer[]; double SIGNAL_ALARM_SELL_Divergence_Regular_Buffer[]; double SIGNAL_ALARM_SELL_Divergence_Hidden_Buffer[]; //+-----------------------------------+ //---- input bool On_Push = true; //allow to send push-messages input bool On_Email = true; //allow to send e-mail messages input bool On_Alert= true; //allow to put alert input bool On_Play_Sound = true; //allow to put sound signal input string NameFileSound="alert.wav"; //name of the file with sound input string CommentSirName="Divergence on Awesome Oscillator: "; //the first part of the allert comment //+-----------------------------------+ bool foreground; //+------------------------------------------------------------------+ void OnInit() { //------------------------------------------------------------------------------------------ Awesome_Oscillator SetIndexBuffer(0,Ext_AO_Buffer,INDICATOR_DATA); SetIndexBuffer(1,ExtColor_AO_Buffer,INDICATOR_COLOR_INDEX); PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,DATA_LIMIT); AO_handle=iAO(NULL,0); ArraySetAsSeries(Ext_AO_Buffer,true); ArraySetAsSeries(ExtColor_AO_Buffer,true); //+++++++ SetIndexBuffer(2,AO_BUY_Divergence_Regular_Buffer,INDICATOR_DATA); SetIndexBuffer(3,AO_SELL_Divergence_Regular_Buffer,INDICATOR_DATA); ArraySetAsSeries(AO_BUY_Divergence_Regular_Buffer,true); ArraySetAsSeries(AO_SELL_Divergence_Regular_Buffer,true); PlotIndexSetInteger(1,PLOT_ARROW,AO_BUY_Divergence_Regular_Code); PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,Zero_Value); PlotIndexSetInteger(1,PLOT_ARROW_SHIFT,AO_BUY_Divergence_Regular_Shift); PlotIndexSetInteger(2,PLOT_ARROW,AO_SELL_Divergence_Regular_Code); PlotIndexSetDouble(2,PLOT_EMPTY_VALUE,Zero_Value); PlotIndexSetInteger(2,PLOT_ARROW_SHIFT,-AO_SELL_Divergence_Regular_Shift); //+++ SetIndexBuffer(4,AO_BUY_Divergence_Hidden_Buffer,INDICATOR_DATA); SetIndexBuffer(5,AO_SELL_Divergence_Hidden_Buffer,INDICATOR_DATA); ArraySetAsSeries(AO_BUY_Divergence_Hidden_Buffer,true); ArraySetAsSeries(AO_SELL_Divergence_Hidden_Buffer,true); PlotIndexSetInteger(3,PLOT_ARROW,AO_BUY_Divergence_Hidden_Code); PlotIndexSetDouble(3,PLOT_EMPTY_VALUE,Zero_Value); PlotIndexSetInteger(3,PLOT_ARROW_SHIFT,AO_BUY_Divergence_Hidden_Shift); PlotIndexSetInteger(4,PLOT_ARROW,AO_SELL_Divergence_Hidden_Code); PlotIndexSetDouble(4,PLOT_EMPTY_VALUE,Zero_Value); PlotIndexSetInteger(4,PLOT_ARROW_SHIFT,-AO_SELL_Divergence_Hidden_Shift); //+-----------------------------------+ SetIndexBuffer(6,SIGNAL_ALARM_BUY_Divergence_Regular_Buffer,INDICATOR_DATA); ArraySetAsSeries(SIGNAL_ALARM_BUY_Divergence_Regular_Buffer,true); SetIndexBuffer(7,SIGNAL_ALARM_BUY_Divergence_Hidden_Buffer,INDICATOR_DATA); ArraySetAsSeries(SIGNAL_ALARM_BUY_Divergence_Hidden_Buffer,true); SetIndexBuffer(8,SIGNAL_ALARM_SELL_Divergence_Regular_Buffer,INDICATOR_DATA); ArraySetAsSeries(SIGNAL_ALARM_SELL_Divergence_Regular_Buffer,true); SetIndexBuffer(9,SIGNAL_ALARM_SELL_Divergence_Hidden_Buffer,INDICATOR_DATA); ArraySetAsSeries(SIGNAL_ALARM_SELL_Divergence_Hidden_Buffer,true); foreground=ChartGetInteger(0,CHART_FOREGROUND); ChartSetInteger(0,CHART_FOREGROUND,false); short_name="AO_Divergence"; IndicatorSetString(INDICATOR_SHORTNAME,short_name); //--- initialization done } //+------------------------------------------------------------------+ 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[]) { ArraySetAsSeries(Time,true); ArraySetAsSeries(High,true); ArraySetAsSeries(Low,true); TOTAL_BAR=rates_total; //------------------------------------------------------------------------------------------ Awesome_Oscillator int copy_AO=CopyBuffer(AO_handle,0,0,rates_total,Ext_AO_Buffer); int copy_AO_Color=CopyBuffer(AO_handle,1,0,rates_total,ExtColor_AO_Buffer); //----------------------------------- for(int j=ObjectsTotal(0)-1;j>=0;j--) { if(StringFind(ObjectName(0,j),"AO_Indicator_Divergence")!=-1 || StringFind(ObjectName(0,j),"AO_Price_Divergence")!=-1) if(!ObjectDelete(0,ObjectName(0,j))) Print("Error in deleting object (",GetLastError(),")"); } for(int ad=0; ad0; bar--) { //---- int draw_TrendLine=1; //---- Indicator_count_bar_above(bar); if(Indicator_count_bar_above(bar)!=0) { IsIndicatorPeak(count_bar_above,bar); GetIndicatorLastPeak(count_bar_above,bar); LastPeak_bar=GetIndicatorLastPeak(count_bar_above,bar); GetIndicatorALL_LastPeak_bar(count_bar_above,bar); } if(IsIndicatorPeak(count_bar_above,bar)==true && GetIndicatorLastPeak(count_bar_above,bar)>=3) { for(int l=1;l<200;l++) { if(l>=2 && ALL_LastPeak_bar[l]==0 && ALL_LastPeak_bar[l-1]>=0) break; if(High[bar]>=High[ALL_LastPeak_bar[l]] && Ext_AO_Buffer[bar]=Time[bar]) break; if(Ext_AO_Buffer[ALL_LastPeak_bar[l]-d]>(VALUE_body_line*(1.0+Deviation_percent))) { draw_TrendLine=0; break; } } } if(draw_TrendLine==1) { DrawIndicatorTrendLine(bar,Time[bar],Time[ALL_LastPeak_bar[l]], Ext_AO_Buffer[bar],Ext_AO_Buffer[ALL_LastPeak_bar[l]],Red,STYLE_SOLID); DrawPriceTrendLine(bar,Time[bar],Time[ALL_LastPeak_bar[l]], High[bar],High[ALL_LastPeak_bar[l]],Red,STYLE_SOLID); AO_SELL_Divergence_Regular_Buffer[bar]=Ext_AO_Buffer[bar]; } } //--- if(Low[bar]<=Low[ALL_LastPeak_bar[l]] && Ext_AO_Buffer[bar]>Ext_AO_Buffer[ALL_LastPeak_bar[l]]) { //+++ Indicator lines must going across through the indicators/histogram body ? yes/no if(Indicator_Trend_Line_Allowed_across_through_the_histogram_body==no) { int BAR_body_check=Bars(Symbol(),Period(),Time[ALL_LastPeak_bar[l]],Time[bar])-1; double SLOPE=(Ext_AO_Buffer[bar]-Ext_AO_Buffer[ALL_LastPeak_bar[l]])/(BAR_body_check); for(int d=1; d<=BAR_body_check; d++) { double VALUE_body_line=Ext_AO_Buffer[ALL_LastPeak_bar[l]]+SLOPE*d; if(Time[ALL_LastPeak_bar[l]-d]>=Time[bar]) break; if(Ext_AO_Buffer[ALL_LastPeak_bar[l]-d]>(VALUE_body_line*(1.0+Deviation_percent))) { draw_TrendLine=0; break; } } } if(draw_TrendLine==1) { DrawIndicatorTrendLine(bar,Time[bar],Time[ALL_LastPeak_bar[l]], Ext_AO_Buffer[bar],Ext_AO_Buffer[ALL_LastPeak_bar[l]],Aqua,STYLE_DOT); DrawPriceTrendLine(bar,Time[bar],Time[ALL_LastPeak_bar[l]], Low[bar],Low[ALL_LastPeak_bar[l]],Aqua,STYLE_DOT); AO_BUY_Divergence_Hidden_Buffer[bar]=Ext_AO_Buffer[bar]; } } } } //----------------------------------- Indicator_count_bar_down(bar); if(Indicator_count_bar_down(bar)!=0) { IsIndicatorTrough(count_bar_down,bar); GetIndicatorLastTrough(count_bar_down,bar); LastTrough_bar=GetIndicatorLastTrough(count_bar_down,bar); GetIndicatorALL_LastTrough_bar(count_bar_down,bar); } if(IsIndicatorTrough(count_bar_down,bar)==true && GetIndicatorLastTrough(count_bar_down,bar)>=3) { for(int l=1;l<200;l++) { if(l>=2 && ALL_LastTrough_bar[l]==0 && ALL_LastTrough_bar[l-1]>=0) break; if(Low[bar]<=Low[ALL_LastTrough_bar[l]] && Ext_AO_Buffer[bar]>Ext_AO_Buffer[ALL_LastTrough_bar[l]]) { //+++ Indicator lines must going across through the indicators/histogram body ? yes/no if(Indicator_Trend_Line_Allowed_across_through_the_histogram_body==no) { int BAR_body_check=Bars(Symbol(),Period(),Time[ALL_LastTrough_bar[l]],Time[bar])-1; double SLOPE=(Ext_AO_Buffer[bar]-Ext_AO_Buffer[ALL_LastTrough_bar[l]])/(BAR_body_check); for(int d=1; d<=BAR_body_check; d++) { double VALUE_body_line=Ext_AO_Buffer[ALL_LastTrough_bar[l]]+SLOPE*d; if(Time[ALL_LastTrough_bar[l]-d]>=Time[bar]) break; if(Ext_AO_Buffer[ALL_LastTrough_bar[l]-d]<(VALUE_body_line*(1.0+Deviation_percent))) { draw_TrendLine=0; break; } } } if(draw_TrendLine==1) { DrawIndicatorTrendLine(bar,Time[bar],Time[ALL_LastTrough_bar[l]], Ext_AO_Buffer[bar],Ext_AO_Buffer[ALL_LastTrough_bar[l]],Blue,STYLE_SOLID); DrawPriceTrendLine(bar,Time[bar],Time[ALL_LastTrough_bar[l]], Low[bar],Low[ALL_LastTrough_bar[l]],Blue,STYLE_SOLID); AO_BUY_Divergence_Regular_Buffer[bar]=Ext_AO_Buffer[bar]; } } //--- if(Low[bar]>=Low[ALL_LastTrough_bar[l]] && Ext_AO_Buffer[bar]=Time[bar]) break; if(Ext_AO_Buffer[ALL_LastTrough_bar[l]-d]<(VALUE_body_line*(1.0+Deviation_percent))) { draw_TrendLine=0; break; } } } if(draw_TrendLine==1) { DrawIndicatorTrendLine(bar,Time[bar],Time[ALL_LastTrough_bar[l]], Ext_AO_Buffer[bar],Ext_AO_Buffer[ALL_LastTrough_bar[l]],Orange,STYLE_DOT); DrawPriceTrendLine(bar,Time[bar],Time[ALL_LastTrough_bar[l]], Low[bar],Low[ALL_LastTrough_bar[l]],Orange,STYLE_DOT); AO_SELL_Divergence_Hidden_Buffer[bar]=Ext_AO_Buffer[bar]; } } } } } //----------------------------------------------------------------------------------------------------------// for(int i=0; i=0;j--) { if(StringFind(ObjectName(0,j),"AO_Indicator_Divergence")!=-1 || StringFind(ObjectName(0,j),"AO_Price_Divergence")!=-1) if(!ObjectDelete(0,ObjectName(0,j))) Print("Error in deleting object (",GetLastError(),")"); } ChartRedraw(); ChartSetInteger(0,CHART_FOREGROUND,foreground); } //----------------------------------------------------------------------------------------------------------// //+------------------------------------------------------------------+ //| Check domain_above Awesome_Oscillator bar | //+------------------------------------------------------------------+ int Indicator_count_bar_above(int bar) { count_bar_above=0; for(int j=bar; j=0.0) { if(Ext_AO_Buffer[j]>=0.0 && Ext_AO_Buffer[j+1]<=0.0) { count_bar_above=j+1; break; } } else break; } return(count_bar_above); } //----------------------------------------------------------------------------------------------------------// //+------------------------------------------------------------------+ //| Check domain_down Awesome_Oscillator bar | //+------------------------------------------------------------------+ int Indicator_count_bar_down(int bar) { count_bar_down=0; for(int j=bar; j=0.0) { count_bar_down=j+1; break; } } else break; } return(count_bar_down); } //----------------------------------------------------------------------------------------------------------// //+------------------------------------------------------------------+ //| Get Awesome_Oscillator last Peak | //+------------------------------------------------------------------+ int GetIndicatorLastPeak(int count_bar,int bar) { int LastPeak=0; if(count_bar<6) return(LastPeak); for(int j=bar+3; j<=count_bar-2; j++) { if(Ext_AO_Buffer[j]>=Ext_AO_Buffer[j+1]&& Ext_AO_Buffer[j]>Ext_AO_Buffer[j+2]&& Ext_AO_Buffer[j]>=Ext_AO_Buffer[j-1]&& Ext_AO_Buffer[j]>Ext_AO_Buffer[j-2]) { LastPeak=j; break; } } return(LastPeak); } //----------------------------------------------------------------------------------------------------------// int GetIndicatorALL_LastPeak_bar(int count_bar,int bar) { for(int i=0;i<200; i++) ALL_LastPeak_bar[i]=0; ALL_LastPeak_bar[0]=bar; int ii=1; for(int j=bar+3; j<=count_bar-2; j++) { ALL_LastPeak_bar[ii]=GetIndicatorLastPeak(count_bar,ALL_LastPeak_bar[ii-1]); if(ii>1 && ALL_LastPeak_bar[ii]==0 && ALL_LastPeak_bar[ii-1]>=0) break; ii++; } return(0); } //+------------------------------------------------------------------+ //| Get Awesome_Oscillator last Trough | //+------------------------------------------------------------------+ int GetIndicatorLastTrough(int count_bar,int bar) { int LastTrough=0; if(count_bar<6) return(LastTrough); for(int j=bar+3; j<=count_bar-2; j++) { if(Ext_AO_Buffer[j]<=Ext_AO_Buffer[j+1]&& Ext_AO_Buffer[j]1 && ALL_LastTrough_bar[ii]==0 && ALL_LastTrough_bar[ii-1]>=0) break; ii++; } return(0); } //+------------------------------------------------------------------+ //| Is Awesome_Oscillator Peak | //+------------------------------------------------------------------+ bool IsIndicatorPeak(int Peak,int bar) { if(Peak<4) return(false); if(Ext_AO_Buffer[bar]>=Ext_AO_Buffer[bar+1] && Ext_AO_Buffer[bar]>Ext_AO_Buffer[bar+2] && Ext_AO_Buffer[bar]>Ext_AO_Buffer[bar-1]) return(true); return(false); } //----------------------------------------------------------------------------------------------------------// //+------------------------------------------------------------------+ //| Is Awesome_Oscillator Trough | //+------------------------------------------------------------------+ bool IsIndicatorTrough(int Trough,int bar) { if(Trough<4) return(false); if(Ext_AO_Buffer[bar]<=Ext_AO_Buffer[bar+1] && Ext_AO_Buffer[bar]