Questions for the files GARCH.py in Option volatility trading course

Question1:for the below function:

def def initialize(context):

    """

        A function to define things to do at the start of the strategy

    """

    

    # Define assets

    context.stock = superSymbol(

        secType='FUT',

        symbol='NIFTY50',

        exchange='NSE',

        currency='INR',

        expiry='20230727',

        includeExpired=True)




I notice that the secType='FUT' is a future, but for the def get_contract function, the secType='OPT'. Could you explain why?



def get_contract(context, strike, option_type):

    option_symbol = superSymbol(

        secType='OPT',

        symbol=context.symbol,

        exchange='CBOE',

        currency='USD',

        expiry=context.expiry,

        strike=strike,

        right=option_type)




Question 2: If I want to calculate the monthly data, the below code is correct?

 data_daily = data.history(context.stock, ['open', 'high', 'low', 'close'],

                              context.lookback, '1d') 



    # Create 'ohlc_dict' dictionary

    ohlc_dict = {'open': 'first', 'high': 'max', 'low': 'min', 'close': 'last'}

    monthly_data = data_daily.resample('M', offset="-1M").agg(ohlc_dict)




Question 3: if I want to add one more condition that the last of 7 days on each month would not be traded, how can I add in def enter for the below Bold condition? 

def enter(context, data):

    forecast = context.forecasted_volatility

    forecast_m = context.forecasted_volatility_m

    context.atm_call = get_contract(context, context.atm_strike, 'C')

    context.atm_put = get_contract(context, context.atm_strike, 'P')



    # Current ATM call IV

    call_atm_iv = get_option_info(context.atm_call, "impliedVol")



    # Current ATM put IV

    put_atm_iv = get_option_info(context.atm_put, "impliedVol")



    # Condition for trading long straddle

    if (forecast > call_atm_iv) and (forecast > put_atm_iv):

        print(f"{get_datetime()} Entering long straddle")

        close_out(context, data)




Question 4: Why the schedule_function for close_out is days_offset=1?

    schedule_function(

            close_out, date_rules.week_end(days_offset=1), 

            time_rules.market_close(hours=0, minutes=30))



Thanks !

Hi Kevin, here are the answers to your questions. 



Question1: 

In the initialize function, we are defining a context variable context.stock that represents a futures contract (secType='FUT'). In the get_contract function, we are creating an options contract using the superSymbol function with secType='OPT' because we are dealing with options contracts.

The volatility is calculated and forecasted on futures contract and traded in options hence both assets were used. 



Question 2:

The code you provided for calculating monthly data seems to be on the right track. This code should work for calculating monthly OHLC data from daily data.



Question 3:

If you want to add a condition to prevent trading on the last 7 days of each month, you can add a check in your enter function. You can use the get_datetime function to get the current date and then check if it's within the last 7 days of the month before entering a trade. 



For example, you can calculate and store the number of days till month end from the current day and store in the days_to_month_end variable and use this as one of the conditions in the code you highlighted, as follows

if (forecast > call_atm_iv) and (forecast > put_atm_iv) and days_to_month_end > 7:
        print(f"{get_datetime()} Entering long straddle")
        close_out(context, data)

Question 4:

The schedule_function with days_offset=1 is used to schedule the close_out function to be executed on the day after the end of the trading week. This is often done to ensure that any end-of-week tasks or closing positions are executed after the end of the regular trading week. The days_offset=1 effectively schedules the function to run on the next available trading day after the regular week has ended. 



Hope this clarifies your queries, let us know if you have any further questions. 

 

Thank you for your reply. For the above questions, let me clarify a few things.



Question1: 

https://pennies.interactivebrokers.com/cstools/contract_info/v3.10/index.php

from the above IB Link, I cannot find S&P 500 Index Futures data when I am going to trade S&P 500 Index Options.



Question 3:

I wrote the below function for calculate the difference date with current day and end of the month:

def calculate_days_until_end_of_month(context):

    current_date = context.get_datetime().date()

    end_of_month = (datetime(current_date.year, current_date.month, 1) + relativedelta(months=1) - relativedelta(days=1)).date()

    days_until_end = (end_of_month - current_date).days

    return days_until_end    


 

if (forecast > call_atm_iv) and (forecast > put_atm_iv) and days_until_end     > 7:
        print(f"{get_datetime()} Entering long straddle")
        close_out(context, data)

       Are they correct?



Thank you so much

Hi Kevin, 



Question1: 



Use 'E-mini' as Description/Name and 'ES' as the symbol to get the list of all E-mini S&P 500 Futures contracts available



Question2: 



You wrote a function calculate_days_until_end_of_month(context) that correctly calculates the number of days until the end of the month. However, you need to use the result of this function in your if statement for it to work as intended. In the if statement, instead of using days_until_end, you should call the calculate_days_until_end_of_month(context) function. 



Hope this helps!

 

Thanks for your reply



actually, I don't understand why we use future of SPX to capture price information instead of using spot price of SPX. Also, I notice there are different expiry date of future of SPX. Generally, which one I should use?



Thanks!

Hi Kevin,

The futures contract of the index is used since it is tradable.

However, in this case, since positions are only taken in the options market, you can even use the spot price of the index or the index ETF (SPY). To obtain the spot price, you can use yfinance to extract data and store it in a data frame for further analysis. If you are using futures, then you can use the near-month contract, which is a continuous futures contract.

Please feel free to comment here if you have any additional questions on this.