Example #1
0
        // Override OnBarUpdate method
        protected override void OnBarUpdate()
        {
            // Protect against too few bars
            if (Bars == null)
            {
                return;
            }
            if (!Data.BarsType.GetInstance(Bars.Period.Id).IsIntraday)
            {
                return;
            }
            if (Bars.Period.Id == PeriodType.Minute && Bars.Period.Value > timeFrame / 2)
            {
                return;
            }
            if (!full && !apt)
            {
                apt        = true;
                periodBars = Data.Bars.GetBars(Bars.Instrument, new Period(PeriodType.Minute, timeFrame, MarketDataType.Last), Bars.From, Bars.To, (Session)Bars.Session.Clone(), Data.Bars.SplitAdjust, Data.Bars.DividendAdjust);
                apt        = false;
                full       = true;
            }

            // Remove calculation for first time span after open
            IBar periodBar;

            Bars.Session.GetNextBeginEnd(Time[0], out sessionBegin, out sessionEnd);
            if (Time[0] >= sessionBegin && Time[0] <= sessionBegin.AddMinutes(timeFrame))
            {
                Pivot.Reset();
                Resistance1.Reset();
                Support1.Reset();
                Resistance2.Reset();
                Support2.Reset();
                Resistance3.Reset();
                Support3.Reset();
                Resistance4.Reset();
                Support4.Reset();
                Resistance5.Reset();
                Support5.Reset();
            }
            else
            {
                // Determine open, high, low, and close
                DateTime intradayBarTime = Time[0].AddMinutes(-timeFrame);
                periodBar = periodBars.Get(periodBars.GetBar(intradayBarTime));
                double Open  = periodBar.Open;
                double High  = periodBar.High;
                double Low   = periodBar.Low;
                double Close = periodBar.Close;
                double pivot = periodBar.Close;

                // Switch central pivot calculation
                switch (centralPivotCalculation)
                {
                // Standard calculation case
                case CentralPivotCalculation.Standard:
                {
                    pivot = (High + Low + Close) / 3;
                    break;
                }

                // Open included calculation case
                case CentralPivotCalculation.OpenIncluded:
                {
                    pivot = (Open + High + Low + Close) / 4;
                    break;
                }

                // Close weighted calculation case
                case CentralPivotCalculation.CloseWeighted:
                {
                    pivot = (High + Low + Close + Close) / 4;
                    break;
                }

                // High weighted calculation case
                case CentralPivotCalculation.HighWeighted:
                {
                    pivot = (High + High + Low + Close) / 4;
                    break;
                }

                // Low weighted calculation case
                case CentralPivotCalculation.LowWeighted:
                {
                    pivot = (High + Low + Low + Close) / 4;
                    break;
                }
                }

                // Switch round to tick option
                switch (roundToTick)
                {
                // Rounding case
                case RoundToTick.Yes:
                {
                    // Set all additional pivots, rounded
                    Pivot.Set(Instrument.MasterInstrument.Round2TickSize(pivot));
                    Resistance1.Set(Instrument.MasterInstrument.Round2TickSize(2 * Pivot[0] - Low));
                    Support1.Set(Instrument.MasterInstrument.Round2TickSize(2 * Pivot[0] - High));
                    Resistance2.Set(Instrument.MasterInstrument.Round2TickSize(Pivot[0] + (High - Low)));
                    Support2.Set(Instrument.MasterInstrument.Round2TickSize(Pivot[0] - (High - Low)));
                    Resistance3.Set(Instrument.MasterInstrument.Round2TickSize(High + 2 * (Pivot[0] - Low)));
                    Support3.Set(Instrument.MasterInstrument.Round2TickSize(Low - 2 * (High - Pivot[0])));
                    Resistance4.Set(Instrument.MasterInstrument.Round2TickSize(Pivot[0] + 2 * (High - Low)));
                    Support4.Set(Instrument.MasterInstrument.Round2TickSize(Pivot[0] - 2 * (High - Low)));
                    Resistance5.Set(Instrument.MasterInstrument.Round2TickSize(2 * (Pivot[0] + High) - 3 * Low));
                    Support5.Set(Instrument.MasterInstrument.Round2TickSize(2 * (Pivot[0] + Low) - 3 * High));
                    break;
                }

                // No rounding case
                case RoundToTick.No:
                {
                    // Set all additional pivots, not rounded
                    Pivot.Set(pivot);
                    Resistance1.Set(2 * Pivot[0] - Low);
                    Support1.Set(2 * Pivot[0] - High);
                    Resistance2.Set(Pivot[0] + (High - Low));
                    Support2.Set(Pivot[0] - (High - Low));
                    Resistance3.Set(High + 2 * (Pivot[0] - Low));
                    Support3.Set(Low - 2 * (High - Pivot[0]));
                    Resistance4.Set(Pivot[0] + 2 * (High - Low));
                    Support4.Set(Pivot[0] - 2 * (High - Low));
                    Resistance5.Set(2 * (Pivot[0] + High) - 3 * Low);
                    Support5.Set(2 * (Pivot[0] + Low) - 3 * High);
                    break;
                }
                }
            }
        }