itsjafer / schwab-api

A python library for placing trades on Charles Schwab
MIT License
201 stars 64 forks source link

increase default time out to 120s and append explanation for order_v2 buy fail #41

Closed cnMuggle closed 7 months ago

cnMuggle commented 7 months ago
  1. solve time out error below
    logged_in = api_inst.login(
    File "../../git/schwab-api/schwab_api/authentication.py", line 171, in login
    self.page.goto("https://www.schwab.com/")
    File "../lib/python3.9/site-packages/playwright/sync_api/_generated.py", line 9312, in goto
    self._sync(
    File "../lib/python3.9/site-packages/playwright/_impl/_sync_base.py", line 115, in _sync
    return task.result()
    File "../lib/python3.9/site-packages/playwright/_impl/_page.py", line 475, in goto
    return await self._main_frame.goto(**locals_to_params(locals()))
    File "../lib/python3.9/site-packages/playwright/_impl/_frame.py", line 139, in goto
    await self._channel.send("goto", locals_to_params(locals()))
    File "../lib/python3.9/site-packages/playwright/_impl/_connection.py", line 62, in send
    return await self._connection.wrap_api_call(
    File "../lib/python3.9/site-packages/playwright/_impl/_connection.py", line 492, in wrap_api_call
    return await cb()
    File "../lib/python3.9/site-packages/playwright/_impl/_connection.py", line 100, in inner_send
    result = next(iter(done)).result()
    playwright._impl._errors.TimeoutError: Timeout 30000ms exceeded.
  2. the setup of default valid_return_codes ={0,10} may result in order_v2 buy fail but didn't tell why it failed (sell is successful and is not impacted).
valid_return_codes (set) - Schwab returns an orderReturnCode in the response to both
                        the verification and execution requests, and it appears to be the
                        "severity" for the highest severity message.
                        Verification response messages with severity 10 include:
                            - The market is now closed. This order will be placed for the next
                              trading day
                            - You are purchasing an ETF...please read the prospectus
                            - It is your responsibility to choose the cost basis method
                              appropriate to your tax situation
                            - Quote at the time of order verification: $xx.xx
                        Verification response messages with severity 20 include at least:
                            - Insufficient settled funds (different from insufficient buying power)
                        Verification response messages with severity 25 include at least:
                            - This order is executable because the buy (or sell) limit is higher
                              (lower) than the ask (bid) price.
                        For the execution response, the orderReturnCode is typically 0 for a
                        successfully placed order.
                        Execution response messages with severity 30 include:
                            - Order Affirmation required (This means Schwab wants you to confirm
                              that you really meant to place this order as-is since something about
                              it meets Schwab's criteria for requiring verification. This is
                              usually analogous to a checkbox you would need to check when using
                              the web interface)

The issue can be resolved by forcing the below code in the screenshot

and dry_run

image but it's not desired. It's better to specify the type of valid_return_codes as example code below.

messages, success = api.trade_v2(    
                ticker="AAPL", 
                side='Buy', #or Sell
                qty=sharesNum, 
                account_id=account_id, # Replace with your account number
                dry_run=False, # If dry_run=True, we won't place the order, we'll just verify it.                
                valid_return_codes = {0,10,20}
                )

Before this code update, it's not obvious why this buy fail happened. So this fix add this message and also append the missing messages from dry_run to final order place.

cnMuggle commented 7 months ago

still has some issues with timeout, cancel PR first