示例#1
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="CoppockCurve" /> indicator
 /// </summary>
 /// <param name="name">A name for the indicator</param>
 /// <param name="shortRocPeriod">The period for the short ROC</param>
 /// <param name="longRocPeriod">The period for the long ROC</param>
 /// <param name="lwmaPeriod">The period for the LWMA</param>
 public CoppockCurve(string name, int shortRocPeriod, int longRocPeriod, int lwmaPeriod)
     : base(name)
 {
     _shortRoc = new RateOfChangePercent(shortRocPeriod);
     _longRoc  = new RateOfChangePercent(longRocPeriod);
     _lwma     = new LinearWeightedMovingAverage(lwmaPeriod);
 }
示例#2
0
        public override void Initialize()
        {
            this.UnderlyingTicker = this.GetParameter("symbol");
            //AAPL
            SetStartDate(2018, 01, 01);
            SetEndDate(2018, 11, 30);

            //
            //SetStartDate(2018, 03, 01);
            //SetEndDate(2018, 03, 01);
            //SetStartDate(2015, 01, 01);
            //SetStartDate(2018, 02, 15);
            //SetEndDate(2018, 03, 09);
            SetCash(100000);

            var equity = AddEquity(UnderlyingTicker, RESOLUTION);
            var option = AddOption(UnderlyingTicker, RESOLUTION);

            // use the underlying equity as the benchmark
            SetBenchmark(equity.Symbol);

            var consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(MINUTE_RATE));

            consolidator.DataConsolidated += Consolidator_DataConsolidated;
            _rocp = new RateOfChangePercent(9);
            RegisterIndicator(equity.Symbol, _rocp, consolidator);
            SubscriptionManager.AddConsolidator(equity.Symbol, consolidator);

            // init strategy
            _Statistics = new OptionStatistics(this, option);
        }
示例#3
0
        public void ComputesCorrectly()
        {
            var    rocp    = new RateOfChangePercent(50);
            double epsilon = 1e-3;

            TestHelper.TestIndicator(rocp, "spy_with_rocp50.txt", "Rate of Change % 50", (ind, expected) => Assert.AreEqual(expected, (double)ind.Current.Value, epsilon));
        }
示例#4
0
        /// <summary>
        /// Creates a new RateOfChangePercent indicator. This will compute the n-period percentage rate of change in the security.
        /// The indicator will be automatically updated on the given resolution.
        /// </summary>
        /// <param name="symbol">The symbol whose rateofchange we want</param>
        /// <param name="period">The period over which to compute the rateofchangepercent</param>
        /// <param name="resolution">The resolution</param>
        /// <param name="selector">Selects a value from the BaseData to send into the indicator, if null defaults to the Value property of BaseData (x => x.Value)</param>
        /// <returns>The rateofchangepercent indicator for the requested symbol over the specified period</returns>
        public RateOfChangePercent ROCP(string symbol, int period, Resolution?resolution = null, Func <BaseData, decimal> selector = null)
        {
            string name = CreateIndicatorName(symbol, "ROCP" + period, resolution);
            var    rateofchangepercent = new RateOfChangePercent(name, period);

            RegisterIndicator(symbol, rateofchangepercent, resolution, selector);
            return(rateofchangepercent);
        }
示例#5
0
        public void ComputesCorrectly()
        {
            var    rocp    = new RateOfChangePercent(50);
            double epsilon = 1e-3;

            TestHelper.TestIndicator(rocp, "spy_with_rocp50.txt", "Rate of Change % 50",
                                     (ind, expected) => ((double)ind.Current.Price).Should().BeApproximately(expected, epsilon));
        }
示例#6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Trix"/> class using the specified name and period.
 /// </summary>
 /// <param name="name">The name of this indicator</param>
 /// <param name="period">The period of the indicator</param>
 public Trix(string name, int period)
     : base(name)
 {
     _period = period;
     _ema1   = new ExponentialMovingAverage(name + "_1", period);
     _ema2   = new ExponentialMovingAverage(name + "_2", period);
     _ema3   = new ExponentialMovingAverage(name + "_3", period);
     _roc    = new RateOfChangePercent(name + "_ROCP1", 1);
 }
示例#7
0
        public void ResetsProperly()
        {
            var rocp = new RateOfChangePercent(50);
            foreach (var data in TestHelper.GetDataStream(51))
            {
                rocp.Update(data);
            }
            Assert.IsTrue(rocp.IsReady);

            rocp.Reset();

            TestHelper.AssertIndicatorIsInDefaultState(rocp);
        }
                public SymbolData(QCAlgorithm algorithm, Symbol symbol, int dailyLookback, int lookback, Resolution resolution)
                {
                    Symbol = symbol;

                    _dailyReturn          = new RateOfChangePercent($"{symbol}.DailyROCP(1)", 1);
                    _dailyConsolidator    = algorithm.ResolveConsolidator(symbol, Resolution.Daily);
                    _dailyReturnHistory   = new RollingWindow <IndicatorDataPoint>(dailyLookback);
                    _dailyReturn.Updated += (s, e) => _dailyReturnHistory.Add(e);
                    algorithm.RegisterIndicator(symbol, _dailyReturn, _dailyConsolidator);

                    Return        = new RateOfChangePercent($"{symbol}.ROCP({lookback})", lookback);
                    _consolidator = algorithm.ResolveConsolidator(symbol, resolution);
                    algorithm.RegisterIndicator(symbol, Return, _consolidator);
                }
示例#9
0
        public void ResetsProperly()
        {
            var rocp = new RateOfChangePercent(50);

            foreach (var data in TestHelper.GetDataStream(51))
            {
                rocp.Update(data);
            }
            Assert.IsTrue(rocp.IsReady);

            rocp.Reset();

            TestHelper.AssertIndicatorIsInDefaultState(rocp);
        }
示例#10
0
        public override void Initialize()
        {
            SetStartDate(2015, 6, 26);
            SetEndDate(2015, 7, 2);
            SetCash(25000);

            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);

            // define our 15 minute consolidator
            var fifteenMinuteConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(15));

            // if we want to make decisions every 15 minutes as well, we can add an event handler
            // to the DataConsolidated event
            fifteenMinuteConsolidator.DataConsolidated += OnFiftenMinuteSPY;

            int fast = 15;
            int slow = 30;

            // define our EMA, we'll manually register this, so we aren't using the helper function 'EMA(...)'
            var fastEmaOnFifteenMinuteBars = new ExponentialMovingAverage("SPY_EMA15", fast);
            var slowEmaOnFifteenMinuteBars = new ExponentialMovingAverage("SPY_EMA30", slow);

            // we can define complex indicator's using various extension methods.
            // here I use the 'Over' extension method which performs division
            // so this will be fast/slow. This returns a new indicator that represents
            // the division operation between the two
            var ratio = fastEmaOnFifteenMinuteBars.Over(slowEmaOnFifteenMinuteBars, "SPY_Ratio_EMA");

            // now we can use the 'Of' extension method to define the ROC on the ratio
            // The 'Of' extension method allows combining multiple indicators together such
            // that the data from one gets sent into the other
            var rocpOfRatio = new RateOfChangePercent("SPY_ROCP_Ratio", fast).Of(ratio);

            // we an even define a smoothed version of this indicator
            var smoothedRocpOfRatio = new ExponentialMovingAverage("SPY_Smoothed_ROCP_Ratio", 5).Of(rocpOfRatio);

            // register our indicator and consolidator together. this will wire the consolidator up to receive
            // data for the specified symbol, and also set up the indicator to receive its data from the consolidator
            RegisterIndicator("SPY", fastEmaOnFifteenMinuteBars, fifteenMinuteConsolidator, Field.Close);
            RegisterIndicator("SPY", slowEmaOnFifteenMinuteBars, fifteenMinuteConsolidator, Field.Close);

            // register the indicator to be plotted along
            PlotIndicator("SPY", fastEmaOnFifteenMinuteBars);
            PlotIndicator("SPY", slowEmaOnFifteenMinuteBars);
            PlotIndicator("SPY_ROCP_Ratio", rocpOfRatio, smoothedRocpOfRatio);
            PlotIndicator("SPY_Ratio_EMA", ratio);
        }
示例#11
0
        public override void Initialize()
        {
            SetStartDate(2015, 6, 26);
            SetEndDate(2015, 7, 2);
            SetCash(25000);

            AddSecurity(SecurityType.Equity, "SPY", Resolution.Minute);

            // define our 15 minute consolidator
            var fifteenMinuteConsolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(15));

            // if we want to make decisions every 15 minutes as well, we can add an event handler
            // to the DataConsolidated event
            fifteenMinuteConsolidator.DataConsolidated += OnFiftenMinuteSPY;

            int fast = 15;
            int slow = 30;

            // define our EMA, we'll manually register this, so we aren't using the helper function 'EMA(...)'
            var fastEmaOnFifteenMinuteBars = new ExponentialMovingAverage("SPY_EMA15", fast);
            var slowEmaOnFifteenMinuteBars = new ExponentialMovingAverage("SPY_EMA30", slow);

            // we can define complex indicator's using various extension methods.
            // here I use the 'Over' extension method which performs division
            // so this will be fast/slow. This returns a new indicator that represents
            // the division operation between the two
            var ratio = fastEmaOnFifteenMinuteBars.Over(slowEmaOnFifteenMinuteBars, "SPY_Ratio_EMA");

            // now we can use the 'Of' extension method to define the ROC on the ratio
            // The 'Of' extension method allows combining multiple indicators together such
            // that the data from one gets sent into the other
            var rocpOfRatio = new RateOfChangePercent("SPY_ROCP_Ratio", fast).Of(ratio);

            // we an even define a smoothed version of this indicator
            var smoothedRocpOfRatio = new ExponentialMovingAverage("SPY_Smoothed_ROCP_Ratio", 5).Of(rocpOfRatio);

            // register our indicator and consolidator together. this will wire the consolidator up to receive
            // data for the specified symbol, and also set up the indicator to receive its data from the consolidator
            RegisterIndicator("SPY", fastEmaOnFifteenMinuteBars, fifteenMinuteConsolidator, Field.Close);
            RegisterIndicator("SPY", slowEmaOnFifteenMinuteBars, fifteenMinuteConsolidator, Field.Close);

            // register the indicator to be plotted along
            PlotIndicator("SPY", fastEmaOnFifteenMinuteBars);
            PlotIndicator("SPY", slowEmaOnFifteenMinuteBars);
            PlotIndicator("SPY_ROCP_Ratio", rocpOfRatio, smoothedRocpOfRatio);
            PlotIndicator("SPY_Ratio_EMA", ratio);
        }
示例#12
0
        public override void Initialize()
        {
            UnderlyingTicker = GetParameter("symbol");
            Underlying       = QuantConnect.Symbol.Create(UnderlyingTicker, SecurityType.Equity, Market.USA);
            //AAPL
            //SetStartDate(2014, 06, 06);
            //SetEndDate(2014, 06, 06);

            DateTime startDate = DateTime.Parse(GetParameter("start-date"));
            DateTime endDate   = DateTime.Parse(GetParameter("end-date"));

            _ROC_THRESHOLD = parseInt(GetParameter("roc"), _ROC_THRESHOLD);
            _RocPeriod     = parseInt(GetParameter("roc-period"), _RocPeriod);

            /*
             *          _MinDaysRemaining = parseInt(GetParameter("min-days"), _MinDaysRemaining);
             * _MinDaysRemaining = parseInt(GetParameter("max-days"), _MinDaysRemaining);
             * _AtmSpread = parseInt(GetParameter("atm-spread"), _AtmSpread);
             */

            //
            SetStartDate(startDate);
            SetEndDate(endDate);
            //SetStartDate(2015, 01, 01);
            //SetStartDate(2018, 02, 15);
            //SetEndDate(2018, 03, 09);
            SetCash(50000);

            //this.

            var equity = AddEquity(UnderlyingTicker, RESOLUTION);
            var option = AddOption(UnderlyingTicker, RESOLUTION);

            // use the underlying equity as the benchmark
            SetBenchmark(equity.Symbol);

            var consolidator = new TradeBarConsolidator(TimeSpan.FromMinutes(MINUTE_RATE));

            consolidator.DataConsolidated += Consolidator_DataConsolidated;
            _rocp = new RateOfChangePercent(_RocPeriod);
            RegisterIndicator(Underlying, _rocp, consolidator);
            SubscriptionManager.AddConsolidator(Underlying, consolidator);

            // init strategy
            _Strategy = new OptionTraverseStrategy(this, option);
        }
示例#13
0
 public void ComputesCorrectly()
 {
     var rocp = new RateOfChangePercent(50);
     double epsilon = 1e-3;
     TestHelper.TestIndicator(rocp, "spy_with_rocp50.txt", "Rate of Change % 50", (ind, expected) => Assert.AreEqual(expected, (double)ind.Current.Value, epsilon));
 }
示例#14
0
        /// <summary>
        /// Executes the Instant Trend strategy
        /// </summary>
        /// <param name="data">TradeBars - the current OnData</param>
        /// <param name="tradesize"></param>
        /// <param name="trendCurrent">IndicatorDataPoint - the current trend value trend</param>
        /// <param name="orderId">int - the orderId if one is placed, -1 if order has not filled and 0 if no order was placed</param>
        public string ExecuteStrategy(TradeBars data, int tradesize, IndicatorDataPoint max, IndicatorDataPoint min, RateOfChangePercent rocp, ref SimpleMovingAverage sma20, out int orderId)
        {
            maximum = max;
            minimum = min;
            Price.Add(idp(data[_symbol].EndTime, (data[_symbol].Close + data[_symbol].Open) / 2));
            orderId = 0;
            comment = string.Empty;



            if (_algorithm.Portfolio[_symbol].IsLong)
            {
                nStatus = 1;
            }
            if (_algorithm.Portfolio[_symbol].IsShort)
            {
                nStatus = -1;
            }


            #region "Strategy Execution"

            bReverseTrade = false;

            try
            {
                if (!_algorithm.Portfolio.Invested)
                {
                    if (PricePassedAValley() && rocp.Current.Value < 0)
                    {
                        ticket  = GetLong(tradesize);
                        orderId = ticket.OrderId;
                        comment = "Bot new position ppMin && rocp < 0";
                    }
                    if (PricePassedAPeak() && rocp.Current.Value > 0)
                    {
                        ticket  = GetShort(tradesize);
                        orderId = ticket.OrderId;
                        comment = "Sld new position ppMin && rocp < 0";
                    }
                }
                else
                {
                    if (PricePassedAValley() && _algorithm.Portfolio[_symbol].IsShort)
                    {
                        if (Price[0].Value > sma20.Current.Value && (Math.Abs(sma20.Current.Value - Price[0].Value) / Price[0].Value) > .001m)
                        {
                            ticket  = ReverseToLong();
                            comment = "Rev2Long Passed a Valley";
                        }
                    }

                    if (PricePassedAPeak() && _algorithm.Portfolio[_symbol].IsLong)
                    {
                        if (Price[0].Value <sma20.Current.Value && (Math.Abs(sma20.Current.Value - Price[0].Value) / Price[0].Value)> .001m)
                        {
                            ticket  = ReverseToShort();
                            comment = "Rev2Short Passed a Peak";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }
            #endregion

            return(comment);
        }
        /// <summary>
        /// Executes the Instant Trend strategy
        /// </summary>
        /// <param name="data">TradeBars - the current OnData</param>
        /// <param name="tradesize"></param>
        /// <param name="trendCurrent">IndicatorDataPoint - the current trend value trend</param>
        /// <param name="orderId">int - the orderId if one is placed, -1 if order has not filled and 0 if no order was placed</param>
        public string ExecuteStrategy(TradeBars data, int tradesize, IndicatorDataPoint max, IndicatorDataPoint min, RateOfChangePercent rocp, ref SimpleMovingAverage sma20, out int orderId)
        {
            maximum = max;
            minimum = min;
            Price.Add(idp(data[_symbol].EndTime, (data[_symbol].Close + data[_symbol].Open) / 2));
            orderId = 0;
            comment = string.Empty;

            if (_algorithm.Portfolio[_symbol].IsLong) nStatus = 1;
            if (_algorithm.Portfolio[_symbol].IsShort) nStatus = -1;

            #region "Strategy Execution"

            bReverseTrade = false;

            try
            {

                if (!_algorithm.Portfolio.Invested)
                {
                    if (PricePassedAValley() && rocp.Current.Value < 0)
                    {
                        ticket = GetLong(tradesize);
                        orderId = ticket.OrderId;
                        comment = "Bot new position ppMin && rocp < 0";
                    }
                    if (PricePassedAPeak() && rocp.Current.Value > 0)
                    {
                        ticket = GetShort(tradesize);
                        orderId = ticket.OrderId;
                        comment = "Sld new position ppMin && rocp < 0";
                    }
                }
                else
                {
                    if (PricePassedAValley() && _algorithm.Portfolio[_symbol].IsShort)
                    {
                        if (Price[0].Value > sma20.Current.Value && (Math.Abs(sma20.Current.Value - Price[0].Value) / Price[0].Value) > .001m)
                        {
                            ticket = ReverseToLong();
                            comment = "Rev2Long Passed a Valley";
                        }
                    }

                    if (PricePassedAPeak() && _algorithm.Portfolio[_symbol].IsLong)
                    {
                        if (Price[0].Value < sma20.Current.Value && (Math.Abs(sma20.Current.Value - Price[0].Value) / Price[0].Value) > .001m)
                        {
                            ticket = ReverseToShort();
                            comment = "Rev2Short Passed a Peak";
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.StackTrace);
            }
            #endregion

            return comment;
        }