Option

class ally.Ally(params=None, timeout: float = 1.0)

The ally.Ally.Ally class.

This is the main class for this library.

expirations(symbol, useDatetime=True, block: bool = True)

Gets list of available expiration dates for a symbol.

Calls the ‘market/options/expirations.json’ endpoint to get list of all exp_dates available for some given equity.

Parameters
  • symbol – Specify the stock symbol against which to query

  • useDatetime – Specify whether to return datetime objects, or strings

  • block – Specify whether to block thread if request exceeds rate limit

Returns

List of dates (datetime obj, or string)

Raises

RateLimitException – If block=False, rate limit problems will be raised

Example

a.expirations('spy')
# [ datetime.datetime(2022, 3, 18, 0, 0), ... ]

a.expirations('spy', useDatetime = False)
# [ '2022-03-18', ... ]
search(symbol, query: List = [], fields=[], dataframe=True, block: bool = True)

Searches for all option quotes on a symbol that satisfy some set of criteria

Calls the ‘market/options/search.json’ endpoint, querying against certain parameters provided. Specify a single value or a list of values to expand the size of the search.

Option queries are composed of three elements:

  1. a condition

  2. an operator

  3. a value

in the format field-operator:value (i.e., xyear-eq:2012)

Queryable Fields:

strikeprice: possible values: 5 or 7.50, integers or decimals

xdate: YYYYMMDD

xmonth: MM

xyear: YYYY

put_call: possible values: put, call

unique: possible values: strikeprice, xdate

Operators:

lt: less than

gt: greater than

gte: greater than or equal to

lte: less than or equal to

eq: equal to

Visit the ally website to see the full API behavior.

Parameters
  • symbol (str) – Specify the stock symbol against which to query

  • fields – (Optional) List of attributes requested for each option contract found. If not specified, will return all applicable fields

  • dataframe – (Optional) Return quotes in pandas dataframe

  • block (bool) – Specify whether to block thread if request exceeds rate limit

Returns

Pandas dataframe Otherwise: flat list of dictionaries

Return type

Default

Raises

RateLimitException – If block=False, rate limit problems will be raised

Example

a.search(
    'spy',
    query=[
        optionSearchQuery(condition='xdate', operator='eq', value='20200814'),    # Only consider contracts expiring on 2020-08-14
        optionSearchQuery(condition='put_call', operator='eq', value='put'),      # Only consider puts
        optionSearchQuery(condition='strikeprice', operator='lte', value='350'),  # Only consider strikes <= 350
        optionSearchQuery(condition='strikeprice', operator='gte', value='315')   # Only consider strikes <= 315
    ]
)

# Alternatively, the queries can be specified as Strings, but it is recommended to use the `optionSearchQuery` class for validation of the query
a.search(
    'spy',
    query=[
        'xdate-eq:20200814',    # Only consider contracts expiring on 2020-08-14
        'put_call-eq:put',      # Only consider puts
        'strikeprice-lte:350',  # Only consider strikes <= 350
        'strikeprice-gte:315'   # Only consider strikes >= 315
    ]
)
strikes(symbol, block: bool = True)

Gets list of available strike prices for a symbol.

Calls the ‘market/options/strikes.json’ endpoint to get list of all strikes available for some given equity.

Parameters
  • symbol – Specify the stock symbol against which to query

  • block – Specify whether to block thread if request exceeds rate limit

Returns

List of strikes (float)

Raises

RateLimitException – If block=False, rate limit problems will be raised

Example

a.strikes('spy')
# [ 5.0, 10.0, ... ]