Пример #1
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBars[0] < BarsRequired || CurrentBars[1] < BarsRequired)
            {
                return;
            }
            if (BarsInProgress == 1)
            {
                if (CurrentBar < 2 * period)
                {
                    return;
                }

                Bars.Session.GetNextBeginEnd(Times[1][0], out sessionBegin, out sessionEnd);

                //sessionDate = Date(sessionBegin);
                //sessionBegin =

                sessionBegin = Convert.ToDateTime(start);
                sessionEnd   = Convert.ToDateTime(start);

                Print("Session Start: " + sessionBegin + "Session End: " + sessionEnd);
                DateTime barTime          = Times[1][0];
                TimeSpan inSessionTime    = barTime.Subtract(sessionBegin);
                int      inSessionMinutes = 60 * inSessionTime.Hours + inSessionTime.Minutes;
                int      cutoff           = inSessionMinutes % period;
                DateTime cutoffTime       = barTime.Subtract(new TimeSpan(0, cutoff, 0));
                int      barsAgo          = CurrentBar - Bars.GetBar(cutoffTime);
                high  = MAX(Highs[1], period)[barsAgo];
                low   = MIN(Lows[1], period)[barsAgo];
                close = Closes[1][barsAgo];
                pp    = (high + low + close) / 3;
                range = high - low;
                r1    = 2 * pp - low;
                r2    = pp + range;
                r3    = r1 + range;
                r4    = r3 + (pp - low);
                s1    = 2 * pp - high;
                s2    = pp - range;
                s3    = s1 - range;
                s4    = s3 - (high - pp);
            }


            if (BarsInProgress == 0)
            {
                if (CurrentBar == 0)
                {
                    return;
                }
                if (pp != 0.0)
                {
                    PP.Set(pp);
                    R1.Set(r1);
                    R2.Set(r2);
                    R3.Set(r3);
                    R4.Set(r4);
                    S1.Set(s1);
                    S2.Set(s2);
                    S3.Set(s3);
                    S4.Set(s4);
                    if (pp != PP[1] || r1 != R1[1] || s1 != S1[1])
                    {
                        PlotColors[0][0] = Color.Transparent;
                        PlotColors[1][0] = Color.Transparent;
                        PlotColors[2][0] = Color.Transparent;
                        PlotColors[3][0] = Color.Transparent;
                        PlotColors[4][0] = Color.Transparent;
                        PlotColors[5][0] = Color.Transparent;
                        PlotColors[6][0] = Color.Transparent;
                        PlotColors[7][0] = Color.Transparent;
                        PlotColors[8][0] = Color.Transparent;
                    }
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (Bars == null)
            {
                return;
            }
            if (!Bars.BarsType.IsIntraday && Bars.Period.Id != PeriodType.Day)
            {
                return;
            }
            if (Bars.Period.Id == PeriodType.Day && pivotRangeType == PivotRange.Daily)
            {
                return;
            }
            if (Bars.Period.Id == PeriodType.Day && Bars.Period.Value > 1)
            {
                return;
            }

            // pivots only work for
            // - intraday
            // - 1 day chart with PivotRange Weekly or Monthly

            if (!isDailyDataLoaded)
            {
                if (priorDayHLC == HLCCalculationMode.DailyBars && Bars.BarsType.IsIntraday)
                {
                    Enabled = false;
                    System.Threading.ThreadPool.QueueUserWorkItem(new System.Threading.WaitCallback(GetBarsNow));
                    return;
                }

                existsHistDailyData = false;
                isDailyDataLoaded   = true;
            }

            IBar dailyBar;

            if (existsHistDailyData)
            {
                sessionDateDaily = GetLastBarSessionDate(Time[0], Bars, PivotRange.Daily);
                dailyBar         = dailyBars.Get(dailyBars.GetBar(sessionDateDaily));

                if (dailyBar.Time.Date > sessionDateDaily.Date)
                {
                    for (DateTime i = sessionDateDaily; i >= dailyBars.GetTime(0); i = i.AddDays(-1))
                    {
                        dailyBar = dailyBars.Get(dailyBars.GetBar(i));
                        if (dailyBar.Time.Date == i.Date)
                        {
                            break;
                        }
                    }
                }
            }
            else
            {
                dailyBar = null;
            }

            double high  = existsHistDailyData ? dailyBar.High : High[0];
            double low   = existsHistDailyData ? dailyBar.Low : Low[0];
            double close = existsHistDailyData ? dailyBar.Close : Close[0];

            DateTime lastBarTimeStamp = GetLastBarSessionDate(Time[0], Bars, pivotRangeType);

            if ((currentDate != Cbi.Globals.MinDate && pivotRangeType == PivotRange.Daily && lastBarTimeStamp != currentDate) ||
                (currentWeek != Cbi.Globals.MinDate && pivotRangeType == PivotRange.Weekly && lastBarTimeStamp != currentWeek) ||
                (currentMonth != Cbi.Globals.MinDate && pivotRangeType == PivotRange.Monthly && lastBarTimeStamp != currentMonth))
            {
                pp           = (currentHigh + currentLow + currentClose) / 3;
                s1           = 2 * pp - currentHigh;
                r1           = 2 * pp - currentLow;
                s2           = pp - (currentHigh - currentLow);
                r2           = pp + (currentHigh - currentLow);
                s3           = pp - 2 * (currentHigh - currentLow);
                r3           = pp + 2 * (currentHigh - currentLow);
                ppr1         = (pp + r1) / 2;
                pps1         = (pp + s1) / 2;
                r1r2         = (r1 + r2) / 2;
                s1s2         = (s1 + s2) / 2;
                r2r3         = (r2 + r3) / 2;
                s2s3         = (s2 + s3) / 2;
                currentClose = (priorDayHLC == HLCCalculationMode.UserDefinedValues) ? userDefinedClose : close;
                currentHigh  = (priorDayHLC == HLCCalculationMode.UserDefinedValues) ? userDefinedHigh : high;
                currentLow   = (priorDayHLC == HLCCalculationMode.UserDefinedValues) ? userDefinedLow : low;
            }
            else
            {
                currentClose = (priorDayHLC == HLCCalculationMode.UserDefinedValues) ? userDefinedClose : close;
                currentHigh  = (priorDayHLC == HLCCalculationMode.UserDefinedValues) ? userDefinedHigh : Math.Max(currentHigh, high);
                currentLow   = (priorDayHLC == HLCCalculationMode.UserDefinedValues) ? userDefinedLow : Math.Min(currentLow, low);
            }

            if (pivotRangeType == PivotRange.Daily)
            {
                currentDate = lastBarTimeStamp;
            }
            if (pivotRangeType == PivotRange.Weekly)
            {
                currentWeek = lastBarTimeStamp;
            }
            if (pivotRangeType == PivotRange.Monthly)
            {
                currentMonth = lastBarTimeStamp;
            }

            if ((pivotRangeType == PivotRange.Daily && currentDate != Cbi.Globals.MinDate) ||
                (pivotRangeType == PivotRange.Weekly && currentWeek != Cbi.Globals.MinDate) ||
                (pivotRangeType == PivotRange.Monthly && currentMonth != Cbi.Globals.MinDate))
            {
                PP.Set(pp);
                R1.Set(r1);
                S1.Set(s1);
                R2.Set(r2);
                S2.Set(s2);
                R3.Set(r3);
                S3.Set(s3);
                //    PPR1.Set(ppr1);
                //    PPS1.Set(pps1);
                //    R1R2.Set(r1r2);
                //    S1S2.Set(s1s2);
                //    R2R3.Set(r2r3);
                //    S2S3.Set(s2s3);
            }
        }