order_submit | R Documentation |
Places/Replaces/Cancels an order, or cancels all orders depending on argument to action
. See parameter documentation and Orders for details.Depending on the action
specified, some arguments are required:
action = 'submit'
All arguments can be submitted. See Arguments for which are required.
action = 'replace'
qty
, time_in_force
, limit
, stop
, client_order_id
are all eligible. Only one is required.
action = 'cancel'
Only symbol_id
is required.
action = 'cancel_all'
No arguments necessary.
order_submit(
symbol_id,
action = "submit",
qty = NULL,
side = NULL,
type = NULL,
time_in_force = "day",
limit = NULL,
stop = NULL,
extended_hours = NULL,
client_order_id = NULL,
order_class = NULL,
take_profit = NULL,
stop_loss = NULL,
notional = NULL,
trail_price,
trail_percent,
live = get_live()
)
symbol_id |
|
action |
|
qty |
|
side |
|
type |
See Understand Orders for details. |
time_in_force |
|
limit |
|
stop |
|
extended_hours |
|
client_order_id |
|
order_class |
|
take_profit |
|
stop_loss |
|
notional |
|
trail_price |
|
trail_percent |
|
live |
|
Order (tibble)
Order object or Array of Order Objects with the following information:
id
(character)
order id
client_order_id
(character)
client unique order id
created_at
(POSIXct)
nullable When the order was created
updated_at
(POSIXct)
nullable When the order was updated
submitted_at
(POSIXct)
nullable When the order was submitted
filled_at
(POSIXct)
nullable When the order was filled
expired_at
(POSIXct)
nullable When the order was expired
canceled_at
(POSIXct)
nullable When the order was canceled
failed_at
(POSIXct)
nullable When the order failed
replaced_at
(POSIXct)
nullable When the order was replaced
replaced_by
(character)
id of replacement order
replaces
(character)
id of the replaced order
asset_id
(character)
asset ID
symbol
(character)
Asset symbol
exchange
(character)
Asset exchange
asset_class
(character)
Asset class
qty
(integer)
Ordered quantity
filled_qty
(integer)
Filled quantity
filled_avg_price
(numeric)
Filled average price
order_class
(character)
order_type
(character)
type
(character)
Valid values: market, limit, stop, stop_limit
side
(character)
Valid values: buy, sell
time_in_force
(character)
time in force
limit_price
(numeric)
Limit price
stop_price
(numeric)
Stop price
status
(character)
Status of the order
extended_hours
(logical)
If true, eligible for execution outside regular trading hours.
legs
(character)
When querying non-simple order_class orders in a nested style, an array of Order entities associated with this order. Otherwise, null.
Other Orders:
orders()
## Not run:
# most orders (except limit) must be placed during market hours or they will not be filled until the next trading day.
.c <- clock()
if (.c$is_open) {
#' (bo <- order_submit("AAPL", qty = 1, side = "buy", type = "market"))
# Or you can submit a limit order (`type` is assumed to be `"limit"` when only `limit` is set):
lt <- market_data("AAPL", timeframe = "lt")
(so <- order_submit("AAPL", q = 1, side = "s", limit = lt$p * .99))
# cancel an order with `action = "cancel"`. symbol_id can be either the id of the order to cancel or the order tbl object.
order_submit(so, a = "c")
# expedite a simple "sell" order by providing the id of a buy order. This can be linked to it's original buy order on the Alpaca side via the `client_order_id` by simply setting `client_order_id = T`
(so <- order_submit(bo, stop = lt$p * .95, client = TRUE)) # here the id is used
identical(so$client_order_id, bo$id)
# replace `"r"` parameters for simple orders (Alpaca devs are working on replacement for complex orders as of 2020-05-06)
order_submit(so, a = "r", stop = lt$p * .94)
# Complex orders
# Here is an example of setting a "bracket" order
# first retrieve the going price
(.lt <- market_data(timeframe = "lt", symbol_id = "AMZN"))
# sell if the price moves up 3% by setting `take_profit`
tp <- list(l = .lt$p * 1.03)
# hedge risk by setting a stop if it loses 5% and limit if it loses 6% with `stop_loss`
sl <- list(l = .lt$p * .94, s = .lt$p * .95)
# note that the names of these list items can be partial
(br_o <- order_submit("AMZN", order_class = "bracket", qty = 2, take_profit = tp, stop_loss = sl))
# side is assumed to be buy, and type is assumed to be market
# Set a trailing stop by price
m_o <- order_submit("AMZN", side = "buy", type = "market", qty = 1)
ts_o <- order_submit(m_o, trail_price = 30)
# Set a trailing stop by percent
m_o <- order_submit("AMZN", side = "buy", type = "market", qty = 1)
ts_o <- order_submit(m_o, trail_percent = 5)
}
# all open orders can be canceled with `action = "cancel_all"`
order_submit(action = "cancel_all")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.