Navigation:Home > Content >

Pivots_Daily_Median_v1.mq4

Time: 2013-12-21 | Download file:Pivots_Daily_Median_v1.mq4

//+------------------------------------------------------------------+
//|   Original Indicator Auto-Pivot Plotter Labels.mq4 Elton Treloar |
//|    Thanks to mladen for help me to fix the labels redraw problem |
//|                                               Linuxser, May 2008 |
//|                                                                  |
//+------------------------------------------------------------------+
//|                                              Written by Linuxser |   
//|                                       http://www.linuxser.com.ar |                               
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, Forex-TSD.com "
#property link      "http://www.forex-tsd.com/"

#property indicator_chart_window
#property indicator_buffers 6
#property indicator_color1 C'37,116,47'
#property indicator_color2 C'37,116,47'
#property indicator_color3 C'37,116,47'
#property indicator_color4 C'218,73,73'
#property indicator_color5 C'218,73,73'
#property indicator_color6 C'218,73,73'
#property indicator_style1 STYLE_DOT
#property indicator_style2 STYLE_DOT
#property indicator_style3 STYLE_DOT
#property indicator_style4 STYLE_DOT
#property indicator_style5 STYLE_DOT
#property indicator_style6 STYLE_DOT

#define IND_NAME "Pivot Point Medians"

//---- input parameters
extern int       StartHour=0;
extern int       StartMinute=0;
extern int       HistoryDays=15;
extern color     LabelColor=C'0,0,0';
extern int       fontsize=8;
extern int       LabelShiftToRight = 10;
double           DaysToPlot;
int nDigits;

//---- buffers
double M0Buffer[];
double M1Buffer[];
double M2Buffer[];
double M3Buffer[];
double M4Buffer[];
double M5Buffer[];

string Pivot="M0",Sup1="M1", Res1="M2", Sup2="M3", Res2="M4", Sup3="M5";

datetime LabelShiftTime;

double LastRedraw = 0;

double M0,M1,M2,M3,M4,M5;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_LINE,EMPTY);
   SetIndexStyle(1,DRAW_LINE,EMPTY);
   SetIndexStyle(2,DRAW_LINE,EMPTY);
   SetIndexStyle(3,DRAW_LINE,EMPTY);
   SetIndexStyle(4,DRAW_LINE,EMPTY);
   SetIndexStyle(5,DRAW_LINE,EMPTY);   
   SetIndexBuffer(0,M0Buffer);
   SetIndexBuffer(1,M1Buffer);
   SetIndexBuffer(2,M2Buffer);
   SetIndexBuffer(3,M3Buffer);
   SetIndexBuffer(4,M4Buffer);
   SetIndexBuffer(5,M5Buffer);
   SetIndexLabel(0,NULL);
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
   SetIndexLabel(3,NULL);
   SetIndexLabel(4,NULL);
   SetIndexLabel(5,NULL);

   
   IndicatorShortName(IND_NAME);   
   
   if(Symbol()=="GBPJPY" || Symbol()=="EURJPY" || Symbol()=="USDJPY" || Symbol()=="GOLD" || Symbol()=="USDMXN") nDigits = 2;
   if(Symbol()=="GBPUSD" || Symbol()=="EURUSD" || Symbol()=="NZDUSD" || Symbol()=="USDCHF"  ||
   Symbol()=="USDCAD" || Symbol()=="AUDUSD" || Symbol()=="EURUSD" || Symbol()=="EURCHF"  || Symbol()=="EURGBP"
   || Symbol()=="EURCAD" || Symbol()=="EURAUD" || Symbol()=="AUDNZD")nDigits = 4;
//----

   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----

   ObjectDelete("M0");
   ObjectDelete("M1");
   ObjectDelete("M2");
   ObjectDelete("M3");
   ObjectDelete("M4");
   ObjectDelete("M5");
   ObjectDelete("M6");   
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
   
//---- exit if period is greater than daily charts
    if(Period() > 240)
   {
   Print("Error - Chart period is greater than 1 day.");
   return(-1); // then exit
   }
    
  
//---- indicator calculation

{
   if (counted_bars==0)
   if(counted_bars<0) return(-1);
   //---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;  


}

   int limit=Bars-counted_bars;
   int StartMinutesIntoDay=(StartHour*60)+StartMinute; // 8' o'clock x 60 = 480
   int CloseMinutesIntoDay=StartMinutesIntoDay-Period(); //
   
   if (DaysToPlot>Bars) DaysToPlot=Bars;
   DaysToPlot = HistoryDays*1440/Period();
   SetIndexDrawBegin(0,Bars-DaysToPlot);
   SetIndexDrawBegin(1,Bars-DaysToPlot);
   SetIndexDrawBegin(2,Bars-DaysToPlot);
   SetIndexDrawBegin(3,Bars-DaysToPlot);
   SetIndexDrawBegin(4,Bars-DaysToPlot);
   SetIndexDrawBegin(5,Bars-DaysToPlot);

   
   // ****************************************************
   //    Check That cloes time isn't now a negative number.
   //    Correct if it is by adding a full day's worth 
   //    of minutes.
   // ****************************************************
   
   if (CloseMinutesIntoDay<0)
      {
         CloseMinutesIntoDay=CloseMinutesIntoDay+1440;
      }
      
   // ****************************************************
   //    Establish the nuber of bars in a day.
   // ****************************************************
   int BarsInDay=1440/Period();
    
   // ******************************************************************************************
   // ******************************************************************************************
   //                                        Main Loop                                      
   // ******************************************************************************************
   // ******************************************************************************************
   
   for(int i=0; iPreviousHigh) PreviousHigh=High[SearchHighLow];
         if (Low[SearchHighLow] 600)   
      {
      Print(i);
      ObjectCreate("M0", OBJ_TEXT, 0, LabelShiftTime, 0);
      ObjectSetText("M0", "M0 " +DoubleToStr(M0,nDigits),fontsize,"Trebuchet MS",LabelColor);
      ObjectSet("M0",OBJPROP_BACK,1);
      ObjectMove("M0", 0, LabelShiftTime,M0);
      ObjectCreate("M1", OBJ_TEXT, 0, LabelShiftTime, 0);
      ObjectSetText("M1", "M1 " +DoubleToStr(M1,nDigits),fontsize,"Trebuchet MS",LabelColor);
      ObjectSet("M1",OBJPROP_BACK,1);
      ObjectMove("M1", 0, LabelShiftTime,M1);
      ObjectCreate("M2", OBJ_TEXT, 0, LabelShiftTime, 0);
      ObjectSetText("M2", "M2 " +DoubleToStr(M2,nDigits),fontsize,"Trebuchet MS",LabelColor);
      ObjectSet("M2",OBJPROP_BACK,1);
      ObjectMove("M2", 0, LabelShiftTime,M2);
      ObjectCreate("M3", OBJ_TEXT, 0, LabelShiftTime, 0);
      ObjectSetText("M3", "M3 " +DoubleToStr(M3,nDigits),fontsize,"Trebuchet MS",LabelColor);
      ObjectSet("M3",OBJPROP_BACK,1);
      ObjectMove("M3", 0, LabelShiftTime,M3);
      ObjectCreate("M4", OBJ_TEXT, 0, LabelShiftTime, 0);
      ObjectSetText("M4", " M4 " +DoubleToStr(M4,nDigits),fontsize,"Trebuchet MS",LabelColor);
      ObjectSet("M4",OBJPROP_BACK,1);
      ObjectMove("M4", 0, LabelShiftTime,M4);
      ObjectCreate("M5", OBJ_TEXT, 0, LabelShiftTime, 0);
      ObjectSetText("M5", "M5 " +DoubleToStr(M5,nDigits),fontsize,"Trebuchet MS",LabelColor);
      ObjectSet("M5",OBJPROP_BACK,1);
      ObjectMove("M5", 0, LabelShiftTime,M5);
      Print("ObjectSet Done");  
      Print("Redrawing Pivot Labels after 10 minutes have elapsed since the last Redraw!"); 
      Print("ObjectMove Done");
      
      ObjectsRedraw(); 
      Print("ObjectsRedraw Done");
      LastRedraw = TimeLocal();
      }
   }
      
}  
    M0Buffer[i]=M0;
    M1Buffer[i]=M1;
    M2Buffer[i]=M2;
    M3Buffer[i]=M3;
    M4Buffer[i]=M4;
    M5Buffer[i]=M5;
    
    
   }
      // Extra1[i]=OpenPriceAt+i;

      //   **********************************************
      //      Calculate the mid-levels in to the buffers. 
      //      (for mid-levels version)
      //   **********************************************(Twe)
      // Res1[i] =((R1-P)/2)+P;    //M3
      // Res2[i] =((R2-R1)/2)+R1;  //M4
      // Res3[i] =((R3-R2)/2)+R2;  
      // Supp1[i]=((P-S1)/2)+S1;   //M2
      // Supp2[i]=((S1-S2)/2)+S2;  //M1
      // Supp3[i]=((S2-S3)/2)+S3;


       //End of 'DaysToPlot 'if' statement.
     
      // ***************************************************************************************
      //                            End of Main Loop
      // ***************************************************************************************
  

   // *****************************************
   //    Return from Start() (Main Routine)
   return(0);
  
  }
//+-------------------------------------------------------------------------------------------------------+
//  END Custom indicator iteration function
//+-------------------------------------------------------------------------------------------------------+


// *****************************************************************************************
// *****************************************************************************************
// -----------------------------------------------------------------------------------------
//    The following routine will use "StartingBar"'s time and use it to find the 
//    general area that SHOULD contain the bar that matches "TimeToLookFor"
// -----------------------------------------------------------------------------------------
int FindLastTimeMatchFast(int TimeToLookFor,int StartingBar)
   {
   int StartingBarsTime=(TimeHour(Time[StartingBar])*60)+TimeMinute(Time[StartingBar]);
   
   // ***************************************************
   //    Check that our search isn't on the otherside of 
   //    the midnight boundary and correct for calculations
   // ***************************************************
   if (StartingBarsTime0)
      {
        for (int SearchCount=SuggestedBar ;SearchCount>(SuggestedBar-DeltaBarFound-2) ;SearchCount--)
            {
            // ******************************************************************************
            //    Find time (in minutes) of the current bar AND the two bars either side of it.
            // This is done to allow for any missing bars in the data.   i.e. If THE bar you 
            // were after were to be missing you'd never get a valid close/open time.  
            //    This is the same reason for searching rather than just looking back a certain
            // number of bars from the close time. Missing bars upset the count back and 
            // screw up all pivot calculations. There is still room for error but it's improved.
            // The best calculations will come from having the best data. ...of course. :)
            // ******************************************************************************
            //
            int PreviousBarsTime=(TimeHour(Time[SearchCount+1])*60)+TimeMinute(Time[SearchCount+1]);
            int CurrentBarsTime=(TimeHour(Time[SearchCount])*60)+TimeMinute(Time[SearchCount]);
            int NextBarsTime=(TimeHour(Time[SearchCount-1])*60)+TimeMinute(Time[SearchCount-1]);
      
            if (CurrentBarsTime==TimeToLookFor)
               {
               return(SearchCount);    // *** If current bar is what we are after than lets get out.
                                 // *** without mucking about with the rest of the checks.
               }
      
            // **********************************
            //    Check that previous bar doesn't
            // lay on a different day.
            //    Adjust if it is
            // **********************************
            if(PreviousBarsTime>CurrentBarsTime)  
               {                                       
               PreviousBarsTime=PreviousBarsTime-1440;
               }
            // **********************************
            //    Check that following bar doesn't
            // lay on a different day. 
            //    Adjust if it is.
            // **********************************
            if(NextBarsTime        

Recommend