Navigation´╝ÜHome > Content >

Buy_Sell_volume_2_mtf_&_alerts.mq4

Time: 2013-04-23 | Download file:Buy_Sell_volume_2_mtf_&_alerts.mq4

//------------------------------------------------------------------
//
//------------------------------------------------------------------
#property copyright "www.forex-tsd.com"
#property link      "www.forex-tsd.com"

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1  PaleVioletRed
#property indicator_color2  DeepSkyBlue
#property indicator_color3  DimGray
#property indicator_color4  DimGray
#property indicator_width1  2
#property indicator_width2  2
#property indicator_width3  2
#property indicator_width4  2
#property strict

extern string TimeFrame        = "Current time frame";  // Time frame to use
extern int    SmoothPeriod     = 14;                    // smoothing period
extern bool   alertsOn         = false;                 // Turn alert on or off
extern bool   alertsOnCurrent  = true;                  // Alerts on current (still opened) bar
extern bool   alertsMessage    = true;                  // Alerts should show pop-up message
extern bool   alertsPushNotif  = false;                 // Alerts should send push notification
extern bool   alertsSound      = false;                 // Alerts should play a sound
extern bool   alertsEmail      = false;                 // Alerts should send email
extern bool   Interpolate      = true;                  // Interpolate in multi time frame mode

//
//
//
//
//

double bb[];
double bs[];
double vols[];
double volb[];
double trend[];
string indicatorFileName;
bool   returnBars;
int    timeFrame;

//------------------------------------------------------------------
//
//------------------------------------------------------------------
//
//
//
//
//

int init()
{
   IndicatorBuffers(5);
   SetIndexBuffer(0, bs); SetIndexStyle(0, DRAW_HISTOGRAM);
   SetIndexBuffer(1, bb); SetIndexStyle(1, DRAW_HISTOGRAM);
   SetIndexBuffer(2, vols);
   SetIndexBuffer(3, volb);
   SetIndexBuffer(4, trend);
            timeFrame         = stringToTimeFrame(TimeFrame);
            indicatorFileName = WindowExpertName();
            returnBars        = TimeFrame=="returnBars";  if (returnBars) return(0);

     IndicatorShortName(timeFrameToString(timeFrame)+" Buy Sell presure volume ["+(string)SmoothPeriod+"]");
   return(0);
}

//
//
//

int start()
{
   int counted_bars=IndicatorCounted();
      if(counted_bars<0) return(-1);
      if(counted_bars>0) counted_bars--;
         int limit=MathMin(Bars-counted_bars,Bars-1);
         if (returnBars) { bs[0] = MathMin(limit+1,Bars-1); return(0); }
         if (timeFrame!=Period())
         {
               limit = (int)MathMax(limit,MathMin(Bars-1,iCustom(NULL,timeFrame,indicatorFileName,"returnBars",0,0)*timeFrame/Period()));
               for(int i=limit; i>=0; i--)
               {
                  int y = iBarShift(NULL,timeFrame,Time[i]);
                     bs[i]   = iCustom(NULL,timeFrame,indicatorFileName,"",SmoothPeriod,alertsOn,alertsOnCurrent,alertsMessage,alertsPushNotif,alertsSound,alertsEmail,0,y);
                     bb[i]   = iCustom(NULL,timeFrame,indicatorFileName,"",SmoothPeriod,alertsOn,alertsOnCurrent,alertsMessage,alertsPushNotif,alertsSound,alertsEmail,1,y);
                     vols[i] = iCustom(NULL,timeFrame,indicatorFileName,"",SmoothPeriod,alertsOn,alertsOnCurrent,alertsMessage,alertsPushNotif,alertsSound,alertsEmail,2,y);
                     volb[i] = iCustom(NULL,timeFrame,indicatorFileName,"",SmoothPeriod,alertsOn,alertsOnCurrent,alertsMessage,alertsPushNotif,alertsSound,alertsEmail,3,y);
                     if (!Interpolate || (y!=0 && y==iBarShift(NULL,timeFrame,Time[MathMax(i-1,0)]))) continue;

                     //
                     //
                     //
                     //
                     //

                     int n,k;
                     datetime time = iTime(NULL,timeFrame,y);
                        for(n = 1; i+n < Bars && Time[i+n] >= time; n++) continue;	
                        for(k = 1; k < n && (i+n)=0; i--)
      {
         if (i>=Bars-1) { volb[i] = 0;  vols[i] = 0;  continue;}
         double volume = 0; 
            if (Close[i]>iMA(NULL,0,SmoothPeriod,0,MODE_SMA,PRICE_CLOSE,i)) volume = (double) Volume[i];
            if (Close[i]0) 
                  { volb[i] = volb[i+1]+alpha*(volume-volb[i+1]); bb[i] = volb[i]; trend[i] =  1; }
            else  { volb[i] = volb[i+1]; }
            if (volume<0)
                  { vols[i] = vols[i+1]+alpha*(volume-vols[i+1]); bs[i] = vols[i]; trend[i] = -1; }
            else  { vols[i] = vols[i+1]; }
      }
   manageAlerts();
   return(0);
}

//-------------------------------------------------------------------
//                                                                  
//-------------------------------------------------------------------
//
//
//
//
//

void manageAlerts()
{
   if (alertsOn)
   {
      int whichBar = 1; if (alertsOnCurrent) whichBar = 0;
      if (trend[whichBar] != trend[whichBar+1])
      {
         if (trend[whichBar+1]==-1) doAlert(whichBar,"trend changed up");
         if (trend[whichBar+1]== 1) doAlert(whichBar,"trend changed down");
      }
   }
}

//
//
//
//
//

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 = timeFrameToString(Period())+" "+Symbol()+" at "+TimeToStr(TimeLocal(),TIME_SECONDS)+" Buy Sell volume "+doWhat;
          if (alertsMessage)   Alert(message);
          if (alertsEmail)     SendMail(StringConcatenate(Symbol()," Buy Sell volume "),message);
          if (alertsPushNotif) SendNotification(StringConcatenate(Symbol()," Buy Sell volume "+message));
          if (alertsSound)     PlaySound("alert2.wav");
   }
}

//-------------------------------------------------------------------
//
//-------------------------------------------------------------------
//
//
//
//
//

string sTfTable[] = {"M1","M5","M15","M30","H1","H4","D1","W1","MN"};
int    iTfTable[] = {1,5,15,30,60,240,1440,10080,43200};

//
//
//
//
//

int stringToTimeFrame(string tfs)
{
   StringToUpper(tfs);
   for (int i=ArraySize(iTfTable)-1; i>=0; i--)
         if (tfs==sTfTable[i] || tfs==""+(string)iTfTable[i]) return(MathMax(iTfTable[i],Period()));
                                                      return(Period());
}
string timeFrameToString(int tf)
{
   for (int i=ArraySize(iTfTable)-1; i>=0; i--) 
         if (tf==iTfTable[i]) return(sTfTable[i]);
                              return("");
}

Recommend