示例#1
0
        public void ComputesCorrectly()
        {
            var max = new Maximum(3);

            var reference = DateTime.MinValue;

            max.Update(reference.AddDays(1), 1m);
            Assert.AreEqual(1m, max.Current.Value);
            Assert.AreEqual(0, max.PeriodsSinceMaximum);

            max.Update(reference.AddDays(2), -1m);
            Assert.AreEqual(1m, max.Current.Value);
            Assert.AreEqual(1, max.PeriodsSinceMaximum);

            max.Update(reference.AddDays(3), 0m);
            Assert.AreEqual(1m, max.Current.Value);
            Assert.AreEqual(2, max.PeriodsSinceMaximum);

            max.Update(reference.AddDays(4), -2m);
            Assert.AreEqual(0m, max.Current.Value);
            Assert.AreEqual(1, max.PeriodsSinceMaximum);

            max.Update(reference.AddDays(5), -2m);
            Assert.AreEqual(0m, max.Current.Value);
            Assert.AreEqual(2, max.PeriodsSinceMaximum);
        }
示例#2
0
        public void ComputesCorrectlyMaximum()
        {
            const int period = 5;
            var       max    = new Maximum(period);

            Assert.AreEqual(0m, max.Current.Value);

            // test an increasing stream of data
            for (int i = 0; i < period; i++)
            {
                max.Update(DateTime.Now.AddDays(i), i);
                Assert.AreEqual(i, max.Current.Value);
                Assert.AreEqual(0, max.PeriodsSinceMaximum);
            }

            // test a decreasing stream of data
            for (int i = 0; i < period; i++)
            {
                max.Update(DateTime.Now.AddDays(period + i), period - i - 1);
                Assert.AreEqual(period - 1, max.Current.Value);
                Assert.AreEqual(i, max.PeriodsSinceMaximum);
            }

            Assert.AreEqual(max.Period, max.PeriodsSinceMaximum + 1);
        }
示例#3
0
        public void ComputesCorrectly()
        {
            var max = new Maximum(3);

            var reference = DateTime.MinValue;

            max.Update(reference.AddDays(1), 1m);
            Assert.AreEqual(1m, max.Current.Value);
            Assert.AreEqual(0, max.PeriodsSinceMaximum);

            max.Update(reference.AddDays(2), -1m);
            Assert.AreEqual(1m, max.Current.Value);
            Assert.AreEqual(1, max.PeriodsSinceMaximum);

            max.Update(reference.AddDays(3), 0m);
            Assert.AreEqual(1m, max.Current.Value);
            Assert.AreEqual(2, max.PeriodsSinceMaximum);

            max.Update(reference.AddDays(4), -2m);
            Assert.AreEqual(1m, max.Current.Value);
            Assert.AreEqual(3, max.PeriodsSinceMaximum);

            max.Update(reference.AddDays(5), -2m);
            Assert.AreEqual(0m, max.Current.Value);
            Assert.AreEqual(2, max.PeriodsSinceMaximum);
        }
示例#4
0
        public void ComputesCorrectly2()
        {
            const int period = 5;
            var max = new Maximum(period);

            Assert.AreEqual(0m, max.Current.Value);

            // test an increasing stream of data
            for (int i = 0; i < period; i++)
            {
                max.Update(DateTime.Now.AddDays(1), i);
                Assert.AreEqual(i, max.Current.Value);
                Assert.AreEqual(0, max.PeriodsSinceMaximum);
            }

            // test a decreasing stream of data
            for (int i = 0; i <= period; i++)
            {
                max.Update(DateTime.Now.AddDays(period + i), period - i - 1);
                Assert.AreEqual(period - 1, max.Current.Value);
                Assert.AreEqual(i, max.PeriodsSinceMaximum);
            }

            Assert.AreEqual(max.Period, max.PeriodsSinceMaximum);
        }
示例#5
0
        public void ResetsProperly()
        {
            var max = new Maximum(3);
            max.Update(DateTime.Today, 1m);
            max.Update(DateTime.Today.AddSeconds(1), 2m);
            max.Update(DateTime.Today.AddSeconds(2), 1m);
            Assert.IsTrue(max.IsReady);

            max.Reset();
            Assert.AreEqual(0, max.PeriodsSinceMaximum);
            TestHelper.AssertIndicatorIsInDefaultState(max);
        }
示例#6
0
        public void ResetsProperlyMaximum()
        {
            var max = new Maximum(3);

            max.Update(DateTime.Today, 1m);
            max.Update(DateTime.Today.AddSeconds(1), 2m);
            max.Update(DateTime.Today.AddSeconds(2), 1m);
            Assert.IsTrue(max.IsReady);

            max.Reset();
            Assert.AreEqual(0, max.PeriodsSinceMaximum);
            TestHelper.AssertIndicatorIsInDefaultState(max);
        }
示例#7
0
        public void TimeAdvancesWithDataToOnlyOneSide()
        {
            var left      = new Maximum("left", 2);
            var right     = new Minimum("right", 2);
            var composite = new CompositeIndicator <IndicatorDataPoint>(left, right, (l, r) => l.Current.Value + r.Current.Value);

            Assert.AreEqual(DateTime.MinValue, composite.Current.Time);

            left.Update(Today, 1m);

            Assert.AreEqual(Today, composite.Current.Time);

            left.Update(Today.AddDays(1), -1m);

            Assert.AreEqual(Today.AddDays(1), composite.Current.Time);
        }
示例#8
0
文件: MidPoint.cs 项目: w1r2p1/Core-2
        /// <summary>
        /// Computes the next value of this indicator from the given state
        /// </summary>
        /// <param name="input">The input given to the indicator</param>
        /// <returns>A new value for this indicator</returns>
        protected override decimal ComputeNextValue(IndicatorDataPoint input)
        {
            _maximum.Update(input);
            _minimum.Update(input);

            return((_maximum + _minimum) / 2);
        }
示例#9
0
        /// <summary>
        ///      Computes the next value of this indicator from the given state
        /// </summary>
        /// <param name="time"></param>
        /// <param name="input">The input given to the indicator</param>
        /// <returns>A new value for this indicator</returns>
        protected override DoubleArray Forward(long time, DoubleArray input)
        {
            _maximum.Update(time, input);
            _minimum.Update(time, input);

            return((_maximum + _minimum) / 2);
        }
示例#10
0
        public void ResetsProperly() {
            var left = new Maximum("left", 2);
            var right = new Minimum("right", 2);
            var composite = new CompositeIndicator<IndicatorDataPoint>(left, right, (l, r) => l + r);

            left.Update(DateTime.Today, 1m);
            right.Update(DateTime.Today,-1m);

            left.Update(DateTime.Today.AddDays(1), -1m);
            right.Update(DateTime.Today.AddDays(1), 1m);

            Assert.AreEqual(left.PeriodsSinceMaximum, 1);
            Assert.AreEqual(right.PeriodsSinceMinimum, 1);

            composite.Reset();
            TestHelper.AssertIndicatorIsInDefaultState(composite);
            TestHelper.AssertIndicatorIsInDefaultState(left);
            TestHelper.AssertIndicatorIsInDefaultState(right);
            Assert.AreEqual(left.PeriodsSinceMaximum, 0);
            Assert.AreEqual(right.PeriodsSinceMinimum, 0);
        }
        public void ResetsProperly()
        {
            var left      = new Maximum("left", 2);
            var right     = new Minimum("right", 2);
            var composite = new CompositeIndicator(left, right, (l, r) => l.Current + r.Current);

            left.Update(DateTime.Today, 1d);
            right.Update(DateTime.Today, -1d);

            left.Update(DateTime.Today.AddDays(1), -1d);
            right.Update(DateTime.Today.AddDays(1), 1d);

            Assert.AreEqual(left.PeriodsSinceMaximum, 1);
            Assert.AreEqual(right.PeriodsSinceMinimum, 1);

            composite.Reset();
            TestHelper.AssertIndicatorIsInDefaultState(composite);
            TestHelper.AssertIndicatorIsInDefaultState(left);
            TestHelper.AssertIndicatorIsInDefaultState(right);
            Assert.AreEqual(left.PeriodsSinceMaximum, 0);
            Assert.AreEqual(right.PeriodsSinceMinimum, 0);
        }
示例#12
0
        public void ResetsProperly()
        {
            var left      = new Maximum("left", 2);
            var right     = new Minimum("right", 2);
            var composite = new CompositeIndicator <IndicatorDataPoint>(left, right, (l, r) => l + r);

            left.Update(DateTime.Today, TimeZone.Utc, 1m);
            right.Update(DateTime.Today, TimeZone.Utc, -1m);

            left.Update(DateTime.Today.AddDays(1), TimeZone.Utc, -1m);
            right.Update(DateTime.Today.AddDays(1), TimeZone.Utc, 1m);

            Assert.Equal(1, left.PeriodsSinceMaximum);
            Assert.Equal(1, right.PeriodsSinceMinimum);

            composite.Reset();
            TestHelper.AssertIndicatorIsInDefaultState(composite);
            TestHelper.AssertIndicatorIsInDefaultState(left);
            TestHelper.AssertIndicatorIsInDefaultState(right);
            Assert.Equal(0, left.PeriodsSinceMaximum);
            Assert.Equal(0, right.PeriodsSinceMinimum);
        }
示例#13
0
        private void CalculateDailyProfits()
        {
            foreach (SecurityHolding holding in Portfolio.Values)
            {
                #region logging
                dayprofit  = holding.Profit - lastprofit;
                dayfees    = holding.TotalFees - lastfees;
                daynet     = dayprofit - dayfees;
                lastprofit = holding.Profit;
                lastfees   = holding.TotalFees;
                string msg = String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10}",
                                           tradingDate.ToShortDateString(),
                                           dayprofit,
                                           dayfees,
                                           daynet,
                                           holding.Profit,
                                           holding.TotalFees,
                                           holding.Profit - holding.TotalFees,
                                           _tradecount - lasttradecount,
                                           Portfolio[symbol].HoldingsValue,
                                           sharesOwned,
                                           ""
                                           );
                dailylog.Debug(msg);

                MaxDailyProfit.Update(idp(tradingDate, daynet));
                MinDailyProfit.Update(idp(tradingDate, daynet));

                lasttradecount = _tradecount;
                dayprofit      = 0;
                dayfees        = 0;
                daynet         = 0;


                #endregion
            }
        }
示例#14
0
        /// <summary>
        /// OnData event is the primary entry point for your algorithm. Each new data point will be pumped in here.
        /// </summary>
        /// <param name="data">TradeBars IDictionary object with your stock data</param>
        public void OnData(TradeBars data)
        {
            barcount++;
            //decimal dp;
            try
            {
                maxHigh.Update(new IndicatorDataPoint(this.Time, data[_symbol].Close));
                minLow.Update(new IndicatorDataPoint(this.Time, data[_symbol].Close));
                fx.Update(new IndicatorDataPoint(this.Time, data[_symbol].Close));
                if (fx.IsReady)
                {
                    var Price = data[_symbol].Close;
                    var MinL  = minLow.Current.Value;
                    var MaxH  = maxHigh.Current.Value;


                    var v0 = value1[0].Value;
                    value1.Add(new IndicatorDataPoint(this.Time, .33m * 2m * ((Price - MinL) / (MaxH - MinL) - .5m) + .67m * v0));

                    if (value1[0].Value > .9999m)
                    {
                        value1[0].Value = .9999m;
                    }
                    if (value1[0].Value < -.9999m)
                    {
                        value1[0].Value = -.9999m;
                    }
                    var fish0 = fish[0];
                    var fish1 =
                        System.Convert.ToDecimal(.5 * 2.0 *
                                                 Math.Log((1.0 + (double)value1[0].Value) /
                                                          (1.0 - (double)value1[0].Value))) + .5m * fish0.Value;
                    fish.Add(new IndicatorDataPoint(this.Time, fish1));
                    //wma.Update(fish[0]);
                    //wwma.Add(new IndicatorDataPoint(this.Time, wma.Current));
                    //fishHigh.Update(fish[0]);
                    //fishLow.Update(fish[0]);

                    var fishdirval    = fish[1].Value - fish[0].Value;
                    var fishdirection = fishdirval > 0m ? "Up" : "Down";
                    var signal        = CalculateSignal();

                    string logmsg =
                        string.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9},{10},{11},{12},{13},{14},{15},{16},{17}",
                                      this.Time,
                                      barcount,
                                      data[_symbol].Open,
                                      data[_symbol].High,
                                      data[_symbol].Low,
                                      data[_symbol].Close,
                                      MinL,
                                      MaxH,
                                      fish[0].Value,
                                      fish[1].Value,
                                      signal,
                                      fx.Current.Value,
                                      Portfolio[_symbol].UnrealizedProfit,
                                      fishdirection,
                                      Portfolio[_symbol].LastTradeProfit,
                                      Portfolio[_symbol].TotalFees,
                                      Portfolio[_symbol].TotalCloseProfit(),
                                      ""
                                      );
                    mylog.Debug(logmsg);

                    /*
                     * Here is a way to generate trading signals using the fisher transform indicator:
                     * A bullish signal is generated when the fisher line turns up below -1 threshold and crosses above the signal line.
                     * A bearish signal is generated when the fisher line turns down above the 1 threshold and crosses below the signal line.
                     *
                     * Read more: http://www.quantshare.com/item-528-fisher-transform-technical-indicator#ixzz3ZV8fR0Dw
                     * Follow us: @quantshare on Twitter
                     */

                    if (signal != 0)
                    {
                        if (Math.Abs(lastTransactionFish - fish[0].Value) > 1)
                        {
                            SetHoldings(_symbol, 0.5 * signal);
                            lastTransactionFish = fish[0].Value;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Error(ex.Message);
            }
        }
示例#15
0
 /// <summary>
 /// AroonUp = 100 * (period - {periods since max})/period
 /// </summary>
 /// <param name="upPeriod">The AroonUp period</param>
 /// <param name="max">A Maximum indicator used to compute periods since max</param>
 /// <param name="input">The next input data</param>
 /// <returns>The AroonUp value</returns>
 private static decimal ComputeAroonUp(int upPeriod, Maximum max, IndicatorDataPoint input)
 {
     max.Update(input);
     return 100m * (upPeriod - max.PeriodsSinceMaximum) / upPeriod;
 }
示例#16
0
        /// <summary>
        ///  The Cycle is a High Pass Filter.  We are going to cyberCycleSmooth the price so that the lag is 1.5 bars
        /// to eliminate two and 3 bar cyberCycle components, and to remove some noise.
        /// </summary>
        /// <param name="time"></param>
        private void UpdateCyberCycle(DateTime time)
        {
            if (barcount > 2)
            {
                cyberCycleSmooth.Add(idp(time, (OptimalTrackingFilter[0].Value + 2 * OptimalTrackingFilter[1].Value + OptimalTrackingFilter[2].Value / 6)));

                if (barcount < 7)
                {
                    cyberCycle.Add(idp(time, (OptimalTrackingFilter[0].Value - 2 * OptimalTrackingFilter[1].Value + OptimalTrackingFilter[2].Value) / 4));
                }
                else
                {
                    // From Ehlers page 15 equation 2.7
                    var hfp = (1 - a / 2) * (1 - a / 2) * (cyberCycleSmooth[0].Value - 2 * cyberCycleSmooth[1].Value + cyberCycleSmooth[2].Value)
                              + 2 * (1 - a) * cyberCycle[0].Value - (1 - a) * (1 - a) * cyberCycle[1].Value;
                    cyberCycle.Add(idp(time, hfp));
                }
            }
            else
            {
                cyberCycleSmooth.Add(idp(time, OptimalTrackingFilter[0].Value));
                cyberCycle.Add(idp(time, 0));
            }
            var x = cyberCycle[0];

            // Update the stoch version
            maxCyberCycle.Update(cyberCycle[0]);
            minCyberCycle.Update(cyberCycle[0]);
            //decimal maxCycle = GetMaxOf(stochCyberCycleValue1);
            //decimal minCycle = GetMinOf(stochCyberCycleValue1);
            decimal sccvalue1 = 0;

            if ((maxCyberCycle.Current.Value != minCyberCycle.Current.Value))
            {
                sccvalue1 = (cyberCycle[0].Value - minCyberCycle.Current.Value) / (maxCyberCycle.Current.Value - minCyberCycle.Current.Value);
            }


            stochCyberCycleValue1.Add(idp(time, sccvalue1));
            if (barcount > 4)
            {
                var value2 = (4 * stochCyberCycleValue1[0].Value
                              + 3 * stochCyberCycleValue1[1].Value
                              + 2 * stochCyberCycleValue1[2].Value
                              + stochCyberCycleValue1[3].Value);

                stochCyberCycleValue2[0].Value = 2 * (value2 - .5m);

                // v2 is the double version of value2 for use in the fishers
                double v2 = System.Convert.ToDouble(value2);
                // limit the new OptimalValue1 so that it falls within positive or negative unity
                if (v2 > .9999)
                {
                    v2 = .9999;
                }
                if (v2 < -.9999)
                {
                    v2 = -.9999;
                }


                // Inverse Fisherized value2
                // From inverse fisher
                //(Math.Exp(2*(double) OptimalValue1[0].Value) - 1)/(Math.Exp(2*(double) OptimalValue1[0].Value) + 1);
                double  v3 = (Math.Exp(2 * v2) - 1) / (Math.Exp(2 * v2) + 1);
                decimal v5 = System.Convert.ToDecimal(v3);
                stochCyberCycleInverseFisher.Add(idp(time, v5));
                if (barcount == 25)
                {
                    System.Diagnostics.Debug.WriteLine("here");
                }


                // Fisherized value2
                // From Fisher
                //Convert.ToDecimal(.5* Math.Log((1.0 + (double) OptimalValue1[0].Value)/(1.0 - (double) OptimalValue1[0].Value)));
                decimal v6 = Convert.ToDecimal(.5 * Math.Log((1.0 + v2) / (1.0 - v2)));

                //double v4 = .5 * Math.Log((1 + 1.98 * (v2 - .5) / 1 - 1.98 * (v2 - .5)));
                //decimal v6 = System.Convert.ToDecimal(v4);
                stochCyberCycleFisher.Add(idp(time, v6));
            }
            else
            {
                stochCyberCycleValue2.Add(idp(time, 0m));
                stochCyberCycleFisher.Add(idp(time, 0));
                stochCyberCycleInverseFisher.Add(idp(time, 0));
            }
        }