示例#1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ReturnsSymbolData"/> class
 /// </summary>
 /// <param name="symbol">The symbol of the data that updates the indicators</param>
 /// <param name="lookback">Look-back period for the RateOfChange indicator</param>
 /// <param name="period">Size of rolling window that contains historical RateOfChange</param>
 public ReturnsSymbolData(Symbol symbol, int lookback, int period)
 {
     _symbol       = symbol;
     _roc          = new RateOfChange($"{_symbol}.ROC({lookback})", lookback);
     _window       = new RollingWindow <IndicatorDataPoint>(period);
     _roc.Updated += OnRateOfChangeUpdated;
 }
示例#2
0
        public void ComputesCorrectly()
        {
            var    roc     = new RateOfChange(50);
            double epsilon = 1e-3;

            TestHelper.TestIndicator(roc, "spy_with_rocp50.txt", "Rate of Change % 50", (ind, expected) => Assert.AreEqual(expected, (double)ind.Current.Value * 100, epsilon));
        }
 public SymbolData(QCAlgorithmFramework algorithm, Symbol symbol, int lookback, Resolution resolution)
 {
     _symbol       = symbol;
     Return        = new RateOfChange($"{symbol}.ROC({lookback})", lookback);
     _consolidator = algorithm.ResolveConsolidator(symbol, resolution);
     algorithm.RegisterIndicator(symbol, Return, _consolidator);
 }
示例#4
0
        /// <summary>
        /// Creates a new RateOfChange indicator. This will compute the n-period 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 rateofchange</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 rateofchange indicator for the requested symbol over the specified period</returns>
        public RateOfChange ROC(string symbol, int period, Resolution?resolution = null, Func <BaseData, decimal> selector = null)
        {
            string name         = CreateIndicatorName(symbol, "ROC" + period, resolution);
            var    rateofchange = new RateOfChange(name, period);

            RegisterIndicator(symbol, rateofchange, resolution, selector);
            return(rateofchange);
        }
示例#5
0
 public SymbolData(QCAlgorithm algorithm, Security security, int lookback, Resolution resolution)
 {
     Security     = security;
     Consolidator = algorithm.ResolveConsolidator(security.Symbol, resolution);
     algorithm.SubscriptionManager.AddConsolidator(security.Symbol, Consolidator);
     ROC = new RateOfChange(security.Symbol.ToString(), lookback);
     algorithm.RegisterIndicator(security.Symbol, ROC, Consolidator);
 }
示例#6
0
        public void ComputesCorrectly()
        {
            var    roc     = new RateOfChange(50);
            double epsilon = 1e-3;

            TestHelper.TestIndicator(roc, "spy_with_rocp50.txt", "Rate of Change % 50",
                                     (ind, expected) => ((double)ind.Current.Price * 100).Should().BeApproximately(expected, epsilon));
        }
示例#7
0
        public void ResetsProperly()
        {
            var roc = new RateOfChange(50);
            foreach (var data in TestHelper.GetDataStream(51))
            {
                roc.Update(data);
            }
            Assert.IsTrue(roc.IsReady);

            roc.Reset();

            TestHelper.AssertIndicatorIsInDefaultState(roc);
        }
示例#8
0
            private double[] GetReturns(QCAlgorithm algorithm, Symbol symbol)
            {
                var window       = new RollingWindow <double>(period);
                var rateOfChange = new RateOfChange(1);

                rateOfChange.Updated += (s, item) => window.Add((double)item.Value);

                foreach (var bar in algorithm.History(symbol, period, Resolution.Daily))
                {
                    rateOfChange.Update(bar.EndTime, bar.Close);
                }
                return(window.ToArray());
            }
示例#9
0
        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        /// <seealso cref="QCAlgorithm.SetStartDate(System.DateTime)"/>
        /// <seealso cref="QCAlgorithm.SetEndDate(System.DateTime)"/>
        /// <seealso cref="QCAlgorithm.SetCash(decimal)"/>
        public override void Initialize()
        {
            //Initialize dates
            SetStartDate(2015, 6, 15);
            SetEndDate(2015, 6, 15);
            SetCash(25000);

            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
            #region "Init indicators"
            Price                 = new RollingWindow <IndicatorDataPoint>(samplesize);
            ema                   = new RollingWindow <IndicatorDataPoint>(samplesize);
            zema                  = new RollingWindow <IndicatorDataPoint>(samplesize);
            OptimalValue1         = new RollingWindow <IndicatorDataPoint>(samplesize);
            OmtimalValue2         = new RollingWindow <IndicatorDataPoint>(samplesize);
            OptimalTrackingFilter = new RollingWindow <IndicatorDataPoint>(samplesize);
            lambda                = new RollingWindow <IndicatorDataPoint>(samplesize);
            alpha                 = new RollingWindow <IndicatorDataPoint>(samplesize);
            priceOptimalDiff      = new RollingWindow <IndicatorDataPoint>(samplesize);
            priceOptimalSign      = new RollingWindow <IndicatorDataPoint>(samplesize);
            priceOptimalCross     = new RollingWindow <IndicatorDataPoint>(samplesize);
            fudge                 = new RollingWindow <IndicatorDataPoint>(samplesize);
            instantTrend          = new RollingWindow <IndicatorDataPoint>(samplesize);
            instantTrendTrigger   = new RollingWindow <IndicatorDataPoint>(samplesize);
            cyberCycle            = new RollingWindow <IndicatorDataPoint>(samplesize);
            centerGravity         = new RollingWindow <IndicatorDataPoint>(samplesize);
            cyberCycleSmooth      = new RollingWindow <IndicatorDataPoint>(samplesize);
            rvi                   = new RelativeVigorIndex(8);
            rviHistory            = new RollingWindow <IndicatorDataPoint>(samplesize);

            stochCenterGravityValue1 = new RollingWindow <IndicatorDataPoint>(8);
            stochCenterGravityValue2 = new RollingWindow <IndicatorDataPoint>(8);

            stochCyberCycleValue1        = new RollingWindow <IndicatorDataPoint>(8);
            stochCyberCycleValue2        = new RollingWindow <IndicatorDataPoint>(8);
            stochCyberCycleInverseFisher = new RollingWindow <IndicatorDataPoint>(8);
            stochCyberCycleFisher        = new RollingWindow <IndicatorDataPoint>(8);

            stochRviHistoryValue1 = new RollingWindow <IndicatorDataPoint>(8);
            stochRviHistoryValue2 = new RollingWindow <IndicatorDataPoint>(8);

            ROC           = new RateOfChange(4);
            maxCyberCycle = new Maximum(8);
            minCyberCycle = new Minimum(8);
            #endregion
            //mylog.Debug(transheader);
            mylog.Debug(ondataheader);
            string msg = "Security,Date,Day Profit,Day Fees, Day Net, Total Profit, Total Fees";
            mylog.Debug(msg);
            mylog.Debug(tradeheader);
        }
示例#10
0
        public void ResetsProperly()
        {
            var roc = new RateOfChange(50);

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

            roc.Reset();

            TestHelper.AssertIndicatorIsInDefaultState(roc);
        }
示例#11
0
        /// <summary>
        /// Creates a new Sharpe Ratio indicator using the specified periods
        /// </summary>
        /// <param name="name">The name of this indicator</param>
        /// <param name="period">Period of historical observation for sharpe ratio calculation</param>
        /// <param name="riskFreeRate">Risk-free rate for sharpe ratio calculation</param>
        public SharpeRatio(string name, int period, decimal riskFreeRate = 0.0m)
            : base(name)
        {
            _period = period;

            // calculate sharpe ratio using indicators
            _roc = new RateOfChange(1);
            var std = new StandardDeviation(period).Of(_roc);
            var sma = _roc.SMA(period);

            _sharpeRatio = sma.Minus(riskFreeRate).Over(std);

            // define warmup value
            WarmUpPeriod = _period + 2;
        }
示例#12
0
        public void IndicatorValueIsNotZeroWhenReady()
        {
            var indicator = new RateOfChange(1);
            var time      = DateTime.Now;

            for (int i = 1; i <= indicator.WarmUpPeriod; i++)
            {
                indicator.Update(time, i);
                time = time.AddSeconds(1);

                Assert.AreEqual(i == indicator.WarmUpPeriod, indicator.IsReady);
            }

            Assert.IsTrue(indicator.Current > 0);
        }
示例#13
0
        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        public override void Initialize()
        {
            SetStartDate(2014, 06, 06);  //Set Start Date
            SetEndDate(2014, 06, 06);    //Set End Date
            SetCash(100000);             //Set Strategy Cash

            // Find more symbols here: http://quantconnect.com/data
            // Forex, CFD, Equities Resolutions: Tick, Second, Minute, Hour, Daily.
            // Futures Resolution: Tick, Second, Minute
            // Options Resolution: Minute Only.
            AddEquity("AAPL", Resolution.Minute);
            AddOption("AAPL", Resolution.Minute);

            // There are other assets with similar methods. See "Selecting Options" etc for more details.
            // AddFuture, AddForex, AddCfd, AddOption
            _roc = new RateOfChange(9);
        }
示例#14
0
文件: VXXSurfer.cs 项目: Tollan/Lean
        //Initialize the data and resolution you require for your strategy:
        public override void Initialize()
        {
            SetStartDate(2017, 1, 3);                                    //SetStartDate(2009, 1, 30);
            SetEndDate(2018, 2, 15);                                     //SetEndDate(2015, 11, 30);
            SetCash(10000);
            AddSecurity(SecurityType.Equity, symbol, Resolution.Minute); // Resolution.Daily);

            close0 = SMA(symbol, 1, Resolution.Daily);

            roc1     = ROC(symbol, 1, Resolution.Daily);
            roc2     = ROC(symbol, 2, Resolution.Daily);
            roc3     = ROC(symbol, 3, Resolution.Daily);
            roc4     = ROC(symbol, 10, Resolution.Daily);
            atr1     = ATR(symbol, 1, MovingAverageType.Simple, Resolution.Daily);
            rsi2     = RSI(symbol, 2, MovingAverageType.Simple, Resolution.Daily);
            emaShort = EMA(symbol, 10, Resolution.Daily);

            // Set commissions to $1
            Securities[symbol].FeeModel = new ConstantFeeModel(1);
        }
        /// <summary>
        /// CALLED WHEN THE ROBOT FIRST STARTS, IT IS ONLY CALLED ONCE.
        /// </summary>
        protected override void OnStart()
        {
            // TRADE MANAGER DECLERATION
            //tm = new TradeManager(this);

            // INSTANTIATE THE INDICATORS
            //VindIchi = Indicators.IchimokuKinkoHyo(9, KijunSen, 52);
            // AVERAGE TRUE RANGE
            Vatr = Indicators.AverageTrueRange(ATRPeriod, MovingAverageType.Simple);

            //  CUSTOM INDICATORS
            // AROON OSCILLATOR
            Vao = Indicators.GetIndicator <AroonOscilator>(ArroonPeriod);
            // HULL FORECAST
            Vhfc = Indicators.GetIndicator <HullForcast>(HFPeriod, HFCPeriod, MarketSeries.Close, false, "");
            // INCORRECT NUMBER OF PARAMATERS
            // RATE OF CHANGE
            Vroc = Indicators.GetIndicator <RateOfChange>(MarketSeries.Close, ROCPeriod);
            // TREND MAGIC
            Vtm = Indicators.GetIndicator <TrendMagic>(TMCCI, TMATR);
            // TRUE STRENGTH INDEX
            Vtsi = Indicators.GetIndicator <TSI>(MarketSeries.Close, TSIShortPeriod, TSILongPeriod, TSISignalPeriod, MovingAverageType.Weighted);
        }
        /// <summary>
        /// Initialise the data and resolution required, as well as the cash and start-end dates for your algorithm. All algorithms must initialized.
        /// </summary>
        /// <seealso cref="QCAlgorithm.SetStartDate(System.DateTime)"/>
        /// <seealso cref="QCAlgorithm.SetEndDate(System.DateTime)"/>
        /// <seealso cref="QCAlgorithm.SetCash(decimal)"/>
        public override void Initialize()
        {
            //Initialize dates
            SetStartDate(2015, 6, 15);
            SetEndDate(2015, 6, 15);
            SetCash(25000);

            //Add as many securities as you like. All the data will be passed into the event handler:
            AddSecurity(SecurityType.Equity, symbol, Resolution.Minute);
            #region "Init indicators"
            Price = new RollingWindow<IndicatorDataPoint>(samplesize);
            ema = new RollingWindow<IndicatorDataPoint>(samplesize);
            zema = new RollingWindow<IndicatorDataPoint>(samplesize);
            OptimalValue1 = new RollingWindow<IndicatorDataPoint>(samplesize);
            OmtimalValue2 = new RollingWindow<IndicatorDataPoint>(samplesize);
            OptimalTrackingFilter = new RollingWindow<IndicatorDataPoint>(samplesize);
            lambda = new RollingWindow<IndicatorDataPoint>(samplesize);
            alpha = new RollingWindow<IndicatorDataPoint>(samplesize);
            priceOptimalDiff = new RollingWindow<IndicatorDataPoint>(samplesize);
            priceOptimalSign = new RollingWindow<IndicatorDataPoint>(samplesize);
            priceOptimalCross = new RollingWindow<IndicatorDataPoint>(samplesize);
            fudge = new RollingWindow<IndicatorDataPoint>(samplesize);
            instantTrend = new RollingWindow<IndicatorDataPoint>(samplesize);
            instantTrendTrigger = new RollingWindow<IndicatorDataPoint>(samplesize);
            cyberCycle = new RollingWindow<IndicatorDataPoint>(samplesize);
            centerGravity = new RollingWindow<IndicatorDataPoint>(samplesize);
            cyberCycleSmooth = new RollingWindow<IndicatorDataPoint>(samplesize);
            rvi = new RelativeVigorIndex(8);
            rviHistory = new RollingWindow<IndicatorDataPoint>(samplesize);

            stochCenterGravityValue1 = new RollingWindow<IndicatorDataPoint>(8);
            stochCenterGravityValue2 = new RollingWindow<IndicatorDataPoint>(8);

            stochCyberCycleValue1 = new RollingWindow<IndicatorDataPoint>(8);
            stochCyberCycleValue2 = new RollingWindow<IndicatorDataPoint>(8);
            stochCyberCycleInverseFisher = new RollingWindow<IndicatorDataPoint>(8);
            stochCyberCycleFisher = new RollingWindow<IndicatorDataPoint>(8);

            stochRviHistoryValue1 = new RollingWindow<IndicatorDataPoint>(8);
            stochRviHistoryValue2 = new RollingWindow<IndicatorDataPoint>(8);

            ROC = new RateOfChange(4);
            maxCyberCycle = new Maximum(8);
            minCyberCycle = new Minimum(8);
            #endregion
            //mylog.Debug(transheader);
            mylog.Debug(ondataheader);
            string msg = "Security,Date,Day Profit,Day Fees, Day Net, Total Profit, Total Fees";
            mylog.Debug(msg);
            mylog.Debug(tradeheader);
        }
示例#17
0
        /// <summary>
        /// Initialize test
        /// </summary>
        public TestIndicatorRateOfChange()
        {
            DataStream stream = new OHLCBarStream(new ForexSecurity("EURUSD"), BarInterval.FiveMin);

            _sut = new RateOfChange(Period, stream.DefaultInterval, stream);
        }
示例#18
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MSAStrategy" /> class.
        /// </summary>
        /// <param name="smoothedSeries">The smoothed series.</param>
        /// <param name="previousDaysN">How many daily means will be used to estimate the thresholds.</param>
        /// <param name="runsPerDay">How many runs will be used to estimate the daily mean.</param>
        /// <param name="minRunThreshold">The minimum run threshold.</param>
        /// <param name="DailyIndicatorReset">if set to <c>true</c> [daily indicator reset].</param>
        public MSAStrategy(IndicatorBase<IndicatorDataPoint> smoothedSeries, int previousDaysN = 3, int runsPerDay = 5, decimal minRunThreshold = 0.0001m, bool DailyIndicatorReset = true)
        {
            _runsPerDay = runsPerDay;
            _actualRun = 1m;
            _turnAround = false;
            _minRunThreshold = minRunThreshold;
            _dailyIndicatorReset = DailyIndicatorReset;

            _smoothedSeries = smoothedSeries;
            _smoothedSeriesROC = new RateOfChange(1).Of(_smoothedSeries);
            _SSROCRW = new RollingWindow<IndicatorDataPoint>(2);

            _todayRuns = new List<decimal>();
            _previousDaysDownwardRuns = new RollingWindow<decimal>(previousDaysN);
            _previousDaysUpwardRuns = new RollingWindow<decimal>(previousDaysN);

            ActualSignal = OrderSignal.doNothing;
            Position = StockState.noInvested;

            _smoothedSeriesROC.Updated += (object sender, IndicatorDataPoint updated) =>
                {
                    if (_smoothedSeriesROC.IsReady) _SSROCRW.Add(updated);
                    if (_SSROCRW.IsReady) RunStrategy();
                    if (_SSROCRW.IsReady && IsReady) CheckSignal();
                };
        }
示例#19
0
 public void ComputesCorrectly()
 {
     var roc = new RateOfChange(50);
     double epsilon = 1e-3;
     TestHelper.TestIndicator(roc, "spy_with_roc50.txt", "Rate of Change 50", (ind, expected) => Assert.AreEqual(expected, (double)ind.Current.Value, epsilon));
 }