Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
schwab-client-js
Advanced tools
Disclaimer: This project is not affiliated with, endorsed by, or associated with The Charles Schwab Corporation. All registered trademarks are the property of their respective owners. Use of these names, logos, and brands is for identification purposes only. This project is licensed under the MIT license, and acts in accordance with Schwab's API terms and conditions.
Two Streaming Dashboards From The examples directory
Software prerequisites: nodejs version 18 or newer and a nodejs package manager such as yarn or npm
Install the package:
$ npm install schwab-client-js
or
$ yarn add schwab-client-js
See the Schwab API Configuration doc for details on creating your Schwab app.
Schwab uses three-legged OAuth for authentication. The details are on developer.schwab.com here and here.
schwab-client-js uses environment-based security e.g. it uses a .env
file to store your security tokens.
[Note: For situtations where using a .env
file or environment variables may not be optimal (possibly AWS Lambda, for example), schwab-client-js also supports injecting your security tokens directly e.g. const mktclient = new MarketApiClient(appKey,appSecret,appRefresh);
]
.env
file at the root of your project and add the App Key and Secret from your app on developer.schwab.com. Optionally, you can add your callback URL to your .env file like this (for details on creating your SCHWAB_CALLBACK_URL
, see the Schwab API Configuration doc):SCHWAB_CALLBACK_URL=https://127.0.0.1:5556
SCHWAB_APP_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SCHWAB_SECRET=zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
If you don't specify a SCHWAB_CALLBACK_URL
, I will assume a default value of https://127.0.0.1:5556
SCHWAB_REFRESH_TOKEN
. Run schwab-authorize
OR manual-authorize
to help you create the SCHWAB_REFRESH_TOKEN
. You should be able to run schwab-authorize
from the root of your project (on MacOS and Linux) by merely typing schwab-authorize
. On Windows, you'll likely have to run it with node and specify the full path:
C:\> node node_modules/schwab-client-js/bin/schwab-authorize.js
Same with manual-authorize
. The script manual-authorize
is for users who would rather not deal with the self-signed SSL certificate that schwab-authorize
generates.See the Schwab API Configuration doc for details about schwab-authorize
and manual-authorize
.
.env
file should now look like this (as previously mentioned, the SCHWAB_CALLBACK_URL
is optional and will default to https://127.0.0.1:5556
if not provided):SCHWAB_CALLBACK_URL=https://127.0.0.1:5556
SCHWAB_APP_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
SCHWAB_SECRET=zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
SCHWAB_REFRESH_TOKEN=yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
SCHWAB_REFRESH_TOKEN
lasts for seven days. It then expires and you will no longer be able to make API calls. You must run schwab-authorize
OR manual-authorize
again to generate a new SCHWAB_REFRESH_TOKEN
(sorry folks, I don't make these rules).schwab-client-js has three classes that you use to make API calls, and some helper functions that you can use to help create input parameters. The three classes are:
MarketApiClient()
- Get market dataTradingApiClient()
- Execute trades and get your personal account and trade-related dataStreamingApiClient()
- Stream market dataThere are also about 30 helper functions that help you create JSON trade objects and option symbols. See the schwab-client-js developer reference guide for more details
import { MarketApiClient } from "schwab-client-js";
const mktclient = new MarketApiClient();
// Get stock information by ticker symbol
const data1 = await mktclient.quoteById("AMD", "quote,fundamental");
console.log("quoteById DATA=", JSON.stringify(data1));
// Get security information by cusip
const data2 = await mktclient.instrumentsCusip("023135106");
console.log("instrumentsCusip DATA=", JSON.stringify(data2));
// Get market hours information
const data3 await mktclient.markets("bond,option,forex");
console.log("markets DATA=", JSON.stringify(data3));
schwab-client-js has various helper functions to make is easier to create order objects and option symbols.
// Create a Schwab-formatted option symbol
import { optionSymbol } from "schwab-client-js/orderhelp";
const optsymbol = optionSymbol("XYZ", "210115", "C", "62.50");
// optsymbol is now: "XYZ 210115C00062500"
// Returns a pre-filled JSON order object for an equity buy-to-cover market order.
import { equityBuyToCoverMarket } from "schwab-client-js/orderhelp";
const orderObj = equityBuyToCoverMarket("TSLA", 38);
console.log(JSON.stringify(orderObj, null, 2));
The output is:
{
"orderType": "MARKET",
"session": "NORMAL",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"orderLegCollection": [
{
"instruction": "BUY_TO_COVER",
"quantity": 38,
"instrument": {
"symbol": "TSLA",
"assetType": "EQUITY"
}
}
]
}
orderObj
can be used as input to the placeOrderByAcct()
method as shown in the section below.
import { TradingApiClient } from "schwab-client-js";
const trdclient = new TradingApiClient();
// Get your authorized Schwab account numbers and hashed account numbers
const data1 = await trdclient.accountsNumbers();
console.log("accountsNumbers DATA=", JSON.stringify(data1));
// Get orders (all statuses) withing the specified date range
const accountHash="4B9A9B50B7886A574E2A793DFE9B944EA2DAB9"; // Your hashed account number
const fromDate="2024-01-10T12:17:41+02:00";
const toDate="2024-09-10T12:17:41.000Z";
const data2 = await trdclient.ordersByAccount(accountHash, fromDate, toDate);
console.log("ordersByAccount DATA=", JSON.stringify(data2));
// Create a limit order during normal trading hours to buy
// one share of CTRN (Citi Trends Inc) stock for $1 USD.
// You can use the helper functions mentioned in the section
// above to create this object:
// const orderObj = equityBuyLimit("CTRN", 1, "1.00");
const orderObj = {
"orderType": "LIMIT",
"session": "NORMAL",
"duration": "DAY",
"orderStrategyType": "SINGLE",
"price": "1.00",
"orderLegCollection": [
{
"instruction": "BUY",
"quantity": 1,
"instrument": {
"symbol": "CTRN",
"assetType": "EQUITY"
}
}
]
};
// Before placing the order, you can preview the order to see the fees (if any) and status
const accountHash="4B9A9B50B7886A574E2A793DFE9B944EA2DAB9"; // Your hashed account number
const data3 = await trdclient.orderPreview(accountHash, orderObj);
console.log("orderPreview DATA=", JSON.stringify(data3)); // Should return a `trueCommission` section and `status`
// Place the trade using the specified Schwab account
const data4 = await trdclient.placeOrderByAcct(accountHash, orderObj);
console.log("placeOrderByAcct DATA=", JSON.stringify(data4)); // Should return an orderId
// Delete an existing order
const orderID="435234523452345";
const data5 = await trdclient.orderDelete(accountHash, orderId);
console.log("orderDelete DATA=", JSON.stringify(data5)); // Successful deletes return null so this should show no JSON
marketById()
or markets()
calls to get the market hours.import { StreamingApiClient } from "schwab-client-js";
// Create your streaming client object
const streamclient = new StreamingApiClient();
// Listen for the "open" request
streamclient.streamListen('open', () => {
console.log("Received open message: webSocket connection opened.");
});
// Listen for messages from Schwab. This is where your data arreves
streamclient.streamListen('message', (message) => {
console.log("Received data message:", message);
});
// Listen for close
streamclient.streamListen('close', (code, reason) => {
console.log(`Connection closed: Code=${code}, Reason=${reason}`);
});
// Listen for errors
streamclient.streamListen('error', (error) => {
console.error("Received error message:", error);
});
// Initialize the stream
await streamclient.streamInit();
// Login to the Schwab stream
await streamclient.streamSchwabLogin();
// Subscribe to some stock prices
let params = { keys: "AMD,TSLA,GOOG", fields: "0,1,2,3,4,5" };
await streamclient.streamSchwabRequest("ADD", "LEVELONE_EQUITIES", params);
// Streaming data happens.......
// Close the Schwab stream
await streamclient.streamSchwabLogout()
streamclient.streamClose();
The trading, market-data, and streaming directories contain examples of using most of the API calls.
The algo-trading folder contains scripts that conveniently generate many common statistical indicators that you can use in your algo-trading projects. There is also a function StockList()
which generates a large set of stock symbols and associated data.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Javascript client for the Schwab API
We found that schwab-client-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.