def get_available_option_calls(symbol,info=None): """Returns a list of all available option calls for a stock. :param symbol: The ticker of the stock. :type symbol: str :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for all calls of the stock. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ try: symbol = symbol.upper().strip() except AttributeError as message: print(message) return [None] url = urls.option_instruments() payload = { 'chain_id' : helper.id_for_chain(symbol), 'state' : 'active', 'tradability' : 'tradable', 'type' : 'call'} data = helper.request_get(url,'pagination',payload) return(helper.filter(data,info))
def find_tradable_options_for_stock(symbol,optionType='both',info=None): """Returns a list of all available options for a stock. :param symbol: The ticker of the stock. :type symbol: str :param optionType: Can be either 'call' or 'put' or left blank to get both. :type optionType: Optional[str] :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for all calls of the stock. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ try: symbol = symbol.upper().strip() optionType = optionType.lower().strip() except AttributeError as message: print(message) return [None] url = urls.option_instruments() if (optionType == 'call' or optionType == 'put'): payload = { 'chain_id' : helper.id_for_chain(symbol), 'state' : 'active', 'tradability' : 'tradable', 'type' : optionType} else: payload = { 'chain_id' : helper.id_for_chain(symbol), 'state' : 'active', 'tradability' : 'tradable'} data = helper.request_get(url,'pagination',payload) return(helper.filter(data,info))
def get_option_instrument_data(symbol,expirationDate,strike,optionType,info=None): """Returns the option instrument data for the stock option. :param symbol: The ticker of the stock. :type symbol: str :param expirationDate: Represents the expiration date in the format YYYY-MM-DD. :type expirationDate: str :param strike: Represents the price of the option. :type strike: str :param optionType: Can be either 'call' or 'put'. :type optionType: str :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a dictionary of key/value pairs for the stock. \ If info parameter is provided, the value of the key that matches info is extracted. """ try: symbol = symbol.upper().strip() optionType = optionType.lower().strip() except AttributeError as message: print(message) return [None] optionID= helper.id_for_option(symbol,expirationDate,strike,optionType) url = urls.option_instruments(optionID) data = helper.request_get(url) return(helper.filter(data,info))
def get_list_market_data(inputSymbols,expirationDate,info=None): """Returns a list of option market data for several stock tickers. :param inputSymbols: May be a single stock ticker or a list of stock tickers. :type inputSymbols: str or list :param expirationDate: Represents the expiration date in the format YYYY-MM-DD. :type expirationDate: str :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for all stock option market data. \ If info parameter is provided, a list of strings is returned where the strings are the value of the key that matches info. """ symbols = helper.inputs_to_set(inputSymbols) ids = [] data = [] url = urls.option_instruments() for symbol in symbols: payload = { 'chain_id' : helper.id_for_chain(symbol), 'expiration_date' : expirationDate, 'state' : 'active', 'tradability' : 'tradable'} otherData = helper.request_get(url,'pagination',payload) for item in otherData: ids.append(item['id']) for id in ids: url = urls.marketdata(id) otherData = helper.request_get(url) data.append(otherData) return(helper.filter(data,info))
def order_option_spread(direction, price, symbol, quantity, spread, timeInForce='gfd'): """Submits a limit order for an option spread. i.e. place a debit / credit spread :param direction: credit or debit spread :type direction: str :param price: The limit price to trigger a trade of the option. :type price: float :param symbol: The stock ticker of the stock to trade. :type symbol: str :param quantity: The number of options to trade. :type quantity: int :param spread: A dictionary of spread options with the following keys: \n - expirationDate: The expiration date of the option in 'YYYY-MM-DD' format.\n - strike: The strike price of the option.\n - optionType: This should be 'call' or 'put' :type spread: dict :param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \ 'gfd' = good for the day. 'ioc' = immediate or cancel. 'opg' execute at opening. :type timeInForce: Optional[str] :returns: Dictionary that contains information regarding the selling of options, \ such as the order id, the state of order (queued,confired,filled, failed, canceled, etc.), \ the price, and the quantity. """ try: symbol = symbol.upper().strip() except AttributeError as message: print(message) return None legs = [] for each in spread: optionID = helper.id_for_option(symbol, each['expirationDate'], each['strike'], each['optionType']) legs.append({'position_effect': each['effect'], 'side' : each['action'], 'ratio_quantity': 1, 'option': urls.option_instruments(optionID)}) payload = { 'account': profiles.load_account_profile(info='url'), 'direction': direction, 'time_in_force': timeInForce, 'legs': legs, 'type': 'limit', 'trigger': 'immediate', 'price': price, 'quantity': quantity, 'override_day_trade_checks': False, 'override_dtbp_checks': False, 'ref_id': str(uuid4()), } url = urls.option_orders() data = helper.request_post(url,payload, json=True) return(data)
def find_options_for_list_of_stocks_by_expiration_date(inputSymbols, expirationDate, optionType='both', info=None): """Returns a list of all the option orders that match the seach parameters :param inputSymbols: May be a single stock ticker or a list of stock tickers. :type inputSymbols: str or list :param expirationDate: Represents the expiration date in the format YYYY-MM-DD. :type expirationDate: str :param optionType: Can be either 'call' or 'put' or leave blank to get both. :type optionType: Optional[str] :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for all options of the stock that match the search parameters. \ If info parameter is provided, a list of strings is returned where the strings are the value of the key that matches info. """ symbols = helper.inputs_to_set(inputSymbols) try: optionType = optionType.lower().strip() except AttributeError as message: print(message) return [None] data = [] url = urls.option_instruments() for symbol in symbols: if (optionType == 'put' or optionType == 'call'): payload = { 'chain_id': helper.id_for_chain(symbol), 'expiration_date': expirationDate, 'state': 'active', 'tradability': 'tradable', 'rhs_tradability': 'tradable', 'type': optionType } else: payload = { 'chain_id': helper.id_for_chain(symbol), 'expiration_date': expirationDate, 'state': 'active', 'tradability': 'tradable', 'rhs_tradability': 'tradable' } otherData = helper.request_get(url, 'pagination', payload) for item in otherData: if (item['expiration_date'] == expirationDate ): # and item['rhs_tradability'] == 'tradable' data.append(item) for item in data: marketData = get_option_market_data_by_id(item['id']) item.update(marketData) return (helper.filter(data, info))
def get_list_options_of_specific_profitability(inputSymbols,expirationDate,typeProfit="chance_of_profit_short",profitFloor=0.0, profitCeiling=1.0,info=None): """Returns a list of option market data for several stock tickers that match a range of profitability. :param inputSymbols: May be a single stock ticker or a list of stock tickers. :type inputSymbols: str or list :param expirationDate: Represents the expiration date in the format YYYY-MM-DD. :type expirationDate: str :param typeProfit: Will either be "chance_of_profit_short" or "chance_of_profit_long". :type typeProfit: str :param profitFloor: The lower percentage on scale 0 to 1. :type profitFloor: int :param profitCeiling: The higher percentage on scale 0 to 1. :type profitCeiling: int :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for all stock option market data. \ If info parameter is provided, a list of strings is returned where the strings are the value of the key that matches info. """ symbols = helper.inputs_to_set(inputSymbols) ids = [] data = [] returnData = [] url = urls.option_instruments() if (typeProfit != "chance_of_profit_short" and typeProfit != "chance_of_profit_long"): print("Invalid string for 'typeProfit'. Defaulting to 'chance_of_profit_short'.") typeProfit = "chance_of_profit_short" for symbol in symbols: payload = { 'chain_id' : helper.id_for_chain(symbol), 'expiration_date' : expirationDate, 'state' : 'active', 'tradability' : 'tradable', 'rhs_tradability' : 'tradable'} otherData = helper.request_get(url,'pagination',payload) for item in otherData: if (item['tradability'] == 'tradable'): ids.append(item['id']) for id in ids: url = urls.marketdata_options(id) otherData = helper.request_get(url) data.append(otherData) for item in data: try: floatValue = float(item[typeProfit]) if (floatValue > profitFloor and floatValue < profitCeiling): returnData.append(item) except: pass return(helper.filter(returnData,info))
def order_option_by_id(option_id, price, quantity, direction='credit', effect='close', side='sell', time_in_force='gfd'): """ :param option_id: :param price: :param quantity: :param direction: :param effect: :param side: :param time_in_force: :return: """ payload = { 'account': profiles.load_account_profile(info='url'), 'direction': direction, 'time_in_force': time_in_force, 'legs': [ { 'position_effect': effect, 'side': side, 'ratio_quantity': 1, 'option': urls.option_instruments(option_id) }, ], 'type': 'limit', 'trigger': 'immediate', 'price': price, 'quantity': quantity, 'override_day_trade_checks': False, 'override_dtbp_checks': False, 'ref_id': str(uuid4()), } url = urls.option_orders() data = helper.request_post(url, payload, json=True) print(data) return data
def order_sell_option_limit(price, symbol, quantity, expirationDate, strike, optionType='both', timeInForce='gfd'): """Submits a limit order for an option. i.e. place a short call or a short put. :param price: The limit price to trigger a sell of the option. :type price: float :param symbol: The stock ticker of the stock to trade. :type symbol: str :param quantity: The number of options to sell. :type quantity: int :param expirationDate: The expiration date of the option in 'YYYY-MM-DD' format. :type expirationDate: str :param strike: The strike price of the option. :type strike: float :param optionType: This should be 'call' or 'put' :type optionType: str :param timeInForce: Changes how long the order will be in effect for. 'gtc' = good until cancelled. \ 'gfd' = good for the day. 'ioc' = immediate or cancel. 'opg' execute at opening. :type timeInForce: Optional[str] :returns: Dictionary that contains information regarding the selling of options, \ such as the order id, the state of order (queued,confired,filled, failed, canceled, etc.), \ the price, and the quantity. """ try: symbol = symbol.upper().strip() except AttributeError as message: print(message) return None optionID = helper.id_for_option(symbol,expirationDate,strike,optionType) payload = { 'account': profiles.load_account_profile(info='url'), 'direction': 'credit', 'time_in_force': timeInForce, 'legs': [ {'position_effect': 'close', 'side' : 'sell', 'ratio_quantity': 1, 'option': urls.option_instruments(optionID) }, ], 'type': 'limit', 'trigger': 'immediate', 'price': price, 'quantity': quantity, 'override_day_trade_checks': False, 'override_dtbp_checks': False, 'ref_id': str(uuid4()), } url = urls.option_orders() data = helper.request_post(url,payload, json=True) return(data)
def get_option_instrument_data_by_id(id,info=None): """Returns the option instrument information. :param id: The id of the stock. :type id: str :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a dictionary of key/value pairs for the stock. \ If info parameter is provided, the value of the key that matches info is extracted. """ url = urls.option_instruments(id) data = helper.request_get(url) return(helper.filter(data,info))
def find_options_for_stock_by_expiration_and_strike(symbol, expirationDate, strike, optionType='both', info=None): """Returns a list of all the option orders that match the seach parameters :param symbol: The ticker of the stock. :type symbol: str :param expirationDate: Represents the expiration date in the format YYYY-MM-DD. :type expirationDate: str :param strike: Represents the price of the option. :type strike: str :param optionType: Can be either 'call' or 'put' or leave blank to get both. :type optionType: Optional[str] :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for all options of the stock that match the search parameters. \ If info parameter is provided, a list of strings is returned where the strings are the value of the key that matches info. """ try: symbol = symbol.upper().strip() option_type = optionType.lower().strip() except AttributeError as message: print(message) return [None] url = urls.option_instruments() payload = { 'chain_id': helper.id_for_chain(symbol), 'expiration_date': expirationDate, 'strike_price': strike, 'state': 'active', 'tradability': 'tradable', 'rhs_tradability': 'tradable' } if option_type == 'put' or option_type == 'call': payload['type'] = option_type data = helper.request_get(url, 'pagination', payload) data = [ item for item in data if item['expiration_date'] == expirationDate and item['tradability'] == 'tradable' ] for item in data: market_data = get_option_market_data_by_id(item['id']) item.update(market_data) return helper.filter(data, info)
def find_tradable_options(symbol, expirationDate=None, strikePrice=None, optionType=None, info=None): """Returns a list of all available options for a stock. :param symbol: The ticker of the stock. :type symbol: str :param expirationDate: Represents the expiration date in the format YYYY-MM-DD. :type expirationDate: str :param strikePrice: Represents the strike price of the option. :type strikePrice: str :param optionType: Can be either 'call' or 'put' or left blank to get both. :type optionType: Optional[str] :param info: Will filter the results to get a specific value. :type info: Optional[str] :returns: Returns a list of dictionaries of key/value pairs for all calls of the stock. If info parameter is provided, \ a list of strings is returned where the strings are the value of the key that matches info. """ try: symbol = symbol.upper().strip() except AttributeError as message: print(message, file=helper.get_output()) return [None] url = urls.option_instruments() if not helper.id_for_chain(symbol): print("Symbol {} is not valid for finding options.".format(symbol), file=helper.get_output()) return [None] payload = { 'chain_id': helper.id_for_chain(symbol), 'chain_symbol': symbol, 'state': 'active' } if expirationDate: payload['expiration_dates'] = expirationDate if strikePrice: payload['strike_price'] = strikePrice if optionType: payload['type'] = optionType data = helper.request_get(url, 'pagination', payload) return (helper.filter_data(data, info))