Repository: OpenBB-finance/OpenBBTerminal
File: openbb_sdk/providers/intrinio/openbb_intrinio/models/options_chains.py (Python)

Code before:
        )
        urls = []

        for expiration in expirations:
            urls.append(
                IntrinioOptionsChainsFetcher.generate_url(
                    symbol, expiration, date, api_key
                )
            )

        results = IntrinioOptionsChainsFetcher.download_json_files(urls)

        # Error handling for when data from the current date is not available yet.
        if len(results) == 0:
            date = pd.to_datetime(date) + timedelta(days=-1)
            for expiration in expirations:
                urls.append(
                    IntrinioOptionsChainsFetcher.generate_url(
                        symbol, expiration, date, api_key
                    )
                )
            results = IntrinioOptionsChainsFetcher.download_json_files(urls)

        df = pd.DataFrame()

        for i in range(0, len(results)):
            chains = results[i]

            contract_symbol: List[str] = []
            expiry: List[str] = []
            strikes: List[float] = []
            option_type: List[str] = []
            close: List[float] = []
            close_bid: List[float] = []
            close_ask: List[float] = []
            volume: List[int] = []
            open: List[float] = []
            open_bid: List[float] = []
            open_ask: List[float] = []
            open_interest: List[int] = []
            high: List[float] = []
            low: List[float] = []
            mark: List[float] = []
            ask_high: List[float] = []
            ask_low: List[float] = []
            bid_high: List[float] = []
            bid_low: List[float] = []
            implied_volatility: List[float] = []
            delta: List[float] = []
            gamma: List[float] = []
            theta: List[float] = []

Review comment (performance):
This function has n^2 complexity and can be optimized. Hardcoding can be avoided.

Code after:
                close.append(item["prices"]["close"])
                close_bid.append(item["prices"]["close_bid"])
                close_ask.append(item["prices"]["close_ask"])
                volume.append(item["prices"]["volume"])
                open.append(item["prices"]["open"])
                open_bid.append(item["prices"]["open_bid"])
                open_ask.append(item["prices"]["open_ask"])
                open_interest.append(item["prices"]["open_interest"])
                high.append(item["prices"]["high"])
                low.append(item["prices"]["low"])
                mark.append(item["prices"]["mark"])
                ask_high.append(item["prices"]["ask_high"])
                ask_low.append(item["prices"]["ask_low"])
                bid_high.append(item["prices"]["bid_high"])
                bid_low.append(item["prices"]["bid_low"])
                implied_volatility.append(item["prices"]["implied_volatility"])
                delta.append(item["prices"]["delta"])
                gamma.append(item["prices"]["gamma"])
                theta.append(item["prices"]["theta"])
                vega.append(item["prices"]["vega"])
                eod_date.append(item["prices"]["date"])

            data = pd.DataFrame()
            data["contract_symbol"] = contract_symbol
            data["expiration"] = expiry
            data["strike"] = strikes
            data["option_type"] = option_type
            data["close"] = close
            data["close_bid"] = close_bid
            data["close_ask"] = close_ask
            data["volume"] = volume
            data["open"] = open
            data["open_bid"] = open_bid
            data["open_ask"] = open_ask
            data["open_interest"] = open_interest
            data["high"] = high
            data["low"] = low
            data["mark"] = mark
            data["bid_high"] = bid_high
            data["ask_high"] = ask_high
            data["bid_low"] = bid_low
            data["ask_low"] = ask_low
            data["implied_volatility"] = implied_volatility
            data["delta"] = delta
            data["gamma"] = gamma
            data["theta"] = theta
            data["vega"] = vega
            data["eod_date"] = eod_date

            data = data.set_index(["expiration", "strike", "option_type"]).sort_index()