C# example gives error message

I am trying to run the c# “Market Data Example” when I enter the line:

var bars = await client.GetBarsetAsync(new[] { “AAPL” }, TimeFrame.Day, 5);

I get the error message:

‘AlpacaTradingClient’ does not contain a definition for ‘GetBarsetAsync’…

Any suggestions on how to fix this error.

Thanks

You have to use the AlpacaDataClient class and GetBarSetAsync method. I’ll prepare PR for fixing documentation soon. Sorry for this inconvenience and best regards.

Thanks for replying,

I have used:

var client = Alpaca.Markets.Environments.Paper
.GetAlpacaTradingClient(API_KEY, new SecretKey(API_SECRET));

// Get daily price data for AAPL over the last 5 trading days.
var bars = await client.GetBarsetAsync(new[] { “AAPL” }, TimeFrame.Day, 5);

GetBarsetAsync gives me the error

Let me know if I should change anything in the published example.

There is updated example for GetBarSetAsync method (still under review by Alpaca maintainers):

using Alpaca.Markets;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace CodeExamples
{
    internal static class Example
    {
        private static string API_KEY = "your_api_key";

        private static string API_SECRET = "your_secret_key";

        public static async Task Main(string[] args)
        {
            // First, open the API connection
            var client = Alpaca.Markets.Environments.Paper
                .GetAlpacaDataClient(API_KEY, new SecretKey(API_SECRET));

            // Get daily price data for AAPL over the last 5 trading days.
            var bars = await client.GetBarSetAsync(new[] { "AAPL" }, TimeFrame.Day, 5);

            // See how much AAPL moved in that timeframe.
            var startPrice = bars["AAPL"].First().Open;
            var endPrice = bars["AAPL"].Last().Close;

            var percentChange = (endPrice - startPrice) / startPrice * 100;
            Console.WriteLine($"AAPL moved {percentChange} over the last 5 days.");

            Console.Read();
        }
    }
}
1 Like

Thanks for the information

1 Like

https://alpaca.markets/docs/api-documentation/how-to/market-data/ The docs has been updated as well.

Thanks for the reply