Navigation:Home > Content >

peak_n_valley_IND.mq4

Time: 2012-07-06 | Download file:peak_n_valley_IND.mq4

//+------------------------------------------------------------------+
//|                                            peak_n_valley_IND.mq4 |
//+------------------------------------------------------------------+

#property indicator_chart_window

input int PeakValley_Period=24;
string name="pv";
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
void OnDeinit(const int reason){ObjectsDelete(0,-1,name);}
//+------------------------------------------------------------------+

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   double PeakBuffer[],ValleyBuffer[];
   double FastShift=NormalizeDouble((PeakValley_Period/3),0);
   double PVDomain=PeakValley_Period*5;
   int i;
   int TotalBar=Bars-PVDomain;
   ArrayResize(PeakBuffer,TotalBar);
   ArrayResize(ValleyBuffer,TotalBar);
   for(i=0; i=High[iHighest(NULL,0,MODE_HIGH,ShiftFast,pw-ShiftFast)])
   {
      if(iHigh(NULL,0,pw)>=High[iHighest(NULL,0,MODE_HIGH,ShiftFast,pw+1)])
      {
         for(int Apw=pw-ShiftFast;Apw>=pw-(PeriodPV-ShiftFast);Apw--)
         if(iHigh(NULL,0,pw)==High[iHighest(NULL,0,MODE_HIGH,PeriodPV,Apw)])
         {
            Peak=iHigh(NULL,0,pw);
            bsPeak=pw;
            if(ObjectFind(0,name+"Peak Period: "+IntegerToString(PeakValley_Period)+" Time: "+TimeToStr(iTime(NULL,0,pw),TIME_DATE|TIME_MINUTES))==-1)
            {
               string objPeak = name+"Peak Period: "+IntegerToString(PeakValley_Period)+" Time: "+TimeToStr(iTime(NULL,0,pw),TIME_DATE|TIME_MINUTES);
               ObjectCreate(objPeak, OBJ_ARROW, 0, iTime(NULL,0,pw), iHigh(NULL,0,pw));
               ObjectSet(objPeak, OBJPROP_COLOR, clrBlue);
               ObjectSet(objPeak, OBJPROP_ARROWCODE, 117);
               ObjectSet(objPeak, OBJPROP_WIDTH, 1);
               ObjectSet(objPeak, OBJPROP_BACK, true);
            }
            break;
         }
         if(bsPeak>0)break;else continue;   
      }
   }
   
   return(Peak);
}

//====================================================================
// calculate valley
double valley_function(int i,int PeriodPV,int ShiftFast,int DomainPV)
{
   double Valley=0;
   int bsValley=0;
   
   for(int vw=i+ShiftFast;vw<=i+DomainPV;vw++)
   if(iLow(NULL,0,vw)<=Low[iLowest(NULL,0,MODE_LOW,ShiftFast,vw-ShiftFast)])
   {
      if(iLow(NULL,0,vw)<=Low[iLowest(NULL,0,MODE_LOW,ShiftFast,vw+1)])
      {
         for(int Avw=vw-ShiftFast;Avw>=vw-(PeriodPV-ShiftFast);Avw--)
         if(iLow(NULL,0,vw)==Low[iLowest(NULL,0,MODE_LOW,PeriodPV,Avw)])
         {
            Valley=iLow(NULL,0,vw);
            bsValley=vw;
            if(ObjectFind(0,name+"Valley Period: "+IntegerToString(PeakValley_Period)+" Time: "+TimeToStr(iTime(NULL,0,vw),TIME_DATE|TIME_MINUTES))==-1)
            {
               string objValley = name+"Valley Period: "+IntegerToString(PeakValley_Period)+" Time: "+TimeToStr(iTime(NULL,0,vw),TIME_DATE|TIME_MINUTES);
               ObjectCreate(objValley, OBJ_ARROW, 0, iTime(NULL,0,vw), iLow(NULL,0,vw));
               ObjectSet(objValley, OBJPROP_COLOR, clrRed);
               ObjectSet(objValley, OBJPROP_ARROWCODE, 117);
               ObjectSet(objValley, OBJPROP_WIDTH, 1);
               ObjectSet(objValley, OBJPROP_BACK, true);
            }
            break;
         }
         if(bsValley>0)break;else continue;      
      }
   }
   
   return(Valley);
}
void ObjectsDelete(int wnd=-1,int type=-1,string pref=""){
    string names[];int i,n=ObjectsTotal();ArrayResize(names,n);
    for(i=0;i=0)if(ObjectFind(names[i])!=wnd)continue;
    if(type>=0)if(ObjectType(names[i])!=type)continue;
    if(pref!="")if(StringSubstr(names[i],0,StringLen(pref))!=pref)continue;
    ObjectDelete(names[i]);}}

Recommend