Navigation´╝ÜHome > Content >

asymmetric_bands_allerts_&_arrows.mq4

Time: 2012-02-03 | Download file:asymmetric_bands_allerts_&_arrows.mq4

//+------------------------------------------------------------------+
//|                                             asymmetric bands.mq4 |
//|                                                                  |
//| forex-tsd elite section only                                     |
//+------------------------------------------------------------------+
#property copyright "mladen"
#property link      "[email protected]"

#property indicator_chart_window
#property indicator_buffers  5
#property indicator_color1   DimGray
#property indicator_color2   Red
#property indicator_color3   LimeGreen
#property indicator_color4   LimeGreen
#property indicator_color5   Red
#property indicator_style1   STYLE_DOT

//
//
//
//
//

extern int    bandsPeriod     = 14;
extern int    bandsMethod     = MODE_SMA;
extern int    bandsPrice      = PRICE_CLOSE;
extern double bandsDeviations = 2;
extern int    arrowSize       = 2;
extern bool   alertsOn        = false;
extern bool   alertsOnCurrent = true;
extern bool   alertsMessage   = true;
extern bool   alertsSound     = false;
extern bool   alertsNotify    = false;
extern bool   alertsEmail     = false;

//
//
//
//
//

double maBuffer[];
double upBuffer[];
double dnBuffer[];
double wuBuffer[];
double wdBuffer[];
double arrowUp[];
double arrowDn[];

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int init()
{
   IndicatorBuffers(7);
   SetIndexBuffer(0,maBuffer);
   SetIndexBuffer(1,upBuffer);
   SetIndexBuffer(2,dnBuffer);
   SetIndexBuffer(3,arrowUp); SetIndexStyle(3,DRAW_ARROW,0,arrowSize); SetIndexArrow(3,241);
   SetIndexBuffer(4,arrowDn); SetIndexStyle(4,DRAW_ARROW,0,arrowSize); SetIndexArrow(4,242);
   SetIndexBuffer(5,wuBuffer);
   SetIndexBuffer(6,wdBuffer);
   return(0);
}
int deinit() { return(0); }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
//
//
//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
   int i,limit;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
         
   //
   //
   //
   //
   //
            
   for(i=limit; i>=0; i--)
   {
      double price = iMA(NULL,0,1          ,0,MODE_SMA   ,bandsPrice,i);
      maBuffer[i]  = iMA(NULL,0,bandsPeriod,0,bandsMethod,bandsPrice,i);
    
         if (i==(Bars-1))
         {
            upBuffer[i] = maBuffer[i];
            dnBuffer[i] = maBuffer[i];
            wuBuffer[i] = price-maBuffer[i];
            wdBuffer[i] = price-maBuffer[i];
            continue;
         }
         
      //
      //
      //
      //
      //
      
         double diff = price-maBuffer[i];
         if(diff>=0)
            {
               wuBuffer[i] = (wuBuffer[i+1]*(bandsPeriod-1)+MathPow(diff,2))/bandsPeriod;
               wdBuffer[i] =  wdBuffer[i+1]*(bandsPeriod-1)/bandsPeriod;
            }
         else
            {
               wdBuffer[i] = (wdBuffer[i+1]*(bandsPeriod-1)+MathPow(diff,2))/bandsPeriod;
               wuBuffer[i] =  wuBuffer[i+1]*(bandsPeriod-1)/bandsPeriod;
            }
         upBuffer[i] = maBuffer[i] + bandsDeviations*MathSqrt(wuBuffer[i]);
         dnBuffer[i] = maBuffer[i] - bandsDeviations*MathSqrt(wdBuffer[i]);
         arrowUp[i]  = EMPTY_VALUE;
         arrowDn[i]  = EMPTY_VALUE;
         if (High[i]>=upBuffer[i] && High[i+1]<=upBuffer[i+1]) arrowDn[i] = upBuffer[i] + iATR(NULL,0,20,i)/2.0;
         if ( Low[i]<=dnBuffer[i] &&  Low[i+1]>=dnBuffer[i+1]) arrowUp[i] = dnBuffer[i] - iATR(NULL,0,20,i)/2.0;   
   }

   //
   //
   //
   //
   //
   
      if (alertsOn)
      {
        if (alertsOnCurrent)
             int whichBar = 0;
        else     whichBar = 1; 
      
        //
        //
        //
        //
        //
      
        if (arrowUp[whichBar+1] == EMPTY_VALUE && arrowUp[whichBar] != EMPTY_VALUE) doAlert(whichBar,"low price crossing lower band");
        if (arrowDn[whichBar+1] == EMPTY_VALUE && arrowDn[whichBar] != EMPTY_VALUE) doAlert(whichBar,"high price crossing upper band");
      }         
   return(0);
}


//
//
//
//
//

void doAlert(int forBar, string doWhat)
{
   static string   previousAlert="nothing";
   static datetime previousTime;
   string message;
   
      if (previousAlert != doWhat || previousTime != Time[forBar]) {
          previousAlert  = doWhat;
          previousTime   = Time[forBar];

          //
          //
          //
          //
          //

          message =  StringConcatenate(Symbol()," at ",TimeToStr(TimeLocal(),TIME_SECONDS)," assymetric bands ",doWhat);
             if (alertsMessage) Alert(message);
             if (alertsNotify)  SendNotification(message);
             if (alertsEmail)   SendMail(StringConcatenate(Symbol(), Period(), " assymetric bands "),message);
             if (alertsSound)   PlaySound("alert2.wav");
      }
}

Recommend