Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming code update for different format in the response from schwab #33

Closed
hn4002 opened this issue May 2, 2024 · 2 comments · Fixed by #38
Closed

Streaming code update for different format in the response from schwab #33

hn4002 opened this issue May 2, 2024 · 2 comments · Fixed by #38

Comments

@hn4002
Copy link

hn4002 commented May 2, 2024

I tried to use the streaming feature from schwab-py and encountered some errors during the initialization. These are the issues I found which I fixed locally and then the streaming initialization went fine.

The below issues are because of the difference in the response from the Schwab's API as compared to the TDA API.

At https://github.com/alexgolec/schwab-py/blob/main/schwab/streaming.py#L215, it should be changed like this:

<                 if int(account['accountId']) == self._account_id:
---
>                 if int(account['accountNumber']) == self._account_id:

At https://github.com/alexgolec/schwab-py/blob/main/schwab/streaming.py#L229 , it should be changed like this:

229c229
<         stream_info = prefs['streamerInfo'][streamer_info_idx]
---
>         stream_info = prefs['streamerInfo'][0]

The reason is that Schwab API returns only one object in the streamerInfo array even though there are multiple accounts.

With the above changes, I was able to use schwab-py to start the streaming and subscribe to quote, candle and l2 data.

@hn4002
Copy link
Author

hn4002 commented May 2, 2024

This is the response from https://api.schwabapi.com/trader/v1/userPreference (response masked for sensitive information):

{
    "accounts": [
        {
            "accountNumber": "12345678",
            "primaryAccount": false,
            "type": "BROKERAGE",
            "nickName": "XXXXXXX",
            "displayAcctId": "...xxx",
            "autoPositionEffect": false,
            "accountColor": "Green"
        },
        {
            "accountNumber": "23456789",
            "primaryAccount": false,
            "type": "BROKERAGE",
            "nickName": "xxxxxxxxx",
            "displayAcctId": "...xxx",
            "autoPositionEffect": false,
            "accountColor": "Green"
        },
        {
            "accountNumber": "34567890",
            "primaryAccount": false,
            "type": "BROKERAGE",
            "nickName": "xxxxxxxxxx",
            "displayAcctId": "...xxx",
            "autoPositionEffect": false,
            "accountColor": "Blue"
        },
        {
            "accountNumber": "45678901",
            "primaryAccount": false,
            "type": "BROKERAGE",
            "nickName": "xxxxxxxx",
            "displayAcctId": "...xxx",
            "autoPositionEffect": false,
            "accountColor": "Green"
        },
        {
            "accountNumber": "56789012",
            "primaryAccount": true,
            "type": "BROKERAGE",
            "nickName": "xxxxxxxxx",
            "displayAcctId": "...xxx",
            "autoPositionEffect": false,
            "accountColor": "Green"
        }
    ],
    "streamerInfo": [
        {
            "streamerSocketUrl": "wss://streamer-api.schwab.com/ws",
            "schwabClientCustomerId": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
            "schwabClientCorrelId": "xxxxxxxx",
            "schwabClientChannel": "N9",
            "schwabClientFunctionId": "APIAPP"
        }
    ],
    "offers": [
        {
            "level2Permissions": true,
            "mktDataPermission": "NP"
        }
    ]
}

@alexgolec
Copy link
Owner

This is now fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants