Free, open-source .NET Client for the TD Ameritrade Trading Platform. Helps developers integrate TD Ameritrade API into custom trading solutions.
- Authentication Flow
- Principal
- Quotes
- Historical Charts
- Option Chain
- Streaming QOS
- Streaming Charts
- Streaming Level 1 Quotes
- Streaming Level 2 Quotes
- Streaming Time & Sales
// Credentials are saved, please use the protected cache
var cache = TDProtectedCache();
var client = new TDAmeritradeClient(cache);
// Sign in first time
var url = client.GetSignInUrl("consumerkey");
client.SignIn("consumerkey", "codefromloginurl");
Assert.IsTrue(client.IsSignedIn);
// Sign in second time
client.SignIn();
Assert.IsTrue(client.IsSignedIn);
//Use!
var data = await client.GetQuote_Equity("SPY");
var data = await client.GetQuote_Future("/NQ");
var data = await client.GetQuote_Option("SPY_231215C500");
var data = await client.GetPriceHistory(new TDPriceHistoryRequest
{
symbol = "SPY",
frequencyType = TDPriceHistoryRequest.FrequencyType.minute,
frequency = 5,
periodType = TDPriceHistoryRequest.PeriodTypes.day,
period = 2,
});
var data = await client.GetOptionsChain(new TDOptionChainRequest
{
symbol = "SPY",
});
var data = await client.GetPrincipals(TDPrincipalsFields.preferences);
using (var socket = new TDAmeritradeStreamClient(client))
{
socket.OnHeartbeatSignal += o => { };
socket.OnQuoteSignal += o => { };
socket.OnTimeSaleSignal += o => { };
socket.OnChartSignal += o => { };
socket.OnBookSignal += o => { };
await socket.Connect();
await socket.SubscribeQuote("QQQ");
await socket.SubscribeChart("QQQ", TDChartSubs.CHART_EQUITY);
await socket.SubscribeTimeSale("QQQ", TDTimeSaleServices.TIMESALE_EQUITY);
await socket.SubscribeBook("QQQ", TDBookOptions.NASDAQ_BOOK);
}
Setup is multi-step process. To use this SDK, you must run the console app and set up your security file.
- Run the console app, this will prompt you for a consumer key.
- Consumer key is located under MyApps
- Sign in using the opened web browser.
- Copy the code (?code={code}) from the returned website upon completion.
- Input the code into the console app.
- This will write a security file (TDAmeritradeKey). Place it in the runtime root when needed.
- Copy this key to the test bin/debug folder next to the exe and dll
I have included a AspNetCore example with Dataprotection implemented using a web page.
- Run the web application
- Start authentication by submitting your consumer key.
- Sign in using the opened web browser.
- Copy the code (?code={code}) from the returned website upon completion.
- Input the code into the 'PostAccessToken' field
- This will write a security file (TDAmeritradeKey).
- Enjoy
Copyright (c) 2021 Nicholas Ventimiglia
Logo image is a trademark of TD Ameritrade, Inc.