Ejemplo n.º 1
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBar < (windowLength + 1))
            {
                ProxyLine.Set(med[0]);
                dirUp = true;
                return;
            }

            var slope = calcSlope(lookback);

            if ((med[0] > med[1]) &&
                (slope <= 0))
            {
                ProxyLine.Set(ProxyLine[1]);
            }
            else if ((med[0] < med[1]) &&
                     (slope >= 0))
            {
                ProxyLine.Set(ProxyLine[1]);
            }
            else
            {
                // either everything is flat, or the
                // med and slope agree...
                bool assumeUp = (slope > 0);
                if (slope == 0)
                {
                    if (med[0] != med[1])
                    {
                        assumeUp = (med[0] > med[1]);
                    }
                    else
                    {
                        assumeUp = dirUp;
                    }
                }

                if (assumeUp)
                {
                    ProxyLine.Set(Math.Max(ProxyLine[1], med[0]));
                }
                else
                {
                    ProxyLine.Set(Math.Min(ProxyLine[1], med[0]));
                }
            }

            var newDirUp = dirUp;

            if (ProxyLine[0] > ProxyLine[1])
            {
                newDirUp = true;
            }
            else if (ProxyLine[0] < ProxyLine[1])
            {
                newDirUp = false;
            }

            bool upbar = ham.UpBar;             // ( (Close[0] > Open[0]) || (Close[0] == High[0]));
            bool dnbar = ham.DnBar;             // ( (Close[0] < Open[0]) || (Close[0] == Low[0]) );

            // exceptions that hold the proxy in place for a bit...
            if ((!newDirUp && dirUp && upbar) ||
                (newDirUp && !dirUp && dnbar) ||
                (!newDirUp && dirUp && (ProxyLine[1] <= (High[0] + tolerance * TickSize))) ||
                (newDirUp && !dirUp && (ProxyLine[1] >= (Low[0] - tolerance * TickSize))) ||
                ((ProxyLine[0] > ProxyLine[1]) && (dnbar || (High[0] < ProxyLine[0]))) ||
                ((ProxyLine[0] < ProxyLine[1]) && (upbar || (Low[0] > ProxyLine[0])))
                )
            {
                newDirUp = dirUp;
                ProxyLine.Set(ProxyLine[1]);
            }

            PlotColors[0][0] = (newDirUp?upColor:dnColor);
            dirUp            = newDirUp;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            bars.update();
            var nextFilt = filter.next((bars.High + bars.Low) * 0.5);
            var slope    = linreg.next(nextFilt);

            if ((nextFilt > lastFilt) &&
                (slope <= 0))
            {
                ProxyLine.Set(lastProxy);
            }
            else if ((nextFilt < lastFilt) &&
                     (slope >= 0))
            {
                ProxyLine.Set(lastProxy);
            }
            else
            {
                // either everything is flat, or the
                // med and slope agree...
                bool assumeUp = (slope > 0);
                if (slope == 0)
                {
                    if (nextFilt != lastFilt)
                    {
                        assumeUp = (nextFilt > lastFilt);
                    }
                    else
                    {
                        assumeUp = dirUp;
                    }
                }

                if (assumeUp)
                {
                    ProxyLine.Set(Math.Max(lastProxy, nextFilt));
                }
                else
                {
                    ProxyLine.Set(Math.Min(lastProxy, nextFilt));
                }
            }

            var newDirUp = dirUp;

            if (ProxyLine[0] > lastProxy)
            {
                newDirUp = true;
            }
            else if (ProxyLine[0] < lastProxy)
            {
                newDirUp = false;
            }

            bool upbar = bars.Close > bars.Open;
            bool dnbar = bars.Close < bars.Open;

            // if we're based on bars, use the actual bars themselves, too...
            if (onBasicBars)
            {
                upbar = upbar || (Close[0] > Open[0]) || (High[0] == Close[0]);
                dnbar = dnbar || (Close[0] < Open[0]) || (Low[0] == Close[0]);
            }

            double upRefLoc = 0;
            double dnRefLoc = 0;

            if (onBasicBars)
            {
                upRefLoc = High[0];
                dnRefLoc = Low[0];
            }
            else
            {
                upRefLoc = Input[0];
                dnRefLoc = Input[0];
            }

            // exceptions that hold the proxy in place for a bit...
            if ((!newDirUp && dirUp && upbar) ||
                (newDirUp && !dirUp && dnbar) ||
                (!newDirUp && dirUp && (lastProxy <= (upRefLoc + actualTolerance))) ||
                (newDirUp && !dirUp && (lastProxy >= (dnRefLoc - actualTolerance))) ||
                ((ProxyLine[0] > lastProxy) && (dnbar || (upRefLoc < ProxyLine[0]))) ||
                ((ProxyLine[0] < lastProxy) && (upbar || (dnRefLoc > ProxyLine[0])))
                )
            {
                newDirUp = dirUp;
                ProxyLine.Set(lastProxy);
            }

            PlotColors[0][0] = (newDirUp?upColor:dnColor);
            dirUp            = newDirUp;
            lastFilt         = nextFilt;
            lastProxy        = ProxyLine[0];
        }