|
| 1 | +from modules import api, stream |
| 2 | +from datetime import datetime, timedelta |
| 3 | + |
| 4 | +def main(): |
| 5 | + # Get account numbers for linked accounts |
| 6 | + print(api.accounts.get_account_numbers().json()) |
| 7 | + |
| 8 | + # Get positions for linked accounts |
| 9 | + print(api.accounts.get_all_accounts().json()) |
| 10 | + |
| 11 | + # Get specific account positions |
| 12 | + print(api.accounts.get_account(fields="positions").json()) |
| 13 | + |
| 14 | + # Get up to 3000 orders for an account for the past week |
| 15 | + print(api.orders.get_orders(3000, datetime.now() - timedelta(days=7), datetime.now()).json()) |
| 16 | + |
| 17 | + # Place an order (uncomment to test) |
| 18 | + """ |
| 19 | + order = { |
| 20 | + "orderType": "LIMIT", |
| 21 | + "session": "NORMAL", |
| 22 | + "duration": "DAY", |
| 23 | + "orderStrategyType": "SINGLE", |
| 24 | + "price": '10.00', |
| 25 | + "orderLegCollection": [ |
| 26 | + {"instruction": "BUY", "quantity": 1, "instrument": {"symbol": "INTC", "assetType": "EQUITY"}} |
| 27 | + ] |
| 28 | + } |
| 29 | + response = api.orders.place_order(order) |
| 30 | + print(f"Place order response: {response}") |
| 31 | + order_id = response.headers.get('location', '/').split('/')[-1] |
| 32 | + print(f"OrderID: {order_id}") |
| 33 | +
|
| 34 | + # Get a specific order |
| 35 | + print(api.orders.get_order(order_id).json()) |
| 36 | +
|
| 37 | + # Cancel specific order |
| 38 | + print(api.orders.cancel_order(order_id)) |
| 39 | + """ |
| 40 | + |
| 41 | + # Replace specific order |
| 42 | + # api.orders.replace_order(order_id, order) |
| 43 | + |
| 44 | + # Get up to 3000 orders for all accounts for the past week |
| 45 | + print(api.orders.get_all_orders(3000, datetime.now() - timedelta(days=7), datetime.now()).json()) |
| 46 | + |
| 47 | + # Get all transactions for an account |
| 48 | + print(api.transactions.get_transactions(datetime.now() - timedelta(days=7), datetime.now(), "TRADE").json()) |
| 49 | + |
| 50 | + # Get user preferences for an account |
| 51 | + print(api.user_preference.get_user_preference().json()) |
| 52 | + |
| 53 | + # Get a list of quotes |
| 54 | + print(api.quotes.get_list(["AAPL", "AMD"]).json()) |
| 55 | + |
| 56 | + # Get a single quote |
| 57 | + print(api.quotes.get_single("INTC").json()) |
| 58 | + |
| 59 | + # Get an option expiration chain |
| 60 | + print(api.options.get_expiration_chain("AAPL").json()) |
| 61 | + |
| 62 | + # Get movers for an index |
| 63 | + print(api.movers.get_movers("$DJI").json()) |
| 64 | + |
| 65 | + # Get market hours for symbols |
| 66 | + print(api.market_hours.get_by_markets("equity,option").json()) |
| 67 | + |
| 68 | + # Get market hours for a market |
| 69 | + print(api.market_hours.get_by_market("equity").json()) |
| 70 | + |
| 71 | + # Get instruments for a symbol |
| 72 | + print(api.instruments.get_by_symbol("AAPL", "search").json()) |
| 73 | + |
| 74 | + # Get instruments for a CUSIP |
| 75 | + print(api.instruments.get_by_cusip("037833100").json()) # 037833100 = AAPL |
| 76 | + |
| 77 | + # Send a subscription request to the stream (uncomment if you start the stream below) |
| 78 | + """ |
| 79 | + stream.send(stream.utilities.basic_request("CHART_EQUITY", "SUBS", parameters={"keys": "AMD,INTC", "fields": "0,1,2,3,4,5,6,7,8"})) |
| 80 | + # Stop the stream after 30s |
| 81 | + stream.stop() |
| 82 | + """ |
| 83 | + |
| 84 | +if __name__ == '__main__': |
| 85 | + print("Welcome to the unofficial Schwab API interface!\nGitHub: https://github.com/tylerebowers/Schwab-API-Python") |
| 86 | + api.initialize() # checks tokens & loads variables |
| 87 | + api.update_tokens_automatically() # starts thread to update tokens automatically |
| 88 | + # stream.start_manual() # start the stream manually |
| 89 | + main() # call the user code above |
0 commit comments