示例#1
0
        /// <summary>
        /// Updates this alpha model with the latest data from the algorithm.
        /// This is called each time the algorithm receives data for subscribed securities
        /// </summary>
        /// <param name="algorithm">The algorithm instance</param>
        /// <param name="data">The new data available</param>
        /// <returns>The new insights generated</returns>
        public override IEnumerable <Insight> Update(QCAlgorithmFramework algorithm, Slice data)
        {
            if (_mean?.IsReady != true)
            {
                return(Enumerable.Empty <Insight>());
            }

            // don't re-emit the same direction
            if (_state != State.LongRatio && _ratio > _upperThreshold)
            {
                _state = State.LongRatio;

                // asset1/asset2 is more than 2 std away from mean, short asset1, long asset2
                var shortAsset1 = Insight.Price(_asset1, TimeSpan.FromMinutes(15), InsightDirection.Down);
                var longAsset2  = Insight.Price(_asset2, TimeSpan.FromMinutes(15), InsightDirection.Up);

                // creates a group id and set the GroupId property on each insight object
                return(Insight.Group(shortAsset1, longAsset2));
            }

            // don't re-emit the same direction
            if (_state != State.ShortRatio && _ratio < _lowerThreshold)
            {
                _state = State.ShortRatio;

                // asset1/asset2 is less than 2 std away from mean, long asset1, short asset2
                var longAsset1  = Insight.Price(_asset1, TimeSpan.FromMinutes(15), InsightDirection.Up);
                var shortAsset2 = Insight.Price(_asset2, TimeSpan.FromMinutes(15), InsightDirection.Down);

                // creates a group id and set the GroupId property on each insight object
                return(Insight.Group(longAsset1, shortAsset2));
            }

            return(Enumerable.Empty <Insight>());
        }
            /// <summary>
            /// Gets the insights group for the pair
            /// </summary>
            /// <returns>Insights grouped by an unique group id</returns>
            public IEnumerable <Insight> GetInsightGroup()
            {
                if (!_mean.IsReady)
                {
                    return(Enumerable.Empty <Insight>());
                }

                // don't re-emit the same direction
                if (_state != State.LongRatio && _ratio > _upperThreshold)
                {
                    _state = State.LongRatio;

                    // asset1/asset2 is more than 2 std away from mean, short asset1, long asset2
                    var shortAsset1 = Insight.Price(_asset1, _predictionInterval, InsightDirection.Down);
                    var longAsset2  = Insight.Price(_asset2, _predictionInterval, InsightDirection.Up);

                    // creates a group id and set the GroupId property on each insight object
                    return(Insight.Group(shortAsset1, longAsset2));
                }

                // don't re-emit the same direction
                if (_state != State.ShortRatio && _ratio < _lowerThreshold)
                {
                    _state = State.ShortRatio;

                    // asset1/asset2 is less than 2 std away from mean, long asset1, short asset2
                    var longAsset1  = Insight.Price(_asset1, _predictionInterval, InsightDirection.Up);
                    var shortAsset2 = Insight.Price(_asset2, _predictionInterval, InsightDirection.Down);

                    // creates a group id and set the GroupId property on each insight object
                    return(Insight.Group(longAsset1, shortAsset2));
                }

                return(Enumerable.Empty <Insight>());
            }