Created
September 2, 2024 22:38
-
-
Save classicvalues/27e14136079a015b1f8e062d90a597b6 to your computer and use it in GitHub Desktop.
TD Ameritrade Trade -ALL ASSETS- API Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from td.client import TDClient | |
| from datetime import datetime, timedelta | |
| import time | |
| # Set up TD Ameritrade API credentials | |
| consumer_key = 'YOUR_TD_AMERITRADE_CONSUMER_KEY' | |
| redirect_uri = 'YOUR_REDIRECT_URI' | |
| credentials_path = 'path_to_credentials.json' # Path where credentials will be stored | |
| # Login to TD Ameritrade | |
| td_client = TDClient( | |
| client_id=consumer_key, | |
| redirect_uri=redirect_uri, | |
| credentials_path=credentials_path | |
| ) | |
| td_client.login() | |
| # Define the trading parameters | |
| symbol = 'GOOGL' | |
| investment_amount = 10.0 # Amount to use per transaction | |
| # Define the time for trading | |
| time_after_open = timedelta(minutes=2) | |
| time_before_close = timedelta(minutes=2) | |
| # Helper function to get the current price | |
| def get_current_price(symbol): | |
| quote = td_client.get_quotes(instruments=[symbol]) | |
| return (quote[symbol]['bidPrice'] + quote[symbol]['askPrice']) / 2 # Midpoint between bid and ask | |
| # Helper function to get the current account balance | |
| def get_account_balance(): | |
| accounts = td_client.get_accounts(fields=['positions']) | |
| cash_available = float(accounts[0]['securitiesAccount']['currentBalances']['cashAvailableForTrading']) | |
| return cash_available | |
| # Helper function to place orders | |
| def place_order(symbol, notional, action): | |
| order = { | |
| "orderType": "MARKET", | |
| "session": "NORMAL", | |
| "duration": "DAY", | |
| "orderStrategyType": "SINGLE", | |
| "orderLegCollection": [ | |
| { | |
| "instruction": action, | |
| "quantity": notional / get_current_price(symbol), # Calculate the quantity based on current price | |
| "instrument": { | |
| "symbol": symbol, | |
| "assetType": "EQUITY" | |
| } | |
| } | |
| ] | |
| } | |
| td_client.place_order(account_id='YOUR_ACCOUNT_ID', order=order) | |
| # Helper function to close all positions | |
| def close_all_positions(): | |
| positions = td_client.get_accounts(fields=['positions'])[0]['securitiesAccount']['positions'] | |
| for position in positions: | |
| symbol = position['instrument']['symbol'] | |
| quantity = position['longQuantity'] | |
| if quantity > 0: | |
| place_order(symbol, quantity, 'SELL') | |
| print("Closed all positions.") | |
| # Trading logic | |
| def trade(): | |
| initial_price = get_current_price(symbol) | |
| peak_price = initial_price | |
| trough_price = initial_price | |
| while True: | |
| current_time = datetime.now() | |
| # Assuming market opens at 9:30 AM and closes at 4:00 PM | |
| market_open_time = datetime.combine(current_time.date(), datetime.strptime('09:30', '%H:%M').time()) | |
| market_close_time = datetime.combine(current_time.date(), datetime.strptime('16:00', '%H:%M').time()) | |
| # Calculate trade start and stop times | |
| start_trading_time = market_open_time + time_after_open | |
| stop_trading_time = market_close_time - time_before_close | |
| # Check if current time is within trading hours | |
| if start_trading_time <= current_time <= stop_trading_time: | |
| current_price = get_current_price(symbol) | |
| balance = get_account_balance() | |
| # Update the peak and trough prices | |
| if current_price > peak_price: | |
| peak_price = current_price | |
| if current_price < trough_price: | |
| trough_price = current_price | |
| # Buy when there's a gain, using all available balance | |
| if current_price > initial_price and balance >= investment_amount: | |
| place_order(symbol, investment_amount, 'BUY') | |
| print(f"Bought ${investment_amount} worth of {symbol} at {current_price} due to price gain.") | |
| wait_for_balance_to_update() | |
| # Sell when there's a loss | |
| positions = td_client.get_accounts(fields=['positions'])[0]['securitiesAccount']['positions'] | |
| if symbol in [pos['instrument']['symbol'] for pos in positions] and current_price < initial_price: | |
| position_qty = next(pos['longQuantity'] for pos in positions if pos['instrument']['symbol'] == symbol) | |
| place_order(symbol, position_qty * current_price, 'SELL') | |
| print(f"Sold all shares of {symbol} at {current_price} due to price loss.") | |
| wait_for_balance_to_update() | |
| # Close all positions a couple of minutes before market close | |
| if current_time > stop_trading_time: | |
| close_all_positions() | |
| break # Exit the loop after closing positions for the day | |
| time.sleep(1) # Check as frequently as possible | |
| # Helper function to wait until the account balance updates after a sell | |
| def wait_for_balance_to_update(): | |
| while True: | |
| balance = get_account_balance() | |
| if balance >= investment_amount: | |
| break | |
| time.sleep(1) | |
| # Run the trading strategy | |
| trade() | |
| # Log out of TD Ameritrade | |
| td_client.logout() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment