コード例 #1
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBar > length)
            {
                var todel   = Input[length];
                var toadd   = Input[0];
                var endIndx = length - 1;

                for (int i = 0; i < length; ++i)
                {
                    if (prices[i] == todel)
                    {
                        if ((i == endIndx) ||
                            (prices[i + 1] >= toadd))
                        {
                            prices[i] = toadd;
                            break;
                        }
                        prices[i] = prices[i + 1];
                        todel     = prices[i];
                    }
                    else if (prices[i] > toadd)
                    {
                        var tmp = prices[i];
                        prices[i] = toadd;
                        toadd     = tmp;
                    }
                }
            }
            else if (CurrentBar < length)
            {
                Median.Set(Input[0]);   return;
            }
            else               // (CurrentBar == length) {
            {
                for (int j = 0; j < length; j++)
                {
                    prices[j] = Input[j];
                }
                Array.Sort(prices);
            }

            if ((length & 1) == 1)
            {
                Median.Set(prices[length / 2]);
            }
            else
            {
                Median.Set(0.5 * (prices[length / 2] + prices[length / 2 - 1]));
            }
        }
コード例 #2
0
        /// <summary>
        /// Called on each bar update event (incoming tick)
        /// </summary>
        protected override void OnBarUpdate()
        {
            if (CurrentBar < length)
            {
                return;
            }
            if (CurrentBar == length)
            {
                for (int j = 0; j < length; ++j)
                {
                    prices[j] = Input[j];
                }

                Array.Sort(prices);
                return;
            }

            if (!CalculateOnBarClose)
            {
                if (oldprices == null)
                {
                    oldprices = (double[])prices.Clone();
                }
                if (CurrentBar != lastSeen)
                {
                    lastSeen = CurrentBar;
                    // this is a new bar, so copy prices to oldprices...
                    prices.CopyTo(oldprices, 0);
                }
                else
                {
                    // this is the same bar as the last tick... copy over prices with oldprices...
                    oldprices.CopyTo(prices, 0);
                }
            }

            double todel = Input[length];
            double toadd = Input[0];
            double sum   = 0;

            for (int i = 0; i < length; ++i)
            {
                if (prices[i] == todel)
                {
                    if ((i == (length - 1)) ||
                        (prices[i + 1] >= toadd))
                    {
                        prices[i] = toadd;
                        break;
                    }
                    prices[i] = prices[i + 1];
                    todel     = prices[i];
                }
                else if (prices[i] > toadd)
                {
                    double tmp = prices[i];
                    prices[i] = toadd;
                    toadd     = tmp;
                }
            }

            if ((length & 1) == 1)
            {
                Median.Set(prices[length / 2]);
            }
            else
            {
                Median.Set(0.5 * (prices[length / 2] + prices[length / 2 - 1]));
            }
        }