Navigation:Home > Content >

PivSuppRes.mq4

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

//+------------------------------------------------------------------+
//|                                                   PivSuppRes.mq4 |
//|                                Copyright © 2013, HarmonicsTrader |
//|                                   http://www.HarmonicsTrader.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2013, HarmonicsTrader"
#property link      "http://www.HarmonicsTrader.com"

#property indicator_chart_window
#property indicator_buffers 3

#define version            "v1.0"

extern string  textFont          = "Arial";
extern int     textSize          = 8;
extern bool    addSundayToMonday = true;
extern int     lineWidth         = 2;
extern color   colorDaily        = Lime;
extern color   colorWeekly       = RoyalBlue;
extern color   colorMonthly      = OrangeRed;

int     maxBars           = 2000;
bool    showDailyLine     = false;
bool    showWeeklyLine    = false;
bool    showMonthlyLine   = false;
extern bool    showDailySR       = true;
bool    showWeeklySR      = true;
bool    showMonthlySR     = true;
bool    startSRAtOwnBar   = false;
int     shiftTextDaily    = 8;
int     shiftTextWeekly   = 8;
int     shiftTextMonthly  = 8;

//---- buffers
double DBuffer[];
double WBuffer[];
double MBuffer[];

//---- global variables
datetime currentTime;
double PointValue;
double   lastHigh, lastLow, lastClose;
double   DP, DS1, DR1, DS2, DR2, DS3, DR3;
double   WP, WS1, WR1, WS2, WR2, WS3, WR3;
double   MP, MS1, MR1, MS2, MR2, MS3, MR3;
int      pivotBar;
string   pivotNames[21] = { "DP", "DS1", "DR1", "DS2", "DR2", "DS3", "DR3",
                            "WP", "WS1", "WR1", "WS2", "WR2", "WS3", "WR3",
                            "MP", "MS1", "MR1", "MS2", "MR2", "MS3", "MR3"
                          };
bool     sundayCandlesDetected;                          

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
{
   PointValue = Point*10;
   
   sundayCandlesDetected = false;
   for ( int i = 0; i < 8; i++ )
   {
      if ( TimeDayOfWeek( iTime( NULL, PERIOD_D1, i ) ) == 0 )
      {
         sundayCandlesDetected = true;
         break;
      }
   }

   SetIndexBuffer ( 0, DBuffer );
   SetIndexBuffer ( 1, WBuffer );
   SetIndexBuffer ( 2, MBuffer );

   SetIndexLabel ( 0, "DP" );
   SetIndexLabel ( 1, "WP" );
   SetIndexLabel ( 2, "MP" );
   
   if ( Period() >= PERIOD_D1 )
   {
      showDailyLine = false;
      showDailySR = false; 
   }   
   if ( Period() >= PERIOD_W1 )
   {
      showWeeklyLine = false;
      showWeeklySR = false; 
   }   
   if ( Period() >= PERIOD_MN1 )
   {
      showMonthlyLine = false;
      showMonthlySR = false; 
   }   

   // Line text objects
   // Daily
   if ( showDailySR )
   {
      createSR( 0, 7, colorDaily );
      createExtension( 0, 7, colorDaily );
   }
   // Weekly
   if ( showWeeklySR )
   {
      createSR( 7, 14, colorWeekly );
      createExtension( 7, 14, colorWeekly );
   }
   // Monthly
   if ( showMonthlySR )
   {
      createSR( 14, 21, colorMonthly );
      createExtension( 14, 21, colorMonthly );
   }
   return ( 0 );
}

//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   for ( int i = ObjectsTotal() - 1; i >= 0; i-- )
   {
      for ( int j = 0; j < ArraySize( pivotNames ); j++ )
      {
         if ( ObjectFind( pivotNames[j] ) != -1 ) ObjectDelete( pivotNames[j] );
         if ( ObjectFind( pivotNames[j] + "Line" ) != -1 ) ObjectDelete( pivotNames[j] + "Line" );
      }
   }      

   return ( 0 );
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   SetIndexStyle ( 0, DRAW_LINE, STYLE_SOLID, lineWidth, colorDaily );
   SetIndexStyle ( 1, DRAW_LINE, STYLE_SOLID, lineWidth, colorWeekly );
   SetIndexStyle ( 2, DRAW_LINE, STYLE_SOLID, lineWidth, colorMonthly );

   int i, j;
   int counted_bars = IndicatorCounted();

   if ( counted_bars < 0 )
   {
      return ( -1 );
   }

   if ( counted_bars > 0 )
   {
      counted_bars--;
   }
   int limit = Bars - counted_bars;

   if ( limit > maxBars )
   {
      limit = maxBars;
   }
   for ( i = 0; i < limit; i++ )
   {
      currentTime = Time[i];

      // Daily
      pivotBar = iBarShift ( NULL, PERIOD_D1, currentTime ) + 1;

      if ( addSundayToMonday && sundayCandlesDetected )
      {
         if ( TimeDayOfWeek( iTime( NULL, PERIOD_D1, pivotBar ) ) == 0 )
         {
            // Sunday, shift to friday
            pivotBar = pivotBar + 1;
         }
      }   

      // Weekday
      lastLow = iLow ( NULL, PERIOD_D1, pivotBar );
      lastHigh = iHigh ( NULL, PERIOD_D1, pivotBar );
      lastClose = iClose ( NULL, PERIOD_D1, pivotBar );

      if ( addSundayToMonday && sundayCandlesDetected )
      {
         if ( TimeDayOfWeek( iTime( NULL, PERIOD_D1, pivotBar ) ) == 1 )
         {
            // Monday, add Sunday
            lastLow = MathMin( iLow ( NULL, PERIOD_D1, pivotBar ), iLow ( NULL, PERIOD_D1, pivotBar + 1 ) );
            lastHigh = MathMax( iHigh ( NULL, PERIOD_D1, pivotBar ), iHigh ( NULL, PERIOD_D1, pivotBar + 1 ) );
         }
      }

      DP = ( lastHigh + lastLow + lastClose ) / 3;
      DR1 = ( 2 * DP ) - lastLow;
      DS1 = ( 2 * DP ) - lastHigh;
      DR2 = DP + ( lastHigh - lastLow );
      DS2 = DP - ( lastHigh - lastLow );
      DR3 = ( 2 * DP ) + ( lastHigh - ( 2 * lastLow ) );
      DS3 = ( 2 * DP ) - ( ( 2 * lastHigh ) - lastLow );

      if ( showDailyLine ) DBuffer[i] = DP;

      // Weekly
      pivotBar = iBarShift ( NULL, PERIOD_W1, currentTime );
      lastLow = iLow ( NULL, PERIOD_W1, pivotBar + 1 );
      lastHigh = iHigh ( NULL, PERIOD_W1, pivotBar + 1 );
      lastClose = iClose ( NULL, PERIOD_W1, pivotBar + 1 );

      WP = ( lastHigh + lastLow + lastClose ) / 3;
      WR1 = ( 2 * WP ) - lastLow;
      WS1 = ( 2 * WP ) - lastHigh;
      WR2 = WP + ( lastHigh - lastLow );
      WS2 = WP - ( lastHigh - lastLow );
      WR3 = ( 2 * WP ) + ( lastHigh - ( 2 * lastLow ) );
      WS3 = ( 2 * WP ) - ( ( 2 * lastHigh ) - lastLow );

      if ( showWeeklyLine ) WBuffer[i] = WP;

      // Monthly
      pivotBar = iBarShift ( NULL, PERIOD_MN1, currentTime );
      lastLow = iLow ( NULL, PERIOD_MN1, pivotBar + 1 );
      lastHigh = iHigh ( NULL, PERIOD_MN1, pivotBar + 1 );
      lastClose = iClose ( NULL, PERIOD_MN1, pivotBar + 1 );
      
      MP = ( lastHigh + lastLow + lastClose ) / 3;
      MR1 = ( 2 * MP ) - lastLow;
      MS1 = ( 2 * MP ) - lastHigh;
      MR2 = MP + ( lastHigh - lastLow );
      MS2 = MP - ( lastHigh - lastLow );
      MR3 = ( 2 * MP ) + ( lastHigh - ( 2 * lastLow ) );
      MS3 = ( 2 * MP ) - ( ( 2 * lastHigh ) - lastLow );

      if ( showMonthlyLine ) MBuffer[i] = MP;
      
      
   }

   int shiftText;

   // Move text
   // Daily
   if ( showDailySR )
   {
      shiftText = shiftTextDaily * 60 * Period();
      ObjectMove ( "DP", 0, Time[0] + shiftText, DP );
      ObjectSetText ( "DP", "DP (" + DoubleToStr ( ( DP - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "DS1", 0, Time[0] + shiftText, DS1 );
      ObjectSetText ( "DS1", "DS1 (" + DoubleToStr ( ( DS1 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "DR1", 0, Time[0] + shiftText, DR1 );
      ObjectSetText ( "DR1", "DR1 (" + DoubleToStr ( ( DR1 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "DS2", 0, Time[0] + shiftText, DS2 );
      ObjectSetText ( "DS2", "DS2 (" + DoubleToStr ( ( DS2 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "DR2", 0, Time[0] + shiftText, DR2 );
      ObjectSetText ( "DR2", "DR2 (" + DoubleToStr ( ( DR2 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "DS3", 0, Time[0] + shiftText, DS3 );
      ObjectSetText ( "DS3", "DS3 (" + DoubleToStr ( ( DS3 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "DR3", 0, Time[0] + shiftText, DR3 );
      ObjectSetText ( "DR3", "DR3 (" + DoubleToStr ( ( DR3 - Close[0] ) / PointValue, 1 ) + ")" );
   }
   // Weekly
   if ( showWeeklySR )
   {
      shiftText = shiftTextWeekly * 60 * Period();
      ObjectMove ( "WP", 0, Time[0] + shiftText, WP );
      ObjectSetText ( "WP", "WP (" + DoubleToStr ( ( WP - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "WS1", 0, Time[0] + shiftText, WS1 );
      ObjectSetText ( "WS1", "WS1 (" + DoubleToStr ( ( WS1 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "WR1", 0, Time[0] + shiftText, WR1 );
      ObjectSetText ( "WR1", "WR1 (" + DoubleToStr ( ( WR1 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "WS2", 0, Time[0] + shiftText, WS2 );
      ObjectSetText ( "WS2", "WS2 (" + DoubleToStr ( ( WS2 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "WR2", 0, Time[0] + shiftText, WR2 );
      ObjectSetText ( "WR2", "WR2 (" + DoubleToStr ( ( WR2 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "WS3", 0, Time[0] + shiftText, WS3 );
      ObjectSetText ( "WS3", "WS3 (" + DoubleToStr ( ( WS3 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "WR3", 0, Time[0] + shiftText, WR3 );
      ObjectSetText ( "WR3", "WR3 (" + DoubleToStr ( ( WR3 - Close[0] ) / PointValue, 1 ) + ")" );
   }
   // Monthly
   if ( showMonthlySR )
   {
      shiftText = shiftTextMonthly * 60 * Period();
      ObjectMove ( "MP", 0, Time[0] + shiftText, MP );
      ObjectSetText ( "MP", "MP (" + DoubleToStr ( ( MP - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "MS1", 0, Time[0] + shiftText, MS1 );
      ObjectSetText ( "MS1", "MS1 (" + DoubleToStr ( ( MS1 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "MR1", 0, Time[0] + shiftText, MR1 );
      ObjectSetText ( "MR1", "MR1 (" + DoubleToStr ( ( MR1 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "MS2", 0, Time[0] + shiftText, MS2 );
      ObjectSetText ( "MS2", "MS2 (" + DoubleToStr ( ( MS2 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "MR2", 0, Time[0] + shiftText, MR2 );
      ObjectSetText ( "MR2", "MR2 (" + DoubleToStr ( ( MR2 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "MS3", 0, Time[0] + shiftText, MS3 );
      ObjectSetText ( "MS3", "MS3 (" + DoubleToStr ( ( MS3 - Close[0] ) / PointValue, 1 ) + ")" );
      ObjectMove ( "MR3", 0, Time[0] + shiftText, MR3 );
      ObjectSetText ( "MR3", "MR3 (" + DoubleToStr ( ( MR3 - Close[0] ) / PointValue, 1 ) + ")" );
   }

   datetime start;
   // Move extension lines
   // Daily
   if ( showDailySR )
   {
      ObjectMove ( "DPLine", 0, Time[1], DP );
      ObjectMove ( "DPLine", 1, Time[0], DP );
      ObjectMove ( "DR1Line", 0, Time[1], DR1 );
      ObjectMove ( "DR1Line", 1, Time[0], DR1 );
      ObjectMove ( "DR2Line", 0, Time[1], DR2 );
      ObjectMove ( "DR2Line", 1, Time[0], DR2 );
      ObjectMove ( "DR3Line", 0, Time[1], DR3 );
      ObjectMove ( "DR3Line", 1, Time[0], DR3 );
      ObjectMove ( "DS1Line", 0, Time[1], DS1 );
      ObjectMove ( "DS1Line", 1, Time[0], DS1 );
      ObjectMove ( "DS2Line", 0, Time[1], DS2 );
      ObjectMove ( "DS2Line", 1, Time[0], DS2 );
      ObjectMove ( "DS3Line", 0, Time[1], DS3 );
      ObjectMove ( "DS3Line", 1, Time[0], DS3 );
   }
   // Weekly
   if ( showWeeklySR )
   {
      start = Time[1];
      if ( startSRAtOwnBar ) start = iTime( NULL, PERIOD_W1, 0 );
      ObjectMove ( "WPLine", 0, start, WP );
      ObjectMove ( "WPLine", 1, Time[0], WP );
      ObjectMove ( "WR1Line", 0, start, WR1 );
      ObjectMove ( "WR1Line", 1, Time[0], WR1 );
      ObjectMove ( "WR2Line", 0, start, WR2 );
      ObjectMove ( "WR2Line", 1, Time[0], WR2 );
      ObjectMove ( "WR3Line", 0, start, WR3 );
      ObjectMove ( "WR3Line", 1, Time[0], WR3 );
      ObjectMove ( "WS1Line", 0, start, WS1 );
      ObjectMove ( "WS1Line", 1, Time[0], WS1 );
      ObjectMove ( "WS2Line", 0, start, WS2 );
      ObjectMove ( "WS2Line", 1, Time[0], WS2 );
      ObjectMove ( "WS3Line", 0, start, WS3 );
      ObjectMove ( "WS3Line", 1, Time[0], WS3 );
   }
   // Monthly
   if ( showMonthlySR )
   {
      start = Time[1];
      if ( startSRAtOwnBar ) start = iTime( NULL, PERIOD_MN1, 0 );
      ObjectMove ( "MPLine", 0, start, MP );
      ObjectMove ( "MPLine", 1, Time[0], MP );
      ObjectMove ( "MR1Line", 0, start, MR1 );
      ObjectMove ( "MR1Line", 1, Time[0], MR1 );
      ObjectMove ( "MR2Line", 0, start, MR2 );
      ObjectMove ( "MR2Line", 1, Time[0], MR2 );
      ObjectMove ( "MR3Line", 0, start, MR3 );
      ObjectMove ( "MR3Line", 1, Time[0], MR3 );
      ObjectMove ( "MS1Line", 0, start, MS1 );
      ObjectMove ( "MS1Line", 1, Time[0], MS1 );
      ObjectMove ( "MS2Line", 0, start, MS2 );
      ObjectMove ( "MS2Line", 1, Time[0], MS2 );
      ObjectMove ( "MS3Line", 0, start, MS3 );
      ObjectMove ( "MS3Line", 1, Time[0], MS3 );
   }

   return ( 0 );
}

//+------------------------------------------------------------------+
//| Create Objects                                                   |
//+------------------------------------------------------------------+
void createSR( int startIndex, int endIndex,  color colorText )
{
   for ( int i = startIndex; i < endIndex; i++ )
   {
      ObjectCreate ( pivotNames[i], OBJ_TEXT, 0, 0, 0 );
      ObjectSetText ( pivotNames[i], pivotNames[i], textSize, textFont, colorText );
   }   
}      
void createExtension( int startIndex, int endIndex, color colorExtension )
{
   for ( int i = startIndex; i < endIndex; i++ )
   {
      ObjectCreate ( pivotNames[i] + "Line", OBJ_TREND, 0, 0, 0, 0, 0 );
      ObjectSet ( pivotNames[i] + "Line", OBJPROP_COLOR, colorExtension );
      ObjectSet ( pivotNames[i] + "Line", OBJPROP_STYLE, STYLE_DOT );
   }   
}
//+------------------------------------------------------------------+

Recommend