Ejemplo n.º 1
0
        protected override void OnBarUpdate()
        {
            var highest = High.Highest(0, Length);
            var lowest  = Low.Lowest(0, Length);

            HL[0] = highest - lowest;
            CL[0] = Close[0] - lowest;
            var sumHL = this.TBSum(HL, SlowLength);
            var sumCL = this.TBSum(CL, SlowLength);

            if (sumHL[0] != 0)
            {
                KValue[0] = sumCL[0] / sumHL[0] * 100;
            }
            else
            {
                KValue[0] = 0;
            }
            DValue[0] = KValue.Average(0, SmoothLength);
        }
Ejemplo n.º 2
0
        protected override void OnBarUpdate()
        {
            int CurrentBar = Input.Count - 1;

            if (CurrentBar < 3)
            {
                return;
            }

            if (CurrentBar == 3)
            {
                // Determine initial position
                longPosition = High[0] > High[1];
                xp           = longPosition ? High.Highest(0, CurrentBar) : Low.Lowest(0, CurrentBar);
                af           = Acceleration;
                Value[0]     = xp + (longPosition ? -1 : 1) * (High.Highest(0, CurrentBar) - Low.Lowest(0, CurrentBar)) * af;
                return;
            }

            // Reset accelerator increase limiter on new bars
            if (afIncreased && prevBar != CurrentBar)
            {
                afIncreased = false;
            }

            // Current event is on a bar not marked as a reversal bar yet
            if (reverseBar != CurrentBar)
            {
                // SAR = SAR[1] + af * (xp - SAR[1])
                todaySAR = TodaySAR(Value[1] + af * (xp - Value[1]));
                for (int x = 1; x <= 2; x++)
                {
                    if (longPosition)
                    {
                        if (todaySAR > Low[x])
                        {
                            todaySAR = Low[x];
                        }
                    }
                    else
                    {
                        if (todaySAR < High[x])
                        {
                            todaySAR = High[x];
                        }
                    }
                }

                // Holding long position
                if (longPosition)
                {
                    // Process a new SAR value only on a new bar or if SAR value was penetrated.
                    if (prevBar != CurrentBar || Low[0] < prevSAR)
                    {
                        Value[0] = todaySAR;
                        prevSAR  = todaySAR;
                    }
                    else
                    {
                        Value[0] = prevSAR;
                    }

                    if (High[0] > xp)
                    {
                        xp = High[0];
                        AfIncrease();
                    }
                }

                // Holding short position
                else if (!longPosition)
                {
                    // Process a new SAR value only on a new bar or if SAR value was penetrated.
                    if (prevBar != CurrentBar || High[0] > prevSAR)
                    {
                        Value[0] = todaySAR;
                        prevSAR  = todaySAR;
                    }
                    else
                    {
                        Value[0] = prevSAR;
                    }

                    if (Low[0] < xp)
                    {
                        xp = Low[0];
                        AfIncrease();
                    }
                }
            }

            // Current event is on the same bar as the reversal bar
            else
            {
                // Only set new xp values. No increasing af since this is the first bar.
                if (longPosition && High[0] > xp)
                {
                    xp = High[0];
                }
                else if (!longPosition && Low[0] < xp)
                {
                    xp = Low[0];
                }

                Value[0] = prevSAR;

                // SAR = SAR[1] + af * (xp - SAR[1])
                todaySAR = TodaySAR(longPosition ? Math.Min(reverseValue, Low[0]) : Math.Max(reverseValue, High[0]));
            }

            prevBar = CurrentBar;

            // Reverse position
            if ((longPosition && (Low[0] < todaySAR || Low[1] < todaySAR)) ||
                (!longPosition && (High[0] > todaySAR || High[1] > todaySAR)))
            {
                Value[0] = Reverse();
            }
        }