Category  Published on 22/12/2014

Pivot Points

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.
 


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
//    {
//    }
}


qualitiedx2's avatar
qualitiedx2

Joined on 30.09.2011

  • Distribution: Free
  • Language: C#
  • Trading platform: cTrader Automate
  • File name: Pivot Points.algo
  • Rating: 5
  • Installs: 50879
Comments
Log in to add a comment.
MA
majochorvatovic · 2 months ago

Hello, it would be possible to program a telegram notification when the level is touched

BO
boubkeri.90 · 1 year ago

please any one can help me how to install the indecator

thanks for advance

SC
scrollmedia1230 · 3 years ago

Fantastic job! I can see not many people have downloaded the corrected version so far. This indicator is a great tool to use, thank you for making it available.  

2bnnp's avatar
2bnnp · 4 years ago

https://ctrader.com/algos/indicators/show/2245 fixed the weekly pivots

MyTradingAnxiety's avatar
MyTradingAnxiety · 4 years ago

Great indicator! It works fine for me!

jani's avatar
jani · 4 years ago

I have been told by a programmer  that there is an issue with cTrader Pivots and 
unfortunately, it seems like it can't be fixed,
Market Data Series containing candle stick info is what makes one able to draw a line on the chart representing the Pivot. Because there is no way to get the candle in tick charts as a
Market Data Series, there is no way to get the correct info on Calgo API and draw correct Pivots on tick charts or any time-based charts below 1M!!


Please let me know if you think you have a solution to this problem!!

BI
bitcoinspeak · 4 years ago

please, how to take the value of this indicator into CBOT?
thank you

DA
damian8 · 5 years ago

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

JC
jcr1818 · 5 years ago

[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.

maurox1's avatar
maurox1 · 6 years ago

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

JF
JFOW · 7 years ago

Hey mate,

thanks for sharing your indicator!

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

cheers!

RU
russell · 7 years ago

Lines 105 and 106 should be:

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

RU
russell · 7 years ago

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

 

 

 

RG
rgpekson · 7 years ago

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

RO
roia.ruben · 8 years ago

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

NE
nealkaplan · 8 years ago

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

30
3088857 · 9 years ago

it's line 82

30
3088857 · 9 years ago

Hello,
Firstly thanks a lot for this indicator.

Please correct this line :

            var close = MarketSeries.Low[startIndex];

to 

            var close = MarketSeries.Close[startIndex];

HU
Hugo · 10 years ago

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

qualitiedx2's avatar
qualitiedx2 · 10 years ago

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

HI
hichem · 11 years ago
This other indicator can be used on all timeframes to display daily, weekly and monthly pivots: /algos/show/211
AN
Anonymous · 11 years ago
hi, where i can see the candle time or is there any indicator i can use?