robin_stocks

repository·master·Indexed 24 days ago

https://github.com/jmfernandes/robin_stocks

A pure Python interface for interacting with the Robinhood, Gemini, and TD Ameritrade APIs. It enables developers to view real-time stock, option, and cryptocurrency data and build automated trading algorithms. The library provides a flattened API for ease of use, including helper functions for custom GET/POST requests, authentication tools for each platform, and utilities for executing trades and exporting order history to CSV.

Tokens
5.1K
Snippets
19
Records
42
Agent score
76%

What's inside robin_stocks

  1. Overview of Robin Stocks

    master

    Robin Stocks is a pure Python interface designed to provide simple-to-use functions for interacting with the Robinhood API. It requires Python 3. The library is intended for users who want to build automated robo-investors or view real-time stock information.

    CRITICAL WARNING: The functions in this library make real-time calls to your Robinhood account. Unlike the official Robinhood mobile app, these commands do not provide warnings before executing a buy, sell, or cancel order. You are responsible for the commands you execute.

  2. Understand the robin_stocks API structure and naming conventions

    master

    The robin_stocks library is designed for ease of use through a flattened API. While the internal code follows strict explicit import patterns, the __init__.py file exports all public functions directly to the top-level package. This allows you to call functions using the robin_stocks.function_name pattern instead of navigating through submodules (e.g., robin_stocks.module.function_name).

    To help you find functions quickly using IDE auto-complete, the library uses standardized prefixes based on the action performed:

    • load: Functions that load user account information.
    • order: Functions that place orders.
    • cancel: Functions that cancel orders.
    • find: Functions that query or search for data.

    Note that the library follows specific casing conventions: snake_case for functions, camelCase for input parameters and local variables, PascalCase for classes and enums, and UPPER_SNAKE_CASE for global variables.

  3. How to call TD Ameritrade functions

    master
    While the internal implementation organizes functions under specific submodules (e.g., robin_stocks.module.function), you should call them directly using the top-level robin_stocks namespace. For example, instead of using the full internal path, simply use robin_stocks.function_name.
  4. Access Robinhood, Gemini, and TD Ameritrade APIs

    master

    The library provides separate modules for each supported API. You can import them as follows:

    • robin_stocks.robinhood for Robinhood interactions.
    • robin_stocks.gemini for Gemini cryptocurrency interactions.
    • robin_stocks.tda for TD Ameritrade interactions.
  5. Generate a TD Ameritrade encryption passcode

    master

    To secure your API credentials, robin_stocks encrypts them in a local pickle file using a passcode. You must generate this passcode once and store it securely (e.g., in a .env file) to use for all future logins.

    To generate a new passcode, use tda.generate_encryption_passcode().

    import robin_stocks.tda as tda
    passcode = tda.generate_encryption_passcode()
    print("my secret passcode is ", passcode)
  6. Import and log in to Robinhood

    master

    To use the library, import robin_stocks. It is recommended to use import robin_stocks rather than from robin_stocks import * to avoid namespace collisions.

    To authenticate, use robin_stocks.robinhood.login(username, password). If you have Multi-Factor Authentication (MFA) enabled, you can provide the MFA code programmatically using the mfa_code parameter.

  7. Install robin_stocks from source for development

    master

    If you want to modify the package or experiment with local changes, clone the repository and install it in editable mode using pip install ..

    git clone https://github.com/jmfernandes/robin_stocks.git
    cd robin_stocks
    pip install .
  8. Perform the initial TD Ameritrade API login

    master

    The tda.login_first_time() function must be called exactly once in the lifetime of your setup. It saves your authentication and refresh tokens to a pickle file in your home directory. After running this once, you should remove the code from your scripts.

    You will need:

    1. Your encryption passcode.
    2. Your application consumer key (from the TDA developer dashboard).
    3. An authorization token.
    4. A refresh token.

    Warning: Do not call this function every time you run a script. It is recommended to run it once from a Python interpreter in your terminal.

    import robin_stocks.tda as tda
    tda.login_first_time("my-encryption-passcode", "my-application-consumer-key",
        "my-authroization-token", "my-refresh-token") # ONLY CALL ME ONCE. EVER.
  9. Login with TOTP MFA

    master

    If you use Time-based One-Time Passwords (TOTP), you can automate the login process using the pyotp library.

    1. In your Robinhood account settings, enable 2FA and select "other" to get an alphanumeric secret code.
    2. Use pyotp to generate the current token using that secret.
    3. Pass the token to the mfa_code parameter in r.login().

    Note: It is highly recommended to add this secret to an authenticator app (like Google Authenticator) on your phone so you can access your account if you are away from your computer.

    import pyotp
    import robin_stocks.robinhood as r
    
    # Replace 'My2factorAppHere' with your alphanumeric secret code
    totp = pyotp.TOTP("My2factorAppHere").now()
    login = r.login('joshsmith@email.com','password', mfa_code=totp)