Skip to content

Commit be06e4b

Browse files
committed
sandbox option via Schwab API Sandbox repo
1 parent 1c8b045 commit be06e4b

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.idea/
22
__pycache__/
33
.env
4-
token_data.json
4+
token_data.json
5+
custom/*
6+
tmp/*

config.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@
22
from dotenv import load_dotenv
33

44
load_dotenv()
5+
SANDBOX = False
56

67

78
class APIConfig:
8-
API_BASE_URL = "https://api.schwabapi.com"
9-
TRADER_BASE_URL = f"{API_BASE_URL}/trader/v1"
10-
ACCOUNTS_BASE_URL = f"{TRADER_BASE_URL}/accounts"
11-
MARKET_DATA_BASE_URL = f"{API_BASE_URL}/marketdata/v1"
12-
ORDERS_BASE_URL = ACCOUNTS_BASE_URL
13-
STREAMER_INFO_URL = f"{API_BASE_URL}/streamer-info"
9+
if SANDBOX:
10+
API_BASE_URL = "http://localhost:4020"
11+
TRADER_BASE_URL = API_BASE_URL
12+
ACCOUNTS_BASE_URL = f"{API_BASE_URL}/accounts"
13+
MARKET_DATA_BASE_URL = f"{API_BASE_URL}/marketdata"
14+
ORDERS_BASE_URL = ACCOUNTS_BASE_URL
15+
STREAMER_INFO_URL = f"{API_BASE_URL}/streamer-info"
16+
else:
17+
API_BASE_URL = "https://api.schwabapi.com"
18+
TRADER_BASE_URL = f"{API_BASE_URL}/trader/v1"
19+
ACCOUNTS_BASE_URL = f"{TRADER_BASE_URL}/accounts"
20+
MARKET_DATA_BASE_URL = f"{API_BASE_URL}/marketdata/v1"
21+
ORDERS_BASE_URL = ACCOUNTS_BASE_URL
22+
STREAMER_INFO_URL = f"{API_BASE_URL}/streamer-info"
1423
REQUEST_TIMEOUT = 30 # Timeout for API requests in seconds
1524
RETRY_STRATEGY = {
1625
'total': 3, # Total number of retries to allow

main.py

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,33 @@ async def main_stream():
3434

3535
stream_client.stop()
3636

37+
3738
def main():
3839
client = APIClient() # Initialize the API client
3940
accounts_api = Accounts(client)
4041
orders_api = Orders(client)
4142

4243
# Get account numbers for linked accounts
43-
# print(accounts_api.get_account_numbers()) # working
44+
client.account_numbers = accounts_api.get_account_numbers() # working
45+
print(client.account_numbers)
4446

4547
# Get positions for linked accounts
46-
# print(accounts_api.get_all_accounts()) # working
48+
print(accounts_api.get_all_accounts()) # working
4749

48-
sample_account = client.account_numbers[0]
49-
account_hash = sample_account['accountHash']
50+
sample_account = client.account_numbers[0] # working
51+
print(sample_account)
52+
account_hash = sample_account['hashValue'] # working
53+
print(account_hash)
5054

5155
# Get specific account positions
52-
# print(accounts_api.get_account(fields="positions"))
56+
print(accounts_api.get_account(account_hash=account_hash, fields="positions"))
5357

5458
# Get up to 3000 orders for an account for the past week
55-
print(orders_api.get_orders(3000, datetime.now() - timedelta(days=7), datetime.now()).json())
59+
print(orders_api.get_orders(account_hash=account_hash,
60+
max_results=3000,
61+
from_entered_time=datetime.now() - timedelta(days=7),
62+
to_entered_time=datetime.now())
63+
)
5664

5765
# Example to place an order (commented out for safety)
5866
"""
@@ -75,11 +83,16 @@ def main():
7583
# print(orders_api.get_order('account_hash', order_id).json())
7684

7785
# Get up to 3000 orders for all accounts for the past week
78-
print(orders_api.get_orders(account_hash=account_hash, max_results=3000, from_entered_time=datetime.now() - timedelta(days=7), to_entered_time=datetime.now()))
86+
for account in client.account_numbers:
87+
account_hash = account['hashValue']
88+
print(orders_api.get_orders(account_hash=account_hash, max_results=3000, from_entered_time=datetime.now() - timedelta(days=7), to_entered_time=datetime.now()))
7989

8090
# Get all transactions for an account
81-
print(accounts_api.get_account_transactions('account_hash', datetime.now() - timedelta(days=7), datetime.now(),
82-
"TRADE").json())
91+
print(accounts_api.get_account_transactions(account_hash=account_hash,
92+
start_date=datetime.now() - timedelta(days=7),
93+
end_date=datetime.now(),
94+
types="TRADE")
95+
)
8396

8497
# Market-data-related requests
8598
quotes = Quotes(client)
@@ -98,6 +111,9 @@ def main():
98111
# Get an option expiration chain
99112
print(options.get_chains("AAPL").json())
100113

114+
# Get price history for a symbol
115+
print(price_history.by_symbol("AAPL", period_type="day", period=1, frequency_type="minute", frequency=5).json())
116+
101117
# Get movers for an index
102118
print(movers.get_movers("$DJI").json())
103119

@@ -117,6 +133,6 @@ def main():
117133
if __name__ == '__main__':
118134
print("Welcome to the unofficial Schwab API interface!\n"
119135
"GitHub: https://github.com/Patch-Code-Prosperity/Pythonic-Schwab-API")
120-
loop = get_event_loop()
121-
loop.run_until_complete(main_stream())
122-
# main()
136+
# loop = get_event_loop()
137+
# loop.run_until_complete(main_stream())
138+
main()

0 commit comments

Comments
 (0)