order_submit: Submit, Cancel & Replace Orders,

View source: R/Orders.R

order_submitR Documentation

Submit, Cancel & Replace Orders,

Description

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.

Usage

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()
)

Arguments

symbol_id

(character) The stock symbol (Required when action = "submit") or Order object (single row tibble) (Required when action = "cancel"/"replace"). To expedite the setting of stops and limits for open positions, an Order ID from a 'buy' order can be provided when action = "submit" to place a 'sell' order with the following parameters such that they do not need to be set manually:

  • side = 'sell'

  • If qty is not provided, it will be populated from the buy order

  • symbol_id will be set to the symbol from the buy order.

  • If client_order_id = TRUE, the client_order_id will be set to the buy Order ID provided, effectively linking the orders for your records.

  • All other parameters can be specified as usual.

action

(character) The action to take:

qty

(integer) The amount of shares to trade (required when action = "submit", optional when action = 'replace').

side

(character) The side of the trade. I.E "buy"/"b" or "sell"/"s". (required when action = "submit"). Assumed to be "buy" if order_class = "bracket".

type

(character) The type of trade order. I.E "market"/"m","limit"/"l","stop"/"s","stop_limit"/"sl", "trailing_stop"/"ts" etc. Default NULL. Typically required except in certain situations where the value can be assumed:

  • stop is set (and type is unset) type = "stop"/"s"

  • limit is set type = "limit"/"l"

  • stop & limit are set type = "stop_limit"/"sl"

  • order_class = "bracket" type = "market"/"m"

  • order_class = "oco" type = "limit"/"l"

  • order_class = "oto" type = "market"/"m"

  • trail_price is set type = "trailing_stop"

  • trail_percent is set type = "trailing_stop"

See Understand Orders for details.

time_in_force

(character) The time in force for the order. Optional when action = "replace". Args can be "day", "gtc", "opg" etc. Default "day". Please see Understand Orders: Time in Force for all types and more info. Must be "day" or "gtc" for Advanced Orders.

limit

(numeric) limit price. Required if type is "limit" or "stop_limit" for action = 'replace'/'submit'.

stop

(numeric) stop price. Required if type is "stop" or "stop_limit" for action = 'replace'/'submit'.

extended_hours

(logical) Default FALSE. If TRUE, order will be eligible to execute in premarket/afterhours. Currently supported hours are: Pre-market: 9:00 - 9:30am, After-hours: 4:00 - 6:00pm ET. Only works with type = 'limit' and time_in_force = 'day' on the V2 API.

client_order_id

(character/logical) <= 48 Characters. A unique identifier for the order. Automatically generated if not sent.

  • `action = 'replace'/'submit'` Optional character vector

  • `action = 'submit'` If an Order object is provided to symbol_id, TRUE will set the client_order_id for the sell order to Order ID in symbol_id. Used to link buy & sell orders for your records.

order_class

(character) 'simple', 'bracket', 'oco' or 'oto'. Required for advanced orders. For details of non-simple order classes, please see Advanced Orders. If order_class = 'bracket'/'oto', type can be omitted as it will always be 'market', this is also true with order_class = "oco" as type will always be 'limit'. Note that order replacement is not supported for all advanced order types.

take_profit

(named list) Additional parameters for take-profit leg of advanced orders:

  • 'limit_price'/'l' numeric required for 'bracket' & 'oco' order classes.

stop_loss

(named list) Additional parameters for stop-loss leg of advanced orders

  • stop_price/s numeric required for bracket orders

  • limit_price/l numeric The stop-loss order becomes a stop-limit order if specified. Required for 'bracket' & 'oco' order classes

notional

num dollar amount to trade. Cannot work with qty. Can only work for market order types and day for time in force.

trail_price

(numeric) a dollar value away from the highest water mark (hwm). If you set this to 2.00 for a sell trailing stop, the stop price is always hwm - 2.00

trail_percent

(numeric) a percent value away from the highest water mark. If you set this to 1.0 for a sell trailing stop, the stop price is always hwm * 0.99. Values less than 1 are assumed to be percentages, ie .07 = 7%, values must be less than 100.

live

(logical) TRUE / FALSE if you are connecting to a live account. Default to FALSE, so it will use the paper url if nothing was provided.

Value

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.

See Also

Other Orders: orders()

Examples

## 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)


jagg19/AlpacaforR documentation built on July 3, 2023, 12:14 p.m.