QuantConnect / Documentation

QuantConnect Wiki Style Documentation Behind QuantConnect
https://www.quantconnect.com/docs/v2/
Apache License 2.0
161 stars 133 forks source link

Update Chain ETF-Option with AddUniverseOptions Example #1583

Closed jaredbroad closed 4 months ago

jaredbroad commented 5 months ago

Expected Behavior

Docs recommend the dedicated way to add option chains. We can keep the example of OptionChainProvider chaining but its kind of hacky.

Actual Behavior

Only show the OptionChainProvider method.

Checklist

namespace QuantConnect.Algorithm.CSharp
{
    public class ETFUniverseOptions : QCAlgorithm
    {
        Symbol _benchmark;
        List<DateTime> _selections = new List<DateTime>(); 

        public override void Initialize()
        {
            SetCash(1000000000);
            SetStartDate(2020, 1, 1);
            SetEndDate(2024, 1, 1);

            UniverseSettings.Resolution = Resolution.Hour;
            UniverseSettings.DataNormalizationMode = DataNormalizationMode.Raw;
            Portfolio.SetPositions(SecurityPositionGroupModel.Null);

            SetSecurityInitializer(security =>
            {
                var seeder = new FuncSecuritySeeder(GetLastKnownPrices).SeedSecurity(security);
                security.SetBuyingPowerModel(new ConstantBuyingPowerModel(1));
            });

            _benchmark = AddEquity("QQQ", dataNormalizationMode: DataNormalizationMode.Raw).Symbol;
            var etfUniverse = AddUniverse(Universe.ETF("QQQ", Market.USA, UniverseSettings, ETFConstituentsFilter));
            AddUniverseOptions(etfUniverse, OptionFilterFunction);
        }

        private IEnumerable<Symbol> ETFConstituentsFilter(IEnumerable<ETFConstituentData> constituents)
        { 
            return constituents.OrderByDescending(c => c.Weight).Take(10).Select(c => c.Symbol);
        }

        private OptionFilterUniverse OptionFilterFunction(OptionFilterUniverse optionFilterUniverse)
        { 
            return optionFilterUniverse.Strikes(0, 2).FrontMonth().CallsOnly();
        }
    }
}
AlexCatarino commented 4 months ago

Closed by 4efe7b4