예제 #1
0
        public void CompositeIsReadyWhenBothAre()
        {
            var left      = new Delay(1);
            var right     = new Delay(2);
            var composite = new CompositeIndicator <IndicatorDataPoint>(left, right, (l, r) => l + r);

            left.Update(DateTime.Today.AddSeconds(0), 1m);
            right.Update(DateTime.Today.AddSeconds(0), 1m);
            Assert.IsFalse(composite.IsReady);
            Assert.IsFalse(composite.Left.IsReady);
            Assert.IsFalse(composite.Right.IsReady);

            left.Update(DateTime.Today.AddSeconds(1), 2m);
            right.Update(DateTime.Today.AddSeconds(1), 2m);
            Assert.IsFalse(composite.IsReady);
            Assert.IsTrue(composite.Left.IsReady);
            Assert.IsFalse(composite.Right.IsReady);

            left.Update(DateTime.Today.AddSeconds(2), 3m);
            right.Update(DateTime.Today.AddSeconds(2), 3m);
            Assert.IsTrue(composite.IsReady);
            Assert.IsTrue(composite.Left.IsReady);
            Assert.IsTrue(composite.Right.IsReady);

            left.Update(DateTime.Today.AddSeconds(3), 4m);
            right.Update(DateTime.Today.AddSeconds(3), 4m);
            Assert.IsTrue(composite.IsReady);
            Assert.IsTrue(composite.Left.IsReady);
            Assert.IsTrue(composite.Right.IsReady);
        }
예제 #2
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(year: 2010, month: 01, day: 01); //Set Start Date
            SetEndDate(year: 2013, month: 12, day: 31);   //Set End Date
            SetCash(startingCash: 100000);                //Set Strategy Cash

            // Set your Intrinino user and password.
            IntrinioConfig.SetUserAndPassword(_user, _password);

            // Set Intrinio config to make 1 call each minute, default is 1 call each 5 seconds.
            // (1 call each minute is the free account limit for historical_data endpoint)
            IntrinioConfig.SetTimeIntervalBetweenCalls(TimeSpan.FromMinutes(1));


            // 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.
            _uso = AddEquity("USO", Resolution.Daily, leverage: 2m).Symbol;
            _bno = AddEquity("BNO", Resolution.Daily, leverage: 2m).Symbol;

            AddData <IntrinioEconomicData>(IntrinioEconomicDataSources.Commodities.CrudeOilWTI, Resolution.Daily);
            AddData <IntrinioEconomicData>(IntrinioEconomicDataSources.Commodities.CrudeOilBrent, Resolution.Daily);
            _spread = _brent.Minus(_wti);

            _emaWti = EMA(Securities[IntrinioEconomicDataSources.Commodities.CrudeOilWTI].Symbol, 10);
        }
        public void CompositeIsReadyWhenBothAre()
        {
            var left      = new Delay(1);
            var right     = new Delay(2);
            var composite = new CompositeIndicator(left, right, (l, r) => l.Current + r.Current);

            left.Update(DateTime.Today.AddSeconds(0), 1d);
            right.Update(DateTime.Today.AddSeconds(0), 1d);
            Assert.IsFalse(composite.IsReady);
            Assert.IsFalse(composite.Left.IsReady);
            Assert.IsFalse(composite.Right.IsReady);

            left.Update(DateTime.Today.AddSeconds(1), 2d);
            right.Update(DateTime.Today.AddSeconds(1), 2d);
            Assert.IsFalse(composite.IsReady);
            Assert.IsTrue(composite.Left.IsReady);
            Assert.IsFalse(composite.Right.IsReady);

            left.Update(DateTime.Today.AddSeconds(2), 3d);
            right.Update(DateTime.Today.AddSeconds(2), 3d);
            Assert.IsTrue(composite.IsReady);
            Assert.IsTrue(composite.Left.IsReady);
            Assert.IsTrue(composite.Right.IsReady);

            left.Update(DateTime.Today.AddSeconds(3), 4d);
            right.Update(DateTime.Today.AddSeconds(3), 4d);
            Assert.IsTrue(composite.IsReady);
            Assert.IsTrue(composite.Left.IsReady);
            Assert.IsTrue(composite.Right.IsReady);
        }
예제 #4
0
        public void CompositeIsReadyWhenBothAre()
        {
            var left = new Delay(1);
            var right = new Delay(2);
            var composite = new CompositeIndicator<IndicatorDataPoint>(left, right, (l, r) => l + r);

            left.Update(DateTime.Today.AddSeconds(0), 1m);
            right.Update(DateTime.Today.AddSeconds(0), 1m);
            Assert.IsFalse(composite.IsReady);
            Assert.IsFalse(composite.Left.IsReady);
            Assert.IsFalse(composite.Right.IsReady);

            left.Update(DateTime.Today.AddSeconds(1), 2m);
            right.Update(DateTime.Today.AddSeconds(1), 2m);
            Assert.IsFalse(composite.IsReady);
            Assert.IsTrue(composite.Left.IsReady);
            Assert.IsFalse(composite.Right.IsReady);

            left.Update(DateTime.Today.AddSeconds(2), 3m);
            right.Update(DateTime.Today.AddSeconds(2), 3m);
            Assert.IsTrue(composite.IsReady);
            Assert.IsTrue(composite.Left.IsReady);
            Assert.IsTrue(composite.Right.IsReady);

            left.Update(DateTime.Today.AddSeconds(3), 4m);
            right.Update(DateTime.Today.AddSeconds(3), 4m);
            Assert.IsTrue(composite.IsReady);
            Assert.IsTrue(composite.Left.IsReady);
            Assert.IsTrue(composite.Right.IsReady);
        }
 /// <summary>
 /// Initializes a new instance of the VWAP class with a given name and period
 /// </summary>
 /// <param name="name">string - the name of the indicator</param>
 /// <param name="period">The period of the VWAP</param>
 public VolumeWeightedAveragePriceIndicator(string name, int period)
     : base(name)
 {
     _price = new Identity("Price");
     _volume = new Identity("Volume");
     
     // This class will be using WeightedBy indicator extension 
     _vwap = _price.WeightedBy(_volume, period);
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="CrossingMovingAverageSignal" /> class.
 /// </summary>
 /// <param name="fast">The fast moving average.</param>
 /// <param name="slow">The slow moving average.</param>
 /// <param name="direction">
 ///     The trade rule direction. Only used if the instance will be part of a
 ///     <see cref="Rule" /> class
 /// </param>
 /// <remarks>
 ///     Both Moving Averages must be registered BEFORE being used by this constructor.
 /// </remarks>
 public CrossingMovingAverageSignal(IndicatorBase <IndicatorDataPoint> fast,
                                    IndicatorBase <IndicatorDataPoint> slow, Direction direction, int survivalPeriod = 1)
 {
     _fast = fast;
     _slow = slow;
     _moving_average_difference          = fast.Minus(slow);
     _moving_average_difference.Updated += ma_Updated;
     _direction   = direction;
     _lastSignals = new int[survivalPeriod];
 }
예제 #7
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="CrossingMovingAverageSignal" /> class.
 /// </summary>
 /// <param name="fast">The fast moving average.</param>
 /// <param name="slow">The slow moving average.</param>
 /// <param name="direction">
 ///     The trade rule direction. Only used if the instance will be part of a
 ///     <see cref="Rule" /> class
 /// </param>
 /// <remarks>
 ///     Both Moving Averages must be registered BEFORE being used by this constructor.
 /// </remarks>
 public CrossingMovingAverageSignal(IndicatorBase <IndicatorDataPoint> fast,
                                    IndicatorBase <IndicatorDataPoint> slow, Direction direction, int survivalPeriod = 1)
 {
     _fast = fast;
     _slow = slow;
     _moving_average_difference          = fast.Minus(slow);
     _moving_average_difference.Updated += ma_Updated;
     _direction     = direction;
     SurvivalWindow = new RollingWindow <int>(survivalPeriod);
 }
        public void CallsDelegateCorrectly()
        {
            var left      = new Identity("left");
            var right     = new Identity("right");
            var composite = new CompositeIndicator(left, right, (l, r) => {
                Assert.AreEqual(left, l);
                Assert.AreEqual(right, r);
                return(l.Current + r.Current);
            });

            left.Update(DateTime.Today, 1d);
            right.Update(DateTime.Today, 1d);
            Assert.AreEqual(2d, composite.Current.Value);
        }
예제 #9
0
        public void CallsDelegateCorrectly()
        {
            var left = new Identity("left");
            var right = new Identity("right");
            var composite = new CompositeIndicator<IndicatorDataPoint>(left, right, (l, r) =>
            {
                Assert.AreEqual(left, l);
                Assert.AreEqual(right, r);
                return l + r;
            });

            composite.Update(DateTime.Today, 1m);
            Assert.AreEqual(2m, composite.Current.Value);
        }
        /// <summary>
        ///     Initialize 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(year: 2014, month: 05, day: 07); //Set Start Date
            SetEndDate(year: 2014, month: 05, day: 15);   //Set End Date
            SetCash(startingCash: 100000);                //Set Strategy Cash
            // Find more symbols here: http://quantconnect.com/data
            EURUSD = AddForex("EURUSD", Resolution.Minute, Market.FXCM).Symbol;

            AddData <FxcmVolume>("EURUSD", Resolution.Minute, DateTimeZone.Utc);
            var _price = Identity(EURUSD);

            fastVWMA = _price.WeightedBy(volume, period: 15);
            slowVWMA = _price.WeightedBy(volume, period: 300);
        }
예제 #11
0
        public void CallsDelegateCorrectly()
        {
            var left      = new Identity("left");
            var right     = new Identity("right");
            var composite = new CompositeIndicator <IndicatorDataPoint>(left, right, (l, r) =>
            {
                Assert.AreEqual(left, l);
                Assert.AreEqual(right, r);
                return(l + r);
            });

            composite.Update(DateTime.Today, 1m);
            Assert.AreEqual(2m, composite.Current.Value);
        }
예제 #12
0
파일: SharpeRatio.cs 프로젝트: yzhao20/Lean
        /// <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;
        }
        /// <summary>
        ///     Initialize 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, 05, 07);     //Set Start Date
            SetEndDate(2014, 05, 15);       //Set End Date
            SetCash(100000);                //Set Strategy Cash

            // Find more symbols here: https://www.quantconnect.com/data
            EURUSD = AddForex("EURUSD", Resolution.Minute).Symbol;

            AddData <FxcmVolume>("EURUSD_Vol", Resolution.Minute, DateTimeZone.Utc);
            var _price = Identity(EURUSD);

            fastVWMA = _price.WeightedBy(volume, period: 15);
            slowVWMA = _price.WeightedBy(volume, period: 300);
        }
예제 #14
0
        public void WhenProcessingDataByLeftOrRightIndicator_Then_OutputUpdated_IsRaised()
        {
            var left  = new IdentityIndicator();
            var right = new IdentityIndicator();
            var sut   = new CompositeIndicator(left, right, (l, r) => l.Output + r.Output);

            var output = 0d;

            sut.OutputUpdated += (sender, o) => output = o;

            left.Process(1);
            Assert.AreEqual(1, output);

            right.Process(2);
            Assert.AreEqual(3, output);
        }
예제 #15
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);
        }
예제 #16
0
        public void DoesNotUpdateUntilDataUpdatedOnBothSides()
        {
            var left      = new Identity("left");
            var right     = new Identity("right");
            var composite = new CompositeIndicator <IndicatorDataPoint>(left, right, (l, r) => l.Current.Value + r.Current.Value);

            // Base value assertion
            Assert.AreEqual(0m, composite.Current.Value);

            // Update one side, should still be base value
            left.Update(Today, 1m);
            Assert.AreEqual(0m, composite.Current.Value);

            // Update the other side, should be updated
            right.Update(Today, 1m);
            Assert.AreEqual(2m, composite.Current.Value);
        }
예제 #17
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(year: 2010, month: 01, day: 01); //Set Start Date
            SetEndDate(year: 2013, month: 12, day: 31);   //Set End Date
            SetCash(startingCash: 100000);                //Set Strategy Cash

            // Set your Intrinino user and password.
            IntrinioConfig.SetUserAndPassword(_user, _password);

            // 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.
            _uso = AddEquity("USO", Resolution.Daily, leverage: 2m).Symbol;
            _bno = AddEquity("BNO", Resolution.Daily, leverage: 2m).Symbol;

            AddData <IntrinioEconomicData>(IntrinioEconomicDataSources.Commodities.CrudeOilWTI, Resolution.Daily);
            AddData <IntrinioEconomicData>(IntrinioEconomicDataSources.Commodities.CrudeOilBrent, Resolution.Daily);
            _spread = _brent.Minus(_wti);
        }
예제 #18
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(2010, 01, 01); //Set Start Date
            SetEndDate(2013, 12, 31);   //Set End Date
            SetCash(100000);            //Set Strategy Cash

            // Set your Intrinino user and password.
            IntrinioConfig.SetUserAndPassword("<YourUser>", "<YourPassword>");
            // The Intrinio user and password can be also defined in the config.json file for local backtest.

            // 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.
            _uso = AddEquity("USO", Resolution.Daily, leverage: 2m).Symbol;
            _bno = AddEquity("BNO", Resolution.Daily, leverage: 2m).Symbol;

            AddData <IntrinioEconomicData>(IntrinioEconomicDataSources.Commodities.CrudeOilWTI, Resolution.Daily);
            AddData <IntrinioEconomicData>(IntrinioEconomicDataSources.Commodities.CrudeOilBrent, Resolution.Daily);
            _spread = _brent.Minus(_wti);
        }
예제 #19
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);
        }
예제 #20
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);
        }
예제 #21
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);
        }