コード例 #1
0
        //Data Event Handler: New data arrives here. "TradeBars" type is a dictionary of strings so you can access it by symbol.
        // "TradeBars" object holds many "TradeBar" objects: it is a dictionary indexed by the symbol:
        //
        //  e.g.  data["MSFT"] data["GOOG"]
        public void OnData(TradeBars data)
        {
            // update Indicators
            decimal            price = data[symbol].Close;
            IndicatorDataPoint datum = new IndicatorDataPoint(data[symbol].Time, price);

            logr14.Update(datum);
            logr30.Update(datum);

            // wait for the indicators to fully initialize
            if (!logr30.IsReady)
            {
                return;
            }

            // stock is moving in an upwards trend
            if (logr30.Current.Value > logr_30.Current.Value * 1.0m)
            {
                if (!Portfolio.HoldStock)
                {
                    SetHoldings(symbol, 1m);
                }
            }
            // stock is moving in a downwards trend
            else if (logr_30.Current.Value > logr30.Current.Value * 1.0m)
            {
                Liquidate();
            }

            Plot(symbol, logr14);
            Plot(symbol, logr30);
        }
コード例 #2
0
        public void LSMAComputesCorrectly()
        {
            int LSMAPeriod = 20;
            LeastSquaresMovingAverage LSMA = new LeastSquaresMovingAverage(LSMAPeriod);
            DateTime time = DateTime.Now;

            #region Array input

            decimal[] expected = new decimal[40]
            {
                125.99m  , 125.91m  , 125.75m  , 125.62m  , 125.54m  , 125.45m  , 125.47m  , 125.4m   , 125.43m  , 125.45m  ,
                125.42m  , 125.36m  , 125.23m  , 125.32m  , 125.26m  , 125.31m  , 125.41m  , 125.5m   , 125.51m  , 125.41m  ,
                125.328m , 125.381m , 125.4423m, 125.4591m, 125.4689m, 125.4713m, 125.4836m, 125.4834m, 125.4803m, 125.4703m,
                125.4494m, 125.4206m, 125.3669m, 125.3521m, 125.3214m, 125.2986m, 125.2909m, 125.2723m, 125.2619m, 125.2224m,
            };

            #endregion Array input

            decimal[] actual = new decimal[prices.Length];

            for (int i = 0; i < prices.Length; i++)
            {
                LSMA.Update(new IndicatorDataPoint(time, prices[i]));
                decimal LSMAValue = Math.Round(LSMA.Current.Value, 4);
                actual[i] = LSMAValue;

                //Console.WriteLine(string.Format("Bar : {0} | {1}, Is ready? {2}", i, LSMA.ToString(), LSMA.IsReady));
                time = time.AddMinutes(1);
            }
            Assert.AreEqual(expected, actual);
        }
コード例 #3
0
        public void LSMAComputesCorrectly()
        {
            int LSMAPeriod = 20;
            LeastSquaresMovingAverage LSMA = new LeastSquaresMovingAverage(LSMAPeriod);
            DateTime time = DateTime.Now;

            #region Array input

            decimal[] expected = new decimal[40]
            {
                125.99m, 125.91m, 125.75m, 125.62m, 125.54m, 125.45m, 125.47m, 125.4m, 125.43m, 125.45m,
                125.42m, 125.36m, 125.23m, 125.32m, 125.26m, 125.31m, 125.41m, 125.5m, 125.51m, 125.41m,
                125.328m, 125.381m, 125.4423m, 125.4591m, 125.4689m, 125.4713m, 125.4836m, 125.4834m, 125.4803m, 125.4703m,
                125.4494m, 125.4206m, 125.3669m, 125.3521m, 125.3214m, 125.2986m, 125.2909m, 125.2723m, 125.2619m, 125.2224m,
            };

            #endregion Array input

            decimal[] actual = new decimal[prices.Length];

            for (int i = 0; i < prices.Length; i++)
            {
                LSMA.Update(new IndicatorDataPoint(time, prices[i]));
                decimal LSMAValue = Math.Round(LSMA.Current.Value, 4);
                actual[i] = LSMAValue;

                //Console.WriteLine(string.Format("Bar : {0} | {1}, Is ready? {2}", i, LSMA.ToString(), LSMA.IsReady));
                time = time.AddMinutes(1);
            }
            Assert.AreEqual(expected, actual);
        }
コード例 #4
0
        public void ResetsProperly()
        {
            int      LSMAPeriod = 10;
            DateTime time       = DateTime.Now;

            LeastSquaresMovingAverage LSMA = new LeastSquaresMovingAverage(LSMAPeriod);

            for (int i = 0; i < LSMAPeriod + 1; i++)
            {
                LSMA.Update(new IndicatorDataPoint(time, 1m));
                time = time.AddMinutes(1);
            }
            Assert.IsTrue(LSMA.IsReady, "LSMA ready");
            LSMA.Reset();
            TestHelper.AssertIndicatorIsInDefaultState(LSMA);
        }
コード例 #5
0
        public void ResetsProperly()
        {
            int LSMAPeriod = 10;
            DateTime time = DateTime.Now;

            LeastSquaresMovingAverage LSMA = new LeastSquaresMovingAverage(LSMAPeriod);

            for (int i = 0; i < LSMAPeriod + 1; i++)
            {
                LSMA.Update(new IndicatorDataPoint(time, 1m));
                time.AddMinutes(1);
            }
            Assert.IsTrue(LSMA.IsReady, "LSMA ready");
            LSMA.Reset();
            TestHelper.AssertIndicatorIsInDefaultState(LSMA);
        }
コード例 #6
0
        public void LSMAComputesCorrectly()
        {
            int LSMAPeriod = 20;
            LeastSquaresMovingAverage LSMA = new LeastSquaresMovingAverage(LSMAPeriod);
            DateTime time = DateTime.Now;

            decimal[] actual = new decimal[prices.Length];

            for (int i = 0; i < prices.Length; i++)
            {
                LSMA.Update(new IndicatorDataPoint(time, prices[i]));
                decimal LSMAValue = Math.Round(LSMA.Current.Value, 4);
                actual[i] = LSMAValue;

                time = time.AddMinutes(1);
            }
            Assert.AreEqual(expected, actual);
        }
コード例 #7
0
        public void LSMAComputesCorrectly()
        {
            int LSMAPeriod = 20;
            LeastSquaresMovingAverage LSMA = new LeastSquaresMovingAverage(LSMAPeriod);
            DateTime time = DateTime.Now;
            
            decimal[] actual = new decimal[prices.Length];

            for (int i = 0; i < prices.Length; i++)
            {
                LSMA.Update(new IndicatorDataPoint(time, prices[i]));
                decimal LSMAValue = Math.Round(LSMA.Current.Value, 4);
                actual[i] = LSMAValue;

                time = time.AddMinutes(1);
            }
            Assert.AreEqual(expected, actual);
        }