访问 C# 代码的输出

本文关键字:输出 代码 访问 | 更新日期: 2023-09-27 18:34:07

我在VS2013中用C#运行这段代码,我从这里得到:http://tda.codeplex.com/。该代码应该从我的TD Ameritrade 401k帐户收集数据。代码运行良好,但以下代码的输出数据保存在哪里?如何访问它?

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

更新:将此代码放在下面:

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

访问 C# 代码的输出

它不会将数据保存在任何地方,上面的所有代码所做的只是检索指定的符号并存储变量名称prices的响应

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

如果你想要一种快速简便的方法来显示已检索到到的控制台窗口的数据,可以使用我开发的库,该库托管在 NuGet 上。

PM> Install-Package DebugUtilities

编辑

若要将结果导出到文本文件,首先需要使用 NuGet 包管理器安装另一个包。

PM> Install-Package Newtonsoft.Json

安装上述库后,您可以使用下面的代码保存到您想要的任何位置。

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

至于保存到文件,无处可去。但是,您正在用股票报价填充quoteserrors在获取这些报价时遇到的任何错误,symbols我假设List<string>符号与*GOO*匹配,并prices七天前到昨天的历史价格。但是,一旦程序完成,它们就会超出范围,并且必须再次检索它们。

要保存它们,您需要将它们保存到文件中或创建一个数据库来保存信息。

在新 API 中,"Quotes" 和 "Quote" 响应是单个回调。 如果需要流式处理,则需要使用更复杂的 https://developer.tdameritrade.com/content/streaming-data 并发送异步回调。以及:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests