-1

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 when I look in the output folder no file is saved. I keep getting this two errors that I am unable to fix. What am I doing wrong?

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

Error 1 A namespace cannot directly contain members such as fields or methods

Error 2   Expected class, delegate, enum, interface, or struct
MMM
  • 3,132
  • 3
  • 20
  • 32
jessica
  • 1,325
  • 2
  • 21
  • 35
  • your code is not formatted properly. Period. Code must be within class, `using` must be outside `namespace` – T.S. Dec 15 '14 at 01:32

2 Answers2

0

this is because you declare some variable outside of a class. just put theme in class Program.

namespace TDAmeritrade.Samples
{
    using System;
    using TDAmeritrade;

    class Program
    {
        const string SaveFileToLocation = @"C:\Users\jessica\Desktop\json_data";

        static void Main()
        {
            // find best place for these codes.
            string json = JsonConvert.SerializeObject(prices, Formatting.Indented);
            using (StreamWriter writer = new StreamWriter(SaveFileToLocation))
            {
                writer.Write(json);   
            }

            // 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));
        }
    }
}
Mohamad Shiralizadeh
  • 8,329
  • 6
  • 58
  • 93
  • This will still not work since you you create `json` before `prices `, and you create the file before anything has been gathered. – Jacob Lambert Dec 14 '14 at 18:29
  • Thank you JR and Mohamad. So I can move the code up. Question: There are 3 curly brackets, where do I put the bottomm code in. Before Curly Bracket 1, Between Curly Bracket 1 & 2 or Between Curly Bracket 2 & 3. – jessica Dec 14 '14 at 18:38
  • JR, how should I rearrange the bottom code? – jessica Dec 14 '14 at 18:38
  • Could it be because I did not load Newtonsoft.Json Package? Dosn't recognize the "json" functions? – jessica Dec 14 '14 at 18:49
0

The way your code should be arranged is thus:

using System;
using TDAmeritrade;

namespace TDAmeritrade.Samples
{    
    class Program
    {
        const string SaveFileToLocation = @"C:\Users\jessica\Desktop\json_data";

        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")

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

            //You cannot create json until the prices variable has been declared!
            string json = JsonConvert.SerializeObject(prices, Formatting.Indented);
            using (StreamWriter writer = new StreamWriter(SaveFileToLocation))
            {
                writer.Write(json);   
            }
        }
    }
}
Jacob Lambert
  • 7,449
  • 8
  • 27
  • 47