Pivot Points

by A T in category Trend at 16/03/2012
Description

Pivot Point is a technical indicator used in technical analysis derived by calculating the numerical average of a particular stock's (or any other financial asset like forex pair or any future contract) high, low and closing prices.

On the following day, if the market price trades above the pivot point it is usually considered as a bullish sentiment, whereas if it trades below the pivot point is seen as bearish

A pivot point and the associated support and resistance levels are often turning points for the direction of price movement in a market. In an up-trending market, the pivot point and the resistance levels may represent a ceiling level in price above which the uptrend is no longer sustainable and a reversal may occur. In a declining market, a pivot point and the support levels may represent a low price level of stability or a resistance to further decline.It is customary to calculate additional levels of support and resistance, below and above the pivot point, respectively, by subtracting or adding price differentials calculated from previous trading ranges of the market.
 

Download
24521 downloads
How to install
Notification Publishing copyrighted material is strictly prohibited. If you believe there is copyrighted material in this section you may use the Copyright Infringement Notification form to submit a claim.
Formula / Source Code
Language: C#
Trading Platform: cAlgocTrader
using System;
using cAlgo.API;
using cAlgo.API.Internals;
using cAlgo.API.Indicators;

namespace cAlgo.Indicators
{
    [Indicator(IsOverlay = true, TimeZone = TimeZones.EasternStandardTime, AccessRights = AccessRights.None)]
    public class PivotPoints : Indicator
    {
        private DateTime _previousPeriodStartTime;
        private int _previousPeriodStartIndex;
        private TimeFrame PivotTimeFrame;
        private VerticalAlignment vAlignment = VerticalAlignment.Top;
        private HorizontalAlignment hAlignment = HorizontalAlignment.Right;

        Colors pivotColor = Colors.White;
        Colors supportColor = Colors.Red;
        Colors resistanceColor = Colors.Green;

        [Parameter("Show Labels", DefaultValue = true)]
        public bool ShowLabels { get; set; }

        [Parameter("Pivot Color", DefaultValue = "White")]
        public string PivotColor { get; set; }
        [Parameter("Support Color", DefaultValue = "Red")]
        public string SupportColor { get; set; }
        [Parameter("Resistance Color", DefaultValue = "Green")]
        public string ResistanceColor { get; set; }


        protected override void Initialize()
        {
            if (TimeFrame <= TimeFrame.Hour)
                PivotTimeFrame = TimeFrame.Daily;
            else if (TimeFrame < TimeFrame.Daily)
            {
                PivotTimeFrame = TimeFrame.Weekly;
            }
            else
                PivotTimeFrame = TimeFrame.Monthly;


            Enum.TryParse(PivotColor, out pivotColor);
            Enum.TryParse(SupportColor, out supportColor);
            Enum.TryParse(ResistanceColor, out resistanceColor);

        }

        private DateTime GetStartOfPeriod(DateTime dateTime)
        {
            return CutToOpenByNewYork(dateTime, PivotTimeFrame);
        }

        private DateTime GetEndOfPeriod(DateTime dateTime)
        {
            if (PivotTimeFrame == TimeFrame.Monthly)
            {
                return new DateTime(dateTime.Year, dateTime.Month, 1).AddMonths(1);
            }

            return AddPeriod(CutToOpenByNewYork(dateTime, PivotTimeFrame), PivotTimeFrame);
        }

        public override void Calculate(int index)
        {
            var currentPeriodStartTime = GetStartOfPeriod(MarketSeries.OpenTime[index]);
            if (currentPeriodStartTime == _previousPeriodStartTime)
                return;

            if (index > 0)
                CalculatePivots(_previousPeriodStartTime, _previousPeriodStartIndex, currentPeriodStartTime, index);

            _previousPeriodStartTime = currentPeriodStartTime;
            _previousPeriodStartIndex = index;
        }

        private void CalculatePivots(DateTime startTime, int startIndex, DateTime startTimeOfNextPeriod, int index)
        {
            var high = MarketSeries.High[startIndex];
            var low = MarketSeries.Low[startIndex];
            var close = MarketSeries.Close[startIndex];
            var i = startIndex + 1;

            while (GetStartOfPeriod(MarketSeries.OpenTime[i]) == startTime && i < MarketSeries.Close.Count)
            {
                high = Math.Max(high, MarketSeries.High[i]);
                low = Math.Min(low, MarketSeries.Low[i]);
                close = MarketSeries.Close[i];

                i++;
            }

            var pivotStartTime = startTimeOfNextPeriod;
            var pivotEndTime = GetEndOfPeriod(startTimeOfNextPeriod);

            var pivot = (high + low + close) / 3;

            var r1 = 2 * pivot - low;
            var s1 = 2 * pivot - high;

            var r2 = pivot + high - low;
            var s2 = pivot - high + low;

            var r3 = high + 2 * (pivot - low);
            var s3 = low - 2 * (high - pivot);

            ChartObjects.DrawLine("pivot " + startIndex, pivotStartTime, pivot, pivotEndTime, pivot, Colors.White);
            ChartObjects.DrawLine("r1 " + startIndex, pivotStartTime, r1, pivotEndTime, r1, Colors.Green);
            ChartObjects.DrawLine("r2 " + startIndex, pivotStartTime, r2, pivotEndTime, r2, Colors.Green);
            ChartObjects.DrawLine("r3 " + startIndex, pivotStartTime, r3, pivotEndTime, r3, Colors.Green);
            ChartObjects.DrawLine("s1 " + startIndex, pivotStartTime, s1, pivotEndTime, s1, Colors.Red);
            ChartObjects.DrawLine("s2 " + startIndex, pivotStartTime, s2, pivotEndTime, s2, Colors.Red);
            ChartObjects.DrawLine("s3 " + startIndex, pivotStartTime, s3, pivotEndTime, s3, Colors.Red);

            if (!ShowLabels)
                return;

            ChartObjects.DrawText("Lpivot " + startIndex, "P", index, pivot, vAlignment, hAlignment, pivotColor);
            ChartObjects.DrawText("Lr1 " + startIndex, "R1", index, r1, vAlignment, hAlignment, resistanceColor);
            ChartObjects.DrawText("Lr2 " + startIndex, "R2", index, r2, vAlignment, hAlignment, resistanceColor);
            ChartObjects.DrawText("Lr3 " + startIndex, "R3", index, r3, vAlignment, hAlignment, resistanceColor);
            ChartObjects.DrawText("Ls1 " + startIndex, "S1", index, s1, vAlignment, hAlignment, supportColor);
            ChartObjects.DrawText("Ls2 " + startIndex, "S2", index, s2, vAlignment, hAlignment, supportColor);
            ChartObjects.DrawText("Ls3 " + startIndex, "S3", index, s3, vAlignment, hAlignment, supportColor);


        }


        private static DateTime CutToOpenByNewYork(DateTime date, TimeFrame timeFrame)
        {
            if (timeFrame == TimeFrame.Daily)
            {
                var hourShift = (date.Hour + 24 - 17) % 24;
                return new DateTime(date.Year, date.Month, date.Day, date.Hour, 0, 0, DateTimeKind.Unspecified).AddHours(-hourShift);
            }

            if (timeFrame == TimeFrame.Weekly)
                return GetStartOfTheWeek(date);

            if (timeFrame == TimeFrame.Monthly)
            {
                return new DateTime(date.Year, date.Month, 1, 0, 0, 0, DateTimeKind.Unspecified);
            }

            throw new ArgumentException(string.Format("Unknown timeframe: {0}", timeFrame), "timeFrame");
        }

        private static DateTime GetStartOfTheWeek(DateTime dateTime)
        {
            return dateTime.Date.AddDays((double)DayOfWeek.Sunday - (double)dateTime.Date.DayOfWeek).AddHours(-7);
        }


        public DateTime AddPeriod(DateTime dateTime, TimeFrame timeFrame)
        {
            if (timeFrame == TimeFrame.Daily)
            {
                return dateTime.AddDays(1);
            }
            if (timeFrame == TimeFrame.Weekly)
            {
                return dateTime.AddDays(7);
            }
            if (timeFrame == TimeFrame.Monthly)
                return dateTime.AddMonths(1);

            throw new ArgumentException(string.Format("Unknown timeframe: {0}", timeFrame), "timeFrame");
        }

    }

//    static internal class DateTimeExtencions
//    {
//    }
}
Comments

kikos - April 07, 2012 @ 09:37

thanks for this new forum

dergham - July 04, 2012 @ 10:52

I am not sure which version of pivot point this is but is it possible to have a standard daily pivot point indicator that uses the previous day's high, low and close and can be applied to any intra day timeframe. please see following link to the pivot point indicator I am referring to. http://stockcharts.com/help/doku.php?id=chart_school:technical_indicators:pivot_points

Anonymous - July 18, 2012 @ 06:15

hi, where i can see the candle time or is there any indicator i can use?

ealmachado - July 31, 2012 @ 20:47

This indicator doesn't work properly on intraday charts. It's essencial that we can reference other time frames (new API class) to develop indicators like pivot points.

Hichem - January 02, 2013 @ 09:04

This other indicator can be used on all timeframes to display daily, weekly and monthly pivots: http://ctdn.com/algos/show/211

A T - January 07, 2014 @ 15:28

The indicator has been updated to work on all Timeframes. Also, option to add labels and custom colors has been added.

Hugo - May 16, 2014 @ 05:11

The pivot point lines are not drawn on the 4hour and 12hour charts, however the labels (P, R1, S1 etc) are.

nissay - December 18, 2014 @ 19:15

Hello,
Firstly thanks a lot for this indicator.

Please correct this line :

            var close = MarketSeries.Low[startIndex];

to 

            var close = MarketSeries.Close[startIndex];

nissay - December 18, 2014 @ 19:17

it's line 82

nealkaplan - October 24, 2015 @ 22:30

Hi, Thank You for the indicator. Is there a way to remove previous days pivots and only have the current weeks pivots in view?

roia.ruben - June 05, 2016 @ 18:29

Hello. The pivot lines are not drawn on the 4h timeframe. There's anyway to correct this?
Thank you!

rgpekson - November 26, 2016 @ 06:37

Hello, How do you remove the support lines, or make them transparent?

russell - December 28, 2016 @ 13:33

Hi,

S1, S2 and R1 and R2 are correct for 60 minute based on classical calcualtion

S3 and R3 are wrong.

Please note: 

R3 = R2 + RANGE

S3 = S2 - RANGE

 

 

 

russell - December 28, 2016 @ 14:17

Lines 105 and 106 should be:

             var r3 = pivot + 2 * (high - low);
            var s3 = pivot - 2 * (high - low);

warnkejonas - April 26, 2017 @ 08:44

Hey mate,

thanks for sharing your indicator!

It would be absolutely fantastic if you could sort that 4h weekly pivots problem out.

cheers!

maurox1 - November 06, 2017 @ 22:03

Hello from today, November 6, 2017, this indicator no longer works on the ctrader platform. I ask if you can restore the indicator. It only works on the index section, but currency pairs do not work anymore. Thank you

jcr1818@gmail.com - July 24, 2018 @ 01:49

roia.ruben - June 05, 2016 @ 18:29

Hello. The pivot lines are not drawn on the 4h timeframe. There's anyway to correct this?
Thank you!

 

Yea, that could be nice. I need to use ETX Capital because I think it`s the only place I can use 4H pivot. Could be very nice to use this too in c-trader.

dmn - November 28, 2018 @ 01:31

Doesnt draw pivits on new v3.3 Range Charts, only wiorks on time and tick charts

5