1- from modules import api , stream
21from datetime import datetime , timedelta
2+ from api_client import APIClient
3+ from accounts import Accounts
4+ from orders import Orders
5+ from market_data import MarketData
36
47def main ():
8+ client = APIClient () # Initialize the API client
9+ accounts_api = Accounts (client )
10+ orders_api = Orders (client )
11+ market_data_api = MarketData (client )
12+
513 # Get account numbers for linked accounts
6- print (api . accounts .get_account_numbers ().json ())
14+ print (accounts_api .get_account_numbers ().json ())
715
816 # Get positions for linked accounts
9- print (api . accounts .get_all_accounts ().json ())
17+ print (accounts_api .get_all_accounts ().json ())
1018
1119 # Get specific account positions
12- print (api . accounts .get_account (fields = "positions" ).json ())
20+ print (accounts_api .get_account (fields = "positions" ).json ())
1321
1422 # 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 ())
23+ print (orders_api .get_orders (3000 , datetime .now () - timedelta (days = 7 ), datetime .now ()).json ())
1624
17- # Place an order (uncomment to test )
25+ # Example to place an order (commented out for safety )
1826 """
19- order = {
27+ order_details = {
2028 "orderType": "LIMIT",
2129 "session": "NORMAL",
2230 "duration": "DAY",
@@ -26,64 +34,55 @@ def main():
2634 {"instruction": "BUY", "quantity": 1, "instrument": {"symbol": "INTC", "assetType": "EQUITY"}}
2735 ]
2836 }
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))
37+ order_response = orders_api.place_order('account_hash', order_details)
38+ print(f"Place order response: {order_response.json()}")
39+ order_id = order_response.headers.get('location', '/').split('/')[-1]
3940 """
4041
41- # Replace specific order
42- # api.orders.replace_order(order_id, order )
42+ # Get a specific order
43+ # print(orders_api.get_order('account_hash', order_id).json() )
4344
4445 # 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+ print (orders_api . get_orders (3000 , datetime .now () - timedelta (days = 7 ), datetime .now ()).json ())
4647
4748 # Get all transactions for an account
48- print (api . transactions . get_transactions ( datetime .now () - timedelta (days = 7 ), datetime .now (), "TRADE" ).json ())
49+ print (accounts_api . get_account_transactions ( 'account_hash' , datetime .now () - timedelta (days = 7 ), datetime .now (), "TRADE" ).json ())
4950
5051 # Get user preferences for an account
51- print (api .user_preference .get_user_preference ().json ())
52+ print (accounts_api .get_user_preferences ('account_hash' ).json ())
53+
54+ # Market-data-related requests
55+ quotes = market_data_api .Quotes (market_data_api )
56+ options = market_data_api .Options (market_data_api )
57+ price_history = market_data_api .PriceHistory (market_data_api )
58+ movers = market_data_api .Movers (market_data_api )
59+ market_hours = market_data_api .MarketHours (market_data_api )
60+ instruments = market_data_api .Instruments (market_data_api )
5261
5362 # Get a list of quotes
54- print (api . quotes .get_list (["AAPL" , "AMD" ]).json ())
63+ print (quotes .get_list (["AAPL" , "AMD" ]).json ())
5564
5665 # Get a single quote
57- print (api . quotes .get_single ("INTC" ).json ())
66+ print (quotes .get_single ("INTC" ).json ())
5867
5968 # Get an option expiration chain
60- print (api . options .get_expiration_chain ("AAPL" ).json ())
69+ print (options .get_chains ("AAPL" ).json ())
6170
6271 # Get movers for an index
63- print (api . movers .get_movers ("$DJI" ).json ())
72+ print (movers .get_movers ("$DJI" ).json ())
6473
6574 # Get market hours for symbols
66- print (api . market_hours .get_by_markets ("equity,option" ).json ())
75+ print (market_hours .by_markets ("equity,option" ).json ())
6776
6877 # Get market hours for a market
69- print (api . market_hours .get_by_market ("equity" ).json ())
78+ print (market_hours .by_market ("equity" ).json ())
7079
7180 # Get instruments for a symbol
72- print (api . instruments .get_by_symbol ("AAPL" , "search" ).json ())
81+ print (instruments .by_symbol ("AAPL" , "search" ).json ())
7382
7483 # 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- """
84+ print (instruments .by_cusip ("037833100" ).json ()) # 037833100 = AAPL
8385
8486if __name__ == '__main__' :
85- print ("Welcome to the unofficial Schwab API interface!\n GitHub: 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
87+ print ("Welcome to the unofficial Schwab API interface!\n GitHub: https://github.com/Patch-Code-Prosperity/Pythonic-Schwab-API" )
88+ main ()
0 commit comments