MA_Price_Cross1.1

MA_Price_Cross1.1
Price Data Components
Series array that contains close prices for each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1It issuies visual alerts to the screenIt plays sound alertsIt sends emails
0 Views
0 Downloads
0 Favorites
MA_Price_Cross1.1
//+------------------------------------------------------------------+
//|                                        MA-Cross/Price Signal.mq4 |
//+------------------------------------------------------------------+

/*                                         Copyright © 2008, GODFREYH   
  +------------------------------------------------------------------+
  | Allows you to enter a ma period and it will then show you and    |
  | email you at which point price crossed the MA.                                |
  +------------------------------------------------------------------+
*/   

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 DodgerBlue
#property indicator_color2 Red

double CrossUp[];
double CrossDown[];
extern string MA_Type_A = "0=sma, 1=ema";
extern string MA_Type_B = "2=smma, 3=lwma";
extern int MA_Type_selected = 1; //0=sma, 1=ema, 2=smma, 
extern int MA_Period =50;
double PRICECLOSE;
extern bool Alert_On_Crossing = true;
extern bool POP_UP_ON = true;
extern bool EMAIL_ON = false;
extern string NameFileSound = "alert.wav";
string MAshort_name;



//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0, DRAW_ARROW,EMPTY);
   SetIndexArrow(0, 233);
   SetIndexBuffer(0, CrossUp);
   SetIndexStyle(1, DRAW_ARROW, EMPTY);
   SetIndexArrow(1, 234);
   SetIndexBuffer(1, CrossDown);
  
   
   //---- indicator short name
    
   switch(MA_Type_selected)
     {
      case 1 : MAshort_name="EMA";  break;
      case 2 : MAshort_name="SMMA"; break;
      case 3 : MAshort_name="LWMA"; break;
      default :
         MA_Type_selected=0;
         MAshort_name="SMA";
     }
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }
 
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   static datetime dt = 0;
   int limit, i, counter;
   double MAnow, Pricenow, MAprevious, Priceprevious, MAafter, Priceafter;
   double Range, AvgRange;
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;
   
  static datetime cDT = 0;
  for(i = 0; i <= limit; i++) {
   
      counter=i;
      Range=0;
      AvgRange=0;
      CrossUp[i] = 0; CrossDown[i] = 0;
      for (counter=i ;counter<=i+9;counter++)
      {
         AvgRange=AvgRange+MathAbs(High[counter]-Low[counter]);
      }
      Range=AvgRange/10;
       
      MAnow = iMA(NULL, 0, MA_Period, 0, MA_Type_selected, PRICE_CLOSE, i);
      MAprevious = iMA(NULL, 0, MA_Period, 0, MA_Type_selected, PRICE_CLOSE, i+1);
      MAafter = iMA(NULL, 0, MA_Period, 0, MA_Type_selected, PRICE_CLOSE, i-1);

      Pricenow =  iClose(NULL,0,i);
      Priceprevious = iClose(NULL,0,i+1);
      Priceafter = iClose(NULL,0,i-1);
      
     
      if ((Pricenow > MAnow) && (Priceprevious < MAprevious) && (Priceafter > MAafter)) {
         CrossUp[i] = Low[i] - Range*0.75;
      if ((i < 2) && (dt != iTime(NULL,0,0)))
      {
       if (Alert_On_Crossing ==true)
      {
      
      if (POP_UP_ON ==true) 
      {
       Alert(Symbol(), " M", Period(), " Price closed above moving average");
      }   
       PlaySound(NameFileSound);
       dt = iTime(NULL,0,0);       	    
      }
      
       if (EMAIL_ON ==true) 
      {
       SendMail("Price closed above MA"," Price closed above moving average");
       dt = iTime(NULL,0,0);
      }   
      
      }
    }  
     else if ((Pricenow < MAnow) && (Priceprevious > MAprevious) && (Priceafter < MAafter)){
       CrossDown[i] = High[i] + Range*0.75;
        if ((i < 2) && (dt != iTime(NULL,0,0)))
      {                
        if (Alert_On_Crossing ==true)
        {
        if (POP_UP_ON ==true) 
      {           
         Alert(Symbol(), " M", Period(), " Price closed below moving average");  
              } 
          PlaySound(NameFileSound);
         dt = iTime(NULL,0,0);       	    
      }
      
       if (EMAIL_ON ==true) 
      {
       SendMail("Price closed below MA"," Price closed below moving average");
       dt = iTime(NULL,0,0);
            
      }
          }}
 
  }
   return(0);
}


Comments

Markdown supported. Formatting help

Markdown Formatting Guide

Element Markdown Syntax
Heading # H1
## H2
### H3
Bold **bold text**
Italic *italicized text*
Link [title](https://www.example.com)
Image ![alt text](image.jpg)
Code `code`
Code Block ```
code block
```
Quote > blockquote
Unordered List - Item 1
- Item 2
Ordered List 1. First item
2. Second item
Horizontal Rule ---