Skip to main content

Python 3 中的非官方 Schwab API 包装器。

项目描述

施瓦布 API

这不是官方 API,甚至不是 Charles Schwab API 的稳定再造。功能可能会随着 Schwab 所做的任何更新而改变。

该软件包支持在嘉信理财上以编程方式买卖证券。目前,我们使用无头浏览器自动登录以获取授权 cookie。所有其他功能都是通过向 Schwab 自己的 API 发出的 Web 请求来完成的。

现场演示

我目前正在使用此软件包在 Schwab 上使用我的网站进行交易。

截屏

贡献

我绝对喜欢贡献;作为刚接触开源的人,我也希望能帮助我为 PR 建立一个可靠的系统 :)

入门

安装

使用 pypi 安装,然后下载并安装 playwright 二进制文件:

pip install schwab-api
python -m playwright install

创建 TOTP 身份验证令牌

为了无需每次都通过 SMS 验证即可登录 Schwab,我们可以创建一个身份验证令牌 (TOTP) 并将其附加到我们的帐户。

如果您想要一种简单的方法来执行此操作,您可以访问我的网站并通过单击“生成 TOTP”并按照说明在其中生成 TOTP。

或者,您可以以编程方式执行此操作:

from schwab_api import generate_totp

symantec_id, totp_secret = generate_totp()

print("Your symantec ID is: " + symantec_id)
print("Your TOTP secret is: " + totp_secret)

对于 TOTP 秘密:

  1. 下载 Duo Mobile、Google Authenticator 或您选择的任何其他身份验证器,并使用 TOTP 密钥创建一个条目。每次登录 Schwab 时,系统都会提示您生成代码,并且需要验证器应用程序来执行此操作。
  2. 将此 TOTP 机密保存在手边,因为您需要将其传递给此 API 才能登录。

对于赛门铁克 ID:

  1. 登录 Schwab 并前往您的安全中心。
  2. 在两步验证下,选择“Always at login”,然后选择“Security Token”。
  3. 在此处输入您使用上述代码生成的 symantec ID。

快速开始

下面是一些登录、获取所有账户持有量并进行股票购买的代码:

from schwab_api import Schwab
import pprint

# Initialize our schwab instance
api = Schwab()

# Login using playwright
print("Logging into Schwab")
logged_in = api.login(
    username=username,
    password=password,
    totp_secret=totp_secret # Get this by generating TOTP at https://itsjafer.com/#/schwab
)

# Get information about all accounts holdings
print("Getting account holdings information")
account_info = api.get_account_info()
pprint.pprint(account_info)

print("The following account numbers were found: " + str(account_info.keys()))

print("Placing a dry run trade for AAPL stock")
# Place a dry run trade for each account
messages, success = api.trade(
    ticker="AAPL", 
    side="Buy", #or Sell
    qty=1, 
    account_id=99999999, # Replace with your account number
    dry_run=True # If dry_run=True, we won't place the order, we'll just verify it.
)

print("The order verification was " + "successful" if success else "unsuccessful")
print("The order verification produced the following messages: ")
pprint.pprint(messages)

特征

  • 买卖股票代码
  • 多个个人账户支持
  • MFA 和 TOTP 身份验证
  • 账户和头寸信息
  • 无头剧作家实现

去做

  • 目前,我们使用无头浏览器登录 Schwab;将来,我们希望纯粹通过请求来做到这一点。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。

源分布

schwab_api-0.2.2.tar.gz (7.9 kB 查看哈希)

已上传 source