jessica
jessica

Reputation: 1355

Accessing Output From C# Code

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);   
}

Upvotes: 0

Views: 285

Answers (3)

Winnie-the-Pough
Winnie-the-Pough

Reputation: 11

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

Upvotes: 0

Aydin
Aydin

Reputation: 15294

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);   
}

Upvotes: 1

Jacob Lambert
Jacob Lambert

Reputation: 7679

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.

Upvotes: 1

Related Questions