AlpacaStreams | R Documentation |
An R6
class that allows interaction with Alpaca and Polygon websockets. \lifecycleexperimental
Channels are joined by calling the $channel()
method. The socket is automatically detected based on the channel request.
"trade_alpaca"/"t"
Alpaca trade stream
All V2 streaming data channels:
V2 Streaming websocket channels are connected to by supplying a named list
to channel
with any or all of the following list items, each of which contains a character vector
of symbol names - or *
for bars
to subscribe to all symbols:
trades
quotes
bars
See the V2 Streaming Documentation for details.
Note: The Alpaca V1 Streaming API has been superseded by the V2 API and is not supported.
T.* Trades
Q.* Quotes
A.* Aggregate ( per second )
AM.* Aggregate ( per minute )
where * is the ticker symbol and the argument is case-insensitive. IE "am.bynd"
will subscribe to by minute aggregates for Beyond Meat. Multiple channels can be subscribed to using a character vector.
Messages returned by the Polygon Websocket depend on the channel:
ev
Event Type
sym
Symbol Ticker
x
Exchange ID
i
Trade ID
z
Tape ( 1=A 2=B 3=C)
p
Price
s
Trade Size
c
Trade Conditions
t
Trade Timestamp ( Unix MS )
ev
Event Type
sym
Symbol Ticker
bx
Bix Exchange ID
bp
Bid Price
bs
Bid Size
ax
Ask Exchange ID
ap
Ask Price
as
Ask Size
c
Quote Condition
t
Quote Timestamp ( Unix MS )
ev
Event Type ( A = Second Agg, AM = Minute Agg )
sym
Symbol Ticker
v
Tick Volume
av
Accumulated Volume ( Today )
op
Todays official opening price
vw
VWAP (Volume Weighted Average Price)
o
Tick Open Price
c
Tick Close Price
h
Tick High Price
l
Tick Low Price
a
Tick Average / VWAP Price
s
Tick Start Timestamp ( Unix MS )
e
Tick End Timestamp ( Unix MS )
Alpaca
slot for AlpacaSocket
. Contains a socket for the v1 account websocket and the v2 data websocket.
Polygon
slot for PolygonSocket
new()
Connect to and authorize Alpaca and Polygon websocket streams and set default options for these websockets.
AlpacaStreams$new( socket = c("account_alpaca", "data_alpaca", "polygon")[1:2], toConsole = TRUE, log = TRUE, log_limit = 5000, bars_limit = 5000, write_dir = file.path(getwd(), "AlpacaStreams"), overwrite = TRUE, msg_action = NULL, live = get_live(), pro = get_pro(), ... )
socket
(character)
Which Websocket stream to connect to:
`"account_alpaca"/"a"`
Alpaca account updates
`"data_alpaca"/"a"`
Alpaca real-time data websockets
`"polygon"/"p"`
Polygon websockets Polygon Subscription Required
toConsole
(logical)
flag for whether to report websocket messages to the console. Default TRUE
.
log
(logical)
flag for whether to retain a log of messages (from Alpaca) or messages & bars from symbol feeds from Polygon. Default TRUE
to store logs.
log_limit
(numeric)
indicating the number of previous messages to retain in the log. Default 5000
. See Details for memory handling specifics.
bars_limit
(numeric)
indicating the number of previous bars (per subscription feed) to retain in the log. Default 5000
. See Details for memory handling specifics.
write_dir
(character/logical)
The directory in which to store logs on disk. Use FALSE
to disable logging to disk. Folders will be created. Default "AlpacaStreams"
.
overwrite
(logical)
indicating whether to overwrite data from previous instances of a websocket connection. Default TRUE
.
msg_action
(expression)
An expression that performs a user-specified action on the receipt of websocket message. These act on the msg
object seen printed to the console when a message is received (if toConsole = TRUE
). The msg
object also contains a $ts
column with the timestamp as a POSIXct
and a $socket
column with the socket name of origin ("Alpaca"/"Polygon"
) that are not visible in what is printed to the console but accessible to msg_action
. The expression can also reference the self
internal environment of this R6Class
.
live
Whether to connect to the live or paper API. See ?account for details.
pro
(logical)
Whether to use a Pro account. See get_pro
...
Passed on to WebSocket
channel()
Sends request to websocket to join the specified channel. Channel options are inherited from defaults set in AlpacaStreams$initialize
unless overridden.
AlpacaStreams$channel( channel = NULL, subscribe = TRUE, overwrite = FALSE, msg_action = NULL, ... )
channel
The channel to join
subscribe
(logical)
Default TRUE
. Set to FALSE
to unsubscribe from specified channel(s).
overwrite
(logical)
Default FALSE
. Set to TRUE
to overwrite data from previous instances of this channel socket.
msg_action
(expression)
An expression that performs a user-specified action on the receipt of websocket message. These act on the msg
object seen printed to the console when a message is received (if toConsole = TRUE
). The msg
object also contains a $ts
column with the timestamp as a POSIXct
and a $socket
column with the socket name of origin ("Alpaca"/"Polygon"
) that are not visible in what is printed to the console but accessible to msg_action
. The expression can also reference the self
internal environment of this R6Class
.
...
Named parameters of AlpacaStreams$new()
to be applied to this particular channel.
If log = TRUE
for a Polygon channel, all non-status messages will be stored as tibbles named according to their respective channel name and accessed via the $logs()
method.
logs()
Retrieve *Socket Logs/Bars
AlpacaStreams$logs(channel)
channel
(character)
The name of the channel for which to retrieve logs. The socket is auto-filled if a matching channel is available. If blank, all logs are returned.
connect()
Connects both AlpacaSocket
& PolygonSocket
to their respective server-side sockets.
AlpacaStreams$connect(...)
...
named arguments passed on to individual socket initialization methods. see AlpacaSocket
close()
Closes connections for both AlpacaSocket
& PolygonSocket
.
AlpacaStreams$close()
clone()
The objects of this class are cloneable with this method.
AlpacaStreams$clone(deep = FALSE)
deep
Whether to make a deep clone.
## Not run:
AS <- AlpacaStreams$new()
AS$channel()
# Subscribe to by-minute data for Beyond Meat Inc
AS$channel(list(bars = "BYND"))
# Turn off console messages
AS$Alpaca$data$opts$b.BYND$toConsole <- FALSE
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.