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);
}
}
}
Varad Kabade
Hi Amol,
In order to access the contracts in the option chain, we need to provide the option chain Symbol instead of the index one:
Best,
Varad Kabade
Amol gupta
Thanks. That helped.
Amol gupta
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!