Code Monkey home page Code Monkey logo

schwab-api-python's Introduction

Schwab-API-Python

PyPI - Version Discord PyPI - Downloads Donate YouTube Video Views
This is an unofficial python program to access the Schwab api.
Discord, PyPI, Youtube, Github.

Installation

pip install schwabdev requests websockets
You may need to use pip3 instead of pip

Quick setup

  1. Setup your Schwab developer account here.
    • Create a new Schwab individual developer app with callback url "https://127.0.0.1" (case sensitive)
    • Wait until the status is "Ready for use", note that "Approved - Pending" will not work.
    • Enable TOS (Thinkorswim) for your Schwab account, it is needed for orders and other api calls.
  2. Install packages
    • Install schwabdev and requirements pip install schwabdev requests websockets
    • You may need to use pip3 instead of pip
  3. Examples on how to use the client are in examples/api_demo.py | examples/stream_demo.py (add your keys in the .env file)
    • The first time you run you will have to sign in to your Schwab account using the generated link in the terminal. After signing in, agree to the terms, and select account(s). Then you will have to copy the link in the address bar and paste it into the terminal.
    • Questions? - join the Discord group.
import schwabdev #import the package

client = schwabdev.Client('Your app key', 'Your app secret')  #create a client

client.update_tokens_auto() #start the auto access token updater

print(client.account_linked().json()) #make api calls

What can this program do?

  • Authenticate and access the api
  • Functions for all api functions (examples in examples/api_demo.py)
  • Auto "access token" updates (client.update_tokens_auto())
  • Stream real-time data with customizable response handler (examples in examples/stream_demo.py)

TBD

  • Automatic refresh token updates. (Waiting for Schwab implementation)

Notes

The schwabdev folder contains code for main operations:

  • api.py contains functions relating to api calls, requests, and automatic token checker threads.
  • stream.py contains functions for streaming data from websockets.
  • color_print.py contains functions to print to the terminal with color.

Youtube Tutorials

  1. Authentication and Requests Github code has significantly changed since this video
  2. Streaming Real-time Data

License (MIT)

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

schwab-api-python's People

Contributors

jrstrunk avatar roninio avatar skytech-cyber avatar tylerebowers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

schwab-api-python's Issues

Building a basic Schwab client

Hi @tylerebowers thank you for this.

Abstract

I'm trying to build my first Schwab Python client and hitting some walls. I was very happy to stumble onto your repo, and want to use it as a stepping stone to a simple client that can fetch a token via OAuth and download a list of positions from my TDA (Schwab) account.

What I've done

I've registered and been approved for developer access, and created my first app on beta-developer.schwab.com. I added my appSecret and appKey to universe.py. The status of my app is currently Approved - Pending, like so:

(that may be the problem, not sure...)

Problems

First "problem" was I had to comment out stream from the main.py imports because the window_terminal package is only compatible with Windows and Linux (I use macOS...)

SUPPORTED_PLATFORMS = ('Windows', 'Linux')
assert (platform.system() in SUPPORTED_PLATFORMS), 'Platform not supported'

Next I was getting this somewhat cryptic error which turned out to be a red herring

-bash: ./main.py: cannot execute: required file not found

The actual problem was the main.py file was saved with Windows line-endings, which was throwing off my shell. Switching it to Unix format cleared that up.

TL;DR

So where I am now is this:

~/Schwab-API-Python ๐Ÿ v3.12.2 (schwab-api)๎‚ main!? $ ./main.py
Welcome to the unofficial Schwab api interface!
Github: https://github.com/tylerebowers/Schwab-API-Python
[ERROR]: Error in reading/writing token file, creating new token file.
[USER]: Please authorize this program to access your schwab account.
[USER]: Click to authenticate: https://api.schwabapi.com/v1/oauth/authorize?client_id=JvcExxxxxxxxxxxxxxxxxxxxxxxxxxxx&redirect_uri=https://127.0.0.1
[INFO]: Opening browser...
[INPUT]: After authorizing, wait for it to load (<1min) and paste the WHOLE url here:
(never loads)

screenshot:

Is any of this normal in your opinion?

websocket connection issue

I got errors from stream.startManual() when run following code:

if __name__ == '__main__':
    api.initialize()  # checks tokens & loads variables
    api.updateTokensAutomatic() # starts thread to update tokens automatically
    stream.startManual() # start the stream manually

The output message:

[INFO]: Access token last updated: 2024-04-18 11:57:00
[INFO]: Refresh token last updated: 2024-04-17 19:08:14
[INFO]: The access token has expired, updating automatically.
[INFO]: Access token updated: 2024-04-18 12:31:52.959456
[WARN]: Access token expires in 1799 seconds!
[WARN]: Refresh token expires in 7 days!
[INFO]: Initialization Complete
[ERROR]: can't register atexit after shutdown
[ERROR]: Stream not alive for more than 1 minute, exiting...

the streamVars.terminal pops up and instantly closed.
I checked in _start() function, everything is fine until: (all streamVars looks normal to me)

async with websockets.connect(streamVars.streamerInfo.get('streamerSocketUrl'),
                                          ping_interval=None) as streamVars.webSocket:

besides normall accessToken, do I need to apply for any special permission from Charles Schwab for websocket stream?

SSL: CERTIFICATE_VERIFY_FAILED - self-signed certificate in certificate chain error when connecting to streaming server

SSL: CERTIFICATE_VERIFY_FAILED - self-signed certificate in certificate chain error when connecting to streaming server

Issue Description

Hello,

I'm encountering an SSL certificate verification issue when trying to connect to the streaming server using the Schwab API. Here are the details:

Environment

  • OS: macOS
  • Python Version: 3.12.4
  • schwabdev Version: Latest from GitHub

Code Snippet

import schwabdev
from dotenv import load_dotenv
import os

def main():
    client = schwabdev.Client(os.getenv('app_key'), os.getenv('app_secret'), os.getenv('callback_url'))
    client.update_tokens_auto()  # update tokens automatically (except refresh token)

    # example of using the default response handler
    client.stream.start()

    client.stream.send(client.stream.level_one_equities("AMD", "0,1,2,3,4,5,6,7,8"))

    # stop the stream after 60 seconds (since this is a demo)
    import time
    time.sleep(60)
    client.stream.stop()

if __name__ == '__main__':
    print("Welcome to the unofficial Schwab interface!\nGithub: https://github.com/tylerebowers/Schwab-API-Python")
    load_dotenv()
    main() 

Error Message

[INFO]: Refresh token last updated: 2024-06-21 10:29:02 (expires in 6 days)
[INFO]: Linked Accounts:
[INFO]: Initialization Complete
[INFO]: Connecting to streaming server -> [ERROR]: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1000)
[ERROR]: Stream not alive for more than 1 minute, exiting...
[WARN]: Stream is not active, request queued.

Steps to Reproduce

Clone the repository.
Set up the environment with the required API keys and secrets.
Run the provided code snippet.

Could you please provide guidance on how to properly configure the SSL certificate validation or any workaround for this issue?

stream.py got NoneType Error

running main.py as following:

if __name__ == '__main__':
    api.initialize()  # checks tokens & loads variables
    api.updateTokensAutomatic() # starts thread to update tokens automatically
    stream.startManual() # start the stream manually

got following error:
File "\modules\stream.py", line 31, in _Start
streamVars.streamerInfo = api.userPreference.userPreference().json().get('streamerInfo', None)[0]
TypeError: 'NoneType' object is not subscriptable

I print out 'api.userPreference.userPreference().json()' got following output:
{'errors': [{'id': 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', 'status': 401, 'title': 'Unauthorized', 'detail': 'Client not authorized'}]}
again, I don't think authorization is the problem, as I can run main() with out any issue if stream.startManual() is comment out.

Move secrets to env file

I noticed you are using a hard coded key and secret in universe.py and I modified the code to use a .env file and then gitignore it so that there is no chance of accidentally committing to a repo. Would you like me submit a PR to integrate into your code base?

So instead of

class credentials:
    appKey = "Your App Key"
    appSecret = "Your App Secret"

it's now

import os
from dotenv import load_dotenv

load_dotenv()

class credentials:
    appKey = os.getenv("APP_KEY")
    appSecret = os.getenv("APP_SECRET")

and there's a .env with

APP_KEY=<key>
APP_SECRET=<secret>

And in gitignore add .env

Where is the implementation of the authentication

Hello and thank you,

Your video and github are helpful. I have been working with Python to connect to my TD account.

Your video, show a bit how you authenticate and get your token, but I am not sure if that part of the process still works. Is this authentication part of your new design? Or are you doing manually on the side?

I am still trying to find the front door...

api.orders.getOrders() does not return any output

I have run the exact code in main.py, and above function only returns None.
The setup and authorization works fine, and following codes runs perfect:

accountNumbers = api.accounts.accountNumbers()
universe.credentials.encryptedId = accountNumbers[0].get('hashValue')
I got correct accountNumbers and encryptedId.
so, what could go wrong here?

2 second timeout for requests is too agressive

I ran into a timeout today when making an API request. I see the 2-second timeout and think it is too aggressive.

Can we add a request_timeout class variable in the client and give it a longer timeout? Maybe 10 seconds?

I can create a pull request if you would like.

This is excellent work, Tyler. Thank you.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.