0

I am running this code in C# in VS2013 which I got from here: http://tda.codeplex.com/. The code is supposed to gather data from my TD Ameritrade 401k account. The code is running fine but where is the outputted data from the code below being saved at? How do I access it?

namespace TDAmeritrade.Samples
{
    using System;
    using TDAmeritrade;

    class Program
    {
        static void Main()
        {
            // Initialize TD Ameritrade client, provide additional config info if needed
            var client = new TDAClient();

            // Log in to the TD Ameritrade website with your user ID and password
            client.LogIn("jessicasusername", "jessicaspassword");

            // Now 'client.User' property contains all the information about currently logged in user
            var accountName = client.User.Account.DisplayName;

            // Get stock quotes snapshot.
            var quotes = client.GetQuotes("GOOG, AAPL, $SPX.X, DUMMY");

            // 'quotes.Error' contains a list of symbols which have not been found
            var errors = quotes.Errors;

            // Find symbols matching the search string
            var symbols = client.FindSymbols("GOO");

            // Get historical prices
            var prices = client.GetHistoricalPrices("GOOG, AAPL", StartDate: DateTime.Today.AddDays(-7), EndDate: DateTime.Today.AddDays(-1));
        }
    }
}

Update: Placed this code below:

PM> Install-Package Newtonsoft.Json

// Change the file path to wherever you wish to save the results
const string SaveFileToLocation = @"C:\Users\jessica\Desktop\json_data";
string json = JsonConvert.SerializeObject(prices, Formatting.Indented);

using (StreamWriter writer = new StreamWriter(SaveFileToLocation))
{
    writer.Write(json);   
}
2
  • 1
    It's being saved... in the variables? Commented Dec 14, 2014 at 3:23
  • Thanks matti, I don't know how to access the variables. Any way maybe to save as a csv? Commented Dec 14, 2014 at 3:40

3 Answers 3

1

It isn't saving the data anywhere, all the above code is doing is retrieving the specified symbols and storing the response the variable name prices

var prices = client.GetHistoricalPrices("GOOG, AAPL", StartDate: DateTime.Today.AddDays(-7), EndDate: DateTime.Today.AddDays(-1));

If you want a quick an easy way to display the data you've retrieved to the console window, you can use a library I developed which is hosted on NuGet.

PM> Install-Package DebugUtilities

EDIT

To export the results to a text file, first you'll need to install another package using the NuGet package manager.

PM> Install-Package Newtonsoft.Json

After you've installed the above library, you can use the code below to save to wherever you wish.

// Change the file path to wherever you wish to save the results
const string SaveFileToLocation = @"C:\Dev\test.json";
string json = JsonConvert.SerializeObject(prices, Formatting.Indented);

using (StreamWriter writer = new StreamWriter(SaveFileToLocation))
{
    writer.Write(json);   
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks so there is no direct way to access the data from C#? Your library would allow so. Is there a way to have other softwares such as python grab the data for charting purposes?
Would saving the response as JSON to a text document and reading it from python be a workable solution for you? What would be the ideal way for you to import it in to python?
Hi Aydin, I recently installed Python .Net. Would that work? I don't know how to use java.
The issue is that I don't know the object structure of the given prices list... exporting to CSV format could create issues if there are nested objects... Instead I'd advise you to save the response to JSON format, and read the response using python. I'll update my answer in a moment as to how you can save the response you're retrieving from the api to your hard drive. As for reading it again from python, you will have to create a new question which python devs should be able to address
@jessica Update that for you, hopefully it should lead you in the right direction.
|
1

As for saving to a file, nowhere. You are however filling quotes with stock quotes, errors with any errors that were encountered in getting those quotes, symbols with I'm assuming a List<string> of symbols matching *GOO*, and prices with the historical prices from seven days prior through yesterday. Once the program finishes however, those fall out of scope and they will have to be retrieved again.

To save them, you with either need to save them to a file or create a database to house the information.

2 Comments

Thanks JR. Do you think it is possible I could load that data into Python for charting, etc?
The only way that I can think of passing the information to Python is by saving it in some fashion, then having Python access the stored information. If you tried to access the memory used by this (I don't even think it is possible), you would get an exception.
0

In the new API, "Quotes" and "Quote" response is a single callback. If you want Streaming you need to use the more complex https://developer.tdameritrade.com/content/streaming-data and send an asynchronous callback. and: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.