Hi

I am trying to do local options backtesting. I have successfully imported options data. I have added options in initialize method. After that, I hope to get options data in slices. But the option chain in the slice is empty.

 

If I check optionchainprovider it is returning 300+ contracts. 

  • How can I get data in a slice? 
  • What is the appropriate way to add contract returned from option chain provider?
  • What I also notice is contract.ID.Symbol is same as underlying. Why would it be so?

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QuantConnect.Data;
using QuantConnect.Indicators;
using QuantConnect.Interfaces;
using QuantConnect.Brokerages;
using QuantConnect.Data.Market;
using QuantConnect.Orders;


namespace QuantConnect.Algorithm.CSharp
{
    class Straddle : QCAlgorithm
    {
        private IEnumerable<Symbol> _options_contract_list;
        private Symbol _nifty50;
        public override void Initialize()
        {
           
            
            SetStartDate(2020,1, 1);
            SetEndDate(2020, 1, 31);
            SetAccountCurrency(Currencies.INR);
            SetCash(100000);
            SetTimeZone(TimeZones.Kolkata);
            _nifty50 = AddIndex("NIFTY50", Resolution.Minute, Market.India).Symbol;
            SetBenchmark(_nifty50);

            var _niftyOptions = AddIndexOption(_nifty50, Resolution.Minute,Market.India);
            _niftyOptions.SetFilter(filterFunc => filterFunc.Strikes(-2, +2)
                                                            .Expiration(0,6)
            );
            //Schedule.On(DateRules.EveryDay(), TimeRules.BeforeMarketClose(_nifty50), () => { });
            Schedule.On(DateRules.EveryDay()  , TimeRules.AfterMarketOpen(_nifty50), () =>{ everyDayAfterOpen(); });
            
        }
        void everyDayAfterOpen()
        {
            _options_contract_list = OptionChainProvider.GetOptionContractList(_nifty50, Time.Date);
            Log("___________________________________________________________________");
            Log($"Following contracts were found for {Time.Date}");
            foreach (var contract in _options_contract_list)
            {
                if  ((contract.ID.Date-Time.Date).TotalDays<7 ) {
                    //Log($"{Time} adding Underlying :{contract.Underlying.ID.Symbol}, Strike:{contract.ID.StrikePrice}, expiry:{contract.ID.Date}, Right:{(contract.ID.OptionRight == OptionRight.Call ? 'C' : 'P')} ");
                    //AddOption(contract.ID.Symbol);
                }
            }
        }


        public override void OnData(Slice slice)
        {
           

            OptionChain chain;
            if (slice.OptionChains.TryGetValue(_nifty50, out chain))
            {
                var atmContract = chain
                        .OrderByDescending(x => x.Expiry)
                        .ThenBy(x => Math.Abs(chain.Underlying.Price - x.Strike))
                        .ThenByDescending(x => x.Right)
                        .FirstOrDefault();

                if (atmContract != null)
                {
                    // if found, trade it
                    MarketOrder(atmContract.Symbol, 1);
                    MarketOnCloseOrder(atmContract.Symbol, -1);
                }
            }
                var _options_contract_list=OptionChainProvider.GetOptionContractList(_nifty50, Time.Date);
            
        }

    }
}

Author