Converting from V1 to v2 fails

Hi, I have been using V1 and just converted to V2.

I used to send: https://data.alpaca.markets/v1/bars/minute?symbols=SHOP&after=0001-01-01T09:30:00-04:00

Now sending: https://data.alpaca.markets/v2/bars/minute?symbols=SHOP&after=0001-01-01T09:30:00-04:00

This returns “not found”.

I am using C# and I updated all my keys this morning. Any help would be appreciated. Must be something simple!

Well, after much work, this now works:

https://data.alpaca.markets/v2/stocks/bars?symbols=AAPL,SHOP&start=2022-03-17T05:00:00Z&timeframe=1Min&feed=IEX

The documentation could be improved with some examples and not assume people know what is going on.

2 Likes

You can always use .NET SDK sources as a reference point for your own implementation if you prefer to control each line of your code.

In case someone lands here instea of the other thread.

This is what i have had success with but only after updating/reinstalling the alpaca module in my terminal.
the useful info for V2 is on the git page not the main website for alpaca.

GitHub - alpacahq/alpaca-trade-api-python: Python client for Alpaca's trade API)

base_url = ‘https://data.alpaca.markets/v2/stocks’
api_key = ‘key’
secret_key = ‘Skey’
api = REST(api_key, secret_key, base_url)
barset = api.get_bars(“IBM”, TimeFrame.Day, “2021-11-04”,“2022-03-16”, adjustment=‘raw’)

The lack of an Alpaca team member watching the forum after ending V1 is kinda crazy right?

Umm, yes it appears c# is no longer supported. Did they take down the V1 API?

I would like a c# example of how to replace this v1:
var bars = await client.GetBarSetAsync(new BarSetRequest(ticker, TimeFrame.Day) { Limit = days });

With V1 there was an example that go me up and running. It looks like they took down the V1 API and now I’m left out in the cold.

What is going on?

Please post a working example of C# using the V2 interface to get historical data.

The announcement about the v1 API deprecation was published on all public channels (this forum, email, documentation pages) many months ago. In any case, the v2 API is mostly compatible with the old one and the upgrade shouldn’t take too much time.

But in your case, you have to calculate both start and end dates for the request upfront and check your code for any assumptions about results ordering. The old API returns data in descending time order but the new one do it in the opposite order.

In general request will look like this:

var page = await alpacaDataClient.ListHistoricalBarsAsync(
    new HistoricalBarsRequest(symbol, startDate, endDate, BarTimeFrame.Day));
var bars = page.Items;

Yes, there is an example as follows. However when I get API keys, they now appear to be a long HEX value, instead of a string. When I enter them as the string, I get “Forbidden”. I am simply trying to execute the provided example. Please help me get the example to work.

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(new SecretKey(API_KEY, API_SECRET));

        var into = DateTime.Today;
        var from = into.AddDays(-5);
        // Get daily price data for AAPL over the last 5 trading days.
        var bars = await client.ListHistoricalBarsAsync(
            new HistoricalBarsRequest("AAPL", from, into, BarTimeFrame.Day));

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

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

        Console.Read();
    }
}

}

What are the expected arguments for SecretKey(ARG1, ARG2)? Are these supposed to be hex values represented as strings?

The keys look like:

static public string APCA_API_KEY_ID = “xxxxxxxxxx25900xxxxx”;
static public string APCA_API_SECRET_KEY = “xxxxxxxxxxPmAUytrkJp7BvwQhtmt3KRdJjxxxxx”;

I cut/pasted them off the dashboard where I generated them. tis is not hex as too many alpha characters, so just a tring.

Is this code using the C# library?

I skipped that and just wrote my own code.

The keys I get are hex and they do not work

did you get your keys from the link here and then cut/paste them out of put on the clipboard?

I see!
I was using the OAuth Apps keys

Hope you got your code running.

So what are you trying to do?

I am doing a lot of machine learning to develop buying algorithms and have about 40 stocks bought so far in my portfolio (real money). I would be happy to discuss trading ideas.

It would be interesting to see what you are doing.

I have written a Windows app that uses indicators to trigger events in a state machine. The states are like: trending up / trending down / ranging (long or no position).

The indicators have parameter ranges, for example SMA could be 50/60/70/80/90/100 days.

The app then backtests the state machine to determine the optimal parameter set for each ticker.

Based on this optimized model, the present state of each ticker is listed, with a recommended action (buy/sell/hold/wait).

This could be considered AI, but it is very deliberate and structured. I’m not pointing the computer at data sets and having it “find” relationships. I have designed a very specific framework and the computer needs to learn within specific bounds.

651.207.9916