itsjafer / schwab-api

A python library for placing trades on Charles Schwab
MIT License
213 stars 66 forks source link

margin account orders hit and miss #22

Closed MaxxRK closed 11 months ago

MaxxRK commented 11 months ago

Orders input sometimes and sometimes do not.

They are also hit or miss on selling


The middle account is a margin account and will not buy or sell.

The account after will always fail selling after the middle account fails.

Schwab 1 account ********: The order verification produced the following messages: ['This order may result in an oversold/overbought position in your account. Please check your position quantity and/or open orders.'] unsuccessful Schwab 1 account ********: The order verification produced the following messages: ['No trades are currently allowed']
unsuccessful Schwab 1 account ********: The order verification produced the following messages: ['']


Here is an example of it buying where the middle account fails. It will buy after the middle account fails in this case. Schwab 1 account ********: The order verification was successful unsuccessful Schwab 1 account ********: The order verification produced the following messages: ['No trades are currently allowed'] Schwab 1 account ********: The order verification was successful


A lot of the time they just all fail

Schwab 1 account ********: The order verification produced the following messages: [''] unsuccessful Schwab 1 account ********: The order verification produced the following messages: [''] unsuccessful Schwab 1 account ********: The order verification produced the following messages: [''] unsuccessful Schwab 1 account ********: The order verification produced the following messages: [''] unsuccessful Schwab 1 account ********: The order verification produced the following messages: ['']

itsjafer commented 11 months ago

Could you help me reproduce the problem? What are the inputs (the security being traded, the direction of the trade, the type of account, etc)

MaxxRK commented 11 months ago

Most of my accounts are just Cash accounts. I have one Margin account

The inputs looked like this for all the securities i tried today, but here are a list of the securities I was using. MCOM, OXLC, VRAX

messages, success = obj.trade_v2(ticker=s, side='Buy' (or Sell), qty=1, account_id=account, dry_run=False)

When selling the margin account would always fail. It would not fail everytime when buying. It always said this message....

Schwab 1 account ********: The order verification produced the following messages: ['No trades are currently allowed']

It was strange as it would succeed to buy and sell on most accounts some of the time, but most of the time it failed on all of them.

itsjafer commented 11 months ago

I think the most helpful thing you can do to help debug is try placing an order through the API and, if it fails, try again through the UI to see if this is a problem with the API or just Schwab limiting trades on your account in general.

itsjafer commented 11 months ago

I just pushed v0.3.5 which should fix a bug in selling when using trade_v2()

MaxxRK commented 11 months ago

I'm not sure what to do there to help? I bought those three in the UI just fine after trying the API. I have not tried the 0.3.5 fix yet. I will try it today.

Update 12/20/23: I have updated the api to 0.3.5. Today I sold SIDU, WNW on all my accounts except my margin account successfully. On the margin account the error was always this.

Schwab 1 account ********: The order verification produced the following messages: ['This order may result in an oversold/overbought position in your account. Please check your position quantity and/or open orders.', 'No trades are currently allowed']

I was able to sell in the UI just fine right after this. I did have a share to sell.

I tried to Buy 1 OXLC again today with 0.3.5 and got this on cash accounts...

unsuccessful Schwab 1 account ********: The order verification produced the following messages: ['']

and this on margin accounts...

unsuccessful Schwab 1 account ********: The order verification produced the following messages: ['No trades are currently allowed']

Update 2 12/20/23: I was able to also Buy 'GMBL' on all accounts accept my margin account as well this morning Margin account gave the same error as above.

Is there anything I can do by downloading the github and running these trades from the example? Print out some info that would be helpful?

itsjafer commented 11 months ago

I assume margin accounts have some specific parameters in the API that I haven't been able to test (since I don't have a margin account).

If it's straightforward enough for me to investigate, I'll try to get to it, otherwise I'll leave it to any other interested contributors with margin accounts to take a look at the underlying requests to see what's missing.

MaxxRK commented 11 months ago

I have determined that this is not always margin accounts and messed with it for about 3 - 4 hours yesterday. I figured out how to find out if it is a margin account or not and added that in. It however still does not work on all of my accounts especially when submitting orders with the same token.

I have found some of the extra parameters needed for margin and can get them to work if I submit an order on one account per auth token.

I confirmed that the update token on the website filling out the form updates the token on a preview and a submit each time. As in the auth token that the update_token() method updates should change each time it is called. At this time it does not and I confirmed this with print outs of the auth token each time it is called for an order.

So I believe this allows it to work on some but not all when run in a loop.

I am still investigating this and trying to figure out how to get it to change the token after the preview order and submit order requests and for each account.

WinesProof commented 11 months ago

Interesting. From a quick run-through, it looks like the web interface for orders uses a token retrieved from https://client.schwab.com/api/auth/authorize/scope/update. I think it's only good for 120s, in which case it's very possible it changes between preview/submit when using the web, and would happen every once in a while using this python package. I (or someone else) should spend more time with this before saying for sure what the most robust process is.

WinesProof commented 11 months ago

For the urls currently used by this package, I looked at the requests generated by the web interface, and I see that there are two different bearer tokens:

For the following endpoints, it uses a token retrieved from https://client.schwab.com/api/auth/authorize/scope/update, and it's good for 120s (2min):

For the following endpoints, it uses a token retrieved from https://client.schwab.com/api/auth/authorize/scope/api, and it's good for 1800s (30min):

I tried these in a copy of the repository, and it seems to work for me, but I wasn't experiencing any problems before, so I don't know if it will fix any of the issues @MaxxRK is having.

itsjafer commented 11 months ago

I tried these in a copy of the repository, and it seems to work for me, but I wasn't experiencing any problems before, so I don't know if it will fix any of the issues @MaxxRK is having.

If it's not too much overhead, I'd love to take a look and test it in the main package!

MaxxRK commented 11 months ago

I found out some information over the past couple of days.

I did figure out some differences when submitting a margin account order, but for the use case of RSA its not important. When I get around to it I will try and share my findings on that. (Need to get it all cleaned up.)

All that to say I will close this issue as it does not really have to do with margin accounts. I really appreciate all of your work on this @itsjafer, @WinesProof and anyone else. This seems to be working for me now.