dm13450 / AlpacaMarkets.jl

Julia interface to the Alpaca Trading API
MIT License
31 stars 7 forks source link

Function stock_bars returns empty array & Nothing #16

Open danvinci opened 9 months ago

danvinci commented 9 months ago

Hi Dean,

thanks for creating this package!

I've tried to replicate some examples in the docs, but I'm not sure if it's me, the library, or the fact I only have a paper account - but I get the following:

Screenshot 2024-01-27 at 15 50 56

Why would it be the case?

Similarly for get_stock_quotes:

Screenshot 2024-01-27 at 15 52 01
danvinci commented 9 months ago

Okay looking at the code and the API docs: https://alpaca.markets/deprecated/docs/api-documentation/api-v2/market-data/alpaca-data-api-v2/historical/

Seems that the symbol must be used as part of the base url: /v2/stocks/{symbol}/bars ? {params}

Doing so does the trick.

Also: asof and currency are not active parameters according to the docs

Below my own temp workaround if anyone bumps in the same issue:

function get_stock_bars(symbol::String, timeframe::String; 
                    startTime=nothing, endTime=nothing, 
                    limit=nothing, page_token=nothing, 
                    adjustment = adjustment, feed = nothing)

    # prepare url for call
    url = join([stock_data_url, symbol, "bars"], "/")

    params = Dict(
            "timeframe" => timeframe,
            "start" => startTime,
            "end" => endTime,
            "limit" => limit,
            "page_token" => page_token,
            "adjustment" => adjustment,
            "feed" => feed
            )

    req_url = url * "?" * params_uri(params) # params_uri is among the utils of this pkg

    res = HTTP.get(req_url, headers = auth_headers)

    parsed_res = JSON3.read(res.body) # using JSON3 instead of JSON

    ret_df = parsed_res[:bars] |> DataFrame # DF to be returned
        ret_df.t = DateTime.(ret_df.t, dateformat"yyyy-mm-ddTHH:MM:SSz")

    # trigger new call if there are more pages -> recursive call and append 
    if !isnothing(parsed_res[:next_page_token])

        next_page_token = parsed_res[:next_page_token]

        next_page_df = get_stock_bars(symbol, timeframe, 
                        startTime=startTime, endTime=endTime, 
                        limit=limit, page_token=next_page_token, 
                        adjustment = adjustment, feed = feed)

        append!(ret_df, next_page_df)

    else
        return ret_df

    end

end
dm13450 commented 9 months ago

Thanks for raising this, looks like there is a bug around the start time/end time arguments, will take a look when I get a chance.