Hi,
A forum member zznbrm kindly assisted me with this indicator code and I am trying to add an alert to this for sound and/or email.
I am not sure I have got the logic right following the if statement but basically when the MA crosses ( the histogram changes to orange) I would like an alert but I can't figure this. My best effort which i had to undo resulted in repeated alerts.
Code below, would appreciate if someone might be able to show me where I am going wrong with this.
PG
A forum member zznbrm kindly assisted me with this indicator code and I am trying to add an alert to this for sound and/or email.
I am not sure I have got the logic right following the if statement but basically when the MA crosses ( the histogram changes to orange) I would like an alert but I can't figure this. My best effort which i had to undo resulted in repeated alerts.
Code below, would appreciate if someone might be able to show me where I am going wrong with this.
PG
Inserted Code
//+------------------------------------------------------------------+ //| MA-Cross-MTF.mq4 | //| Copyright © 2011, zznbrm | //+------------------------------------------------------------------+ #property copyright "Copyright © 2011, zznbrm" #property indicator_separate_window #property indicator_buffers 4 #property indicator_color1 Green #property indicator_color2 Orange #property indicator_color3 Orange #property indicator_color4 Red #property indicator_width1 5 #property indicator_width2 5 #property indicator_width3 5 #property indicator_width4 5 #property indicator_maximum 1 #property indicator_minimum 0 //+------------------------------------------------------------------+ //| User Inputs | //+------------------------------------------------------------------+ extern int eintTF = PERIOD_M15; extern int eintPeriod1 = 5; extern int eintPeriod2 = 10; extern string estrMethod0 = "0=SMA,1=EMA,2=SMMA,3=LWMA"; extern int eintMethod1 = MODE_EMA; extern int eintMethod2 = MODE_EMA; extern bool SoundON=true; extern bool EmailON=false; //+------------------------------------------------------------------+ //| Buffers | //+------------------------------------------------------------------+ double gadblUp[]; double gadblCrossUp[]; double gadblCrossDown[]; double gadblDown[]; //+------------------------------------------------------------------+ //| Global Variables | //+------------------------------------------------------------------+ //********************************************************************************************* //* STANDARD FUNCTIONS BELOW * //********************************************************************************************* //+------------------------------------------------------------------+ //| init() | //+------------------------------------------------------------------+ int init() { string strShortName = "MA-Cross-MTF(" + eintTF + ")"; IndicatorShortName( strShortName ); IndicatorDigits( 0 ); SetIndexBuffer( 0, gadblUp ); SetIndexLabel( 0, "Up" ); SetIndexStyle( 0, DRAW_HISTOGRAM ); SetIndexEmptyValue( 0, 0.0 ); SetIndexBuffer( 1, gadblCrossUp ); SetIndexLabel( 1, "CrossUp" ); SetIndexStyle( 1, DRAW_HISTOGRAM ); SetIndexEmptyValue( 1, 0.0 ); SetIndexBuffer( 2, gadblCrossDown ); SetIndexLabel( 2, "CrossDown" ); SetIndexStyle( 2, DRAW_HISTOGRAM ); SetIndexEmptyValue( 2, 0.0 ); SetIndexBuffer( 3, gadblDown ); SetIndexLabel( 3, "Down" ); SetIndexStyle( 3, DRAW_HISTOGRAM ); SetIndexEmptyValue( 3, 0.0 ); return(0); } //+------------------------------------------------------------------+ //| deinit() | //+------------------------------------------------------------------+ int deinit() { return(0); } //+------------------------------------------------------------------+ //| start() | //+------------------------------------------------------------------+ int start() { int intCountedBars = IndicatorCounted(); if ( intCountedBars < 0 ) return( -1 ); if ( intCountedBars > 0 ) intCountedBars--; int jnx; int intLimit = Bars - intCountedBars; int intMax = Bars - ( MathMax( eintPeriod1, eintPeriod2 ) + 1 ); double dblMA1, dblMA2; if ( intLimit > intMax ) intLimit = intMax; for( int inx = intLimit; inx >= 0; inx-- ) { jnx = iBarShift( Symbol(), eintTF, Time[inx] ); dblMA1 = calcMAAltClose( jnx, eintPeriod1, eintMethod1, Close[inx] ); dblMA2 = calcMAAltClose( jnx, eintPeriod2, eintMethod2, Close[inx] ); gadblUp[inx] = 0.0; gadblCrossUp[inx] = 0.0; gadblCrossDown[inx] = 0.0; gadblDown[inx] = 0.0; if ( dblMA1 > dblMA2 ) { if ( ( gadblCrossUp[inx+1] > 0.0 ) || ( gadblUp[inx+1] > 0.0 ) ) gadblUp[inx] = 1.0; else gadblCrossUp[inx] = 1.0; } else if ( dblMA1 < dblMA2 ) { if ( ( gadblCrossDown[inx+1] > 0.0 ) || ( gadblDown[inx+1] > 0.0 ) ) gadblDown[inx] = 1.0; else gadblCrossDown[inx] = 1.0; } else { gadblUp[inx] = gadblUp[inx+1]; gadblCrossUp[inx] = gadblCrossUp[inx+1]; gadblCrossDown[inx] = gadblCrossDown[inx+1]; gadblDown[inx] = gadblDown[inx+1]; // added to try to have alert function if (SoundON) Alert("MA Crossed =",Ask,"\n Bid=",Bid,"\n Time=",TimeToStr(CurTime(),TIME_DATE)," ",TimeHour(CurTime()),":",TimeMinute(CurTime()),"\n Symbol=",Symbol()," Period=",Period()); PlaySound("alert.wav"); if (EmailON) SendMail("MA Crossed","BUY signal at Ask="+DoubleToStr(Ask,4)+", Bid="+DoubleToStr(Bid,4)+", Date="+TimeToStr(CurTime(),TIME_DATE)+" "+TimeHour(CurTime())+":"+TimeMinute(CurTime())+" Symbol="+Symbol()+" Period="+Period()); } } return(0); } //+------------------------------------------------------------------+ //| calcMAAltClose | //+------------------------------------------------------------------+ double calcMAAltClose( int intShift, int intPeriod, int intMethod, double dblPrice ) { double adblPrice[], adblTemp[]; ArrayResize( adblTemp, intPeriod+100 ); ArraySetAsSeries( adblTemp, true ); ArrayCopySeries( adblPrice, MODE_CLOSE, Symbol(), eintTF ); ArrayCopy( adblTemp, adblPrice, 0, intShift, intPeriod+100 ); adblTemp[0] = dblPrice; return( iMAOnArray( adblTemp, WHOLE_ARRAY, intPeriod, 0, intMethod, 0 ) ); }