PHP code example of michaeldrennen / td-ameritrade-api

1. Go to this page and download the library: Download michaeldrennen/td-ameritrade-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

michaeldrennen / td-ameritrade-api example snippets


// How to create a TDAmeritrade API client.
$userName                     = 'joeuser123';
$accessToken                  = 'stringFromTDAmeritradeAuthentication';
$refreshToken                 = 'anotherStringFromTDAmeritradeAuthentication';
$refreshTokenExpiresInSeconds = intFromTDAmeritradeAuthentication;
$debug                        = false;
$tdaClient = TDAmeritradeAPI( $userName,
                              $accessToken,
                              $refreshToken,
                              $refreshTokenExpiresInSeconds,
                              $debug );

// Once you have a client, you can buy stocks at market price.
$accountId = 123456789;
$ticker    = 'LODE';
$quantity  = 1;

try {
   $tdaClient->buyStockSharesMarketPrice($accountId, $ticker, $quantity);
} catch (Exception $exception) {
   echo $exception->getMessage();
}

// Now, log into your TDAmeritrade account online, and you should see a new order.

// To sell some shares at the market price...
try {
   $tdaClient->sellStockSharesMarketPrice($accountId, $ticker, $quantity);
} catch (Exception $exception) {
   echo $exception->getMessage();
}