Navigation´╝ÜHome > Content >

ADXAlertV10.mq4

Time: 2012-05-20 | Download file:ADXAlertV10.mq4

//+------------------------------------------------------------------+
//|                                               TS_ADXAlertV01.mq4 |
//|                                                   Manfred Uschan |
//|                                     http://www.tradingschule.com |
//+------------------------------------------------------------------+
#property copyright "Manfred Uschan"
#property link      "http://www.tradingschule.com"

#property indicator_separate_window
#include 
#property indicator_minimum -100
#property indicator_maximum 100
#property indicator_buffers 8

#property indicator_color1 DarkGreen
#property indicator_color2 Maroon
#property indicator_color3 DimGray
#property indicator_color4 Navy
#property indicator_color5 Maroon
#property indicator_color6 CornflowerBlue
#property indicator_color7 Yellow
//#property indicator_color8 White


#property indicator_width1 5
#property indicator_width2 5
#property indicator_width3 5
#property indicator_width4 2
#property indicator_width5 2
#property indicator_width6 2
#property indicator_width7 2
//#property indicator_width8 4


#property indicator_level1 0.0
//#property indicator_level2 24.0
//#property indicator_level3 -24.0

/*#property indicator_levelcolor DimGray
#property indicator_levelwidth 1
#property indicator_levelstyle 0*/



extern int ADXPeriod = 28;
extern int ADXLevel = 24;

extern int FastEmaPeriod = 12;
extern int SlowEmaPeriod = 26;
extern int SignalPeriod = 9;

extern bool AlertTrendChange = false;
extern bool SoundOnAlert = false;
extern bool AlertEntry = false;

extern bool ShowWeakSignals = false;

extern int limit = 300;

//---- progvars
double buf_adxplusdi[], buf_adxplusdival[];
double buf_adxminusdi[], buf_adxminusdival[];
double buf_adx[], buf_adxval[];
double buf_adxdifference[];
double buf_linedi[];
double buf_adxinactive[];
double buf_adxstrength[];
double buf_adxstrengthbar[];
double buf_sma[];
double buf_entry[];

double buf_macdmain[];
double buf_macdsignal[];

double buf_arrowLong[];
double buf_arrowShort[];

double buf_zeroline[];
double buf_null[];

double buf_mahigh[];
double buf_malow[];

double buf_copen[];
double buf_cclose[];
double buf_chigh[];
double buf_clow[];

double adx0, adx1, adx2;
double adxplusdi0, adxplusdi1, adxplusdi2;
double adxminusdi0, adxminusdi1, adxminusdi2;
double adxdifference0, adxdifference1, adxdifference2;
double arrowGap;

int PrevSignal = 0;
int markerDiv = 4;
int alertRange = 0;
int maxArrows;

datetime lastPriceAlertedBar=0;

string lastAlert = "";
string lastAlertEntry = "";

double Poin;
int n_digits = 0;
double divider = 1;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,5,DarkGreen);
   SetIndexBuffer(0,buf_adxplusdi);
   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,5, Maroon);
   SetIndexBuffer(1,buf_adxminusdi);
   SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,5, DimGray);
   SetIndexBuffer(2,buf_adxinactive);
   SetIndexStyle(3,DRAW_LINE,STYLE_SOLID,2, Navy);
   SetIndexBuffer(3,buf_null);
   SetIndexStyle(4,DRAW_LINE,STYLE_SOLID,2, Maroon);
   SetIndexBuffer(4,buf_macdmain);
   SetIndexStyle(5,DRAW_LINE,STYLE_SOLID,2, CornflowerBlue);
   SetIndexBuffer(5,buf_macdsignal);
   SetIndexStyle(6,DRAW_LINE,STYLE_SOLID,2, Yellow);
   SetIndexBuffer(6,buf_entry);
   
   /*SetIndexBuffer(6,buf_arrowLong);
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexArrow(6,241); // 159 is a dot code
   SetIndexEmptyValue(6,0.0);
   SetIndexLabel(6,"Buy");*/

   /*SetIndexBuffer(7,buf_arrowShort);
   SetIndexStyle(7,DRAW_ARROW);
   SetIndexArrow(7,242);
   SetIndexEmptyValue(7,0.0);
   SetIndexLabel(7,"Sell");*/

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectsDeleteAll();
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   alertRange = 2;
   int dig = 1;
   if (Digits == 5)  {dig = 10;}
   arrowGap = 15*Point*dig;
   showSpread();
   
//----
   if(counted_bars<0) return(-1);
   //---- the last counted bar will be recounted
   if(counted_bars>0) counted_bars--;
   
   for(int i=0; i < limit; i++)
	{
	   double adxplusdi =  iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_PLUSDI,i);
      double adxminusdi =  -iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_MINUSDI,i);
      double adxval =  iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_MAIN,i);  
      
      double macdmain = iMACD(NULL,0,FastEmaPeriod,SlowEmaPeriod,SignalPeriod,PRICE_CLOSE, MODE_MAIN, i);
      double macdsignal = iMACD(NULL,0,FastEmaPeriod,SlowEmaPeriod,SignalPeriod,PRICE_CLOSE, MODE_SIGNAL, i);
      
      int macloseperiod = 6;
      double maclose = iMA(NULL, 0, macloseperiod, 0, MODE_SMA, PRICE_CLOSE, i);
      
      int maopenperiod = 5;
      double maopen = iMA(NULL, 0, maopenperiod, 0, MODE_SMA, PRICE_OPEN, i);
      
      buf_zeroline[i] = 0;
      
      buf_adxplusdival[i] = adxplusdi;
      buf_adxplusdi[i] = adxplusdi;
      
      buf_adxminusdival[i] = adxminusdi;
      buf_adxminusdi[i] = adxminusdi;
      
      buf_adxval[i] = adxval;
      
      /*double diplus = buf_adxplusdi[i];
      double diminus = -buf_adxminusdi[i];
      double didif = diplus - diminus;*/
      //buf_adxdifference[i] =  didif;
      
      //buf_adxstrengthbar
      if (adxval > ADXLevel && -adxminusdi > adxplusdi) {buf_adxstrengthbar[i] = -10;}
      if (adxval > ADXLevel && -adxminusdi < adxplusdi) {buf_adxstrengthbar[i] = 10;}
      //buf_line
      if (buf_adxplusdi[i] < -buf_adxminusdi[i]) {buf_linedi[i] = buf_adxdifference[i];}
      if (buf_adxplusdi[i] > -buf_adxminusdi[i]) {buf_linedi[i] = buf_adxdifference[i];}
      //buf_adxinactive
      if (buf_adxplusdi[i] < -buf_adxminusdi[i]) {buf_adxinactive[i] = buf_adxplusdi[i];}
      if (buf_adxplusdi[i] > -buf_adxminusdi[i]) {buf_adxinactive[i] = buf_adxminusdi[i];}
      
	   //MACD
	   //while (macdmain < 50){macdmain = macdmain*10;}
	   if (macdmain > 0 && macdsignal > macdmain) {buf_macdmain[i] = 90; buf_macdsignal[i] = 70;}
	   if (macdmain > 0 && macdsignal < macdmain) {buf_macdmain[i] = 70; buf_macdsignal[i] = 90;}
      if (macdmain < 0 && macdsignal < macdmain) {buf_macdmain[i] = -90; buf_macdsignal[i] = -70;}
	   if (macdmain < 0 && macdsignal > macdmain) {buf_macdmain[i] = -70; buf_macdsignal[i] = -90;}
	   
	   //MAs
	   if (maclose > maopen) {buf_sma[i] = 80;}
	   if (maclose < maopen) {buf_sma[i] = -80;}
	   
	   //ALERTS
	   bool triggerBull = false;
	   bool triggerBear = false;
	   double tpdi1 = buf_adxplusdi[1];
	   double tpdi2 = buf_adxplusdi[2];
	   double tmdi1 = buf_adxminusdi[1];
	   double tmdi2 = buf_adxminusdi[2];
	   //if (tpdi1 > -tmdi1 && tpdi2 < -tmdi2) {triggerBull = true; /*Print("BullCross!");*/}
	   //if (tpdi1 < -tmdi1 && tpdi2 > -tmdi2) {triggerBear = true; /*Print("BearCross!");*/}
	   string ident1 = "bull"+Time[i+1];
	   string ident2 = "bear"+Time[i+1];
	   //if (AlertTrendChange && triggerBull) {doAlert("ADX crossed UP", ident1);}
	   //if (AlertTrendChange && triggerBear) {doAlert("ADX crossed DOWN", ident2);}

	   
	   //--------------------------------------------------------------------------------
	   //Patterns
	   //--------------------------------------------------------------------------------
	   //buf_mahigh[i] = 1;
	   buf_cclose[i] = iClose(NULL, 0, i);
	   buf_copen[i] = iOpen(NULL, 0, i);
      buf_chigh[i] = iHigh(NULL, 0, i);
      buf_clow[i] = iLow(NULL, 0, i);
      
      buf_arrowLong[i]=0;
      buf_arrowShort[i]=0;

      int maperiods = 5;
      double mah = iMA(NULL,0,maperiods,0,MODE_SMMA,PRICE_HIGH,i);   
      //Print ("= "+mah);
      buf_mahigh[i] = mah;
      //Print (buf_mahigh[i]);
      
      buf_malow[i] = iMA(NULL,0,maperiods,0,MODE_SMMA,PRICE_LOW,i);
      
      //Print ("i "+i);
      

      //BUY
      if (1==1
         && buf_copen[i+1] < buf_chigh[i+1]
         && buf_cclose[i+1] > buf_chigh[i+1]
         ) {
         buf_entry[i] = 95;
         //if (buf_entry[i+2] != 95) {objArrow("SELL"+Time[i], 241, Time[i], l1 - arrowGap, Green);}
      }
      //SELL
      else if (1==1 
         && 1==2
         ) {
         buf_entry[i] = -95;
         //if (buf_entry[i+2] != -95) {objArrow("SELL"+Time[i], 242, Time[i], h1 + arrowGap, Red);}
      }
      //NEUTRAL
      else {buf_entry[i] = 0;}
      
      //ALERT-ENTRY
      string name = "Alert"+Time[i];
      if (AlertEntry && buf_entry[i+1] > 0 && buf_entry[i+2] < 1) {doAlertEntry("Possible BUY Entry over: "+iClose(NULL, 0, 1), name);}
	   if (AlertEntry && buf_entry[i+1] < 0 && buf_entry[i+2] > -1) {doAlertEntry("Possible SELL Entry under: "+iClose(NULL, 0, 1), name);}
      
      //Print (" +:::  "+iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_PLUSDI,0));
      //Print (" -:::  "+iADX(NULL,0,ADXPeriod,PRICE_CLOSE,MODE_MINUSDI,0));
      //Print (" c1 ::  "+close1);
	   
   }
//----
   return(0);
  }
//+------------------------------------------------------------------+

void objArrow(string name, int code, datetime time, double price, color col, int window=0, int width=1)
  {
   if(ObjectFind(name)==-1) {
      ObjectCreate(name, OBJ_ARROW, window, time, price);
   }
   ObjectSet(name, OBJPROP_ARROWCODE, code);
   ObjectSet(name, OBJPROP_TIME1, time);
   ObjectSet(name, OBJPROP_PRICE1, price);
   ObjectSet(name, OBJPROP_COLOR, col);
   ObjectSet(name, OBJPROP_WIDTH, width);
  }

void objLabel(string name, string tex, int corner, int position_x, int position_y, int window=0, color tex_color=White, string tex_font="Arial", int tex_size=12)
  {
   if(ObjectFind(name)==-1) {
      ObjectCreate(name, OBJ_LABEL, window, 0, 0);
   }
   ObjectSet(name, OBJPROP_CORNER, corner);
   ObjectSet(name, OBJPROP_XDISTANCE, position_x);
   ObjectSet(name, OBJPROP_YDISTANCE, position_y);
   ObjectSetText(name, tex, tex_size, tex_font, tex_color);
  }

void objTrendLine(string name, datetime time_1, double price_1, datetime time_2, double price_2, int window=0, int width=1, color col=White, int style=STYLE_SOLID, bool ray=true)
  {
   if(ObjectFind(name)==-1) {
      ObjectCreate(name, OBJ_TREND, window, time_1, price_1, time_2, price_2);
   }
   ObjectSet(name, OBJPROP_PRICE1, price_1);
   ObjectSet(name, OBJPROP_PRICE2, price_2);
   ObjectSet(name, OBJPROP_TIME1, time_1);
   ObjectSet(name, OBJPROP_TIME2, time_2);
   ObjectSet(name, OBJPROP_WIDTH, width);
   ObjectSet(name, OBJPROP_COLOR, col);
   ObjectSet(name, OBJPROP_STYLE, style);
   ObjectSet(name, OBJPROP_RAY, ray);
  }

/*void objHLine(string name, double price, int window=0, color col=White, int style=STYLE_SOLID)
  {
   if(ObjectFind(name)==-1) {
      ObjectCreate(name, OBJ_HLINE, window, price);
   }
   ObjectSet(name, OBJPROP_PRICE1, price);
   ObjectSet(name, OBJPROP_COLOR, col);
   ObjectSet(name, OBJPROP_STYLE, style);
  }*/
  
void objText(string name, string tex, datetime time, double price, int window=0, color tex_color=White, string tex_font="Arial", int tex_size=12)
  {
   if(ObjectFind(name)==-1) {
      ObjectCreate(name, OBJ_TEXT, window, time, price);
   }
   ObjectSet(name, OBJPROP_TIME1, time);
   ObjectSet(name, OBJPROP_PRICE1, price);
   ObjectSetText(name, tex, tex_size, tex_font, tex_color);
  }

void objRectangle(string name, datetime time_1, double price_1, datetime time_2, double price_2, color color_obj, int window=0, bool draw_back=true)
  {
   if(ObjectFind(name)==-1) {
      ObjectCreate(name, OBJ_RECTANGLE, window, time_1, price_1, time_2, price_2);
   }
   ObjectSet(name, OBJPROP_TIME1, time_1);
   ObjectSet(name, OBJPROP_PRICE1, price_1);
   ObjectSet(name, OBJPROP_TIME2, time_2);
   ObjectSet(name, OBJPROP_PRICE2, price_2);
   ObjectSet(name, OBJPROP_COLOR, color_obj);
   ObjectSet(name, OBJPROP_BACK, draw_back);
  }
  
void objDot(string name, datetime time, double pos, color col)
  {
   if(ObjectFind(name)==-1) {
      ObjectCreate(name, OBJ_TEXT, 0, time, pos); 
   }
   ObjectSetText(name, CharToStr(159), 14, "Wingdings", col); 
  }  

void doAlert(string doWhat, string alertIdent)
   {
   string message;
   Print ("Alert triggered !!");
      if (lastAlert != alertIdent) {
          lastAlert = alertIdent;              
          message =  StringConcatenate(Symbol(),""," => "+doWhat);
          Alert(message);
          if (SoundOnAlert)   PlaySound("alert.wav"); 
          
      }           
   }

void doAlertEntry(string doWhat, string alertIdent)
   {
   string message;
   Print ("Alert triggered !!");
      if (lastAlertEntry != alertIdent) {
          lastAlertEntry = alertIdent;              
          message =  StringConcatenate(Symbol(),""," => "+doWhat);
          Alert(message);
          if (SoundOnAlert)   PlaySound("alert.wav"); 
          
      }           
   }
   
void showSpread() {
   //Checking for unconvetional Point digits number
   if (Point == 0.00001) Poin = 0.0001; //5 digits
   else if (Point == 0.001) Poin = 0.01; //3 digits
   else Poin = Point; //Normal
   
   ObjectCreate("Spread", OBJ_LABEL, 0, 0, 0);
   ObjectSet("Spread", OBJPROP_CORNER, 1);
   ObjectSet("Spread", OBJPROP_XDISTANCE, 10);
   ObjectSet("Spread", OBJPROP_YDISTANCE, 30);
   double spread = MarketInfo(Symbol(), MODE_SPREAD);
   
   if ((Poin > Point) && (true))
   {
      divider = 10.0;
      n_digits = 1;
   }
   
   ObjectSetText("Spread", "Spread: " + DoubleToStr(NormalizeDouble(spread / divider, 1), n_digits), 12, "Arial", White);

}

Recommend