wallstreet Python Library

repository·master·Indexed 23 days ago

https://github.com/mcdallas/wallstreet

A Python 3 library for real-time monitoring and analysis of stocks and options. It provides tools to retrieve quotes via Google Finance or Yahoo Finance, download historical data as pandas DataFrames, and calculate option Greeks (Delta, Gamma, Vega, Theta, Rho) and implied volatility using the Black-Scholes model and US Treasury risk-free rates.

Tokens
1.7K
Snippets
5
Records
15
Agent score
81%

What's inside wallstreet

  1. How Call and Put options work in Wallstreet

    master

    The Option hierarchy (base Option class with Call and Put subclasses) follows a specific lifecycle:

    1. Initialization: You provide the underlying ticker and an expiration date. The object fetches the available expiration dates and the option chain for that date from Yahoo Finance.
    2. Strike Selection: An Option object initially contains the entire chain but no specific contract data. You must call .set_strike(val) to select a specific contract.
    3. Data Hydration: Calling .set_strike() populates the contract-specific fields (bid, ask, volume, etc.) and initializes a BlackandScholes object used for calculating Greeks.
    4. Access: Only after step 2 can you access market data or Greeks. Attempting to access them earlier will raise an AttributeError: Use set_strike() method first due to the @strike_required decorator.
  2. Use Yahoo Finance as a data source

    master

    By default, Wallstreet uses the Google Finance API. You can switch to Yahoo Finance by passing source='yahoo' to the Stock or Call constructors. Note that Yahoo Finance quotes may be delayed.

    from wallstreet import Stock, Call
    
    apple = Stock('AAPL', source='yahoo')
    call = Call('AAPL', strike=apple.price, source='yahoo')
  3. Download historical stock data

    master

    The Stock.historical() method allows you to download historical data as a pandas DataFrame. This requires the pandas library to be installed. Use days_back to specify the lookback period and frequency to set the interval (e.g., 'd' for daily).

    from wallstreet import Stock
    
    s = Stock('BTC-USD')
    df = s.historical(days_back=30, frequency='d')
    print(df)
  4. Analyze options with Call and Put

    master

    Use Call or Put classes to analyze option contracts. You can specify the ticker, expiration date (using d, m, y arguments), and strike.

    Key features:

    • Access the underlying stock via the .underlying attribute.
    • Calculate Greeks: .delta(), .gamma(), .vega(), .theta(), .rho().
    • Calculate .implied_volatility().
    • Use .set_strike() to update the strike price of an existing option object.
    • Use .expirations to see available expiration dates.
    • Use .strikes to see available strike prices.
  5. Reference: Option attributes and methods

    master

    The following attributes and methods are available on Call and Put objects:

    Attributes:
    - strike
    - expiration
    - underlying  (underlying stock object)
    - ticker
    - bid
    - ask
    - price (option price)
    - id
    - exchange
    - change  (in currency)
    - cp      (percentage change)
    - volume
    - open_interest
    - code
    
    Methods/Properties:
    - expirations (list of possible expiration dates)
    - strikes (list of possible strike prices)
    - set_strike()
    - implied_volatility()
    - delta()
    - gamma()
    - vega()
    - theta()
    - rho()
  6. Initialize and use Call and Put options

    master

    The Call and Put classes allow you to access option chain data for a specific underlying stock. When initializing, you must provide the ticker, and optionally the expiration date (day, month, year).

    Important: After initializing an option, you must call .set_strike(strike_price) before accessing market data (bid, ask, price) or Greeks (delta, gamma, etc.). If a strike is not found, the class will attempt to use the closest available strike unless strict=True is passed.

  7. Use the BlackandScholes class for option pricing and Greeks

    master

    The BlackandScholes class implements the Black-Scholes model to calculate option prices, implied volatility, and various Greeks (Delta, Gamma, Vega, Theta, Rho).

    Initialization Parameters:

    • S: Current stock price
    • K: Strike price
    • T: Time to expiration (in years)
    • price: Current market price of the option (used to calculate implied volatility)
    • r: Risk-free interest rate
    • option: Either 'Call' or 'Put'
    • q: Dividend yield (defaults to 0)

    Upon initialization, the class automatically calculates the impvol (implied volatility) attribute.

  8. Initialize and use the Stock class

    master

    The Stock class provides real-time data for a specific ticker symbol. By default, it uses Yahoo Finance as the data source. You can access the current price, ticker name, and historical data.

    To get historical data, use the .historical() method, which returns a pandas DataFrame. This requires pandas to be installed.

  9. Import Stock, Call, and Put classes

    master

    The wallstreet package provides high-level classes for interacting with stock and option data. You can import the core classes directly from the top-level package:

    • Stock: Represents real-time stock data and attributes.
    • Call: Represents real-time call option data and methods.
    • Put: Represents real-time put option data and methods.