## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## -----------------------------------------------------------------------------
require(alphavantagepf)
avpf_api_key("YOUR_API_KEY","delayed")
print(avpf_api_key())

## -----------------------------------------------------------------------------
av_funhelp("SERIES_INTRADAY")

## ----eval=F-------------------------------------------------------------------
# > av_get_pf("IBM","SMA",verbose=T,time_period=30)
# https://www.alphavantage.co/query?function   SMA
# symbol                                       IBM
# interval                                     daily
# time_period                                  30    # << Normally 60
# series_type                                  close
# datatype                                     csv
# > response: 200 type: application/x-download... url copied to clipboard
#       symbol       time   SMA
#       <char>     <IDat> <num>
#    1:    IBM 2026-01-06 304.1
#    2:    IBM 2026-01-05 303.7

## ----eval=F-------------------------------------------------------------------
# av_get_pf("","TOP_GAINERS_LOSERS")
# 
# Key: <variable>
#                symbol             variable     ltype           value_df                                value_str value_num
#                <char>               <char>    <char>             <list>                                   <char>     <num>
# 1: TOP_GAINERS_LOSERS         last_updated   numeric             [NULL]           2026-01-05 16:15:59 US/Eastern      2026
# 2: TOP_GAINERS_LOSERS             metadata character             [NULL] Top gainers, losers, and most actively t        NA
# 3: TOP_GAINERS_LOSERS most_actively_traded      list <data.frame[20x5]>                                     NULL        NA
# 4: TOP_GAINERS_LOSERS          top_gainers      list <data.frame[20x5]>                                     NULL        NA
# 5: TOP_GAINERS_LOSERS           top_losers      list <data.frame[20x5]>                                     NULL        NA

## ----eval=F-------------------------------------------------------------------
# av_get_pf("","TOP_GAINERS_LOSERS") |> av_extract_df("top_losers")
# 
#     <char>   <num>         <num>            <char>     <num>             <char>
#  1:    OCG  0.0378       -0.0654         -63.3721% 216078762 TOP_GAINERS_LOSERS
#  2:   ZBIO 16.6100      -17.8900         -51.8551%   8034469 TOP_GAINERS_LOSERS
#  3:    SGN  0.4627       -0.4873         -51.2947%   2115079 TOP_GAINERS_LOSERS
#  4:  HYT^#  0.0186       -0.0164         -46.8571%    126059 TOP_GAINERS_LOSERS
#  5:  LVROW  0.0122       -0.0079         -39.3035%     10967 TOP_GAINERS_LOSERS

## ----eval=F-------------------------------------------------------------------
# # REAL-TIME QUOTE
# av_get_pf("USD/BRL","CURRENCY_EXCHANGE_RATE") |> av_extract_fx()
# 
# Key: <symbol>
#     symbol   Ask   Bid      QuoteTimestamp   Mid
#     <char> <num> <num>              <POSc> <num>
# 1: USD/BRL  5.37  5.37 2026-01-06 15:47:46  5.37

## ----eval=F-------------------------------------------------------------------
# av_get_pf("IBM","HISTORICAL_OPTIONS") |> av_grep_opts("F,M,put",mindays=2)
# 
#    symbol         contractID expiration strike   type  last  mark   bid bid_size   ask ask_size volume open_interest ...
#    <char>             <char>     <IDat>  <num> <char> <num> <num> <num>    <int> <num>    <int>  <int>      ...
# 1:    IBM IBM260116P00277500 2026-01-16    278    put  0.00  0.80  0.67      158  0.94      254      0      ...
# 2:    IBM IBM260116P00280000 2026-01-16    280    put  0.98  0.96  0.90      180  1.02       10    111      ...
# 3:    IBM IBM260116P00282500 2026-01-16    282    put  1.32  1.29  1.17      213  1.41      158     14      ...

## ----eval=F-------------------------------------------------------------------
#  av_get_pf(c("ORCL","IBM"),"ANALYTICS_FIXED_WINDOW") |> av_extract_analytics(separate_vars=TRUE)
#  av_get_pf: Reurning raw output; send to appropriate helper ----------------------------
#              variable_1              variable_2 variable_3               value
#                  <char>                  <char>     <char>              <char>
# 1:              symbols                    <NA>       <NA>            ORCL,IBM
# 2:               min_dt                    <NA>       <NA>          2025-12-09
# 3:               max_dt                    <NA>       <NA>          2026-01-06
# 4:                 ohlc                    <NA>       <NA>               Close
# 5:             interval                    <NA>       <NA>               DAILY
# 6: RETURNS_CALCULATIONS       CUMULATIVE_RETURN       ORCL   -0.12540062294046
# 7: RETURNS_CALCULATIONS       CUMULATIVE_RETURN        IBM -0.0257987632053596
# 8: RETURNS_CALCULATIONS STDDEV(ANNUALIZED=TRUE)       ORCL   0.575380551599422
# 9: RETURNS_CALCULATIONS STDDEV(ANNUALIZED=TRUE)        IBM   0.171846694713282

## ----eval=F-------------------------------------------------------------------
# av_get_pf("IBM","TIME_SERIES_INTRADAY") |> head()
# 
#    symbol  timestamp  open  high   low close   volume
#    <char>     <IDat> <num> <num> <num> <num>    <int>
# 1:    IBM 1999-11-01  98.5  98.8  96.4  96.8  9551800
# 2:    IBM 1999-11-02  96.8  96.8  93.7  94.8 11105400

## ----eval=F-------------------------------------------------------------------
# av_get_pf("","TOP_GAINERS_LOSERS")
# 
# Key: <variable>
#                symbol             variable     ltype           value_df                                value_str value_num
#                <char>               <char>    <char>             <list>                                   <char>     <num>
# 1: TOP_GAINERS_LOSERS         last_updated   numeric             [NULL]           2026-01-05 16:15:59 US/Eastern      2026
# 2: TOP_GAINERS_LOSERS             metadata character             [NULL] Top gainers, losers, and most actively t        NA
# 3: TOP_GAINERS_LOSERS most_actively_traded      list <data.frame[20x5]>                                     NULL        NA
# 4: TOP_GAINERS_LOSERS          top_gainers      list <data.frame[20x5]>                                     NULL        NA
# 5: TOP_GAINERS_LOSERS           top_losers      list <data.frame[20x5]>                                     NULL        NA

