Exemplo n.º 1
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">Slice object keyed by symbol containing the stock data</param>
        public override void OnData(Slice data)
        {
            if (IsWarmingUp)
            {
                // warm up data
                _warmedUp = true;

                if (!_continuousContract.HasData)
                {
                    throw new Exception($"ContinuousContract did not get any data during warmup!");
                }

                var backMonthExpiration  = data.Keys.Single().Underlying.ID.Date;
                var frontMonthExpiration = FuturesExpiryFunctions.FuturesExpiryFunction(_continuousContract.Symbol)(Time.AddMonths(1));
                if (backMonthExpiration <= frontMonthExpiration.Date)
                {
                    throw new Exception($"Unexpected current mapped contract expiration {backMonthExpiration}" +
                                        $" @ {Time} it should be AFTER front month expiration {frontMonthExpiration}");
                }
            }
            if (data.Keys.Count != 1)
            {
                throw new Exception($"We are getting data for more than one symbols! {string.Join(",", data.Keys.Select(symbol => symbol))}");
            }

            if (!Portfolio.Invested)
            {
                Buy(_continuousContract.Symbol, 1);
            }
        }