Exemple #1
0
        public void ResetsProperly()
        {
            var min = new Minimum(3);
            min.Update(DateTime.Today, 1m);
            min.Update(DateTime.Today.AddSeconds(1), 2m);
            min.Update(DateTime.Today.AddSeconds(2), 1m);
            Assert.IsTrue(min.IsReady);

            min.Reset();
            Assert.AreEqual(0, min.PeriodsSinceMinimum);
            TestHelper.AssertIndicatorIsInDefaultState(min);
        }
Exemple #2
0
        public void ComputesCorrectly()
        {
            var min = new Minimum(3);

            var reference = DateTime.UtcNow;

            min.Update(reference, 1m);
            Assert.AreEqual(1m, min.Current.Value);
            Assert.AreEqual(0, min.PeriodsSinceMinimum);

            min.Update(reference.AddDays(1), 2m);
            Assert.AreEqual(1m, min.Current.Value);
            Assert.AreEqual(1, min.PeriodsSinceMinimum);

            min.Update(reference.AddDays(2), -1m);
            Assert.AreEqual(-1m, min.Current.Value);
            Assert.AreEqual(0, min.PeriodsSinceMinimum);

            min.Update(reference.AddDays(3), 2m);
            Assert.AreEqual(-1m, min.Current.Value);
            Assert.AreEqual(1, min.PeriodsSinceMinimum);

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

            min.Update(reference.AddDays(5), 3m);
            Assert.AreEqual(0m, min.Current.Value);
            Assert.AreEqual(1, min.PeriodsSinceMinimum);

            min.Update(reference.AddDays(6), 2m);
            Assert.AreEqual(0m, min.Current.Value);
            Assert.AreEqual(2, min.PeriodsSinceMinimum);
        }
        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);
        }
Exemple #4
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);
        }
        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.Current.Value + r.Current.Value);

            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);
        }
Exemple #6
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
            }
        }
Exemple #7
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);
            }
        }
Exemple #8
0
 /// <summary>
 /// AroonDown = 100 * (period - {periods since min})/period
 /// </summary>
 /// <param name="downPeriod">The AroonDown period</param>
 /// <param name="min">A Minimum indicator used to compute periods since min</param>
 /// <param name="input">The next input data</param>
 /// <returns>The AroonDown value</returns>
 private static decimal ComputeAroonDown(int downPeriod, Minimum min, IndicatorDataPoint input)
 {
     min.Update(input);
     return 100m * (downPeriod - min.PeriodsSinceMinimum) / downPeriod;
 }
Exemple #9
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));
            }
        }